File tree Expand file tree Collapse file tree 5 files changed +24
-13
lines changed Expand file tree Collapse file tree 5 files changed +24
-13
lines changed Original file line number Diff line number Diff line change @@ -11,17 +11,18 @@ use super::bindings::tsk_table_collection_free;
1111use super :: bindings:: tsk_table_collection_init;
1212use super :: bindings:: tsk_table_collection_t;
1313use super :: tskbox:: TskBox ;
14+ use super :: Error ;
1415
1516pub struct TableCollection ( TskBox < tsk_table_collection_t > ) ;
1617
1718impl TableCollection {
18- pub fn new ( sequence_length : f64 ) -> Self {
19+ pub fn new ( sequence_length : f64 ) -> Result < Self , Error > {
1920 let mut tsk = TskBox :: new (
2021 |tc : * mut tsk_table_collection_t | unsafe { tsk_table_collection_init ( tc, 0 ) } ,
2122 tsk_table_collection_free,
22- ) ;
23+ ) ? ;
2324 tsk. as_mut ( ) . sequence_length = sequence_length;
24- Self ( tsk)
25+ Ok ( Self ( tsk) )
2526 }
2627
2728 // # Safety
Original file line number Diff line number Diff line change @@ -49,7 +49,8 @@ macro_rules! basic_llowningtable_impl {
4949 let table = super :: tskbox:: TskBox :: new(
5050 |x: * mut super :: bindings:: $tsktable| unsafe { super :: bindings:: $init( x, 0 ) } ,
5151 super :: bindings:: $free,
52- ) ;
52+ )
53+ . unwrap( ) ;
5354 Self ( table)
5455 }
5556
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ impl<'treeseq> LLTree<'treeseq> {
2121 super :: bindings:: tsk_tree_init ( x, treeseq. as_ref ( ) , flags. bits ( ) )
2222 } ,
2323 super :: bindings:: tsk_tree_free,
24- ) ;
24+ ) ? ;
2525 // Gotta ask Jerome about this one--why isn't this handled in tsk_tree_init??
2626 if !flags. contains ( TreeFlags :: NO_SAMPLE_COUNTS ) {
2727 // SAFETY: nobody is null here.
Original file line number Diff line number Diff line change 11use std:: ptr:: NonNull ;
22
3+ use super :: Error ;
4+
35#[ derive( Debug ) ]
46pub struct TskBox < T > {
57 tsk : NonNull < T > ,
@@ -19,11 +21,15 @@ impl<T> TskBox<T> {
1921 pub fn new < F : Fn ( * mut T ) -> i32 > (
2022 init : F ,
2123 teardown : unsafe extern "C" fn ( * mut T ) -> i32 ,
22- ) -> Self {
24+ ) -> Result < Self , Error > {
2325 // SAFETY: we will initialize it next
2426 let mut uninit = unsafe { Self :: new_uninit ( teardown) } ;
25- let _ = init ( uninit. as_mut ( ) ) ;
26- uninit
27+ let rv = init ( uninit. as_mut ( ) ) ;
28+ if rv < 0 {
29+ Err ( Error :: Code ( rv) )
30+ } else {
31+ Ok ( uninit)
32+ }
2733 }
2834
2935 // # Safety
@@ -136,7 +142,8 @@ fn test_miri() {
136142 0
137143 } ,
138144 teardown_x,
139- ) ;
145+ )
146+ . unwrap ( ) ;
140147
141148 let _ = unsafe { TskBox :: new_borrowed ( & b) } ;
142149
@@ -161,7 +168,8 @@ fn test_into_raw_miri() {
161168 0
162169 } ,
163170 teardown_x,
164- ) ;
171+ )
172+ . unwrap ( ) ;
165173
166174 let p = unsafe { b. into_raw ( ) } ;
167175
@@ -187,6 +195,7 @@ fn test_table_collection_tskbox_shared_ptr() {
187195 super :: bindings:: tsk_table_collection_init ( t, flags)
188196 } ,
189197 super :: bindings:: tsk_table_collection_free,
190- ) ;
198+ )
199+ . unwrap ( ) ;
191200 let _ = unsafe { TskBox :: new_borrowed ( & tables) } ;
192201}
Original file line number Diff line number Diff line change @@ -80,7 +80,7 @@ impl TableCollection {
8080 expected : "sequence_length >= 0.0" . to_string ( ) ,
8181 } ) ;
8282 }
83- let mut inner = LLTableCollection :: new ( sequence_length. into ( ) ) ;
83+ let mut inner = LLTableCollection :: new ( sequence_length. into ( ) ) ? ;
8484 let views = crate :: table_views:: TableViews :: new_from_ll_table_collection ( & mut inner) ?;
8585 Ok ( Self {
8686 inner,
@@ -101,7 +101,7 @@ impl TableCollection {
101101
102102 pub ( crate ) fn into_raw ( self ) -> Result < * mut ll_bindings:: tsk_table_collection_t , TskitError > {
103103 let mut tables = self ;
104- let mut temp = crate :: sys:: TableCollection :: new ( 1. ) ;
104+ let mut temp = crate :: sys:: TableCollection :: new ( 1. ) ? ;
105105 std:: mem:: swap ( & mut temp, & mut tables. inner ) ;
106106 let ptr = temp. as_mut_ptr ( ) ;
107107 std:: mem:: forget ( temp) ;
You can’t perform that action at this time.
0 commit comments