Skip to content

Commit b2691d1

Browse files
committed
refactor: Deduplicate code for adding node table rows.
1 parent a6e3d32 commit b2691d1

File tree

3 files changed

+66
-102
lines changed

3 files changed

+66
-102
lines changed

src/_macros.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,64 @@ macro_rules! build_owned_tables {
576576
};
577577
}
578578

579+
macro_rules! node_table_add_row {
580+
($(#[$attr:meta])* => $name: ident, $self: ident, $table: ident $(, $table2: ident )?) => {
581+
$(#[$attr])*
582+
pub fn $name(
583+
&mut $self,
584+
flags: impl Into<$crate::NodeFlags>,
585+
time: impl Into<$crate::Time>,
586+
population: impl Into<$crate::PopulationId>,
587+
individual: impl Into<$crate::IndividualId>,
588+
) -> Result<$crate::NodeId, $crate::TskitError> {
589+
let rv = unsafe {
590+
$crate::bindings::tsk_node_table_add_row(
591+
&mut (*$self.$table)$(.$table2)?,
592+
flags.into().bits(),
593+
time.into().0,
594+
population.into().0,
595+
individual.into().0,
596+
std::ptr::null(),
597+
0,
598+
)
599+
};
600+
601+
handle_tsk_return_value!(rv, rv.into())
602+
}
603+
};
604+
}
605+
606+
macro_rules! node_table_add_row_with_metadata {
607+
($(#[$attr:meta])* => $name: ident, $self: ident, $table: ident $(, $table2: ident )?) => {
608+
$(#[$attr])*
609+
pub fn $name<M>(
610+
&mut $self,
611+
flags: impl Into<$crate::NodeFlags>,
612+
time: impl Into<$crate::Time>,
613+
population: impl Into<$crate::PopulationId>,
614+
individual: impl Into<$crate::IndividualId>,
615+
metadata: &M,
616+
) -> Result<$crate::NodeId, $crate::TskitError>
617+
where
618+
M: $crate::metadata::NodeMetadata,
619+
{
620+
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
621+
let rv = unsafe {
622+
$crate::bindings::tsk_node_table_add_row(
623+
&mut (*$self.$table)$(.$table2)?,
624+
flags.into().bits(),
625+
time.into().0,
626+
population.into().0,
627+
individual.into().0,
628+
md.as_ptr(),
629+
md.len().into(),
630+
)
631+
};
632+
handle_tsk_return_value!(rv, rv.into())
633+
}
634+
};
635+
}
636+
579637
#[cfg(test)]
580638
mod test {
581639
use crate::error::TskitError;

src/node_table.rs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -293,53 +293,8 @@ pub struct OwnedNodeTable {
293293
}
294294

295295
impl OwnedNodeTable {
296-
pub fn add_row(
297-
&mut self,
298-
flags: impl Into<NodeFlags>,
299-
time: impl Into<Time>,
300-
population: impl Into<PopulationId>,
301-
individual: impl Into<IndividualId>,
302-
) -> Result<NodeId, TskitError> {
303-
let rv = unsafe {
304-
ll_bindings::tsk_node_table_add_row(
305-
&mut (*self.table),
306-
flags.into().bits(),
307-
time.into().0,
308-
population.into().0,
309-
individual.into().0,
310-
std::ptr::null(),
311-
0,
312-
)
313-
};
314-
315-
handle_tsk_return_value!(rv, rv.into())
316-
}
317-
318-
pub fn add_row_with_metadata<M>(
319-
&mut self,
320-
flags: impl Into<NodeFlags>,
321-
time: impl Into<Time>,
322-
population: impl Into<PopulationId>,
323-
individual: impl Into<IndividualId>,
324-
metadata: &M,
325-
) -> Result<NodeId, TskitError>
326-
where
327-
M: crate::metadata::NodeMetadata,
328-
{
329-
let md = crate::metadata::EncodedMetadata::new(metadata)?;
330-
let rv = unsafe {
331-
ll_bindings::tsk_node_table_add_row(
332-
&mut (*self.table),
333-
flags.into().bits(),
334-
time.into().0,
335-
population.into().0,
336-
individual.into().0,
337-
md.as_ptr(),
338-
md.len().into(),
339-
)
340-
};
341-
handle_tsk_return_value!(rv, rv.into())
342-
}
296+
node_table_add_row!(=> add_row, self, table);
297+
node_table_add_row_with_metadata!(=> add_row_with_metadata, self, table);
343298
}
344299

345300
build_owned_tables!(

src/table_collection.rs

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::IndividualTable;
1111
use crate::IndividualTableSortOptions;
1212
use crate::MigrationTable;
1313
use crate::MutationTable;
14-
use crate::NodeFlags;
1514
use crate::NodeTable;
1615
use crate::PopulationTable;
1716
use crate::Position;
@@ -515,33 +514,12 @@ impl TableCollection {
515514
handle_tsk_return_value!(rv, MigrationId(rv))
516515
}
517516

517+
node_table_add_row!(
518518
/// Add a row to the node table
519-
pub fn add_node<
520-
F: Into<NodeFlags>,
521-
T: Into<Time>,
522-
POP: Into<PopulationId>,
523-
I: Into<IndividualId>,
524-
>(
525-
&mut self,
526-
flags: F,
527-
time: T,
528-
population: POP,
529-
individual: I,
530-
) -> Result<NodeId, TskitError> {
531-
let rv = unsafe {
532-
ll_bindings::tsk_node_table_add_row(
533-
&mut (*self.as_mut_ptr()).nodes,
534-
flags.into().bits(),
535-
time.into().0,
536-
population.into().0,
537-
individual.into().0,
538-
std::ptr::null(),
539-
0,
540-
)
541-
};
519+
=> add_node, self, inner, nodes
520+
);
542521

543-
handle_tsk_return_value!(rv, rv.into())
544-
}
522+
node_table_add_row_with_metadata!(
545523

546524
/// Add a row with optional metadata to the node table
547525
///
@@ -563,35 +541,7 @@ impl TableCollection {
563541
/// assert!(tables.add_node_with_metadata(0, 0.0, -1, -1, &metadata).is_ok());
564542
/// # }
565543
/// ```
566-
pub fn add_node_with_metadata<
567-
F: Into<NodeFlags>,
568-
T: Into<Time>,
569-
POP: Into<PopulationId>,
570-
I: Into<IndividualId>,
571-
M: NodeMetadata,
572-
>(
573-
&mut self,
574-
flags: F,
575-
time: T,
576-
population: POP,
577-
individual: I,
578-
metadata: &M,
579-
) -> Result<NodeId, TskitError> {
580-
let md = EncodedMetadata::new(metadata)?;
581-
let rv = unsafe {
582-
ll_bindings::tsk_node_table_add_row(
583-
&mut (*self.as_mut_ptr()).nodes,
584-
flags.into().bits(),
585-
time.into().0,
586-
population.into().0,
587-
individual.into().0,
588-
md.as_ptr(),
589-
md.len().into(),
590-
)
591-
};
592-
593-
handle_tsk_return_value!(rv, rv.into())
594-
}
544+
=> add_node_with_metadata, self, inner, nodes);
595545

596546
/// Add a row to the site table
597547
pub fn add_site<P: Into<Position>>(
@@ -1226,6 +1176,7 @@ impl crate::traits::NodeListGenerator for TableCollection {}
12261176
#[cfg(test)]
12271177
mod test {
12281178
use super::*;
1179+
use crate::NodeFlags;
12291180

12301181
fn make_small_table_collection() -> TableCollection {
12311182
let mut tables = TableCollection::new(1000.).unwrap();

0 commit comments

Comments
 (0)