@@ -48,12 +48,12 @@ impl Tree {
4848 fn wrap ( num_nodes : tsk_size_t , flags : TreeFlags ) -> Self {
4949 let temp = unsafe {
5050 libc:: malloc ( std:: mem:: size_of :: < ll_bindings:: tsk_tree_t > ( ) )
51- as * mut ll_bindings:: tsk_tree_t
51+ . cast :: < ll_bindings:: tsk_tree_t > ( )
5252 } ;
5353 if temp. is_null ( ) {
5454 panic ! ( "out of memory" ) ;
5555 }
56- let mbox = unsafe { MBox :: from_raw ( temp as * mut ll_bindings:: tsk_tree_t ) } ;
56+ let mbox = unsafe { MBox :: from_raw ( temp. cast :: < ll_bindings:: tsk_tree_t > ( ) ) } ;
5757 Self {
5858 inner : mbox,
5959 current_tree : 0 ,
@@ -410,14 +410,21 @@ impl Tree {
410410
411411 /// Obtain the list of samples for the current tree/tree sequence
412412 /// as a vector.
413+ ///
414+ /// # Panics
415+ ///
416+ /// Will panic if the number of samples is too large to cast to a valid id.
413417 #[ deprecated( since = "0.2.3" , note = "Please use Tree::sample_nodes instead" ) ]
414418 pub fn samples_to_vec ( & self ) -> Vec < NodeId > {
415419 let num_samples =
416420 unsafe { ll_bindings:: tsk_treeseq_get_num_samples ( ( * self . inner ) . tree_sequence ) } ;
417421 let mut rv = vec ! [ ] ;
418422
419423 for i in 0 ..num_samples {
420- let u = unsafe { * ( * ( * self . inner ) . tree_sequence ) . samples . offset ( i as isize ) } ;
424+ let u = match isize:: try_from ( i) {
425+ Ok ( o) => unsafe { * ( * ( * self . inner ) . tree_sequence ) . samples . offset ( o) } ,
426+ Err ( e) => panic ! ( "{e}" ) ,
427+ } ;
421428 rv. push ( u. into ( ) ) ;
422429 }
423430 rv
@@ -713,8 +720,8 @@ impl<'a> PostorderNodeIterator<'a> {
713720 ll_bindings:: tsk_tree_postorder (
714721 tree. as_ptr ( ) ,
715722 NodeId :: NULL . into ( ) , // start from virtual root
716- nodes. as_mut_ptr ( ) as * mut tsk_id_t ,
717- ptr as * mut tsk_size_t ,
723+ nodes. as_mut_ptr ( ) . cast :: < tsk_id_t > ( ) ,
724+ ptr. cast :: < tsk_size_t > ( ) ,
718725 )
719726 } ;
720727
@@ -999,6 +1006,10 @@ impl TreeSequence {
9991006 /// This behavior may change in a future release, which could
10001007 /// break `API`.
10011008 ///
1009+ /// # Panics
1010+ ///
1011+ /// This function allocates a `CString` to pass the file name to the C API.
1012+ /// A panic will occur if the system runs out of memory.
10021013 pub fn dump ( & self , filename : & str , options : TableOutputOptions ) -> TskReturnValue {
10031014 let c_str = std:: ffi:: CString :: new ( filename) . unwrap ( ) ;
10041015 let rv =
@@ -1077,6 +1088,9 @@ impl TreeSequence {
10771088 }
10781089
10791090 /// Get the list of samples as a vector.
1091+ /// # Panics
1092+ ///
1093+ /// Will panic if the number of samples is too large to cast to a valid id.
10801094 #[ deprecated(
10811095 since = "0.2.3" ,
10821096 note = "Please use TreeSequence::sample_nodes instead"
@@ -1086,7 +1100,10 @@ impl TreeSequence {
10861100 let mut rv = vec ! [ ] ;
10871101
10881102 for i in 0 ..num_samples {
1089- let u = NodeId :: from ( unsafe { * ( * self . as_ptr ( ) ) . samples . offset ( i as isize ) } ) ;
1103+ let u = match isize:: try_from ( i) {
1104+ Ok ( o) => NodeId :: from ( unsafe { * ( * self . as_ptr ( ) ) . samples . offset ( o) } ) ,
1105+ Err ( e) => panic ! ( "{e}" ) ,
1106+ } ;
10901107 rv. push ( u) ;
10911108 }
10921109 rv
@@ -1148,7 +1165,10 @@ impl TreeSequence {
11481165 idmap : bool ,
11491166 ) -> Result < ( Self , Option < Vec < NodeId > > ) , TskitError > {
11501167 let mut tables = TableCollection :: new ( unsafe { ( * ( * self . inner ) . tables ) . sequence_length } ) ?;
1151- tables. build_index ( ) . unwrap ( ) ;
1168+ match tables. build_index ( ) {
1169+ Ok ( _) => ( ) ,
1170+ Err ( e) => return Err ( e) ,
1171+ }
11521172 let mut ts = tables. tree_sequence ( TreeSequenceFlags :: default ( ) ) ?;
11531173 let mut output_node_map: Vec < NodeId > = vec ! [ ] ;
11541174 if idmap {
@@ -1158,12 +1178,12 @@ impl TreeSequence {
11581178 ll_bindings:: tsk_treeseq_simplify (
11591179 self . as_ptr ( ) ,
11601180 // NOTE: casting away const-ness:
1161- samples. as_ptr ( ) as * mut tsk_id_t ,
1181+ samples. as_ptr ( ) . cast :: < tsk_id_t > ( ) ,
11621182 samples. len ( ) as tsk_size_t ,
11631183 options. bits ( ) ,
11641184 ts. as_mut_ptr ( ) ,
11651185 match idmap {
1166- true => output_node_map. as_mut_ptr ( ) as * mut tsk_id_t ,
1186+ true => output_node_map. as_mut_ptr ( ) . cast :: < tsk_id_t > ( ) ,
11671187 false => std:: ptr:: null_mut ( ) ,
11681188 } ,
11691189 )
0 commit comments