Skip to content

refactor: generalize slice building in sys #427

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 5, 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
18 changes: 2 additions & 16 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,7 @@ macro_rules! build_table_column_slice_getter {
($(#[$attr:meta])* => $column: ident, $name: ident, $cast: ty) => {
$(#[$attr])*
pub fn $name(&self) -> &[$cast] {
// Caveat: num_rows is u64 but we need usize
// The conversion is fallible but unlikely.
let num_rows =
usize::try_from(self.num_rows()).expect("conversion of num_rows to usize failed");
let ptr = self.as_ref().$column as *const $cast;
// SAFETY: tables are initialzed, num rows comes
// from the C back end.
unsafe { std::slice::from_raw_parts(ptr, num_rows) }
$crate::sys::generate_slice(self.as_ref().$column, self.num_rows())
}
};
}
Expand All @@ -1032,14 +1025,7 @@ macro_rules! build_table_column_slice_mut_getter {
($(#[$attr:meta])* => $column: ident, $name: ident, $cast: ty) => {
$(#[$attr])*
pub fn $name(&mut self) -> &mut [$cast] {
// Caveat: num_rows is u64 but we need usize
// The conversion is fallible but unlikely.
let num_rows =
usize::try_from(self.num_rows()).expect("conversion of num_rows to usize failed");
let ptr = self.as_ref().$column as *mut $cast;
// SAFETY: tables are initialzed, num rows comes
// from the C back end.
unsafe { std::slice::from_raw_parts_mut(ptr, num_rows) }
$crate::sys::generate_slice_mut(self.as_ref().$column, self.num_rows())
}
};
}
Expand Down
14 changes: 2 additions & 12 deletions src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,12 @@ impl NodeTable {

#[deprecated(since = "0.12.0", note = "use flags_slice_mut instead")]
pub fn flags_array_mut(&mut self) -> &mut [NodeFlags] {
unsafe {
std::slice::from_raw_parts_mut(
self.as_ref().flags.cast::<NodeFlags>(),
usize::try_from(self.as_ref().num_rows).unwrap_or(0),
)
}
sys::generate_slice_mut(self.as_ref().flags, self.num_rows())
}

#[deprecated(since = "0.12.0", note = "use time_slice_mut instead")]
pub fn time_array_mut(&mut self) -> &mut [Time] {
unsafe {
std::slice::from_raw_parts_mut(
self.as_ref().time.cast::<Time>(),
usize::try_from(self.as_ref().num_rows).unwrap_or(0),
)
}
sys::generate_slice_mut(self.as_ref().flags, self.num_rows())
}

/// Return the ``population`` value from row ``row`` of the table.
Expand Down
17 changes: 13 additions & 4 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,20 @@ pub fn tsk_ragged_column_access<
.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,
pub fn generate_slice<'a, L: Into<bindings::tsk_size_t>, I, O>(
data: *const I,
length: L,
) -> &'a [crate::NodeId] {
) -> &'a [O] {
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) }
unsafe { std::slice::from_raw_parts(data.cast::<O>(), length.into() as usize) }
}

pub fn generate_slice_mut<'a, L: Into<bindings::tsk_size_t>, I, O>(
data: *mut I,
length: L,
) -> &'a mut [O] {
assert!(!data.is_null());
// SAFETY: pointer is not null, length comes from C API
unsafe { std::slice::from_raw_parts_mut(data.cast::<O>(), length.into() as usize) }
}
20 changes: 10 additions & 10 deletions src/tree_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl TreeInterface {
/// }
/// ```
pub fn parent_array(&self) -> &[NodeId] {
sys::tree_array_slice(self.as_ref().parent, self.array_len)
sys::generate_slice(self.as_ref().parent, self.array_len)
}

/// # Failing examples
Expand Down Expand Up @@ -107,7 +107,7 @@ impl TreeInterface {
unsafe { ll_bindings::tsk_treeseq_get_num_samples((*self.as_ptr()).tree_sequence) };
err_if_not_tracking_samples!(
self.flags,
sys::tree_array_slice(self.as_ref().samples, num_samples)
sys::generate_slice(self.as_ref().samples, num_samples)
)
}

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

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

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

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

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

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

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

// error if we are not tracking samples,
Expand Down Expand Up @@ -405,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) };
sys::tree_array_slice(self.as_ref().samples, num_samples)
sys::generate_slice(self.as_ref().samples, num_samples)
}

/// Return an [`Iterator`] from the node `u` to the root of the tree,
Expand Down
2 changes: 1 addition & 1 deletion src/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,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()) };
sys::tree_array_slice(self.as_ref().samples, num_samples)
sys::generate_slice(self.as_ref().samples, num_samples)
}

/// Get the number of trees.
Expand Down