From 489630c200c31ff8ac845968a66ef2db86591b82 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Sun, 4 Dec 2022 05:41:40 -0800 Subject: [PATCH] style: replace column access macro with generic fn This change improves readability and declutters some internal details. --- src/_macros.rs | 41 ----------------------------------------- src/edge_table.rs | 27 +++++++++------------------ src/individual_table.rs | 7 ++++++- src/lib.rs | 1 + src/migration_table.rs | 27 +++++++++++++-------------- src/mutation_table.rs | 14 ++++++-------- src/node_table.rs | 23 +++++++++++------------ src/site_table.rs | 8 +++----- src/sys.rs | 32 ++++++++++++++++++++++++++++++++ src/tree_interface.rs | 15 ++++++++------- 10 files changed, 89 insertions(+), 106 deletions(-) create mode 100644 src/sys.rs diff --git a/src/_macros.rs b/src/_macros.rs index 28727286f..5a6d19653 100644 --- a/src/_macros.rs +++ b/src/_macros.rs @@ -27,47 +27,6 @@ macro_rules! panic_on_tskit_error { }; } -macro_rules! unsafe_tsk_column_access { - ($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident) => {{ - let x = $crate::tsk_id_t::from($i); - if x < $lo || (x as $crate::tsk_size_t) >= $hi { - None - } else { - debug_assert!(!($owner).$array.is_null()); - if !$owner.$array.is_null() { - // SAFETY: array is not null - // and we did our best effort - // on bounds checking - Some(unsafe { *$owner.$array.offset(x as isize) }) - } else { - None - } - } - }}; - ($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident, $output_id_type: ty) => {{ - let x = $crate::tsk_id_t::from($i); - if x < $lo || (x as $crate::tsk_size_t) >= $hi { - None - } else { - debug_assert!(!($owner).$array.is_null()); - if !$owner.$array.is_null() { - // SAFETY: array is not null - // and we did our best effort - // on bounds checking - unsafe { Some(<$output_id_type>::from(*($owner.$array.offset(x as isize)))) } - } else { - None - } - } - }}; -} - -macro_rules! unsafe_tsk_column_access_and_map_into { - ($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident) => {{ - unsafe_tsk_column_access!($i, $lo, $hi, $owner, $array).map(|v| v.into()) - }}; -} - macro_rules! unsafe_tsk_ragged_column_access { ($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident, $offset_array: ident, $offset_array_len: ident, $output_id_type: ty) => {{ let i = $crate::SizeType::try_from($i).ok()?; diff --git a/src/edge_table.rs b/src/edge_table.rs index adc179cd6..aa1cf5221 100644 --- a/src/edge_table.rs +++ b/src/edge_table.rs @@ -2,6 +2,7 @@ use std::ptr::NonNull; use crate::bindings as ll_bindings; use crate::metadata; +use crate::sys; use crate::Position; use crate::{tsk_id_t, TskitError}; use crate::{EdgeId, NodeId}; @@ -180,14 +181,7 @@ impl EdgeTable { /// * `Some(parent)` if `u` is valid. /// * `None` otherwise. pub fn parent + Copy>(&self, row: E) -> Option { - unsafe_tsk_column_access!( - row.into(), - 0, - self.num_rows(), - self.as_ref(), - parent, - NodeId - ) + sys::tsk_column_access::(row.into(), self.as_ref().parent, self.num_rows()) } /// Return the ``child`` value from row ``row`` of the table. @@ -197,7 +191,7 @@ impl EdgeTable { /// * `Some(child)` if `u` is valid. /// * `None` otherwise. pub fn child + Copy>(&self, row: E) -> Option { - unsafe_tsk_column_access!(row.into(), 0, self.num_rows(), self.as_ref(), child, NodeId) + sys::tsk_column_access::(row.into(), self.as_ref().child, self.num_rows()) } /// Return the ``left`` value from row ``row`` of the table. @@ -207,14 +201,7 @@ impl EdgeTable { /// * `Some(position)` if `u` is valid. /// * `None` otherwise. pub fn left + Copy>(&self, row: E) -> Option { - unsafe_tsk_column_access!( - row.into(), - 0, - self.num_rows(), - self.as_ref(), - left, - Position - ) + sys::tsk_column_access::(row.into(), self.as_ref().left, self.num_rows()) } /// Return the ``right`` value from row ``row`` of the table. @@ -224,7 +211,11 @@ impl EdgeTable { /// * `Some(position)` if `u` is valid. /// * `None` otherwise. pub fn right + Copy>(&self, row: E) -> Option { - unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), right) + sys::tsk_column_access::( + row.into(), + self.as_ref().right, + self.num_rows(), + ) } /// Retrieve decoded metadata for a `row`. diff --git a/src/individual_table.rs b/src/individual_table.rs index b7fca8fa1..13e335653 100644 --- a/src/individual_table.rs +++ b/src/individual_table.rs @@ -2,6 +2,7 @@ use std::ptr::NonNull; use crate::bindings as ll_bindings; use crate::metadata; +use crate::sys; use crate::IndividualFlags; use crate::IndividualId; use crate::Location; @@ -171,7 +172,11 @@ impl IndividualTable { /// * `Some(flags)` if `row` is valid. /// * `None` otherwise. pub fn flags + Copy>(&self, row: I) -> Option { - unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), flags) + sys::tsk_column_access::( + row.into(), + self.as_ref().flags, + self.num_rows(), + ) } /// Return the locations for a given row. diff --git a/src/lib.rs b/src/lib.rs index 27c9b826c..81e5b1228 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,6 +92,7 @@ mod node_table; mod population_table; pub mod prelude; mod site_table; +mod sys; mod table_collection; mod table_iterator; pub mod table_views; diff --git a/src/migration_table.rs b/src/migration_table.rs index 1bd8fe344..47308aa51 100644 --- a/src/migration_table.rs +++ b/src/migration_table.rs @@ -2,6 +2,7 @@ use std::ptr::NonNull; use crate::bindings as ll_bindings; use crate::metadata; +use crate::sys; use crate::Position; use crate::SizeType; use crate::Time; @@ -199,7 +200,7 @@ impl MigrationTable { /// * `Some(position)` if `row` is valid. /// * `None` otherwise. pub fn left + Copy>(&self, row: M) -> Option { - unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), left) + sys::tsk_column_access::(row.into(), self.as_ref().left, self.num_rows()) } /// Return the right coordinate for a given row. @@ -209,7 +210,11 @@ impl MigrationTable { /// * `Some(positions)` if `row` is valid. /// * `None` otherwise. pub fn right + Copy>(&self, row: M) -> Option { - unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), right) + sys::tsk_column_access::( + row.into(), + self.as_ref().right, + self.num_rows(), + ) } /// Return the node for a given row. @@ -219,7 +224,7 @@ impl MigrationTable { /// * `Some(node)` if `row` is valid. /// * `None` otherwise. pub fn node + Copy>(&self, row: M) -> Option { - unsafe_tsk_column_access!(row.into(), 0, self.num_rows(), self.as_ref(), node, NodeId) + sys::tsk_column_access::(row.into(), self.as_ref().node, self.num_rows()) } /// Return the source population for a given row. @@ -229,13 +234,10 @@ impl MigrationTable { /// * `Some(population)` if `row` is valid. /// * `None` otherwise. pub fn source + Copy>(&self, row: M) -> Option { - unsafe_tsk_column_access!( + sys::tsk_column_access::( row.into(), - 0, + self.as_ref().source, self.num_rows(), - self.as_ref(), - source, - PopulationId ) } @@ -246,13 +248,10 @@ impl MigrationTable { /// * `Some(population)` if `row` is valid. /// * `None` otherwise. pub fn dest + Copy>(&self, row: M) -> Option { - unsafe_tsk_column_access!( + sys::tsk_column_access::( row.into(), - 0, + self.as_ref().dest, self.num_rows(), - self.as_ref(), - dest, - PopulationId ) } @@ -263,7 +262,7 @@ impl MigrationTable { /// * `Some(time)` if `row` is valid. /// * `None` otherwise. pub fn time + Copy>(&self, row: M) -> Option