Skip to content

Commit e2c2d33

Browse files
authored
refactor: replace unwrap with errors in table_collection.rs (#343)
1 parent 8e27f06 commit e2c2d33

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/table_collection.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ impl TableCollection {
196196
Err(e) => return Err(e),
197197
};
198198

199-
let c_str = std::ffi::CString::new(filename.as_ref()).unwrap();
199+
let c_str = std::ffi::CString::new(filename.as_ref()).map_err(|_| {
200+
TskitError::LibraryError("call to ffi::CString::new failed".to_string())
201+
})?;
200202
let rv = unsafe {
201203
ll_bindings::tsk_table_collection_load(
202204
tables.as_mut_ptr(),
@@ -547,7 +549,7 @@ impl TableCollection {
547549
Some(unsafe {
548550
std::slice::from_raw_parts(
549551
(*self.as_ptr()).indexes.edge_insertion_order as *const EdgeId,
550-
usize::try_from((*self.as_ptr()).indexes.num_edges).unwrap(),
552+
usize::try_from((*self.as_ptr()).indexes.num_edges).ok()?,
551553
)
552554
})
553555
} else {
@@ -563,7 +565,7 @@ impl TableCollection {
563565
Some(unsafe {
564566
std::slice::from_raw_parts(
565567
(*self.as_ptr()).indexes.edge_removal_order as *const EdgeId,
566-
usize::try_from((*self.as_ptr()).indexes.num_edges).unwrap(),
568+
usize::try_from((*self.as_ptr()).indexes.num_edges).ok()?,
567569
)
568570
})
569571
} else {
@@ -681,7 +683,9 @@ impl TableCollection {
681683
/// This function allocates a `CString` to pass the file name to the C API.
682684
/// A panic will occur if the system runs out of memory.
683685
pub fn dump<O: Into<TableOutputOptions>>(&self, filename: &str, options: O) -> TskReturnValue {
684-
let c_str = std::ffi::CString::new(filename).unwrap();
686+
let c_str = std::ffi::CString::new(filename).map_err(|_| {
687+
TskitError::LibraryError("call to ffi::CString::new failed".to_string())
688+
})?;
685689
let rv = unsafe {
686690
ll_bindings::tsk_table_collection_dump(
687691
self.as_ptr(),

0 commit comments

Comments
 (0)