Skip to content

Commit f1774b4

Browse files
authored
style: use fn to get array slices from Trees. (#424)
Replace another macro with fn.
1 parent b25368e commit f1774b4

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

src/_macros.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,6 @@ macro_rules! iterator_for_nodeiterator {
7777
};
7878
}
7979

80-
macro_rules! tree_array_slice {
81-
($self: ident, $array: ident, $len: expr) => {
82-
unsafe {
83-
std::slice::from_raw_parts(
84-
(*$self.as_ptr()).$array as *const $crate::NodeId,
85-
$len as usize,
86-
)
87-
}
88-
};
89-
}
90-
9180
macro_rules! impl_id_traits {
9281
($idtype: ty) => {
9382
impl $idtype {

src/sys.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ pub fn tsk_ragged_column_access<
8686
tsk_ragged_column_access_detail(row, column, column_length, offset, offset_length)
8787
.map(|(p, n)| unsafe { std::slice::from_raw_parts(p.cast::<O>(), n) })
8888
}
89+
90+
pub fn tree_array_slice<'a, L: Into<bindings::tsk_size_t>>(
91+
data: *const bindings::tsk_id_t,
92+
length: L,
93+
) -> &'a [crate::NodeId] {
94+
assert!(!data.is_null());
95+
// SAFETY: pointer is not null, length comes from C API
96+
unsafe { std::slice::from_raw_parts(data.cast::<crate::NodeId>(), length.into() as usize) }
97+
}

src/tree_interface.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ impl TreeInterface {
3232
}
3333
}
3434

35+
fn as_ref(&self) -> &ll_bindings::tsk_tree_t {
36+
// SAFETY: we have already successfuly constructed
37+
// the NonNull
38+
unsafe { self.non_owned_pointer.as_ref() }
39+
}
40+
3541
/// Pointer to the low-level C type.
3642
pub fn as_ptr(&self) -> *const ll_bindings::tsk_tree_t {
3743
self.non_owned_pointer.as_ptr()
@@ -63,7 +69,7 @@ impl TreeInterface {
6369
/// }
6470
/// ```
6571
pub fn parent_array(&self) -> &[NodeId] {
66-
tree_array_slice!(self, parent, self.array_len)
72+
sys::tree_array_slice(self.as_ref().parent, self.array_len)
6773
}
6874

6975
/// # Failing examples
@@ -99,7 +105,10 @@ impl TreeInterface {
99105
pub fn samples_array(&self) -> Result<&[NodeId], TskitError> {
100106
let num_samples =
101107
unsafe { ll_bindings::tsk_treeseq_get_num_samples((*self.as_ptr()).tree_sequence) };
102-
err_if_not_tracking_samples!(self.flags, tree_array_slice!(self, samples, num_samples))
108+
err_if_not_tracking_samples!(
109+
self.flags,
110+
sys::tree_array_slice(self.as_ref().samples, num_samples)
111+
)
103112
}
104113

105114
/// # Failing examples
@@ -135,7 +144,7 @@ impl TreeInterface {
135144
pub fn next_sample_array(&self) -> Result<&[NodeId], TskitError> {
136145
err_if_not_tracking_samples!(
137146
self.flags,
138-
tree_array_slice!(self, next_sample, (*self.as_ptr()).num_nodes)
147+
sys::tree_array_slice(self.as_ref().next_sample, self.array_len)
139148
)
140149
}
141150

@@ -172,7 +181,7 @@ impl TreeInterface {
172181
pub fn left_sample_array(&self) -> Result<&[NodeId], TskitError> {
173182
err_if_not_tracking_samples!(
174183
self.flags,
175-
tree_array_slice!(self, left_sample, (*self.as_ptr()).num_nodes)
184+
sys::tree_array_slice(self.as_ref().left_sample, self.array_len)
176185
)
177186
}
178187

@@ -209,7 +218,7 @@ impl TreeInterface {
209218
pub fn right_sample_array(&self) -> Result<&[NodeId], TskitError> {
210219
err_if_not_tracking_samples!(
211220
self.flags,
212-
tree_array_slice!(self, right_sample, (*self.as_ptr()).num_nodes)
221+
sys::tree_array_slice(self.as_ref().right_sample, self.array_len)
213222
)
214223
}
215224

@@ -230,7 +239,7 @@ impl TreeInterface {
230239
/// }
231240
/// ```
232241
pub fn left_sib_array(&self) -> &[NodeId] {
233-
tree_array_slice!(self, left_sib, self.array_len)
242+
sys::tree_array_slice(self.as_ref().left_sib, self.array_len)
234243
}
235244

236245
/// # Failing examples
@@ -250,7 +259,7 @@ impl TreeInterface {
250259
/// }
251260
/// ```
252261
pub fn right_sib_array(&self) -> &[NodeId] {
253-
tree_array_slice!(self, right_sib, self.array_len)
262+
sys::tree_array_slice(self.as_ref().right_sib, self.array_len)
254263
}
255264

256265
/// # Failing examples
@@ -270,7 +279,7 @@ impl TreeInterface {
270279
/// }
271280
/// ```
272281
pub fn left_child_array(&self) -> &[NodeId] {
273-
tree_array_slice!(self, left_child, self.array_len)
282+
sys::tree_array_slice(self.as_ref().left_child, self.array_len)
274283
}
275284

276285
/// # Failing examples
@@ -290,7 +299,7 @@ impl TreeInterface {
290299
/// }
291300
/// ```
292301
pub fn right_child_array(&self) -> &[NodeId] {
293-
tree_array_slice!(self, right_child, self.array_len)
302+
sys::tree_array_slice(self.as_ref().right_child, self.array_len)
294303
}
295304

296305
// error if we are not tracking samples,
@@ -396,7 +405,7 @@ impl TreeInterface {
396405
pub fn sample_nodes(&self) -> &[NodeId] {
397406
let num_samples =
398407
unsafe { ll_bindings::tsk_treeseq_get_num_samples((*self.as_ptr()).tree_sequence) };
399-
tree_array_slice!(self, samples, num_samples)
408+
sys::tree_array_slice(self.as_ref().samples, num_samples)
400409
}
401410

402411
/// Return an [`Iterator`] from the node `u` to the root of the tree,

src/trees.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::ops::DerefMut;
44

55
use crate::bindings as ll_bindings;
66
use crate::error::TskitError;
7+
use crate::sys;
78
use crate::NodeId;
89
use crate::SimplificationOptions;
910
use crate::SizeType;
@@ -259,6 +260,10 @@ impl TreeSequence {
259260
})
260261
}
261262

263+
fn as_ref(&self) -> &ll_bindings::tsk_treeseq_t {
264+
&self.inner
265+
}
266+
262267
/// Pointer to the low-level C type.
263268
pub fn as_ptr(&self) -> *const ll_bindings::tsk_treeseq_t {
264269
&self.inner
@@ -391,7 +396,7 @@ impl TreeSequence {
391396
/// Get the list of sample nodes as a slice.
392397
pub fn sample_nodes(&self) -> &[NodeId] {
393398
let num_samples = unsafe { ll_bindings::tsk_treeseq_get_num_samples(self.as_ptr()) };
394-
tree_array_slice!(self, samples, num_samples)
399+
sys::tree_array_slice(self.as_ref().samples, num_samples)
395400
}
396401

397402
/// Get the number of trees.

0 commit comments

Comments
 (0)