@@ -3,6 +3,7 @@ use std::vec;
3
3
4
4
use crate :: error:: TskitError ;
5
5
use crate :: sys:: bindings as ll_bindings;
6
+ use crate :: sys:: TableCollection as LLTableCollection ;
6
7
use crate :: types:: Bookmark ;
7
8
use crate :: IndividualTableSortOptions ;
8
9
use crate :: Position ;
@@ -17,8 +18,6 @@ use crate::TskReturnValue;
17
18
use crate :: { EdgeId , NodeId } ;
18
19
use ll_bindings:: tsk_id_t;
19
20
use ll_bindings:: tsk_size_t;
20
- use ll_bindings:: tsk_table_collection_free;
21
- use mbox:: MBox ;
22
21
23
22
/// A table collection.
24
23
///
@@ -54,31 +53,11 @@ use mbox::MBox;
54
53
/// ```
55
54
///
56
55
pub struct TableCollection {
57
- inner : MBox < ll_bindings :: tsk_table_collection_t > ,
56
+ inner : LLTableCollection ,
58
57
idmap : Vec < NodeId > ,
59
58
views : crate :: table_views:: TableViews ,
60
59
}
61
60
62
- impl Drop for TableCollection {
63
- fn drop ( & mut self ) {
64
- let rv = unsafe { tsk_table_collection_free ( self . as_mut_ptr ( ) ) } ;
65
- assert_eq ! ( rv, 0 ) ;
66
- }
67
- }
68
-
69
- /// Returns a pointer to an uninitialized tsk_table_collection_t
70
- pub ( crate ) fn uninit_table_collection ( ) -> MBox < ll_bindings:: tsk_table_collection_t > {
71
- let temp = unsafe {
72
- libc:: malloc ( std:: mem:: size_of :: < ll_bindings:: tsk_table_collection_t > ( ) )
73
- as * mut ll_bindings:: tsk_table_collection_t
74
- } ;
75
- let nonnull = match std:: ptr:: NonNull :: < ll_bindings:: tsk_table_collection_t > :: new ( temp) {
76
- Some ( x) => x,
77
- None => panic ! ( "out of memory" ) ,
78
- } ;
79
- unsafe { MBox :: from_non_null_raw ( nonnull) }
80
- }
81
-
82
61
impl TableCollection {
83
62
/// Create a new table collection with a sequence length.
84
63
///
@@ -101,26 +80,13 @@ impl TableCollection {
101
80
expected : "sequence_length >= 0.0" . to_string ( ) ,
102
81
} ) ;
103
82
}
104
- let mut mbox = uninit_table_collection ( ) ;
105
- let rv = unsafe { ll_bindings:: tsk_table_collection_init ( & mut * mbox, 0 ) } ;
106
- if rv < 0 {
107
- return Err ( crate :: error:: TskitError :: ErrorCode { code : rv } ) ;
108
- }
109
- let views = crate :: table_views:: TableViews :: new_from_mbox_table_collection ( & mut mbox) ?;
110
- // AHA?
111
- assert ! ( std:: ptr:: eq(
112
- & mbox. as_ref( ) . edges as * const ll_bindings:: tsk_edge_table_t,
113
- views. edges( ) . as_ref( ) as * const ll_bindings:: tsk_edge_table_t
114
- ) ) ;
115
- let mut tables = Self {
116
- inner : mbox,
83
+ let mut inner = LLTableCollection :: new ( sequence_length. into ( ) ) ;
84
+ let views = crate :: table_views:: TableViews :: new_from_ll_table_collection ( & mut inner) ?;
85
+ Ok ( Self {
86
+ inner,
117
87
idmap : vec ! [ ] ,
118
88
views,
119
- } ;
120
- unsafe {
121
- ( * tables. as_mut_ptr ( ) ) . sequence_length = f64:: from ( sequence_length) ;
122
- }
123
- Ok ( tables)
89
+ } )
124
90
}
125
91
126
92
/// # Safety
@@ -130,31 +96,23 @@ impl TableCollection {
130
96
/// Or, it may be initialized and about to be used in a part of the C API
131
97
/// requiring an uninitialized table collection.
132
98
/// Consult the C API docs before using!
133
- pub ( crate ) unsafe fn new_from_mbox (
134
- mbox : MBox < ll_bindings:: tsk_table_collection_t > ,
135
- ) -> Result < Self , TskitError > {
136
- let mut mbox = mbox;
137
- let views = crate :: table_views:: TableViews :: new_from_mbox_table_collection ( & mut mbox) ?;
99
+ pub ( crate ) unsafe fn new_from_ll ( lltables : LLTableCollection ) -> Result < Self , TskitError > {
100
+ let mut inner = lltables;
101
+ let views = crate :: table_views:: TableViews :: new_from_ll_table_collection ( & mut inner) ?;
138
102
Ok ( Self {
139
- inner : mbox ,
103
+ inner,
140
104
idmap : vec ! [ ] ,
141
105
views,
142
106
} )
143
107
}
144
108
145
109
pub ( crate ) fn into_raw ( self ) -> Result < * mut ll_bindings:: tsk_table_collection_t , TskitError > {
146
110
let mut tables = self ;
147
- // rust won't let use move inner out b/c this type implements Drop.
148
- // So we have to replace the existing pointer with a new one.
149
- let table_ptr = unsafe {
150
- libc:: malloc ( std:: mem:: size_of :: < ll_bindings:: tsk_table_collection_t > ( ) )
151
- as * mut ll_bindings:: tsk_table_collection_t
152
- } ;
153
- let rv = unsafe { ll_bindings:: tsk_table_collection_init ( table_ptr, 0 ) } ;
154
-
155
- let mut temp = unsafe { MBox :: from_raw ( table_ptr) } ;
111
+ let mut temp = crate :: sys:: TableCollection :: new ( 1. ) ;
156
112
std:: mem:: swap ( & mut temp, & mut tables. inner ) ;
157
- handle_tsk_return_value ! ( rv, MBox :: into_raw( temp) )
113
+ let ptr = temp. as_mut_ptr ( ) ;
114
+ std:: mem:: forget ( temp) ;
115
+ handle_tsk_return_value ! ( 0 , ptr)
158
116
}
159
117
160
118
/// Load a table collection from a file.
@@ -214,7 +172,7 @@ impl TableCollection {
214
172
/// assert_eq!(tables.sequence_length(), 100.0);
215
173
/// ```
216
174
pub fn sequence_length ( & self ) -> Position {
217
- unsafe { ( * self . as_ptr ( ) ) . sequence_length } . into ( )
175
+ self . inner . sequence_length ( ) . into ( )
218
176
}
219
177
220
178
edge_table_add_row ! (
@@ -789,15 +747,12 @@ impl TableCollection {
789
747
790
748
/// Return a "deep" copy of the tables.
791
749
pub fn deepcopy ( & self ) -> Result < TableCollection , TskitError > {
792
- // The output is UNINITIALIZED tables,
793
- // else we leak memory
794
- let mut inner = uninit_table_collection ( ) ;
795
-
796
- let rv = unsafe { ll_bindings:: tsk_table_collection_copy ( self . as_ptr ( ) , & mut * inner, 0 ) } ;
750
+ let ( rv, inner) = self . inner . copy ( ) ;
797
751
798
752
// SAFETY: we just initialized it.
799
753
// The C API doesn't free NULL pointers.
800
- handle_tsk_return_value ! ( rv, unsafe { Self :: new_from_mbox( inner) ? } )
754
+ let tables = unsafe { TableCollection :: new_from_ll ( inner) } ?;
755
+ handle_tsk_return_value ! ( rv, tables)
801
756
}
802
757
803
758
/// Return a [`crate::TreeSequence`] based on the tables.
@@ -984,7 +939,7 @@ impl TableCollection {
984
939
// to create with null pointers.
985
940
let rv = unsafe {
986
941
ll_bindings:: tsk_edge_table_set_columns (
987
- & mut self . inner . edges ,
942
+ self . inner . edges_mut ( ) ,
988
943
( * edges. as_ptr ( ) ) . num_rows ,
989
944
( * edges. as_ptr ( ) ) . left ,
990
945
( * edges. as_ptr ( ) ) . right ,
@@ -1021,7 +976,7 @@ impl TableCollection {
1021
976
// to create with null pointers.
1022
977
let rv = unsafe {
1023
978
ll_bindings:: tsk_node_table_set_columns (
1024
- & mut self . inner . nodes ,
979
+ self . inner . nodes_mut ( ) ,
1025
980
( * nodes. as_ptr ( ) ) . num_rows ,
1026
981
( * nodes. as_ptr ( ) ) . flags ,
1027
982
( * nodes. as_ptr ( ) ) . time ,
@@ -1058,7 +1013,7 @@ impl TableCollection {
1058
1013
// to create with null pointers.
1059
1014
let rv = unsafe {
1060
1015
ll_bindings:: tsk_site_table_set_columns (
1061
- & mut self . inner . sites ,
1016
+ self . inner . sites_mut ( ) ,
1062
1017
( * sites. as_ptr ( ) ) . num_rows ,
1063
1018
( * sites. as_ptr ( ) ) . position ,
1064
1019
( * sites. as_ptr ( ) ) . ancestral_state ,
@@ -1094,7 +1049,7 @@ impl TableCollection {
1094
1049
// to create with null pointers.
1095
1050
let rv = unsafe {
1096
1051
ll_bindings:: tsk_mutation_table_set_columns (
1097
- & mut self . inner . mutations ,
1052
+ self . inner . mutations_mut ( ) ,
1098
1053
( * mutations. as_ptr ( ) ) . num_rows ,
1099
1054
( * mutations. as_ptr ( ) ) . site ,
1100
1055
( * mutations. as_ptr ( ) ) . node ,
@@ -1137,7 +1092,7 @@ impl TableCollection {
1137
1092
// to create with null pointers.
1138
1093
let rv = unsafe {
1139
1094
ll_bindings:: tsk_individual_table_set_columns (
1140
- & mut self . inner . individuals ,
1095
+ self . inner . individuals_mut ( ) ,
1141
1096
( * individuals. as_ptr ( ) ) . num_rows ,
1142
1097
( * individuals. as_ptr ( ) ) . flags ,
1143
1098
( * individuals. as_ptr ( ) ) . location ,
@@ -1175,7 +1130,7 @@ impl TableCollection {
1175
1130
// to create with null pointers.
1176
1131
let rv = unsafe {
1177
1132
ll_bindings:: tsk_migration_table_set_columns (
1178
- & mut self . inner . migrations ,
1133
+ self . inner . migrations_mut ( ) ,
1179
1134
( * migrations. as_ptr ( ) ) . num_rows ,
1180
1135
( * migrations. as_ptr ( ) ) . left ,
1181
1136
( * migrations. as_ptr ( ) ) . right ,
@@ -1216,7 +1171,7 @@ impl TableCollection {
1216
1171
// to create with null pointers.
1217
1172
let rv = unsafe {
1218
1173
ll_bindings:: tsk_population_table_set_columns (
1219
- & mut self . inner . populations ,
1174
+ self . inner . populations_mut ( ) ,
1220
1175
( * populations. as_ptr ( ) ) . num_rows ,
1221
1176
( * populations. as_ptr ( ) ) . metadata ,
1222
1177
( * populations. as_ptr ( ) ) . metadata_offset ,
@@ -1257,7 +1212,7 @@ impl TableCollection {
1257
1212
// to create with null pointers.
1258
1213
let rv = unsafe {
1259
1214
ll_bindings:: tsk_provenance_table_set_columns (
1260
- & mut self . inner . provenances ,
1215
+ self . inner . provenances_mut ( ) ,
1261
1216
( * provenances. as_ptr ( ) ) . num_rows ,
1262
1217
( * provenances. as_ptr ( ) ) . timestamp ,
1263
1218
( * provenances. as_ptr ( ) ) . timestamp_offset ,
@@ -1279,11 +1234,11 @@ impl TableCollection {
1279
1234
1280
1235
/// Pointer to the low-level C type.
1281
1236
pub fn as_ptr ( & self ) -> * const ll_bindings:: tsk_table_collection_t {
1282
- & * self . inner
1237
+ self . inner . as_ptr ( )
1283
1238
}
1284
1239
1285
1240
/// Mutable pointer to the low-level C type.
1286
1241
pub fn as_mut_ptr ( & mut self ) -> * mut ll_bindings:: tsk_table_collection_t {
1287
- & mut * self . inner
1242
+ self . inner . as_mut_ptr ( )
1288
1243
}
1289
1244
}
0 commit comments