Skip to content

Commit 7a2bb3e

Browse files
committed
Add try_into_inner() for ISO9660 and FileRef
1 parent 3fba6dc commit 7a2bb3e

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum ISOError {
1313
ParseInt(ParseIntError),
1414
ReadSize(usize, usize),
1515
Nom(nom::error::ErrorKind),
16+
IntoInner(&'static str),
1617
}
1718

1819
impl Display for ISOError {
@@ -28,6 +29,7 @@ impl Display for ISOError {
2829
size, size_read
2930
),
3031
ISOError::Nom(ref err) => write!(f, "Parse error: {:?}", err),
32+
ISOError::IntoInner(msg) => write!(f, "Into inner error: {}", msg),
3133
}
3234
}
3335
}

src/fileref.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,14 @@ impl<T: ISO9660Reader> FileRef<T> {
6262
pub fn read_at(&self, buf: &mut [u8], lba: u64) -> Result<usize> {
6363
(*self.0).borrow_mut().read_at(buf, lba)
6464
}
65+
66+
/// Try returning inner value (will work if Rc has exactly one strong ref)
67+
/// Returns Self on error
68+
pub fn try_into_inner(self) -> std::result::Result<T, Self> {
69+
match Rc::<RefCell<T>>::try_unwrap(self.0) {
70+
Ok(refcell) => Ok(refcell.into_inner()),
71+
Err(rcrefcell) => Err(FileRef(rcrefcell))
72+
}
73+
}
74+
6575
}

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ impl<T: ISO9660Reader> ISO9660<T> {
126126
2048 // XXX
127127
}
128128

129+
/// Returns inner reader, consuming self.
130+
/// This will fail if references to T are still used (e.g. ISODirectory<T> or ISOFile<T>).
131+
pub fn try_into_inner(self) -> Result<T> {
132+
// Drop root first since it owns a _file ref
133+
drop(self.root);
134+
self._file.try_into_inner().map_err(|_|
135+
ISOError::IntoInner("Inner reader still referenced")
136+
)
137+
}
138+
129139
primary_prop_str!(volume_set_identifier);
130140
primary_prop_str!(publisher_identifier);
131141
primary_prop_str!(data_preparer_identifier);

0 commit comments

Comments
 (0)