Last active
March 6, 2024 22:40
-
-
Save ThomasHabets/6da0321f6d317da253ac5fd8290c97d9 to your computer and use it in GitHub Desktop.
What's wrong with this Rust?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Aside from the panics, and the fact that the API currently requires first a call to new, then select_entry(), | |
how do I fix the lifetime issues: | |
error: lifetime may not live long enough | |
--> src/sigmf.rs:226:43 | |
| | |
221 | pub fn select_entry(&mut self, filename: &str) -> Result<()> { | |
| - let's call the lifetime of this reference `'1` | |
... | |
226 | self.current_entry = Some(Box::new(entry)); | |
| ^^^^^^^^^^^^^^^ cast requires that `'1` must outlive `'static` | |
*/ | |
pub struct TarFileReader { | |
archive: Archive<File>, | |
current_entry: Option<Box<dyn Read>>, | |
} | |
impl TarFileReader { | |
pub fn new(fname: impl AsRef<Path>) -> Result<Self> { | |
let file = File::open(fname)?; | |
let archive = Archive::new(file); | |
Ok(TarFileReader { | |
archive, | |
current_entry: None, | |
}) | |
} | |
pub fn select_entry(&mut self, filename: &str) -> Result<()> { | |
let entries = self.archive.entries()?; | |
for entry in entries { | |
let entry = entry?; | |
if entry.path()?.ends_with(filename) { | |
self.current_entry = Some(Box::new(entry.clone())); | |
return Ok(()); | |
} | |
} | |
panic!("Entry not found in archive"); | |
} | |
} | |
impl Read for TarFileReader { | |
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | |
match self.current_entry { | |
Some(ref mut entry) => Ok(entry.read(buf)?), | |
None => panic!("no entry selected"), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment