diff --git a/src/_macros.rs b/src/_macros.rs index 62f06cecd..64887e352 100644 --- a/src/_macros.rs +++ b/src/_macros.rs @@ -380,81 +380,6 @@ macro_rules! handle_metadata_return { }; } -macro_rules! individual_table_add_row_details { - ($flags: ident, - $location: ident, - $parents: ident, - $metadata: expr, - $metadata_len: expr, - $table: expr) => {{ - let rv = unsafe { - $crate::bindings::tsk_individual_table_add_row( - $table, - $flags.into().bits(), - $location.get_slice().as_ptr().cast::(), - $location.get_slice().len() as $crate::bindings::tsk_size_t, - $parents - .get_slice() - .as_ptr() - .cast::<$crate::bindings::tsk_id_t>(), - $parents.get_slice().len() as $crate::bindings::tsk_size_t, - $metadata, - $metadata_len, - ) - }; - handle_tsk_return_value!(rv, rv.into()) - }}; -} - -macro_rules! individual_table_add_row { - ($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => { - $(#[$attr])* - pub fn $name(&mut $self, - flags: F, - location: L, - parents: P, - ) -> Result<$crate::IndividualId, $crate::TskitError> - where - F: Into<$crate::IndividualFlags>, - L: $crate::IndividualLocation, - P: $crate::IndividualParents, - { - individual_table_add_row_details!(flags, - location, - parents, - std::ptr::null(), - 0, - $table) - } - }; -} - -macro_rules! individual_table_add_row_with_metadata { - ($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => { - $(#[$attr])* - pub fn $name(&mut $self, - flags: F, - location: L, - parents: P, - metadata: &M, - ) -> Result<$crate::IndividualId, $crate::TskitError> - where - F: Into<$crate::IndividualFlags>, - L: $crate::IndividualLocation, - P: $crate::IndividualParents, - M: $crate::metadata::IndividualMetadata - { - let md = $crate::metadata::EncodedMetadata::new(metadata)?; - individual_table_add_row_details!(flags, - location, - parents, - md.as_ptr(), - md.len()?.into(), - $table) - } - }; -} - macro_rules! mutation_table_add_row_details { ($site: ident, $node: ident, $parent: ident, $time: ident, $derived_state: ident, diff --git a/src/individual_table.rs b/src/individual_table.rs index 08fd98797..2c6983167 100644 --- a/src/individual_table.rs +++ b/src/individual_table.rs @@ -491,8 +491,68 @@ match tables.individuals().metadata::(0.into()) self.table_.clear().map_err(|e| e.into()) } - individual_table_add_row!(=> add_row, self, self.as_mut_ptr()); - individual_table_add_row_with_metadata!(=> add_row_with_metadata, self, self.as_mut_ptr()); + fn add_row_details( + &mut self, + flags: F, + location: L, + parents: P, + metadata: *const i8, + metadata_length: u64, + ) -> Result + where + F: Into, + L: crate::IndividualLocation, + P: crate::IndividualParents, + { + let rv = unsafe { + crate::bindings::tsk_individual_table_add_row( + self.as_mut_ptr(), + flags.into().bits(), + location.get_slice().as_ptr().cast::(), + location.get_slice().len() as crate::bindings::tsk_size_t, + parents + .get_slice() + .as_ptr() + .cast::(), + parents.get_slice().len() as crate::bindings::tsk_size_t, + metadata, + metadata_length, + ) + }; + handle_tsk_return_value!(rv, rv.into()) + } + + pub fn add_row( + &mut self, + flags: F, + location: L, + parents: P, + ) -> Result + where + F: Into, + L: crate::IndividualLocation, + P: crate::IndividualParents, + { + self.add_row_details(flags, location, parents, std::ptr::null(), 0) + } + + pub fn add_row_with_metadata( + &mut self, + flags: F, + location: L, + parents: P, + metadata: &M, + ) -> Result + where + F: Into, + L: crate::IndividualLocation, + P: crate::IndividualParents, + M: crate::metadata::IndividualMetadata, + { + let md = crate::metadata::EncodedMetadata::new(metadata)?; + let mdlen = md.len()?; + self.add_row_details(flags, location, parents, md.as_ptr(), mdlen.into()) + } build_table_column_slice_getter!( /// Get the flags column as a slice diff --git a/src/table_collection.rs b/src/table_collection.rs index ce2e5342b..2cc5dbf71 100644 --- a/src/table_collection.rs +++ b/src/table_collection.rs @@ -313,7 +313,6 @@ impl TableCollection { .add_row_with_metadata(left, right, parent, child, metadata) } - individual_table_add_row!( /// Add a row to the individual table /// /// # Examples @@ -321,7 +320,7 @@ impl TableCollection { /// ## No flags, location, nor parents /// /// ``` - /// # + /// # /// # let mut tables = tskit::TableCollection::new(1.0).unwrap(); /// tables.add_individual(0, None, None).unwrap(); /// # assert!(tables.individuals().location(0).is_none()); @@ -331,7 +330,7 @@ impl TableCollection { /// ## No flags, a 3d location, no parents /// /// ``` - /// # + /// # /// # let mut tables = tskit::TableCollection::new(1.0).unwrap(); /// tables.add_individual(0, &[-0.5, 0.3, 10.0], None).unwrap(); /// # match tables.individuals().location(0) { @@ -343,16 +342,27 @@ impl TableCollection { /// ## No flags, no location, two parents /// ``` /// # let mut tables = tskit::TableCollection::new(1.0).unwrap(); - /// # + /// # /// tables.add_individual(0, None, &[1, 11]); /// # match tables.individuals().parents(0) { /// # Some(parents) => parents.iter().zip([1, 11].iter()).for_each(|(a,b)| assert_eq!(a, b)), /// # None => panic!("expected parents"), /// # } /// ``` - => add_individual, self, &mut (*self.as_mut_ptr()).individuals); + pub fn add_individual( + &mut self, + flags: F, + location: L, + parents: P, + ) -> Result + where + F: Into, + L: crate::IndividualLocation, + P: crate::IndividualParents, + { + self.individuals_mut().add_row(flags, location, parents) + } - individual_table_add_row_with_metadata!( /// Add a row with metadata to the individual table /// /// # Examples @@ -363,7 +373,7 @@ impl TableCollection { /// /// ``` /// # #[cfg(feature = "derive")] { - /// + /// /// # let mut tables = tskit::TableCollection::new(100.).unwrap(); /// # #[derive(serde::Serialize, serde::Deserialize, tskit::metadata::IndividualMetadata)] /// # #[serializer("serde_json")] @@ -376,7 +386,22 @@ impl TableCollection { /// # let decoded = tables.individuals().metadata::(0.into()).unwrap().unwrap(); /// # assert_eq!(decoded.x, 1); /// # } - => add_individual_with_metadata, self, &mut (*self.as_mut_ptr()).individuals); + pub fn add_individual_with_metadata( + &mut self, + flags: F, + location: L, + parents: P, + metadata: &M, + ) -> Result + where + F: Into, + L: crate::IndividualLocation, + P: crate::IndividualParents, + M: crate::metadata::IndividualMetadata, + { + self.individuals_mut() + .add_row_with_metadata(flags, location, parents, metadata) + } migration_table_add_row!( /// Add a row to the migration table