File tree 2 files changed +26
-4
lines changed
2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -151,6 +151,21 @@ impl TableCollection {
151
151
Ok ( tables)
152
152
}
153
153
154
+ pub ( crate ) fn into_raw ( self ) -> Result < * mut ll_bindings:: tsk_table_collection_t , TskitError > {
155
+ let mut tables = self ;
156
+ // rust won't let use move inner out b/c this type implements Drop.
157
+ // So we have to replace the existing pointer with a new one.
158
+ let table_ptr = unsafe {
159
+ libc:: malloc ( std:: mem:: size_of :: < ll_bindings:: tsk_table_collection_t > ( ) )
160
+ as * mut ll_bindings:: tsk_table_collection_t
161
+ } ;
162
+ let rv = unsafe { ll_bindings:: tsk_table_collection_init ( table_ptr, 0 ) } ;
163
+
164
+ let mut temp = unsafe { MBox :: from_raw ( table_ptr) } ;
165
+ std:: mem:: swap ( & mut temp, & mut tables. inner ) ;
166
+ handle_tsk_return_value ! ( rv, MBox :: into_raw( temp) )
167
+ }
168
+
154
169
/// Load a table collection from a file.
155
170
///
156
171
/// # Panics
Original file line number Diff line number Diff line change @@ -1000,15 +1000,22 @@ impl TreeSequence {
1000
1000
/// let tree_sequence = tskit::TreeSequence::new(tables,
1001
1001
/// tskit::TreeSequenceFlags::default()).unwrap();
1002
1002
/// ```
1003
+ ///
1004
+ /// ## Note
1005
+ ///
1006
+ /// This function makes *no extra copies* of the tables.
1007
+ /// There is, however, a temporary allocation of an empty table collection
1008
+ /// in order to convince rust that we are safely handling all memory.
1003
1009
pub fn new < F : Into < TreeSequenceFlags > > (
1004
1010
tables : TableCollection ,
1005
1011
flags : F ,
1006
1012
) -> Result < Self , TskitError > {
1007
- let mut t = tables;
1008
1013
let mut treeseq = Self :: wrap ( ) ;
1009
- let rv = unsafe {
1010
- ll_bindings:: tsk_treeseq_init ( treeseq. as_mut_ptr ( ) , t. as_mut_ptr ( ) , flags. into ( ) . bits ( ) )
1011
- } ;
1014
+ let mut flags: u32 = flags. into ( ) . bits ( ) ;
1015
+ flags |= ll_bindings:: TSK_TAKE_OWNERSHIP ;
1016
+ let raw_tables_ptr = tables. into_raw ( ) ?;
1017
+ let rv =
1018
+ unsafe { ll_bindings:: tsk_treeseq_init ( treeseq. as_mut_ptr ( ) , raw_tables_ptr, flags) } ;
1012
1019
handle_tsk_return_value ! ( rv, treeseq)
1013
1020
}
1014
1021
You can’t perform that action at this time.
0 commit comments