Skip to content

Commit ca132d8

Browse files
committed
refactor: remove unwraps in trees source files.
1 parent e2c2d33 commit ca132d8

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/tree_interface.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,13 @@ impl TreeInterface {
296296
fn left_sample(&self, u: NodeId) -> Result<NodeId, TskitError> {
297297
err_if_not_tracking_samples!(
298298
self.flags,
299-
unsafe_tsk_column_access!(u.0, 0, self.num_nodes, (*self.as_ptr()).left_sample, NodeId)
300-
.unwrap()
299+
unsafe_tsk_column_access!(
300+
u.0,
301+
0,
302+
self.num_nodes,
303+
(*self.as_ptr()).left_sample,
304+
NodeId
305+
)?
301306
)
302307
}
303308

@@ -310,8 +315,7 @@ impl TreeInterface {
310315
self.num_nodes,
311316
(*self.as_ptr()).right_sample,
312317
NodeId
313-
)
314-
.unwrap()
318+
)?
315319
)
316320
}
317321

@@ -588,16 +592,20 @@ struct PreorderNodeIterator<'a> {
588592

589593
impl<'a> PreorderNodeIterator<'a> {
590594
fn new(tree: &'a TreeInterface) -> Self {
595+
debug_assert!(tree.right_child(tree.virtual_root()).is_ok());
591596
let mut rv = PreorderNodeIterator {
592-
current_root: tree.right_child(tree.virtual_root()).unwrap(),
597+
current_root: tree
598+
.right_child(tree.virtual_root())
599+
.unwrap_or(NodeId::NULL),
593600
node_stack: vec![],
594601
tree,
595602
current_node_: None,
596603
};
597604
let mut c = rv.current_root;
598605
while !c.is_null() {
599606
rv.node_stack.push(c);
600-
c = rv.tree.left_sib(c).unwrap();
607+
debug_assert!(rv.tree.left_sib(c).is_ok());
608+
c = rv.tree.left_sib(c).unwrap_or(NodeId::NULL);
601609
}
602610
rv
603611
}
@@ -610,10 +618,12 @@ impl NodeIterator for PreorderNodeIterator<'_> {
610618
// NOTE: process children right-to-left
611619
// because we later pop them from a steck
612620
// to generate the expected left-to-right ordering.
613-
let mut c = self.tree.right_child(u).unwrap();
621+
debug_assert!(self.tree.right_child(u).is_ok());
622+
let mut c = self.tree.right_child(u).unwrap_or(NodeId::NULL);
614623
while c != NodeId::NULL {
615624
self.node_stack.push(c);
616-
c = self.tree.left_sib(c).unwrap();
625+
debug_assert!(self.tree.right_child(c).is_ok());
626+
c = self.tree.left_sib(c).unwrap_or(NodeId::NULL);
617627
}
618628
};
619629
}
@@ -642,7 +652,7 @@ impl<'a> PostorderNodeIterator<'a> {
642652
// NOTE: this fn does not return error codes
643653
usize::try_from(unsafe {
644654
ll_bindings::tsk_tree_get_size_bound(tree.as_ptr())
645-
}).unwrap()
655+
}).unwrap_or(usize::MAX)
646656
];
647657

648658
let rv = unsafe {
@@ -664,7 +674,7 @@ impl<'a> PostorderNodeIterator<'a> {
664674
Self {
665675
nodes,
666676
current_node_index: 0,
667-
num_nodes_current_tree: usize::try_from(num_nodes_current_tree).unwrap(),
677+
num_nodes_current_tree: usize::try_from(num_nodes_current_tree).unwrap_or(0),
668678
tree: std::marker::PhantomData,
669679
}
670680
}
@@ -692,9 +702,10 @@ struct RootIterator<'a> {
692702

693703
impl<'a> RootIterator<'a> {
694704
fn new(tree: &'a TreeInterface) -> Self {
705+
debug_assert!(tree.left_child(tree.virtual_root()).is_ok());
695706
RootIterator {
696707
current_root: None,
697-
next_root: tree.left_child(tree.virtual_root()).unwrap(),
708+
next_root: tree.left_child(tree.virtual_root()).unwrap_or(NodeId::NULL),
698709
tree,
699710
}
700711
}
@@ -707,7 +718,8 @@ impl NodeIterator for RootIterator<'_> {
707718
r => {
708719
assert!(r >= 0);
709720
let cr = Some(r);
710-
self.next_root = self.tree.right_sib(r).unwrap();
721+
debug_assert!(self.tree.right_sib(r).is_ok());
722+
self.next_root = self.tree.right_sib(r).unwrap_or(NodeId::NULL);
711723
cr
712724
}
713725
};
@@ -745,7 +757,8 @@ impl NodeIterator for ChildIterator<'_> {
745757
r => {
746758
assert!(r >= 0);
747759
let cr = Some(r);
748-
self.next_child = self.tree.right_sib(r).unwrap();
760+
debug_assert!(self.tree.right_sib(r).is_ok());
761+
self.next_child = self.tree.right_sib(r).unwrap_or(NodeId::NULL);
749762
cr
750763
}
751764
};
@@ -793,7 +806,8 @@ impl NodeIterator for ParentsIterator<'_> {
793806
r => {
794807
assert!(r >= 0);
795808
let cr = Some(r);
796-
self.next_node = self.tree.parent(r).unwrap();
809+
debug_assert!(self.tree.parent(r).is_ok());
810+
self.next_node = self.tree.parent(r).unwrap_or(NodeId::NULL);
797811
cr
798812
}
799813
};
@@ -834,14 +848,12 @@ impl NodeIterator for SamplesIterator<'_> {
834848
NodeId::NULL => None,
835849
r => {
836850
if r == self.last_sample_index {
837-
//let cr = Some(self.tree.samples(r).unwrap());
838851
let cr =
839852
Some(unsafe { *(*self.tree.as_ptr()).samples.offset(r.0 as isize) }.into());
840853
self.next_sample_index = NodeId::NULL;
841854
cr
842855
} else {
843856
assert!(r >= 0);
844-
//let cr = Some(self.tree.samples(r).unwrap());
845857
let cr =
846858
Some(unsafe { *(*self.tree.as_ptr()).samples.offset(r.0 as isize) }.into());
847859
//self.next_sample_index = self.next_sample[r];

src/trees.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ impl TreeSequence {
259259
/// This function allocates a `CString` to pass the file name to the C API.
260260
/// A panic will occur if the system runs out of memory.
261261
pub fn dump<O: Into<TableOutputOptions>>(&self, filename: &str, options: O) -> TskReturnValue {
262-
let c_str = std::ffi::CString::new(filename).unwrap();
262+
let c_str = std::ffi::CString::new(filename).map_err(|_| {
263+
TskitError::LibraryError("call to ffi::Cstring::new failed".to_string())
264+
})?;
263265
let rv = unsafe {
264266
ll_bindings::tsk_treeseq_dump(self.as_ptr(), c_str.as_ptr(), options.into().bits())
265267
};

0 commit comments

Comments
 (0)