Skip to content

Commit 2fd98a6

Browse files
committed
migrations
1 parent b37a59a commit 2fd98a6

File tree

2 files changed

+62
-64
lines changed

2 files changed

+62
-64
lines changed

src/_macros.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,62 @@ macro_rules! site_table_add_row_with_metadata {
931931
};
932932
}
933933

934+
macro_rules! migration_table_add_row_details {
935+
($span: ident,
936+
$node: ident,
937+
$source_dest: ident,
938+
$time: ident,
939+
$metadata: expr,
940+
$metadata_len: expr,
941+
$table: expr) => {{
942+
let rv = unsafe {
943+
$crate::bindings::tsk_migration_table_add_row(
944+
&mut $table,
945+
$span.0.into().0,
946+
$span.1.into().0,
947+
$node.into().0,
948+
$source_dest.0.into().0,
949+
$source_dest.1.into().0,
950+
$time.into().0,
951+
$metadata,
952+
$metadata_len,
953+
)
954+
};
955+
handle_tsk_return_value!(rv, rv.into())
956+
}};
957+
}
958+
959+
macro_rules! migration_table_add_row {
960+
($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => {
961+
$(#[$attr])*
962+
pub fn $name(&mut $self,
963+
span: (impl Into<$crate::Position>, impl Into<$crate::Position>),
964+
node: impl Into<$crate::NodeId>,
965+
source_dest: (impl Into<$crate::PopulationId>, impl Into<$crate::PopulationId>),
966+
time: impl Into<$crate::Time>)
967+
-> Result<$crate::MigrationId, $crate::TskitError> {
968+
migration_table_add_row_details!(span, node, source_dest, time, std::ptr::null(), 0, $table)
969+
}
970+
};
971+
}
972+
973+
macro_rules! migration_table_add_row_with_metadata {
974+
($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => {
975+
$(#[$attr])*
976+
pub fn $name<M>(&mut $self,
977+
span: (impl Into<$crate::Position>, impl Into<$crate::Position>),
978+
node: impl Into<$crate::NodeId>,
979+
source_dest: (impl Into<$crate::PopulationId>, impl Into<$crate::PopulationId>),
980+
time: impl Into<$crate::Time>,
981+
metadata: &M)
982+
-> Result<$crate::MigrationId, $crate::TskitError>
983+
where M: $crate::metadata::MigrationMetadata {
984+
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
985+
migration_table_add_row_details!(span, node, source_dest, time,
986+
md.as_ptr(), md.len().into(), $table)
987+
}
988+
};
989+
}
934990
#[cfg(test)]
935991
mod test {
936992
use crate::error::TskitError;

src/table_collection.rs

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::bindings as ll_bindings;
22
use crate::error::TskitError;
33
use crate::ffi::WrapTskitType;
4-
use crate::metadata::*;
54
use crate::types::Bookmark;
65
use crate::EdgeTable;
76
use crate::IndividualTable;
@@ -19,12 +18,11 @@ use crate::TableEqualityOptions;
1918
use crate::TableIntegrityCheckFlags;
2019
use crate::TableOutputOptions;
2120
use crate::TableSortOptions;
22-
use crate::Time;
2321
use crate::TreeSequenceFlags;
2422
use crate::TskReturnValue;
2523
use crate::TskitTypeAccess;
2624
use crate::{tsk_id_t, tsk_size_t};
27-
use crate::{EdgeId, MigrationId, NodeId, PopulationId};
25+
use crate::{EdgeId, NodeId};
2826
use ll_bindings::tsk_table_collection_free;
2927
use mbox::MBox;
3028

@@ -314,6 +312,7 @@ impl TableCollection {
314312
/// # }
315313
=> add_individual_with_metadata, self, (*self.inner).individuals);
316314

315+
migration_table_add_row!(
317316
/// Add a row to the migration table
318317
///
319318
/// # Warnings
@@ -329,36 +328,9 @@ impl TableCollection {
329328
/// (0, 1),
330329
/// 53.5).is_ok());
331330
/// ```
332-
pub fn add_migration<
333-
L: Into<Position>,
334-
R: Into<Position>,
335-
N: Into<NodeId>,
336-
SOURCE: Into<PopulationId>,
337-
DEST: Into<PopulationId>,
338-
T: Into<Time>,
339-
>(
340-
&mut self,
341-
span: (L, R),
342-
node: N,
343-
source_dest: (SOURCE, DEST),
344-
time: T,
345-
) -> Result<MigrationId, TskitError> {
346-
let rv = unsafe {
347-
ll_bindings::tsk_migration_table_add_row(
348-
&mut (*self.as_mut_ptr()).migrations,
349-
span.0.into().0,
350-
span.1.into().0,
351-
node.into().0,
352-
source_dest.0.into().0,
353-
source_dest.1.into().0,
354-
time.into().0,
355-
std::ptr::null(),
356-
0,
357-
)
358-
};
359-
handle_tsk_return_value!(rv, MigrationId(rv))
360-
}
331+
=> add_migration, self, (*self.inner).migrations);
361332

333+
migration_table_add_row_with_metadata!(
362334
/// Add a row with optional metadata to the migration table
363335
///
364336
/// # Examples
@@ -388,38 +360,7 @@ impl TableCollection {
388360
///
389361
/// Migration tables are not currently supported
390362
/// by tree sequence simplification.
391-
pub fn add_migration_with_metadata<
392-
L: Into<Position>,
393-
R: Into<Position>,
394-
N: Into<NodeId>,
395-
SOURCE: Into<PopulationId>,
396-
DEST: Into<PopulationId>,
397-
MD: MigrationMetadata,
398-
T: Into<Time>,
399-
>(
400-
&mut self,
401-
span: (L, R),
402-
node: N,
403-
source_dest: (SOURCE, DEST),
404-
time: T,
405-
metadata: &MD,
406-
) -> Result<MigrationId, TskitError> {
407-
let md = EncodedMetadata::new(metadata)?;
408-
let rv = unsafe {
409-
ll_bindings::tsk_migration_table_add_row(
410-
&mut (*self.as_mut_ptr()).migrations,
411-
span.0.into().0,
412-
span.1.into().0,
413-
node.into().0,
414-
source_dest.0.into().0,
415-
source_dest.1.into().0,
416-
time.into().0,
417-
md.as_ptr(),
418-
md.len().into(),
419-
)
420-
};
421-
handle_tsk_return_value!(rv, MigrationId(rv))
422-
}
363+
=> add_migration_with_metadata, self, (*self.inner).migrations);
423364

424365
node_table_add_row!(
425366
/// Add a row to the node table
@@ -970,6 +911,7 @@ impl crate::traits::NodeListGenerator for TableCollection {}
970911
#[cfg(test)]
971912
mod test {
972913
use super::*;
914+
use crate::metadata::*;
973915
use crate::prelude::*;
974916
use crate::NodeFlags;
975917

0 commit comments

Comments
 (0)