Skip to content
Merged
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
refactor: replace unwrap with errors in table_collection.rs
  • Loading branch information
molpopgen committed Oct 28, 2022
commit 4c6e94616ba6980819ffe56346b884be9216c953
12 changes: 8 additions & 4 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ impl TableCollection {
Err(e) => return Err(e),
};

let c_str = std::ffi::CString::new(filename.as_ref()).unwrap();
let c_str = std::ffi::CString::new(filename.as_ref()).map_err(|_| {
TskitError::LibraryError("call to ffi::CString::new failed".to_string())
})?;
let rv = unsafe {
ll_bindings::tsk_table_collection_load(
tables.as_mut_ptr(),
Expand Down Expand Up @@ -547,7 +549,7 @@ impl TableCollection {
Some(unsafe {
std::slice::from_raw_parts(
(*self.as_ptr()).indexes.edge_insertion_order as *const EdgeId,
usize::try_from((*self.as_ptr()).indexes.num_edges).unwrap(),
usize::try_from((*self.as_ptr()).indexes.num_edges).ok()?,
)
})
} else {
Expand All @@ -563,7 +565,7 @@ impl TableCollection {
Some(unsafe {
std::slice::from_raw_parts(
(*self.as_ptr()).indexes.edge_removal_order as *const EdgeId,
usize::try_from((*self.as_ptr()).indexes.num_edges).unwrap(),
usize::try_from((*self.as_ptr()).indexes.num_edges).ok()?,
)
})
} else {
Expand Down Expand Up @@ -681,7 +683,9 @@ impl TableCollection {
/// This function allocates a `CString` to pass the file name to the C API.
/// A panic will occur if the system runs out of memory.
pub fn dump<O: Into<TableOutputOptions>>(&self, filename: &str, options: O) -> TskReturnValue {
let c_str = std::ffi::CString::new(filename).unwrap();
let c_str = std::ffi::CString::new(filename).map_err(|_| {
TskitError::LibraryError("call to ffi::CString::new failed".to_string())
})?;
let rv = unsafe {
ll_bindings::tsk_table_collection_dump(
self.as_ptr(),
Expand Down