-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmod.rs
More file actions
257 lines (236 loc) · 6.73 KB
/
mod.rs
File metadata and controls
257 lines (236 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use thiserror::Error;
mod macros;
#[allow(dead_code)]
#[allow(deref_nullptr)]
#[allow(rustdoc::broken_intra_doc_links)]
pub mod bindings;
mod edge_table;
pub mod flags;
mod individual_table;
mod migration_table;
mod mutation_table;
pub mod newtypes;
mod node_table;
mod population_table;
#[cfg(feature = "provenance")]
mod provenance_table;
mod site_table;
mod table_collection;
mod trait_impls;
mod traits;
mod tree;
mod treeseq;
mod tskbox;
// tskit defines this via a type cast
// in a macro. bindgen thus misses it.
// See bindgen issue 316.
/// "Null" identifier value.
pub(crate) const TSK_NULL: bindings::tsk_id_t = -1;
pub use edge_table::EdgeTable;
pub use individual_table::IndividualTable;
pub use migration_table::MigrationTable;
pub use mutation_table::MutationTable;
pub use node_table::NodeTable;
pub use population_table::PopulationTable;
#[cfg(feature = "provenance")]
pub use provenance_table::ProvenanceTable;
pub use site_table::SiteTable;
pub use table_collection::*;
pub use tree::LLTree;
pub use tree::NodeTraversalOrder;
pub use treeseq::TreeSequence;
use traits::TskTeardown;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum TskitError {
/// Returned when conversion attempts fail
#[error("range error: {}", *.0)]
RangeError(String),
/// Used when bad input is encountered.
#[error("we received {} but expected {}",*got, *expected)]
ValueError { got: String, expected: String },
/// Used when array access is out of range.
/// Typically, this is used when accessing
/// arrays allocated on the C side.
#[error("Invalid index")]
IndexError,
/// Raised when samples are requested from
/// [`crate::Tree`] objects, but sample lists are
/// not being updated.
#[error("Not tracking samples in Trees")]
NotTrackingSamples,
/// Wrapper around tskit C API error codes.
#[error("{}", get_tskit_error_message(*code))]
ErrorCode { code: i32 },
/// A redirection of [``crate::metadata::MetadataError``]
#[error("{value:?}")]
MetadataError {
/// The redirected error
#[from]
value: MetadataError,
},
/// General error variant
#[error("{}", *.0)]
LibraryError(String),
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum MetadataError {
/// Error related to types implementing
/// metadata serialization.
#[error("{}", *value)]
RoundtripError {
#[from]
value: Box<dyn std::error::Error + Send + Sync>,
},
}
//#[non_exhaustive]
//#[derive(Error, Debug)]
//pub enum Error {
// #[error("{}", *.0)]
// Message(String),
// #[error("{}", get_tskit_error_message(*.0))]
// Code(i32),
//}
fn tsk_column_access_detail<R: Into<bindings::tsk_id_t>, L: Into<bindings::tsk_size_t>, T: Copy>(
row: R,
column: *const T,
column_length: L,
) -> Option<T> {
let row = row.into();
let column_length = column_length.into();
if row < 0 || (row as crate::sys::bindings::tsk_size_t) >= column_length {
None
} else {
assert!(!column.is_null());
// SAFETY: pointer is not null.
// column_length is assumed to come directly
// from a table.
Some(unsafe { *column.offset(row as isize) })
}
}
pub fn tsk_column_access<
O: From<T>,
R: Into<bindings::tsk_id_t>,
L: Into<bindings::tsk_size_t>,
T: Copy,
>(
row: R,
column: *const T,
column_length: L,
) -> Option<O> {
tsk_column_access_detail(row, column, column_length).map(|v| v.into())
}
fn tsk_ragged_column_access_detail<
R: Into<bindings::tsk_id_t>,
L: Into<bindings::tsk_size_t>,
T: Copy,
>(
row: R,
column: *const T,
column_length: L,
offset: *const bindings::tsk_size_t,
offset_length: bindings::tsk_size_t,
) -> Option<(*const T, usize)> {
let row = row.into();
let column_length = column_length.into();
if row < 0 || row as bindings::tsk_size_t > column_length || offset_length == 0 {
None
} else {
assert!(!column.is_null());
assert!(!offset.is_null());
// SAFETY: pointers are not null
// and *_length are given by tskit-c
let index = row as isize;
let start = unsafe { *offset.offset(index) };
let stop = if (row as bindings::tsk_size_t) < column_length {
unsafe { *offset.offset(index + 1) }
} else {
offset_length
};
if start == stop {
None
} else {
Some((
unsafe { column.offset(start as isize) },
stop as usize - start as usize,
))
}
}
}
pub fn tsk_ragged_column_access<
'a,
O,
R: Into<bindings::tsk_id_t>,
L: Into<bindings::tsk_size_t>,
T: Copy,
>(
row: R,
column: *const T,
column_length: L,
offset: *const bindings::tsk_size_t,
offset_length: bindings::tsk_size_t,
) -> Option<&'a [O]> {
// SAFETY: see tsk_ragged_column_access_detail
tsk_ragged_column_access_detail(row, column, column_length, offset, offset_length)
.map(|(p, n)| unsafe { std::slice::from_raw_parts(p.cast::<O>(), n) })
}
/// # SAFETY
///
/// * data must not be NULL
/// * length must be a valid offset from data
/// (ideally it comes from the tskit-c API)
pub unsafe fn generate_slice<'a, L: Into<bindings::tsk_size_t>, I, O>(
data: *const I,
length: L,
) -> &'a [O] {
std::slice::from_raw_parts(data.cast::<O>(), length.into() as usize)
}
/// # SAFETY
///
/// * data must not be NULL
/// * length must be a valid offset from data
/// (ideally it comes from the tskit-c API)
pub unsafe fn generate_slice_mut<'a, L: Into<bindings::tsk_size_t>, I, O>(
data: *mut I,
length: L,
) -> &'a mut [O] {
std::slice::from_raw_parts_mut(data.cast::<O>(), length.into() as usize)
}
pub fn get_tskit_error_message(code: i32) -> String {
let c_str = unsafe { std::ffi::CStr::from_ptr(crate::sys::bindings::tsk_strerror(code)) };
c_str
.to_str()
.expect("failed to convert c_str to &str")
.to_owned()
}
#[test]
fn test_error_message() {
fn foo() -> Result<(), TskitError> {
Err(TskitError::LibraryError("foobar".to_owned()))
}
let msg = "foobar".to_owned();
match foo() {
Err(TskitError::LibraryError(m)) => assert_eq!(m, msg),
_ => panic!("unexpected match"),
}
}
#[test]
fn test_error_code() {
fn foo() -> Result<(), TskitError> {
Err(TskitError::ErrorCode { code: -202 })
}
match foo() {
Err(TskitError::ErrorCode { code: x }) => {
assert_eq!(x, -202);
}
_ => panic!("unexpected match"),
}
match foo() {
Err(e) => {
let m = format!("{}", e);
assert_eq!(&m, "Node out of bounds. (TSK_ERR_NODE_OUT_OF_BOUNDS)");
}
_ => panic!("unexpected match"),
}
}