From b2691d175c5ff360a8adcf8ed8bbd96e2dfb6bd3 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Tue, 19 Jul 2022 02:06:25 -0700 Subject: [PATCH] refactor: Deduplicate code for adding node table rows. --- src/_macros.rs | 58 +++++++++++++++++++++++++++++++++++++++ src/node_table.rs | 49 ++------------------------------- src/table_collection.rs | 61 ++++------------------------------------- 3 files changed, 66 insertions(+), 102 deletions(-) diff --git a/src/_macros.rs b/src/_macros.rs index e2e4b5015..54e51a78b 100644 --- a/src/_macros.rs +++ b/src/_macros.rs @@ -576,6 +576,64 @@ macro_rules! build_owned_tables { }; } +macro_rules! node_table_add_row { + ($(#[$attr:meta])* => $name: ident, $self: ident, $table: ident $(, $table2: ident )?) => { + $(#[$attr])* + pub fn $name( + &mut $self, + flags: impl Into<$crate::NodeFlags>, + time: impl Into<$crate::Time>, + population: impl Into<$crate::PopulationId>, + individual: impl Into<$crate::IndividualId>, + ) -> Result<$crate::NodeId, $crate::TskitError> { + let rv = unsafe { + $crate::bindings::tsk_node_table_add_row( + &mut (*$self.$table)$(.$table2)?, + flags.into().bits(), + time.into().0, + population.into().0, + individual.into().0, + std::ptr::null(), + 0, + ) + }; + + handle_tsk_return_value!(rv, rv.into()) + } + }; +} + +macro_rules! node_table_add_row_with_metadata { + ($(#[$attr:meta])* => $name: ident, $self: ident, $table: ident $(, $table2: ident )?) => { + $(#[$attr])* + pub fn $name( + &mut $self, + flags: impl Into<$crate::NodeFlags>, + time: impl Into<$crate::Time>, + population: impl Into<$crate::PopulationId>, + individual: impl Into<$crate::IndividualId>, + metadata: &M, + ) -> Result<$crate::NodeId, $crate::TskitError> + where + M: $crate::metadata::NodeMetadata, + { + let md = $crate::metadata::EncodedMetadata::new(metadata)?; + let rv = unsafe { + $crate::bindings::tsk_node_table_add_row( + &mut (*$self.$table)$(.$table2)?, + flags.into().bits(), + time.into().0, + population.into().0, + individual.into().0, + md.as_ptr(), + md.len().into(), + ) + }; + handle_tsk_return_value!(rv, rv.into()) + } + }; +} + #[cfg(test)] mod test { use crate::error::TskitError; diff --git a/src/node_table.rs b/src/node_table.rs index a187a9a79..ad228de2a 100644 --- a/src/node_table.rs +++ b/src/node_table.rs @@ -293,53 +293,8 @@ pub struct OwnedNodeTable { } impl OwnedNodeTable { - pub fn add_row( - &mut self, - flags: impl Into, - time: impl Into