Skip to content

style: use fn to get array slices from Trees. #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ macro_rules! iterator_for_nodeiterator {
};
}

macro_rules! tree_array_slice {
($self: ident, $array: ident, $len: expr) => {
unsafe {
std::slice::from_raw_parts(
(*$self.as_ptr()).$array as *const $crate::NodeId,
$len as usize,
)
}
};
}

macro_rules! impl_id_traits {
($idtype: ty) => {
impl $idtype {
Expand Down
9 changes: 9 additions & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,12 @@ pub fn tsk_ragged_column_access<
tsk_ragged_column_access_detail(row, column, column_length, offset, offset_length)
.map(|(p, n)| unsafe { std::slice::from_raw_parts(p.cast::<O>(), n) })
}

pub fn tree_array_slice<'a, L: Into<bindings::tsk_size_t>>(
data: *const bindings::tsk_id_t,
length: L,
) -> &'a [crate::NodeId] {
assert!(!data.is_null());
// SAFETY: pointer is not null, length comes from C API
unsafe { std::slice::from_raw_parts(data.cast::<crate::NodeId>(), length.into() as usize) }
}
29 changes: 19 additions & 10 deletions src/tree_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ impl TreeInterface {
}
}

fn as_ref(&self) -> &ll_bindings::tsk_tree_t {
// SAFETY: we have already successfuly constructed
// the NonNull
unsafe { self.non_owned_pointer.as_ref() }
}

/// Pointer to the low-level C type.
pub fn as_ptr(&self) -> *const ll_bindings::tsk_tree_t {
self.non_owned_pointer.as_ptr()
Expand Down Expand Up @@ -63,7 +69,7 @@ impl TreeInterface {
/// }
/// ```
pub fn parent_array(&self) -> &[NodeId] {
tree_array_slice!(self, parent, self.array_len)
sys::tree_array_slice(self.as_ref().parent, self.array_len)
}

/// # Failing examples
Expand Down Expand Up @@ -99,7 +105,10 @@ impl TreeInterface {
pub fn samples_array(&self) -> Result<&[NodeId], TskitError> {
let num_samples =
unsafe { ll_bindings::tsk_treeseq_get_num_samples((*self.as_ptr()).tree_sequence) };
err_if_not_tracking_samples!(self.flags, tree_array_slice!(self, samples, num_samples))
err_if_not_tracking_samples!(
self.flags,
sys::tree_array_slice(self.as_ref().samples, num_samples)
)
}

/// # Failing examples
Expand Down Expand Up @@ -135,7 +144,7 @@ impl TreeInterface {
pub fn next_sample_array(&self) -> Result<&[NodeId], TskitError> {
err_if_not_tracking_samples!(
self.flags,
tree_array_slice!(self, next_sample, (*self.as_ptr()).num_nodes)
sys::tree_array_slice(self.as_ref().next_sample, self.array_len)
)
}

Expand Down Expand Up @@ -172,7 +181,7 @@ impl TreeInterface {
pub fn left_sample_array(&self) -> Result<&[NodeId], TskitError> {
err_if_not_tracking_samples!(
self.flags,
tree_array_slice!(self, left_sample, (*self.as_ptr()).num_nodes)
sys::tree_array_slice(self.as_ref().left_sample, self.array_len)
)
}

Expand Down Expand Up @@ -209,7 +218,7 @@ impl TreeInterface {
pub fn right_sample_array(&self) -> Result<&[NodeId], TskitError> {
err_if_not_tracking_samples!(
self.flags,
tree_array_slice!(self, right_sample, (*self.as_ptr()).num_nodes)
sys::tree_array_slice(self.as_ref().right_sample, self.array_len)
)
}

Expand All @@ -230,7 +239,7 @@ impl TreeInterface {
/// }
/// ```
pub fn left_sib_array(&self) -> &[NodeId] {
tree_array_slice!(self, left_sib, self.array_len)
sys::tree_array_slice(self.as_ref().left_sib, self.array_len)
}

/// # Failing examples
Expand All @@ -250,7 +259,7 @@ impl TreeInterface {
/// }
/// ```
pub fn right_sib_array(&self) -> &[NodeId] {
tree_array_slice!(self, right_sib, self.array_len)
sys::tree_array_slice(self.as_ref().right_sib, self.array_len)
}

/// # Failing examples
Expand All @@ -270,7 +279,7 @@ impl TreeInterface {
/// }
/// ```
pub fn left_child_array(&self) -> &[NodeId] {
tree_array_slice!(self, left_child, self.array_len)
sys::tree_array_slice(self.as_ref().left_child, self.array_len)
}

/// # Failing examples
Expand All @@ -290,7 +299,7 @@ impl TreeInterface {
/// }
/// ```
pub fn right_child_array(&self) -> &[NodeId] {
tree_array_slice!(self, right_child, self.array_len)
sys::tree_array_slice(self.as_ref().right_child, self.array_len)
}

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

/// Return an [`Iterator`] from the node `u` to the root of the tree,
Expand Down
7 changes: 6 additions & 1 deletion src/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops::DerefMut;

use crate::bindings as ll_bindings;
use crate::error::TskitError;
use crate::sys;
use crate::NodeId;
use crate::SimplificationOptions;
use crate::SizeType;
Expand Down Expand Up @@ -259,6 +260,10 @@ impl TreeSequence {
})
}

fn as_ref(&self) -> &ll_bindings::tsk_treeseq_t {
&self.inner
}

/// Pointer to the low-level C type.
pub fn as_ptr(&self) -> *const ll_bindings::tsk_treeseq_t {
&self.inner
Expand Down Expand Up @@ -391,7 +396,7 @@ impl TreeSequence {
/// Get the list of sample nodes as a slice.
pub fn sample_nodes(&self) -> &[NodeId] {
let num_samples = unsafe { ll_bindings::tsk_treeseq_get_num_samples(self.as_ptr()) };
tree_array_slice!(self, samples, num_samples)
sys::tree_array_slice(self.as_ref().samples, num_samples)
}

/// Get the number of trees.
Expand Down