Skip to content

Commit 2e9d139

Browse files
committed
further dedup code
1 parent 92ed430 commit 2e9d139

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

src/node_table.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ where
8383
)
8484
}
8585

86+
pub(crate) fn add_row_with_defaults<T: Into<crate::Time>, D: crate::node_table::DefaultNodeData>(
87+
time: T,
88+
defaults: &D,
89+
table: *mut ll_bindings::tsk_node_table_t,
90+
) -> Result<NodeId, TskitError> {
91+
let md = defaults.metadata()?;
92+
let (ptr, mdlen) = match &md {
93+
Some(value) => (
94+
value.as_ptr().cast::<i8>(),
95+
crate::SizeType::try_from(value.len())?,
96+
),
97+
None => (std::ptr::null(), 0.into()),
98+
};
99+
add_row_details(
100+
defaults.flags().bits(),
101+
time.into().into(),
102+
defaults.population().into(),
103+
defaults.individual().into(),
104+
ptr,
105+
mdlen.into(),
106+
table,
107+
)
108+
}
109+
86110
/// Row of a [`NodeTable`]
87111
#[derive(Debug)]
88112
pub struct NodeTableRow {
@@ -943,23 +967,7 @@ impl OwningNodeTable {
943967
time: T,
944968
defaults: &D,
945969
) -> Result<NodeId, TskitError> {
946-
let md = defaults.metadata()?;
947-
let (ptr, mdlen) = match &md {
948-
Some(value) => (
949-
value.as_ptr().cast::<i8>(),
950-
SizeType::try_from(value.len())?,
951-
),
952-
None => (std::ptr::null(), 0.into()),
953-
};
954-
add_row_details(
955-
defaults.flags().bits(),
956-
time.into().into(),
957-
defaults.population().into(),
958-
defaults.individual().into(),
959-
ptr,
960-
mdlen.into(),
961-
self.as_mut_ptr(),
962-
)
970+
add_row_with_defaults(time, defaults, self.as_mut_ptr())
963971
}
964972
}
965973

src/table_collection.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -435,23 +435,9 @@ impl TableCollection {
435435
time: T,
436436
defaults: &D,
437437
) -> Result<NodeId, TskitError> {
438-
let md = defaults.metadata()?;
439-
let (ptr, mdlen) = match &md {
440-
Some(value) => (
441-
value.as_ptr().cast::<i8>(),
442-
crate::SizeType::try_from(value.len())?,
443-
),
444-
None => (std::ptr::null(), 0.into()),
445-
};
446-
crate::node_table::add_row_details(
447-
defaults.flags().bits(),
448-
time.into().into(),
449-
defaults.population().into(),
450-
defaults.individual().into(),
451-
ptr,
452-
mdlen.into(),
453-
unsafe { &mut (*self.as_mut_ptr()).nodes },
454-
)
438+
crate::node_table::add_row_with_defaults(time, defaults, unsafe {
439+
&mut (*self.as_mut_ptr()).nodes
440+
})
455441
}
456442

457443
/// Add a row with optional metadata to the node table

0 commit comments

Comments
 (0)