Skip to content

Commit 1b455f9

Browse files
committed
feat: add tskit::OwnedMigrationTable
1 parent 7addf5c commit 1b455f9

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ pub use edge_table::{EdgeTable, EdgeTableRow, OwnedEdgeTable};
426426
pub use error::TskitError;
427427
pub use flags::*;
428428
pub use individual_table::{IndividualTable, IndividualTableRow, OwnedIndividualTable};
429-
pub use migration_table::{MigrationTable, MigrationTableRow};
429+
pub use migration_table::{MigrationTable, MigrationTableRow, OwnedMigrationTable};
430430
pub use mutation_table::{MutationTable, MutationTableRow, OwnedMutationTable};
431431
pub use node_table::{NodeTable, NodeTableRow, OwnedNodeTable};
432432
pub use population_table::{OwnedPopulationTable, PopulationTable, PopulationTableRow};

src/migration_table.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::SizeType;
55
use crate::Time;
66
use crate::{tsk_id_t, TskitError};
77
use crate::{MigrationId, NodeId, PopulationId};
8+
use ll_bindings::{tsk_migration_table_free, tsk_migration_table_init};
89

910
/// Row of a [`MigrationTable`]
1011
pub struct MigrationTableRow {
@@ -212,3 +213,59 @@ impl<'a> MigrationTable<'a> {
212213
table_row_access!(r.into().0, self, make_migration_table_row)
213214
}
214215
}
216+
217+
build_owned_table_type!(
218+
/// A standalone migration table that owns its data.
219+
///
220+
/// # Examples
221+
///
222+
/// ```
223+
/// use tskit::OwnedMigrationTable;
224+
///
225+
/// let mut migrations = OwnedMigrationTable::default();
226+
/// let rowid = migrations.add_row((0., 1.), 1, (0, 1), 10.3).unwrap();
227+
/// assert_eq!(rowid, 0);
228+
/// assert_eq!(migrations.num_rows(), 1);
229+
/// ```
230+
///
231+
/// An example with metadata.
232+
/// This requires the cargo feature `"derive"` for `tskit`.
233+
///
234+
/// ```
235+
/// # #[cfg(any(feature="doc", feature="derive"))] {
236+
/// use tskit::OwnedMigrationTable;
237+
///
238+
/// #[derive(serde::Serialize,
239+
/// serde::Deserialize,
240+
/// tskit::metadata::MigrationMetadata)]
241+
/// #[serializer("serde_json")]
242+
/// struct MigrationMetadata {
243+
/// value: i32,
244+
/// }
245+
///
246+
/// let metadata = MigrationMetadata{value: 42};
247+
///
248+
/// let mut migrations = OwnedMigrationTable::default();
249+
///
250+
/// let rowid = migrations.add_row_with_metadata((0., 1.), 1, (0, 1), 10.3, &metadata).unwrap();
251+
/// assert_eq!(rowid, 0);
252+
///
253+
/// if let Some(decoded) = migrations.metadata::<MigrationMetadata>(rowid).unwrap() {
254+
/// assert_eq!(decoded.value, 42);
255+
/// } else {
256+
/// panic!("hmm...we expected some metadata!");
257+
/// }
258+
///
259+
/// # }
260+
/// ```
261+
=> OwnedMigrationTable,
262+
MigrationTable,
263+
tsk_migration_table_t,
264+
tsk_migration_table_init,
265+
tsk_migration_table_free
266+
);
267+
268+
impl OwnedMigrationTable {
269+
migration_table_add_row!(=> add_row, self, *self.table);
270+
migration_table_add_row_with_metadata!(=> add_row_with_metadata, self, *self.table);
271+
}

0 commit comments

Comments
 (0)