Skip to content

Commit 93289f1

Browse files
committed
pinned traits streamline the interface
1 parent b8cf114 commit 93289f1

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/node_table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,15 +819,15 @@ impl OwningNodeTable {
819819
/// ```
820820
/// # let mut nodes = tskit::OwningNodeTable::default();
821821
/// let node_defaults = tskit::NodeDefaults::default();
822-
/// let rv = nodes.add_row_with_defaults(1.0, node_defaults).unwrap();
822+
/// let rv = nodes.add_row_with_defaults(1.0, &node_defaults).unwrap();
823823
/// assert_eq!(rv, 0);
824-
/// let rv = nodes.add_row_with_defaults(1.0, node_defaults).unwrap();
824+
/// let rv = nodes.add_row_with_defaults(1.0, &node_defaults).unwrap();
825825
/// assert_eq!(rv, 1);
826826
/// ```
827827
pub fn add_row_with_defaults<T: Into<crate::Time> + Copy, D: DefaultNodeData>(
828828
&mut self,
829829
time: T,
830-
defaults: D,
830+
defaults: &D,
831831
) -> Result<NodeId, TskitError> {
832832
let md = defaults.metadata()?;
833833
let (ptr, mdlen) = match &md {

src/table_collection.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::bindings as ll_bindings;
55
use crate::error::TskitError;
66
use crate::types::Bookmark;
77
use crate::IndividualTableSortOptions;
8-
use crate::NodeDefaults;
98
use crate::Position;
109
use crate::SimplificationOptions;
1110
use crate::TableClearOptions;
@@ -411,22 +410,43 @@ impl TableCollection {
411410
/// ```
412411
/// # let mut tables = tskit::TableCollection::new(1.).unwrap();
413412
/// let node_defaults = tskit::NodeDefaults::default();
414-
/// let rv = tables.add_node_with_defaults(1.0, node_defaults).unwrap();
413+
/// let rv = tables.add_node_with_defaults(1.0, &node_defaults).unwrap();
415414
/// assert_eq!(rv, 0);
416-
/// let rv = tables.add_node_with_defaults(2.0, node_defaults).unwrap();
415+
/// let rv = tables.add_node_with_defaults(2.0, &node_defaults).unwrap();
417416
/// assert_eq!(rv, 1);
418417
/// ```
419-
pub fn add_node_with_defaults<T: Into<crate::Time> + Copy>(
418+
pub fn add_node_with_defaults<
419+
T: Into<crate::Time>,
420+
D: crate::node_table::DefaultNodeData,
421+
>(
420422
&mut self,
421423
time: T,
422-
defaults: NodeDefaults,
424+
defaults: &D,
423425
) -> Result<NodeId, TskitError> {
424-
self.add_node(
425-
defaults.flags,
426-
time,
427-
defaults.population,
428-
defaults.individual,
429-
)
426+
let md = defaults.metadata()?;
427+
let (ptr, mdlen) = match &md {
428+
Some(value) => (
429+
value.as_ptr().cast::<i8>(),
430+
crate::SizeType::try_from(value.len())?,
431+
),
432+
None => (std::ptr::null(), 0.into()),
433+
};
434+
let flags: ll_bindings::tsk_flags_t = defaults.flags().bits();
435+
let population: ll_bindings::tsk_id_t = defaults.population().into();
436+
let individual: ll_bindings::tsk_id_t = defaults.individual().into();
437+
let time: f64 = time.into().into();
438+
let rv = unsafe {
439+
ll_bindings::tsk_node_table_add_row(
440+
&mut (*self.as_mut_ptr()).nodes,
441+
flags,
442+
time,
443+
population,
444+
individual,
445+
ptr,
446+
mdlen.into(),
447+
)
448+
};
449+
handle_tsk_return_value!(rv, rv.into())
430450
}
431451

432452
node_table_add_row_with_metadata!(

0 commit comments

Comments
 (0)