From 417eefa3ca9024bd62560ca6043a5d423bf59095 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 10 Feb 2022 10:52:37 +0100 Subject: [PATCH] Add try_into_inner() for ISO9660 and FileRef --- src/error.rs | 2 ++ src/fileref.rs | 10 ++++++++++ src/lib.rs | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/error.rs b/src/error.rs index fd167e1..2bd88d9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,6 +13,7 @@ pub enum ISOError { ParseInt(ParseIntError), ReadSize(usize, usize), Nom(nom::error::ErrorKind), + IntoInner(&'static str), } impl Display for ISOError { @@ -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), } } } diff --git a/src/fileref.rs b/src/fileref.rs index a4f19b3..00b9896 100644 --- a/src/fileref.rs +++ b/src/fileref.rs @@ -62,4 +62,14 @@ impl FileRef { pub fn read_at(&self, buf: &mut [u8], lba: u64) -> Result { (*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 { + match Rc::>::try_unwrap(self.0) { + Ok(refcell) => Ok(refcell.into_inner()), + Err(rcrefcell) => Err(FileRef(rcrefcell)) + } + } + } diff --git a/src/lib.rs b/src/lib.rs index 1b12082..014f0cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,6 +126,16 @@ impl ISO9660 { 2048 // XXX } + /// Returns inner reader, consuming self. + /// This will fail if references to T are still used (e.g. ISODirectory or ISOFile). + pub fn try_into_inner(self) -> Result { + // 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);