Skip to content

Commit d017330

Browse files
authored
feat: add tskit::OwnedMutationTable (#281)
1 parent 02ad5c4 commit d017330

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub use error::TskitError;
427427
pub use flags::*;
428428
pub use individual_table::{IndividualTable, IndividualTableRow};
429429
pub use migration_table::{MigrationTable, MigrationTableRow};
430-
pub use mutation_table::{MutationTable, MutationTableRow};
430+
pub use mutation_table::{MutationTable, MutationTableRow, OwnedMutationTable};
431431
pub use node_table::{NodeTable, NodeTableRow, OwnedNodeTable};
432432
pub use population_table::{OwnedPopulationTable, PopulationTable, PopulationTableRow};
433433
pub use site_table::{SiteTable, SiteTableRow};

src/mutation_table.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::SizeType;
44
use crate::Time;
55
use crate::{tsk_id_t, TskitError};
66
use crate::{MutationId, NodeId, SiteId};
7+
use ll_bindings::{tsk_mutation_table_free, tsk_mutation_table_init};
78

89
/// Row of a [`MutationTable`]
910
pub struct MutationTableRow {
@@ -196,3 +197,63 @@ impl<'a> MutationTable<'a> {
196197
table_row_access!(ri.0, self, make_mutation_table_row)
197198
}
198199
}
200+
201+
/// A standalone mutation table that owns its data.
202+
///
203+
/// # Examples
204+
///
205+
/// ```
206+
/// use tskit::OwnedMutationTable;
207+
///
208+
/// let mut mutations = OwnedMutationTable::default();
209+
/// let rowid = mutations.add_row(1, 2, 0, 1.0, None).unwrap();
210+
/// assert_eq!(rowid, 0);
211+
/// assert_eq!(mutations.num_rows(), 1);
212+
/// ```
213+
///
214+
/// An example with metadata.
215+
/// This requires the cargo feature `"derive"` for `tskit`.
216+
///
217+
/// ```
218+
/// # #[cfg(any(feature="doc", feature="derive"))] {
219+
/// use tskit::OwnedMutationTable;
220+
///
221+
/// #[derive(serde::Serialize,
222+
/// serde::Deserialize,
223+
/// tskit::metadata::MutationMetadata)]
224+
/// #[serializer("serde_json")]
225+
/// struct MutationMetadata {
226+
/// value: i32,
227+
/// }
228+
///
229+
/// let metadata = MutationMetadata{value: 42};
230+
///
231+
/// let mut mutations = OwnedMutationTable::default();
232+
///
233+
/// let rowid = mutations.add_row_with_metadata(0, 1, 5, 10.0, None, &metadata).unwrap();
234+
/// assert_eq!(rowid, 0);
235+
///
236+
/// if let Some(decoded) = mutations.metadata::<MutationMetadata>(rowid).unwrap() {
237+
/// assert_eq!(decoded.value, 42);
238+
/// } else {
239+
/// panic!("hmm...we expected some metadata!");
240+
/// }
241+
///
242+
/// # }
243+
/// ```
244+
pub struct OwnedMutationTable {
245+
table: mbox::MBox<ll_bindings::tsk_mutation_table_t>,
246+
}
247+
248+
impl OwnedMutationTable {
249+
mutation_table_add_row!(=> add_row, self, *self.table);
250+
mutation_table_add_row_with_metadata!(=> add_row_with_metadata, self, *self.table);
251+
}
252+
253+
build_owned_tables!(
254+
OwnedMutationTable,
255+
MutationTable,
256+
ll_bindings::tsk_mutation_table_t,
257+
tsk_mutation_table_init,
258+
tsk_mutation_table_free
259+
);

0 commit comments

Comments
 (0)