Skip to content

refactor: replace unwrap with errors in table_collection.rs #343

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

Merged
merged 1 commit into from
Oct 28, 2022
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
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