@@ -48,12 +48,12 @@ impl Tree {
48
48
fn wrap ( num_nodes : tsk_size_t , flags : TreeFlags ) -> Self {
49
49
let temp = unsafe {
50
50
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 > ( )
52
52
} ;
53
53
if temp. is_null ( ) {
54
54
panic ! ( "out of memory" ) ;
55
55
}
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 > ( ) ) } ;
57
57
Self {
58
58
inner : mbox,
59
59
current_tree : 0 ,
@@ -410,14 +410,21 @@ impl Tree {
410
410
411
411
/// Obtain the list of samples for the current tree/tree sequence
412
412
/// as a vector.
413
+ ///
414
+ /// # Panics
415
+ ///
416
+ /// Will panic if the number of samples is too large to cast to a valid id.
413
417
#[ deprecated( since = "0.2.3" , note = "Please use Tree::sample_nodes instead" ) ]
414
418
pub fn samples_to_vec ( & self ) -> Vec < NodeId > {
415
419
let num_samples =
416
420
unsafe { ll_bindings:: tsk_treeseq_get_num_samples ( ( * self . inner ) . tree_sequence ) } ;
417
421
let mut rv = vec ! [ ] ;
418
422
419
423
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
+ } ;
421
428
rv. push ( u. into ( ) ) ;
422
429
}
423
430
rv
@@ -713,8 +720,8 @@ impl<'a> PostorderNodeIterator<'a> {
713
720
ll_bindings:: tsk_tree_postorder (
714
721
tree. as_ptr ( ) ,
715
722
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 > ( ) ,
718
725
)
719
726
} ;
720
727
@@ -999,6 +1006,10 @@ impl TreeSequence {
999
1006
/// This behavior may change in a future release, which could
1000
1007
/// break `API`.
1001
1008
///
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.
1002
1013
pub fn dump ( & self , filename : & str , options : TableOutputOptions ) -> TskReturnValue {
1003
1014
let c_str = std:: ffi:: CString :: new ( filename) . unwrap ( ) ;
1004
1015
let rv =
@@ -1077,6 +1088,9 @@ impl TreeSequence {
1077
1088
}
1078
1089
1079
1090
/// 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.
1080
1094
#[ deprecated(
1081
1095
since = "0.2.3" ,
1082
1096
note = "Please use TreeSequence::sample_nodes instead"
@@ -1086,7 +1100,10 @@ impl TreeSequence {
1086
1100
let mut rv = vec ! [ ] ;
1087
1101
1088
1102
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
+ } ;
1090
1107
rv. push ( u) ;
1091
1108
}
1092
1109
rv
@@ -1148,7 +1165,10 @@ impl TreeSequence {
1148
1165
idmap : bool ,
1149
1166
) -> Result < ( Self , Option < Vec < NodeId > > ) , TskitError > {
1150
1167
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
+ }
1152
1172
let mut ts = tables. tree_sequence ( TreeSequenceFlags :: default ( ) ) ?;
1153
1173
let mut output_node_map: Vec < NodeId > = vec ! [ ] ;
1154
1174
if idmap {
@@ -1158,12 +1178,12 @@ impl TreeSequence {
1158
1178
ll_bindings:: tsk_treeseq_simplify (
1159
1179
self . as_ptr ( ) ,
1160
1180
// NOTE: casting away const-ness:
1161
- samples. as_ptr ( ) as * mut tsk_id_t ,
1181
+ samples. as_ptr ( ) . cast :: < tsk_id_t > ( ) ,
1162
1182
samples. len ( ) as tsk_size_t ,
1163
1183
options. bits ( ) ,
1164
1184
ts. as_mut_ptr ( ) ,
1165
1185
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 > ( ) ,
1167
1187
false => std:: ptr:: null_mut ( ) ,
1168
1188
} ,
1169
1189
)
0 commit comments