Skip to content

Commit fa79e15

Browse files
authored
refactor: TableCollection::simplify returns Option<&[NodeId]> (#316)
BREAKING CHANGE: The return value has changed. Code relying on the return value being a vector rather than a slice will break.
1 parent 0806a70 commit fa79e15

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/table_collection.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::vec;
2+
13
use crate::bindings as ll_bindings;
24
use crate::error::TskitError;
35
use crate::types::Bookmark;
@@ -60,6 +62,7 @@ use mbox::MBox;
6062
///
6163
pub struct TableCollection {
6264
inner: MBox<ll_bindings::tsk_table_collection_t>,
65+
idmap: Vec<NodeId>,
6366
}
6467

6568
impl TskitTypeAccess<ll_bindings::tsk_table_collection_t> for TableCollection {
@@ -119,7 +122,10 @@ impl TableCollection {
119122
if rv < 0 {
120123
return Err(crate::error::TskitError::ErrorCode { code: rv });
121124
}
122-
let mut tables = Self { inner: mbox };
125+
let mut tables = Self {
126+
inner: mbox,
127+
idmap: vec![],
128+
};
123129
unsafe {
124130
(*tables.as_mut_ptr()).sequence_length = sequence_length.0;
125131
}
@@ -134,7 +140,10 @@ impl TableCollection {
134140
/// requiring an uninitialized table collection.
135141
/// Consult the C API docs before using!
136142
pub(crate) unsafe fn new_from_mbox(mbox: MBox<ll_bindings::tsk_table_collection_t>) -> Self {
137-
Self { inner: mbox }
143+
Self {
144+
inner: mbox,
145+
idmap: vec![],
146+
}
138147
}
139148

140149
pub(crate) fn into_raw(self) -> Result<*mut ll_bindings::tsk_table_collection_t, TskitError> {
@@ -762,10 +771,10 @@ impl TableCollection {
762771
samples: &[N],
763772
options: O,
764773
idmap: bool,
765-
) -> Result<Option<Vec<NodeId>>, TskitError> {
766-
let mut output_node_map: Vec<NodeId> = vec![];
774+
) -> Result<Option<&[NodeId]>, TskitError> {
767775
if idmap {
768-
output_node_map.resize(usize::try_from(self.nodes().num_rows())?, NodeId::NULL);
776+
self.idmap
777+
.resize(usize::try_from(self.nodes().num_rows())?, NodeId::NULL);
769778
}
770779
let rv = unsafe {
771780
ll_bindings::tsk_table_collection_simplify(
@@ -774,15 +783,15 @@ impl TableCollection {
774783
samples.len() as tsk_size_t,
775784
options.into().bits(),
776785
match idmap {
777-
true => output_node_map.as_mut_ptr().cast::<tsk_id_t>(),
786+
true => self.idmap.as_mut_ptr().cast::<tsk_id_t>(),
778787
false => std::ptr::null_mut(),
779788
},
780789
)
781790
};
782791
handle_tsk_return_value!(
783792
rv,
784793
match idmap {
785-
true => Some(output_node_map),
794+
true => Some(&self.idmap),
786795
false => None,
787796
}
788797
)

0 commit comments

Comments
 (0)