Skip to content

Add try_into_inner() for ISO9660 and FileRef #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum ISOError {
ParseInt(ParseIntError),
ReadSize(usize, usize),
Nom(nom::error::ErrorKind),
IntoInner(&'static str),
}

impl Display for ISOError {
Expand All @@ -28,6 +29,7 @@ impl Display for ISOError {
size, size_read
),
ISOError::Nom(ref err) => write!(f, "Parse error: {:?}", err),
ISOError::IntoInner(msg) => write!(f, "Into inner error: {}", msg),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/fileref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ impl<T: ISO9660Reader> FileRef<T> {
pub fn read_at(&self, buf: &mut [u8], lba: u64) -> Result<usize> {
(*self.0).borrow_mut().read_at(buf, lba)
}

/// Try returning inner value (will work if Rc has exactly one strong ref)
/// Returns Self on error
pub fn try_into_inner(self) -> std::result::Result<T, Self> {
match Rc::<RefCell<T>>::try_unwrap(self.0) {
Ok(refcell) => Ok(refcell.into_inner()),
Err(rcrefcell) => Err(FileRef(rcrefcell))
}
}

}
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ impl<T: ISO9660Reader> ISO9660<T> {
2048 // XXX
}

/// Returns inner reader, consuming self.
/// This will fail if references to T are still used (e.g. ISODirectory<T> or ISOFile<T>).
pub fn try_into_inner(self) -> Result<T> {
// Drop root first since it owns a _file ref
drop(self.root);
self._file.try_into_inner().map_err(|_|
ISOError::IntoInner("Inner reader still referenced")
)
}

primary_prop_str!(volume_set_identifier);
primary_prop_str!(publisher_identifier);
primary_prop_str!(data_preparer_identifier);
Expand Down