Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor: Remove code duplication when adding population table rows.
  • Loading branch information
molpopgen committed Jan 27, 2023
commit bf90b337f5a2befd3125d45152a0b6619d1678aa
29 changes: 0 additions & 29 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,35 +380,6 @@ macro_rules! handle_metadata_return {
};
}

macro_rules! population_table_add_row_details {
($metadata: expr, $metadata_len: expr, $table: expr) => {{
let rv = unsafe {
$crate::bindings::tsk_population_table_add_row($table, $metadata, $metadata_len)
};
handle_tsk_return_value!(rv, rv.into())
}};
}

macro_rules! population_table_add_row {
($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => {
$(#[$attr])*
pub fn $name(&mut $self) -> Result<$crate::PopulationId, $crate::TskitError> {
population_table_add_row_details!(std::ptr::null(), 0, $table)
}
};
}

macro_rules! population_table_add_row_with_metadata {
($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => {
$(#[$attr])*
pub fn $name<M>(&mut $self, metadata: &M) -> Result<$crate::PopulationId, $crate::TskitError>
where M: $crate::metadata::PopulationMetadata {
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
population_table_add_row_details!(md.as_ptr(), md.len()?.into(), $table)
}
};
}

macro_rules! individual_table_add_row_details {
($flags: ident,
$location: ident,
Expand Down
27 changes: 25 additions & 2 deletions src/population_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,31 @@ impl PopulationTable {
self.table_.clear().map_err(|e| e.into())
}

population_table_add_row!(=> add_row, self, self.as_mut_ptr());
population_table_add_row_with_metadata!(=> add_row_with_metadata, self, self.as_mut_ptr());
fn add_row_details(
&mut self,
metadata: *const i8,
metadata_len: crate::bindings::tsk_size_t,
) -> Result<PopulationId, TskitError> {
let rv = unsafe {
crate::bindings::tsk_population_table_add_row(self.as_mut_ptr(), metadata, metadata_len)
};
handle_tsk_return_value!(rv, rv.into())
}

pub fn add_row(&mut self) -> Result<crate::PopulationId, crate::TskitError> {
self.add_row_details(std::ptr::null(), 0)
}

pub fn add_row_with_metadata<M>(
&mut self,
metadata: &M,
) -> Result<crate::PopulationId, crate::TskitError>
where
M: crate::metadata::PopulationMetadata,
{
let md = crate::metadata::EncodedMetadata::new(metadata)?;
self.add_row_details(md.as_ptr(), md.len()?.into())
}
}

impl Default for PopulationTable {
Expand Down
16 changes: 12 additions & 4 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ impl TableCollection {
/// ```
=> add_mutation_with_metadata, self, &mut (*self.as_mut_ptr()).mutations);

population_table_add_row!(
/// Add a row to the population_table
///
/// # Examples
Expand All @@ -553,9 +552,10 @@ impl TableCollection {
/// # let mut tables = tskit::TableCollection::new(55.0).unwrap();
/// tables.add_population().unwrap();
/// ```
=> add_population, self, &mut (*self.as_mut_ptr()).populations);
pub fn add_population(&mut self) -> Result<crate::PopulationId, crate::TskitError> {
self.populations_mut().add_row()
}

population_table_add_row_with_metadata!(
/// Add a row with optional metadata to the population_table
///
/// # Examples
Expand All @@ -575,7 +575,15 @@ impl TableCollection {
/// let metadata = PopulationMetadata{x: 1};
/// assert!(tables.add_population_with_metadata(&metadata).is_ok());
/// # }
=> add_population_with_metadata, self, &mut (*self.as_mut_ptr()).populations);
pub fn add_population_with_metadata<M>(
&mut self,
metadata: &M,
) -> Result<crate::PopulationId, crate::TskitError>
where
M: crate::metadata::PopulationMetadata,
{
self.populations_mut().add_row_with_metadata(metadata)
}

/// Build the "input" and "output"
/// indexes for the edge table.
Expand Down