Skip to content

Commit cd421fe

Browse files
committed
more
1 parent 41831ba commit cd421fe

File tree

2 files changed

+37
-57
lines changed

2 files changed

+37
-57
lines changed

src/migration_table.rs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,7 @@ impl MigrationTable {
237237
/// * `Some(position)` if `row` is valid.
238238
/// * `None` otherwise.
239239
pub fn left<M: Into<MigrationId> + Copy>(&self, row: M) -> Option<Position> {
240-
assert!(self.num_rows() == 0 || !self.as_ref().time.is_null());
241-
// SAFETY: either the column is empty or the pointer is not null,
242-
// in which case the correct lengths are from the low-level objects
243-
unsafe {
244-
sys::tsk_column_access::<Position, _, _, _>(
245-
row.into(),
246-
self.as_ref().left,
247-
self.num_rows(),
248-
)
249-
}
240+
self.table_.left(row.into())
250241
}
251242

252243
/// Return the right coordinate for a given row.
@@ -256,35 +247,17 @@ impl MigrationTable {
256247
/// * `Some(positions)` if `row` is valid.
257248
/// * `None` otherwise.
258249
pub fn right<M: Into<MigrationId> + Copy>(&self, row: M) -> Option<Position> {
259-
assert!(self.num_rows() == 0 || !self.as_ref().right.is_null());
260-
// SAFETY: either the column is empty or the pointer is not null,
261-
// in which case the correct lengths are from the low-level objects
262-
unsafe {
263-
sys::tsk_column_access::<Position, _, _, _>(
264-
row.into(),
265-
self.as_ref().right,
266-
self.num_rows(),
267-
)
268-
}
250+
self.table_.right(row.into())
269251
}
270252

271253
/// Return the node for a given row.
272254
///
273255
/// # Returns
274-
///
256+
//
275257
/// * `Some(node)` if `row` is valid.
276258
/// * `None` otherwise.
277259
pub fn node<M: Into<MigrationId> + Copy>(&self, row: M) -> Option<NodeId> {
278-
assert!(self.num_rows() == 0 || !self.as_ref().node.is_null());
279-
// SAFETY: either the column is empty or the pointer is not null,
280-
// in which case the correct lengths are from the low-level objects
281-
unsafe {
282-
sys::tsk_column_access::<NodeId, _, _, _>(
283-
row.into(),
284-
self.as_ref().node,
285-
self.num_rows(),
286-
)
287-
}
260+
self.table_.node(row.into())
288261
}
289262

290263
/// Return the source population for a given row.
@@ -294,16 +267,7 @@ impl MigrationTable {
294267
/// * `Some(population)` if `row` is valid.
295268
/// * `None` otherwise.
296269
pub fn source<M: Into<MigrationId> + Copy>(&self, row: M) -> Option<PopulationId> {
297-
assert!(self.num_rows() == 0 || !self.as_ref().time.is_null());
298-
// SAFETY: either the column is empty or the pointer is not null,
299-
// in which case the correct lengths are from the low-level objects
300-
unsafe {
301-
sys::tsk_column_access::<PopulationId, _, _, _>(
302-
row.into(),
303-
self.as_ref().source,
304-
self.num_rows(),
305-
)
306-
}
270+
self.table_.source(row.into())
307271
}
308272

309273
/// Return the destination population for a given row.
@@ -313,16 +277,7 @@ impl MigrationTable {
313277
/// * `Some(population)` if `row` is valid.
314278
/// * `None` otherwise.
315279
pub fn dest<M: Into<MigrationId> + Copy>(&self, row: M) -> Option<PopulationId> {
316-
assert!(self.num_rows() == 0 || !self.as_ref().dest.is_null());
317-
// SAFETY: either the column is empty or the pointer is not null,
318-
// in which case the correct lengths are from the low-level objects
319-
unsafe {
320-
sys::tsk_column_access::<PopulationId, _, _, _>(
321-
row.into(),
322-
self.as_ref().dest,
323-
self.num_rows(),
324-
)
325-
}
280+
self.table_.dest(row.into())
326281
}
327282

328283
/// Return the time of the migration event for a given row.
@@ -332,12 +287,7 @@ impl MigrationTable {
332287
/// * `Some(time)` if `row` is valid.
333288
/// * `None` otherwise.
334289
pub fn time<M: Into<MigrationId> + Copy>(&self, row: M) -> Option<Time> {
335-
assert!(self.num_rows() == 0 || !self.as_ref().time.is_null());
336-
// SAFETY: either the column is empty or the pointer is not null,
337-
// in which case the correct lengths are from the low-level objects
338-
unsafe {
339-
sys::tsk_column_access::<Time, _, _, _>(row.into(), self.as_ref().time, self.num_rows())
340-
}
290+
self.table_.time(row.into())
341291
}
342292

343293
/// Retrieve decoded metadata for a `row`.

src/sys/migration_table.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
use std::ptr::NonNull;
22

3+
use super::newtypes::MigrationId;
4+
use super::newtypes::NodeId;
5+
use super::newtypes::PopulationId;
6+
use super::newtypes::Position;
7+
use super::newtypes::Time;
8+
39
use super::bindings::tsk_id_t;
410
use super::bindings::tsk_migration_table_add_row;
511
use super::bindings::tsk_migration_table_clear;
@@ -70,6 +76,30 @@ impl MigrationTable {
7076
))
7177
}
7278
}
79+
80+
pub fn node(&self, row: MigrationId) -> Option<NodeId> {
81+
safe_tsk_column_access!(self, row, NodeId, node)
82+
}
83+
84+
pub fn source(&self, row: MigrationId) -> Option<PopulationId> {
85+
safe_tsk_column_access!(self, row, PopulationId, source)
86+
}
87+
88+
pub fn dest(&self, row: MigrationId) -> Option<PopulationId> {
89+
safe_tsk_column_access!(self, row, PopulationId, dest)
90+
}
91+
92+
pub fn time(&self, row: MigrationId) -> Option<Time> {
93+
safe_tsk_column_access!(self, row, Time, time)
94+
}
95+
96+
pub fn left(&self, row: MigrationId) -> Option<Position> {
97+
safe_tsk_column_access!(self, row, Position, left)
98+
}
99+
100+
pub fn right(&self, row: MigrationId) -> Option<Position> {
101+
safe_tsk_column_access!(self, row, Position, right)
102+
}
73103
}
74104

75105
impl Default for MigrationTable {

0 commit comments

Comments
 (0)