Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.

Commit b508c1c

Browse files
authored
Merge pull request #23 from toku-sa-n/iter_loadable_headers
refactor: define `ElfBinary::iter_loadable_headers`
2 parents 677c1d5 + 2109500 commit b508c1c

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/lib.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -438,27 +438,7 @@ impl<'s> ElfBinary<'s> {
438438
pub fn load(&self, loader: &mut dyn ElfLoader) -> Result<(), ElfLoaderErr> {
439439
self.is_loadable()?;
440440

441-
// Trying to determine loadeable headers
442-
fn select_load(&pheader: &ProgramHeader) -> bool {
443-
if let Ph64(header) = pheader {
444-
header
445-
.get_type()
446-
.map(|typ| typ == Type::Load)
447-
.unwrap_or(false)
448-
} else {
449-
false
450-
}
451-
}
452-
453-
// Create an iterator (well filter really) that has all loadeable
454-
// headers and pass it to the loader
455-
// TODO: This is pretty ugly, maybe we can do something with impl Trait?
456-
// https://stackoverflow.com/questions/27535289/what-is-the-correct-way-to-return-an-iterator-or-any-other-trait
457-
let load_iter = self
458-
.file
459-
.program_iter()
460-
.filter(select_load as fn(&ProgramHeader) -> bool);
461-
loader.allocate(load_iter)?;
441+
loader.allocate(self.iter_loadable_headers())?;
462442

463443
// Load all headers
464444
for p in self.file.program_iter() {
@@ -496,6 +476,26 @@ impl<'s> ElfBinary<'s> {
496476

497477
Ok(())
498478
}
479+
480+
fn iter_loadable_headers(&self) -> LoadableHeaders {
481+
// Trying to determine loadeable headers
482+
fn select_load(pheader: &ProgramHeader) -> bool {
483+
if let Ph64(header) = pheader {
484+
header
485+
.get_type()
486+
.map(|typ| typ == Type::Load)
487+
.unwrap_or(false)
488+
} else {
489+
false
490+
}
491+
}
492+
493+
// Create an iterator (well filter really) that has all loadeable
494+
// headers and pass it to the loader
495+
// TODO: This is pretty ugly, maybe we can do something with impl Trait?
496+
// https://stackoverflow.com/questions/27535289/what-is-the-correct-way-to-return-an-iterator-or-any-other-trait
497+
self.file.program_iter().filter(select_load)
498+
}
499499
}
500500

501501
#[cfg(test)]

0 commit comments

Comments
 (0)