Skip to content

Commit

Permalink
Remove 'file lifetime from the Object trait.
Browse files Browse the repository at this point in the history
Instead, have the 'file lifetime on the associated traits.
This raises the MSRV to 1.65 because it uses Generic Associated Traits.

This change makes it easier to write generic code
that takes any Object implementation, especially
when taking ownership of the Object.
  • Loading branch information
mstange committed Apr 5, 2024
1 parent 16b441f commit dc3be8f
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 236 deletions.
52 changes: 27 additions & 25 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,20 @@ impl<'data, R: ReadRef<'data>> File<'data, R> {

impl<'data, R: ReadRef<'data>> read::private::Sealed for File<'data, R> {}

impl<'data, 'file, R> Object<'data, 'file> for File<'data, R>
impl<'data, R> Object<'data> for File<'data, R>
where
'data: 'file,
R: 'file + ReadRef<'data>,
R: ReadRef<'data>,
{
type Segment = Segment<'data, 'file, R>;
type SegmentIterator = SegmentIterator<'data, 'file, R>;
type Section = Section<'data, 'file, R>;
type SectionIterator = SectionIterator<'data, 'file, R>;
type Comdat = Comdat<'data, 'file, R>;
type ComdatIterator = ComdatIterator<'data, 'file, R>;
type Symbol = Symbol<'data, 'file, R>;
type SymbolIterator = SymbolIterator<'data, 'file, R>;
type SymbolTable = SymbolTable<'data, 'file, R>;
type DynamicRelocationIterator = DynamicRelocationIterator<'data, 'file, R>;
type Segment<'file> = Segment<'data, 'file, R> where Self: 'file, 'data: 'file;
type SegmentIterator<'file> = SegmentIterator<'data, 'file, R> where Self: 'file, 'data: 'file;
type Section<'file> = Section<'data, 'file, R> where Self: 'file, 'data: 'file;
type SectionIterator<'file> = SectionIterator<'data, 'file, R> where Self: 'file, 'data: 'file;
type Comdat<'file> = Comdat<'data, 'file, R> where Self: 'file, 'data: 'file;
type ComdatIterator<'file> = ComdatIterator<'data, 'file, R> where Self: 'file, 'data: 'file;
type Symbol<'file> = Symbol<'data, 'file, R> where Self: 'file, 'data: 'file;
type SymbolIterator<'file> = SymbolIterator<'data, 'file, R> where Self: 'file, 'data: 'file;
type SymbolTable<'file> = SymbolTable<'data, 'file, R> where Self: 'file, 'data: 'file;
type DynamicRelocationIterator<'file> = DynamicRelocationIterator<'data, 'file, R> where Self: 'file, 'data: 'file;

fn architecture(&self) -> Architecture {
with_inner!(self, File, |x| x.architecture())
Expand All @@ -339,43 +338,46 @@ where
with_inner!(self, File, |x| x.kind())
}

fn segments(&'file self) -> SegmentIterator<'data, 'file, R> {
fn segments(&self) -> SegmentIterator<'data, '_, R> {
SegmentIterator {
inner: map_inner!(self, File, SegmentIteratorInternal, |x| x.segments()),
}
}

fn section_by_name_bytes(&'file self, section_name: &[u8]) -> Option<Section<'data, 'file, R>> {
fn section_by_name_bytes<'file>(
&'file self,
section_name: &[u8],
) -> Option<Section<'data, 'file, R>> {
map_inner_option!(self, File, SectionInternal, |x| x
.section_by_name_bytes(section_name))
.map(|inner| Section { inner })
}

fn section_by_index(&'file self, index: SectionIndex) -> Result<Section<'data, 'file, R>> {
fn section_by_index(&self, index: SectionIndex) -> Result<Section<'data, '_, R>> {
map_inner_option!(self, File, SectionInternal, |x| x.section_by_index(index))
.map(|inner| Section { inner })
}

fn sections(&'file self) -> SectionIterator<'data, 'file, R> {
fn sections(&self) -> SectionIterator<'data, '_, R> {
SectionIterator {
inner: map_inner!(self, File, SectionIteratorInternal, |x| x.sections()),
}
}

fn comdats(&'file self) -> ComdatIterator<'data, 'file, R> {
fn comdats(&self) -> ComdatIterator<'data, '_, R> {
ComdatIterator {
inner: map_inner!(self, File, ComdatIteratorInternal, |x| x.comdats()),
}
}

fn symbol_by_index(&'file self, index: SymbolIndex) -> Result<Symbol<'data, 'file, R>> {
fn symbol_by_index(&self, index: SymbolIndex) -> Result<Symbol<'data, '_, R>> {
map_inner_option!(self, File, SymbolInternal, |x| x
.symbol_by_index(index)
.map(|x| (x, PhantomData)))
.map(|inner| Symbol { inner })
}

fn symbols(&'file self) -> SymbolIterator<'data, 'file, R> {
fn symbols(&self) -> SymbolIterator<'data, '_, R> {
SymbolIterator {
inner: map_inner!(self, File, SymbolIteratorInternal, |x| (
x.symbols(),
Expand All @@ -384,14 +386,14 @@ where
}
}

fn symbol_table(&'file self) -> Option<SymbolTable<'data, 'file, R>> {
fn symbol_table(&self) -> Option<SymbolTable<'data, '_, R>> {
map_inner_option!(self, File, SymbolTableInternal, |x| x
.symbol_table()
.map(|x| (x, PhantomData)))
.map(|inner| SymbolTable { inner })
}

fn dynamic_symbols(&'file self) -> SymbolIterator<'data, 'file, R> {
fn dynamic_symbols(&self) -> SymbolIterator<'data, '_, R> {
SymbolIterator {
inner: map_inner!(self, File, SymbolIteratorInternal, |x| (
x.dynamic_symbols(),
Expand All @@ -400,15 +402,15 @@ where
}
}

fn dynamic_symbol_table(&'file self) -> Option<SymbolTable<'data, 'file, R>> {
fn dynamic_symbol_table(&self) -> Option<SymbolTable<'data, '_, R>> {
map_inner_option!(self, File, SymbolTableInternal, |x| x
.dynamic_symbol_table()
.map(|x| (x, PhantomData)))
.map(|inner| SymbolTable { inner })
}

#[cfg(feature = "elf")]
fn dynamic_relocations(&'file self) -> Option<DynamicRelocationIterator<'data, 'file, R>> {
fn dynamic_relocations(&self) -> Option<DynamicRelocationIterator<'data, '_, R>> {
let inner = match self {
File::Elf32(ref elf) => {
DynamicRelocationIteratorInternal::Elf32(elf.dynamic_relocations()?)
Expand All @@ -423,7 +425,7 @@ where
}

#[cfg(not(feature = "elf"))]
fn dynamic_relocations(&'file self) -> Option<DynamicRelocationIterator<'data, 'file, R>> {
fn dynamic_relocations(&self) -> Option<DynamicRelocationIterator<'data, '_, R>> {
None
}

Expand Down
53 changes: 23 additions & 30 deletions src/read/coff/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,21 @@ impl<'data, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed
{
}

impl<'data, 'file, R, Coff> Object<'data, 'file> for CoffFile<'data, R, Coff>
impl<'data, R, Coff> Object<'data> for CoffFile<'data, R, Coff>
where
'data: 'file,
R: 'file + ReadRef<'data>,
R: ReadRef<'data>,
Coff: CoffHeader,
{
type Segment = CoffSegment<'data, 'file, R, Coff>;
type SegmentIterator = CoffSegmentIterator<'data, 'file, R, Coff>;
type Section = CoffSection<'data, 'file, R, Coff>;
type SectionIterator = CoffSectionIterator<'data, 'file, R, Coff>;
type Comdat = CoffComdat<'data, 'file, R, Coff>;
type ComdatIterator = CoffComdatIterator<'data, 'file, R, Coff>;
type Symbol = CoffSymbol<'data, 'file, R, Coff>;
type SymbolIterator = CoffSymbolIterator<'data, 'file, R, Coff>;
type SymbolTable = CoffSymbolTable<'data, 'file, R, Coff>;
type DynamicRelocationIterator = NoDynamicRelocationIterator;
type Segment<'file> = CoffSegment<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type SegmentIterator<'file> = CoffSegmentIterator<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type Section<'file> = CoffSection<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type SectionIterator<'file> = CoffSectionIterator<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type Comdat<'file> = CoffComdat<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type ComdatIterator<'file> = CoffComdatIterator<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type Symbol<'file> = CoffSymbol<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type SymbolIterator<'file> = CoffSymbolIterator<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type SymbolTable<'file> = CoffSymbolTable<'data, 'file, R, Coff> where Self: 'file, 'data: 'file;
type DynamicRelocationIterator<'file> = NoDynamicRelocationIterator where Self: 'file, 'data: 'file;

fn architecture(&self) -> Architecture {
match self.header.machine() {
Expand Down Expand Up @@ -119,25 +118,22 @@ where
ObjectKind::Relocatable
}

fn segments(&'file self) -> CoffSegmentIterator<'data, 'file, R, Coff> {
fn segments(&self) -> CoffSegmentIterator<'data, '_, R, Coff> {
CoffSegmentIterator {
file: self,
iter: self.common.sections.iter(),
}
}

fn section_by_name_bytes(
fn section_by_name_bytes<'file>(
&'file self,
section_name: &[u8],
) -> Option<CoffSection<'data, 'file, R, Coff>> {
self.sections()
.find(|section| section.name_bytes() == Ok(section_name))
}

fn section_by_index(
&'file self,
index: SectionIndex,
) -> Result<CoffSection<'data, 'file, R, Coff>> {
fn section_by_index(&self, index: SectionIndex) -> Result<CoffSection<'data, '_, R, Coff>> {
let section = self.common.sections.section(index.0)?;
Ok(CoffSection {
file: self,
Expand All @@ -146,24 +142,21 @@ where
})
}

fn sections(&'file self) -> CoffSectionIterator<'data, 'file, R, Coff> {
fn sections(&self) -> CoffSectionIterator<'data, '_, R, Coff> {
CoffSectionIterator {
file: self,
iter: self.common.sections.iter().enumerate(),
}
}

fn comdats(&'file self) -> CoffComdatIterator<'data, 'file, R, Coff> {
fn comdats(&self) -> CoffComdatIterator<'data, '_, R, Coff> {
CoffComdatIterator {
file: self,
index: 0,
}
}

fn symbol_by_index(
&'file self,
index: SymbolIndex,
) -> Result<CoffSymbol<'data, 'file, R, Coff>> {
fn symbol_by_index(&self, index: SymbolIndex) -> Result<CoffSymbol<'data, '_, R, Coff>> {
let symbol = self.common.symbols.symbol(index.0)?;
Ok(CoffSymbol {
file: &self.common,
Expand All @@ -172,19 +165,19 @@ where
})
}

fn symbols(&'file self) -> CoffSymbolIterator<'data, 'file, R, Coff> {
fn symbols(&self) -> CoffSymbolIterator<'data, '_, R, Coff> {
CoffSymbolIterator {
file: &self.common,
index: 0,
}
}

#[inline]
fn symbol_table(&'file self) -> Option<CoffSymbolTable<'data, 'file, R, Coff>> {
fn symbol_table(&self) -> Option<CoffSymbolTable<'data, '_, R, Coff>> {
Some(CoffSymbolTable { file: &self.common })
}

fn dynamic_symbols(&'file self) -> CoffSymbolIterator<'data, 'file, R, Coff> {
fn dynamic_symbols(&self) -> CoffSymbolIterator<'data, '_, R, Coff> {
CoffSymbolIterator {
file: &self.common,
// Hack: don't return any.
Expand All @@ -193,12 +186,12 @@ where
}

#[inline]
fn dynamic_symbol_table(&'file self) -> Option<CoffSymbolTable<'data, 'file, R, Coff>> {
fn dynamic_symbol_table(&self) -> Option<CoffSymbolTable<'data, '_, R, Coff>> {
None
}

#[inline]
fn dynamic_relocations(&'file self) -> Option<NoDynamicRelocationIterator> {
fn dynamic_relocations(&self) -> Option<NoDynamicRelocationIterator> {
None
}

Expand Down
53 changes: 23 additions & 30 deletions src/read/elf/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,21 @@ where
{
}

impl<'data, 'file, Elf, R> Object<'data, 'file> for ElfFile<'data, Elf, R>
impl<'data, Elf, R> Object<'data> for ElfFile<'data, Elf, R>
where
'data: 'file,
Elf: FileHeader,
R: 'file + ReadRef<'data>,
R: ReadRef<'data>,
{
type Segment = ElfSegment<'data, 'file, Elf, R>;
type SegmentIterator = ElfSegmentIterator<'data, 'file, Elf, R>;
type Section = ElfSection<'data, 'file, Elf, R>;
type SectionIterator = ElfSectionIterator<'data, 'file, Elf, R>;
type Comdat = ElfComdat<'data, 'file, Elf, R>;
type ComdatIterator = ElfComdatIterator<'data, 'file, Elf, R>;
type Symbol = ElfSymbol<'data, 'file, Elf, R>;
type SymbolIterator = ElfSymbolIterator<'data, 'file, Elf, R>;
type SymbolTable = ElfSymbolTable<'data, 'file, Elf, R>;
type DynamicRelocationIterator = ElfDynamicRelocationIterator<'data, 'file, Elf, R>;
type Segment<'file> = ElfSegment<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type SegmentIterator<'file> = ElfSegmentIterator<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type Section<'file> = ElfSection<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type SectionIterator<'file> = ElfSectionIterator<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type Comdat<'file> = ElfComdat<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type ComdatIterator<'file> = ElfComdatIterator<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type Symbol<'file> = ElfSymbol<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type SymbolIterator<'file> = ElfSymbolIterator<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type SymbolTable<'file> = ElfSymbolTable<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;
type DynamicRelocationIterator<'file> = ElfDynamicRelocationIterator<'data, 'file, Elf, R> where Self: 'file, 'data: 'file;

fn architecture(&self) -> Architecture {
match (
Expand Down Expand Up @@ -214,25 +213,22 @@ where
}
}

fn segments(&'file self) -> ElfSegmentIterator<'data, 'file, Elf, R> {
fn segments(&self) -> ElfSegmentIterator<'data, '_, Elf, R> {
ElfSegmentIterator {
file: self,
iter: self.segments.iter(),
}
}

fn section_by_name_bytes(
fn section_by_name_bytes<'file>(
&'file self,
section_name: &[u8],
) -> Option<ElfSection<'data, 'file, Elf, R>> {
self.raw_section_by_name(section_name)
.or_else(|| self.zdebug_section_by_name(section_name))
}

fn section_by_index(
&'file self,
index: SectionIndex,
) -> read::Result<ElfSection<'data, 'file, Elf, R>> {
fn section_by_index(&self, index: SectionIndex) -> read::Result<ElfSection<'data, '_, Elf, R>> {
let section = self.sections.section(index)?;
Ok(ElfSection {
file: self,
Expand All @@ -241,24 +237,21 @@ where
})
}

fn sections(&'file self) -> ElfSectionIterator<'data, 'file, Elf, R> {
fn sections(&self) -> ElfSectionIterator<'data, '_, Elf, R> {
ElfSectionIterator {
file: self,
iter: self.sections.iter().enumerate(),
}
}

fn comdats(&'file self) -> ElfComdatIterator<'data, 'file, Elf, R> {
fn comdats(&self) -> ElfComdatIterator<'data, '_, Elf, R> {
ElfComdatIterator {
file: self,
iter: self.sections.iter().enumerate(),
}
}

fn symbol_by_index(
&'file self,
index: SymbolIndex,
) -> read::Result<ElfSymbol<'data, 'file, Elf, R>> {
fn symbol_by_index(&self, index: SymbolIndex) -> read::Result<ElfSymbol<'data, '_, Elf, R>> {
let symbol = self.symbols.symbol(index.0)?;
Ok(ElfSymbol {
endian: self.endian,
Expand All @@ -268,15 +261,15 @@ where
})
}

fn symbols(&'file self) -> ElfSymbolIterator<'data, 'file, Elf, R> {
fn symbols(&self) -> ElfSymbolIterator<'data, '_, Elf, R> {
ElfSymbolIterator {
endian: self.endian,
symbols: &self.symbols,
index: 0,
}
}

fn symbol_table(&'file self) -> Option<ElfSymbolTable<'data, 'file, Elf, R>> {
fn symbol_table(&self) -> Option<ElfSymbolTable<'data, '_, Elf, R>> {
if self.symbols.is_empty() {
return None;
}
Expand All @@ -286,15 +279,15 @@ where
})
}

fn dynamic_symbols(&'file self) -> ElfSymbolIterator<'data, 'file, Elf, R> {
fn dynamic_symbols(&self) -> ElfSymbolIterator<'data, '_, Elf, R> {
ElfSymbolIterator {
endian: self.endian,
symbols: &self.dynamic_symbols,
index: 0,
}
}

fn dynamic_symbol_table(&'file self) -> Option<ElfSymbolTable<'data, 'file, Elf, R>> {
fn dynamic_symbol_table(&self) -> Option<ElfSymbolTable<'data, '_, Elf, R>> {
if self.dynamic_symbols.is_empty() {
return None;
}
Expand All @@ -304,7 +297,7 @@ where
})
}

fn dynamic_relocations(
fn dynamic_relocations<'file>(
&'file self,
) -> Option<ElfDynamicRelocationIterator<'data, 'file, Elf, R>> {
Some(ElfDynamicRelocationIterator {
Expand Down
Loading

0 comments on commit dc3be8f

Please sign in to comment.