Skip to content

fix: impl DerefMut for "owned" tables. #371

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
Nov 2, 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
8 changes: 8 additions & 0 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,14 @@ macro_rules! build_owned_tables {
}
}

impl std::ops::DerefMut for $name {
fn deref_mut(&mut self) -> &mut Self::Target {
// SAFETY: that T* and &T have same layout,
// and Target is repr(transparent).
unsafe { std::mem::transmute(&mut self.table) }
}
}

impl Drop for $name {
fn drop(&mut self) {
let rv = unsafe { $free(&mut (*self.table)) };
Expand Down
18 changes: 14 additions & 4 deletions src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,20 @@ impl<'a> NodeTable<'a> {
/// table collection:
///
/// ```
/// // let mut nodes = tskit::OwnedNodeTable::default();
/// // assert!(nodes.add_row(tskit::NodeFlags::IS_SAMPLE, 10., -1, -1).is_ok());
/// // let flags = nodes.flags_array_mut();
/// // assert!(flags.iter().all(|f| f.is_sample()));
/// let mut nodes = tskit::OwnedNodeTable::default();
/// assert!(nodes.add_row(tskit::NodeFlags::IS_SAMPLE, 10., -1, -1).is_ok());
/// # assert_eq!(nodes.num_rows(), 1);
/// let flags = nodes.flags_array_mut();
/// # assert_eq!(flags.len(), 1);
/// assert!(flags.iter().all(|f| f.is_sample()));
///
/// // while we are at it, let's use our node
/// // table to populate a table collection.
/// # use tskit::TableAccess;
/// let mut tables = tskit::TableCollection::new(10.0).unwrap();
/// tables.set_nodes(&nodes);
/// assert_eq!(tables.nodes().num_rows(), 1);
/// assert_eq!(tables.nodes().flags_array_mut().iter().filter(|f| f.is_sample()).count(), 1);
/// ```
///
/// # Note
Expand Down