Skip to content

Commit 0ccaa7a

Browse files
authored
refactor: replace macros with fn for adding node table rows (#489)
1 parent e342718 commit 0ccaa7a

File tree

3 files changed

+157
-87
lines changed

3 files changed

+157
-87
lines changed

src/_macros.rs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -430,87 +430,6 @@ macro_rules! build_owned_tables {
430430
};
431431
}
432432

433-
macro_rules! node_table_add_row_details {
434-
($flags: ident,
435-
$time: ident,
436-
$population: ident,
437-
$individual: ident,
438-
$metadata: expr,
439-
$metadata_len: expr,
440-
$table: expr) => {{
441-
let rv = unsafe {
442-
$crate::bindings::tsk_node_table_add_row(
443-
$table,
444-
$flags.into().bits(),
445-
$time.into().into(),
446-
$population.into().into(),
447-
$individual.into().into(),
448-
$metadata,
449-
$metadata_len,
450-
)
451-
};
452-
handle_tsk_return_value!(rv, rv.into())
453-
}};
454-
}
455-
456-
macro_rules! node_table_add_row {
457-
($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => {
458-
$(#[$attr])*
459-
pub fn $name<F,T,P,I>(
460-
&mut $self,
461-
flags: F ,
462-
time: T ,
463-
population: P ,
464-
individual: I ,
465-
) -> Result<$crate::NodeId, $crate::TskitError>
466-
where
467-
F: Into<$crate::NodeFlags>,
468-
T:Into<$crate::Time>,
469-
P:Into<$crate::PopulationId>,
470-
I:Into<$crate::IndividualId>
471-
{
472-
node_table_add_row_details!(flags,
473-
time,
474-
population,
475-
individual,
476-
std::ptr::null(),
477-
0,
478-
$table)
479-
}
480-
};
481-
}
482-
483-
macro_rules! node_table_add_row_with_metadata {
484-
($(#[$attr:meta])* => $name: ident, $self: ident, $table: expr) => {
485-
$(#[$attr])*
486-
pub fn $name<F,T,P,I,M>(
487-
&mut $self,
488-
flags: F,
489-
time: T,
490-
population: P,
491-
individual: I,
492-
metadata: &M,
493-
) -> Result<$crate::NodeId, $crate::TskitError>
494-
where
495-
F: Into<$crate::NodeFlags>,
496-
T:Into<$crate::Time>,
497-
P:Into<$crate::PopulationId>,
498-
I:Into<$crate::IndividualId>,
499-
M: $crate::metadata::NodeMetadata,
500-
{
501-
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
502-
let mdlen = md.len()?;
503-
node_table_add_row_details!(flags,
504-
time,
505-
population,
506-
individual,
507-
md.as_ptr(),
508-
mdlen.into(),
509-
$table)
510-
}
511-
};
512-
}
513-
514433
macro_rules! edge_table_add_row_details {
515434
($left: ident,
516435
$right: ident,

src/node_table.rs

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,88 @@
11
use crate::bindings as ll_bindings;
22
use crate::metadata;
3+
use crate::metadata::NodeMetadata;
34
use crate::sys;
45
use crate::NodeFlags;
56
use crate::SizeType;
67
use crate::Time;
78
use crate::{tsk_id_t, TskitError};
89
use crate::{IndividualId, NodeId, PopulationId};
910

11+
pub(crate) fn add_row_details(
12+
flags: ll_bindings::tsk_flags_t,
13+
time: f64,
14+
population: ll_bindings::tsk_id_t,
15+
individual: ll_bindings::tsk_id_t,
16+
metadata: *const std::os::raw::c_char,
17+
metadata_length: ll_bindings::tsk_size_t,
18+
table: *mut ll_bindings::tsk_node_table_t,
19+
) -> Result<NodeId, TskitError> {
20+
let rv = unsafe {
21+
ll_bindings::tsk_node_table_add_row(
22+
table,
23+
flags,
24+
time,
25+
population,
26+
individual,
27+
metadata,
28+
metadata_length,
29+
)
30+
};
31+
handle_tsk_return_value!(rv, rv.into())
32+
}
33+
34+
pub(crate) fn add_row<F, T, P, I>(
35+
flags: F,
36+
time: T,
37+
population: P,
38+
individual: I,
39+
table: *mut ll_bindings::tsk_node_table_t,
40+
) -> Result<NodeId, TskitError>
41+
where
42+
F: Into<NodeFlags>,
43+
T: Into<Time>,
44+
P: Into<PopulationId>,
45+
I: Into<IndividualId>,
46+
{
47+
add_row_details(
48+
flags.into().bits(),
49+
time.into().into(),
50+
population.into().into(),
51+
individual.into().into(),
52+
std::ptr::null(),
53+
0,
54+
table,
55+
)
56+
}
57+
58+
pub(crate) fn add_row_with_metadata<F, T, P, I, N>(
59+
flags: F,
60+
time: T,
61+
population: P,
62+
individual: I,
63+
metadata: &N,
64+
table: *mut ll_bindings::tsk_node_table_t,
65+
) -> Result<NodeId, TskitError>
66+
where
67+
F: Into<NodeFlags>,
68+
T: Into<Time>,
69+
P: Into<PopulationId>,
70+
I: Into<IndividualId>,
71+
N: NodeMetadata,
72+
{
73+
let md = crate::metadata::EncodedMetadata::new(metadata)?;
74+
let mdlen = md.len()?;
75+
add_row_details(
76+
flags.into().bits(),
77+
time.into().into(),
78+
population.into().into(),
79+
individual.into().into(),
80+
md.as_ptr(),
81+
mdlen.into(),
82+
table,
83+
)
84+
}
85+
1086
/// Row of a [`NodeTable`]
1187
#[derive(Debug)]
1288
pub struct NodeTableRow {
@@ -584,8 +660,46 @@ build_owned_table_type!(
584660
);
585661

586662
impl OwningNodeTable {
587-
node_table_add_row!(=> add_row, self, self.as_mut_ptr());
588-
node_table_add_row_with_metadata!(=> add_row_with_metadata, self, self.as_mut_ptr());
663+
pub fn add_row<F, T, P, I>(
664+
&mut self,
665+
flags: F,
666+
time: T,
667+
population: P,
668+
individual: I,
669+
) -> Result<NodeId, TskitError>
670+
where
671+
F: Into<NodeFlags>,
672+
T: Into<Time>,
673+
P: Into<PopulationId>,
674+
I: Into<IndividualId>,
675+
{
676+
add_row(flags, time, population, individual, self.as_mut_ptr())
677+
}
678+
679+
pub fn add_row_with_metadata<F, T, P, I, N>(
680+
&mut self,
681+
flags: F,
682+
time: T,
683+
population: P,
684+
individual: I,
685+
metadata: &N,
686+
) -> Result<NodeId, TskitError>
687+
where
688+
F: Into<NodeFlags>,
689+
T: Into<Time>,
690+
P: Into<PopulationId>,
691+
I: Into<IndividualId>,
692+
N: NodeMetadata,
693+
{
694+
add_row_with_metadata(
695+
flags,
696+
time,
697+
population,
698+
individual,
699+
metadata,
700+
self.as_mut_ptr(),
701+
)
702+
}
589703
}
590704

591705
#[cfg(test)]

src/table_collection.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,25 @@ impl TableCollection {
399399
/// by tree sequence simplification.
400400
=> add_migration_with_metadata, self, &mut (*self.as_mut_ptr()).migrations);
401401

402-
node_table_add_row!(
403402
/// Add a row to the node table
404-
=> add_node, self, &mut (*self.as_mut_ptr()).nodes);
403+
pub fn add_node<F, T, P, I>(
404+
&mut self,
405+
flags: F,
406+
time: T,
407+
population: P,
408+
individual: I,
409+
) -> Result<NodeId, TskitError>
410+
where
411+
F: Into<crate::NodeFlags>,
412+
T: Into<crate::Time>,
413+
P: Into<crate::PopulationId>,
414+
I: Into<crate::IndividualId>,
415+
{
416+
crate::node_table::add_row(flags, time, population, individual, unsafe {
417+
&mut (*self.as_mut_ptr()).nodes
418+
})
419+
}
405420

406-
node_table_add_row_with_metadata!(
407421
/// Add a row with optional metadata to the node table
408422
///
409423
/// # Examples
@@ -424,7 +438,30 @@ impl TableCollection {
424438
/// assert!(tables.add_node_with_metadata(0, 0.0, -1, -1, &metadata).is_ok());
425439
/// # }
426440
/// ```
427-
=> add_node_with_metadata, self, &mut (*self.as_mut_ptr()).nodes);
441+
pub fn add_node_with_metadata<F, T, P, I, N>(
442+
&mut self,
443+
flags: F,
444+
time: T,
445+
population: P,
446+
individual: I,
447+
metadata: &N,
448+
) -> Result<NodeId, TskitError>
449+
where
450+
F: Into<crate::NodeFlags>,
451+
T: Into<crate::Time>,
452+
P: Into<crate::PopulationId>,
453+
I: Into<crate::IndividualId>,
454+
N: crate::metadata::NodeMetadata,
455+
{
456+
crate::node_table::add_row_with_metadata(
457+
flags,
458+
time,
459+
population,
460+
individual,
461+
metadata,
462+
unsafe { &mut (*self.as_mut_ptr()).nodes },
463+
)
464+
}
428465

429466
site_table_add_row!(
430467
/// Add a row to the site table

0 commit comments

Comments
 (0)