@@ -15,9 +15,9 @@ use bindings::tsk_site_table_t;
15
15
#[ non_exhaustive]
16
16
#[ derive( Error , Debug ) ]
17
17
pub enum Error {
18
- #[ error( "{}" , 0 ) ]
18
+ #[ error( "{}" , * . 0 ) ]
19
19
Message ( String ) ,
20
- #[ error( "{}" , 0 ) ]
20
+ #[ error( "{}" , get_tskit_error_message ( * . 0 ) ) ]
21
21
Code ( i32 ) ,
22
22
}
23
23
@@ -150,6 +150,22 @@ impl LLTreeSeq {
150
150
pub fn num_samples ( & self ) -> bindings:: tsk_size_t {
151
151
unsafe { bindings:: tsk_treeseq_get_num_samples ( self . as_ptr ( ) ) }
152
152
}
153
+
154
+ fn free ( & mut self ) -> Result < ( ) , Error > {
155
+ match unsafe { bindings:: tsk_treeseq_free ( self . as_mut_ptr ( ) ) } {
156
+ code if code < 0 => Err ( Error :: Code ( code) ) ,
157
+ _ => Ok ( ( ) ) ,
158
+ }
159
+ }
160
+ }
161
+
162
+ impl Drop for LLTreeSeq {
163
+ fn drop ( & mut self ) {
164
+ match self . free ( ) {
165
+ Ok ( _) => ( ) ,
166
+ Err ( e) => panic ! ( "{:?}" , e) ,
167
+ }
168
+ }
153
169
}
154
170
155
171
fn tsk_column_access_detail < R : Into < bindings:: tsk_id_t > , L : Into < bindings:: tsk_size_t > , T : Copy > (
@@ -256,3 +272,46 @@ pub fn generate_slice_mut<'a, L: Into<bindings::tsk_size_t>, I, O>(
256
272
// SAFETY: pointer is not null, length comes from C API
257
273
unsafe { std:: slice:: from_raw_parts_mut ( data. cast :: < O > ( ) , length. into ( ) as usize ) }
258
274
}
275
+
276
+ pub fn get_tskit_error_message ( code : i32 ) -> String {
277
+ let c_str = unsafe { std:: ffi:: CStr :: from_ptr ( crate :: bindings:: tsk_strerror ( code) ) } ;
278
+ c_str
279
+ . to_str ( )
280
+ . expect ( "failed to convert c_str to &str" )
281
+ . to_owned ( )
282
+ }
283
+
284
+ #[ test]
285
+ fn test_error_message ( ) {
286
+ fn foo ( ) -> Result < ( ) , Error > {
287
+ Err ( Error :: Message ( "foobar" . to_owned ( ) ) )
288
+ }
289
+
290
+ let msg = "foobar" . to_owned ( ) ;
291
+ match foo ( ) {
292
+ Err ( Error :: Message ( m) ) => assert_eq ! ( m, msg) ,
293
+ _ => panic ! ( "unexpected match" ) ,
294
+ }
295
+ }
296
+
297
+ #[ test]
298
+ fn test_error_code ( ) {
299
+ fn foo ( ) -> Result < ( ) , Error > {
300
+ Err ( Error :: Code ( -202 ) )
301
+ }
302
+
303
+ match foo ( ) {
304
+ Err ( Error :: Code ( x) ) => {
305
+ assert_eq ! ( x, -202 ) ;
306
+ }
307
+ _ => panic ! ( "unexpected match" ) ,
308
+ }
309
+
310
+ match foo ( ) {
311
+ Err ( e) => {
312
+ let m = format ! ( "{}" , e) ;
313
+ assert_eq ! ( & m, "Node out of bounds. (TSK_ERR_NODE_OUT_OF_BOUNDS)" ) ;
314
+ }
315
+ _ => panic ! ( "unexpected match" ) ,
316
+ }
317
+ }
0 commit comments