11use crate :: bindings as ll_bindings;
22use crate :: error:: TskitRustError ;
3+ use crate :: ffi:: TskitType ;
34use crate :: types:: Bookmark ;
45use crate :: EdgeTable ;
56use crate :: MutationTable ;
@@ -8,19 +9,7 @@ use crate::PopulationTable;
89use crate :: SiteTable ;
910use crate :: TskReturnValue ;
1011use crate :: { tsk_flags_t, tsk_id_t, tsk_size_t} ;
11-
12- /// Handle allocation details.
13- fn new_tsk_table_collection_t ( ) -> Result < Box < ll_bindings:: tsk_table_collection_t > , TskitRustError >
14- {
15- let mut tsk_tables: std:: mem:: MaybeUninit < ll_bindings:: tsk_table_collection_t > =
16- std:: mem:: MaybeUninit :: uninit ( ) ;
17- let rv = unsafe { ll_bindings:: tsk_table_collection_init ( tsk_tables. as_mut_ptr ( ) , 0 ) } ;
18- if rv < 0 {
19- return Err ( TskitRustError :: ErrorCode { code : rv } ) ;
20- }
21- let rv = unsafe { Box :: < ll_bindings:: tsk_table_collection_t > :: new ( tsk_tables. assume_init ( ) ) } ;
22- Ok ( rv)
23- }
12+ use ll_bindings:: tsk_table_collection_free;
2413
2514/// A table collection.
2615///
@@ -69,9 +58,15 @@ fn new_tsk_table_collection_t() -> Result<Box<ll_bindings::tsk_table_collection_
6958///
7059/// Addressing point 3 may require API breakage.
7160pub struct TableCollection {
72- tables : Box < ll_bindings:: tsk_table_collection_t > ,
61+ inner : Box < ll_bindings:: tsk_table_collection_t > ,
7362}
7463
64+ build_tskit_type ! (
65+ TableCollection ,
66+ ll_bindings:: tsk_table_collection_t,
67+ tsk_table_collection_free
68+ ) ;
69+
7570impl TableCollection {
7671 /// Create a new table collection with a sequence length.
7772 pub fn new ( sequence_length : f64 ) -> Result < Self , TskitRustError > {
@@ -81,16 +76,13 @@ impl TableCollection {
8176 expected : "sequence_length >= 0.0" . to_string ( ) ,
8277 } ) ;
8378 }
84- let tables = new_tsk_table_collection_t ( ) ;
85- match tables {
86- Ok ( _ ) => ( ) ,
87- Err ( e ) => return Err ( e ) ,
79+ let mut tables = Self :: wrap ( ) ;
80+ let rv = unsafe { ll_bindings :: tsk_table_collection_init ( tables . as_mut_ptr ( ) , 0 ) } ;
81+ if rv < 0 {
82+ return Err ( crate :: error :: TskitRustError :: ErrorCode { code : rv } ) ;
8883 }
89- let mut rv = TableCollection {
90- tables : tables. unwrap ( ) ,
91- } ;
92- rv. tables . sequence_length = sequence_length;
93- Ok ( rv)
84+ tables. inner . sequence_length = sequence_length;
85+ Ok ( tables)
9486 }
9587
9688 /// Load a table collection from a file.
@@ -119,16 +111,6 @@ impl TableCollection {
119111 }
120112 }
121113
122- /// Access to raw C pointer as const tsk_table_collection_t *.
123- pub fn as_ptr ( & self ) -> * const ll_bindings:: tsk_table_collection_t {
124- & * self . tables
125- }
126-
127- /// Access to raw C pointer as tsk_table_collection_t *.
128- pub fn as_mut_ptr ( & mut self ) -> * mut ll_bindings:: tsk_table_collection_t {
129- & mut * self . tables
130- }
131-
132114 /// Length of the sequence/"genome".
133115 pub fn sequence_length ( & self ) -> f64 {
134116 unsafe { ( * self . as_ptr ( ) ) . sequence_length }
@@ -138,35 +120,35 @@ impl TableCollection {
138120 /// Lifetime of return value is tied to (this)
139121 /// parent object.
140122 pub fn edges < ' a > ( & ' a self ) -> EdgeTable < ' a > {
141- EdgeTable :: < ' a > :: new_from_table ( & self . tables . edges )
123+ EdgeTable :: < ' a > :: new_from_table ( & self . inner . edges )
142124 }
143125
144126 /// Get reference to the [``NodeTable``](crate::NodeTable).
145127 /// Lifetime of return value is tied to (this)
146128 /// parent object.
147129 pub fn nodes < ' a > ( & ' a self ) -> NodeTable < ' a > {
148- NodeTable :: < ' a > :: new_from_table ( & self . tables . nodes )
130+ NodeTable :: < ' a > :: new_from_table ( & self . inner . nodes )
149131 }
150132
151133 /// Get reference to the [``SiteTable``](crate::SiteTable).
152134 /// Lifetime of return value is tied to (this)
153135 /// parent object.
154136 pub fn sites < ' a > ( & ' a self ) -> SiteTable < ' a > {
155- SiteTable :: < ' a > :: new_from_table ( & self . tables . sites )
137+ SiteTable :: < ' a > :: new_from_table ( & self . inner . sites )
156138 }
157139
158140 /// Get reference to the [``MutationTable``](crate::MutationTable).
159141 /// Lifetime of return value is tied to (this)
160142 /// parent object.
161143 pub fn mutations < ' a > ( & ' a self ) -> MutationTable < ' a > {
162- MutationTable :: < ' a > :: new_from_table ( & self . tables . mutations )
144+ MutationTable :: < ' a > :: new_from_table ( & self . inner . mutations )
163145 }
164146
165147 /// Get reference to the [``PopulationTable``](crate::PopulationTable).
166148 /// Lifetime of return value is tied to (this)
167149 /// parent object.
168150 pub fn populations < ' a > ( & ' a self ) -> PopulationTable < ' a > {
169- PopulationTable :: < ' a > :: new_from_table ( & self . tables . populations )
151+ PopulationTable :: < ' a > :: new_from_table ( & self . inner . populations )
170152 }
171153
172154 /// Add a row to the edge table
@@ -347,13 +329,6 @@ impl TableCollection {
347329 }
348330}
349331
350- impl Drop for TableCollection {
351- fn drop ( & mut self ) {
352- let rv = unsafe { ll_bindings:: tsk_table_collection_free ( & mut * self . tables ) } ;
353- panic_on_tskit_error ! ( rv) ;
354- }
355- }
356-
357332#[ cfg( test) ]
358333mod test {
359334 use super :: * ;
0 commit comments