I have the following Rust struct:
struct Binary<'a> {
path: PathBuf,
bytes: Vec<u8>,
pe: PE<'a>,
}
impl<'a> Binary<'a> {
fn read(p: PathBuf) -> Binary<'a> {
todo!();
}
}
Here PE
comes from the goblin crate.
I've tried a few things to implement the read
function, but I'm struggling with the dependency between the bytes
field and the PE
that is referencing those bytes:
For instance:
fn new(p: PathBuf) -> Binary<'a> {
if let Ok(bytes) = std::fs::read(&p) {
Binary {
path: p,
bytes: bytes,
pe: PE::parse(&bytes).unwrap()
}
} else {
panic!()
}
}
Obviously that doesn't work, bytes
is borrowed after it has been moved at the bytes: bytes
line. I don't see a way to initialize the PE field from the moved value of the bytes field.
Currently I've worked around it by not storing the PE
in the Binary
struct and simply created a function that returns a PE from the bytes when I need it:
fn pe(&self) -> Option<PE> {
match PE::parse(&self.bytes) {
Ok(p) => Some(p),
Err(_) => None,
}
}
But I'm curious to know what I am missing and how can I parse the file only once.