Skip to content

Commit

Permalink
refactor: move newtypes into module
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Dec 4, 2022
1 parent 4984aca commit e787d77
Show file tree
Hide file tree
Showing 14 changed files with 476 additions and 531 deletions.
22 changes: 13 additions & 9 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,32 @@ macro_rules! panic_on_tskit_error {

macro_rules! unsafe_tsk_column_access {
($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident) => {{
if $i < $lo || ($i as $crate::tsk_size_t) >= $hi {
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($i as isize) })
Some(unsafe { *$owner.$array.offset(x as isize) })
} else {
None
}
}
}};
($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident, $output_id_type: expr) => {{
if $i < $lo || ($i as $crate::tsk_size_t) >= $hi {
($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(*($owner.$array.offset($i as isize)))) }
unsafe { Some(<$output_id_type>::from(*($owner.$array.offset(x as isize)))) }
} else {
None
}
Expand All @@ -78,10 +80,11 @@ macro_rules! unsafe_tsk_ragged_column_access {
if $owner.$array.is_null() {
return None;
}
let offset = i.as_usize() as isize;
// SAFETY: we have checked bounds and ensured not null
let start = unsafe { *$owner.$offset_array.offset($i as isize) };
let start = unsafe { *$owner.$offset_array.offset(offset) };
let stop = if i < $hi {
unsafe { *$owner.$offset_array.offset(($i + 1) as isize) }
unsafe { *$owner.$offset_array.offset((offset + 1) as isize) }
} else {
$owner.$offset_array_len as tsk_size_t
};
Expand Down Expand Up @@ -145,9 +148,10 @@ macro_rules! unsafe_tsk_ragged_char_column_access_to_slice_u8 {
} else {
assert!(!$owner.$array.is_null());
assert!(!$owner.$offset_array.is_null());
let start = unsafe { *$owner.$offset_array.offset($i as isize) };
let offset = i.as_usize() as isize;
let start = unsafe { *$owner.$offset_array.offset(offset) };
let stop = if i < $hi {
unsafe { *$owner.$offset_array.offset(($i + 1) as isize) }
unsafe { *$owner.$offset_array.offset((offset + 1) as isize) }
} else {
$owner.$offset_array_len as tsk_size_t
};
Expand Down
25 changes: 6 additions & 19 deletions src/edge_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl EdgeTable {
/// * `None` otherwise.
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
unsafe_tsk_column_access!(
row.into().0,
row.into(),
0,
self.num_rows(),
self.as_ref(),
Expand All @@ -197,14 +197,7 @@ impl EdgeTable {
/// * `Some(child)` if `u` is valid.
/// * `None` otherwise.
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
unsafe_tsk_column_access!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
child,
NodeId
)
unsafe_tsk_column_access!(row.into(), 0, self.num_rows(), self.as_ref(), child, NodeId)
}

/// Return the ``left`` value from row ``row`` of the table.
Expand All @@ -215,7 +208,7 @@ impl EdgeTable {
/// * `None` otherwise.
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
unsafe_tsk_column_access!(
row.into().0,
row.into(),
0,
self.num_rows(),
self.as_ref(),
Expand All @@ -231,13 +224,7 @@ impl EdgeTable {
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
unsafe_tsk_column_access_and_map_into!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
right
)
unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), right)
}

/// Retrieve decoded metadata for a `row`.
Expand All @@ -261,7 +248,7 @@ impl EdgeTable {
row: EdgeId,
) -> Option<Result<T, TskitError>> {
let table_ref = self.as_ref();
let buffer = metadata_to_vector!(self, table_ref, row.0)?;
let buffer = metadata_to_vector!(self, table_ref, row.into())?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}

Expand All @@ -287,7 +274,7 @@ impl EdgeTable {
/// * `Some(row)` if `r` is valid
/// * `None` otherwise
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRow> {
table_row_access!(r.into().0, self, make_edge_table_row)
table_row_access!(r.into().into(), self, make_edge_table_row)
}

/// Return a view of row `r` of the table.
Expand Down
16 changes: 5 additions & 11 deletions src/individual_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,7 @@ impl IndividualTable {
/// * `Some(flags)` if `row` is valid.
/// * `None` otherwise.
pub fn flags<I: Into<IndividualId> + Copy>(&self, row: I) -> Option<IndividualFlags> {
unsafe_tsk_column_access_and_map_into!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
flags
)
unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), flags)
}

/// Return the locations for a given row.
Expand All @@ -188,7 +182,7 @@ impl IndividualTable {
/// * `None` otherwise.
pub fn location<I: Into<IndividualId> + Copy>(&self, row: I) -> Option<&[Location]> {
unsafe_tsk_ragged_column_access!(
row.into().0,
row.into(),
0,
self.num_rows(),
self.as_ref(),
Expand All @@ -207,7 +201,7 @@ impl IndividualTable {
/// * `None` otherwise.
pub fn parents<I: Into<IndividualId> + Copy>(&self, row: I) -> Option<&[IndividualId]> {
unsafe_tsk_ragged_column_access!(
row.into().0,
row.into(),
0,
self.num_rows(),
self.as_ref(),
Expand Down Expand Up @@ -393,7 +387,7 @@ match tables.individuals().metadata::<MutationMetadata>(0.into())
row: IndividualId,
) -> Option<Result<T, TskitError>> {
let table_ref = self.as_ref();
let buffer = metadata_to_vector!(self, table_ref, row.0)?;
let buffer = metadata_to_vector!(self, table_ref, row.into())?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}

Expand All @@ -419,7 +413,7 @@ match tables.individuals().metadata::<MutationMetadata>(0.into())
/// * `Some(row)` if `r` is valid
/// * `None` otherwise
pub fn row<I: Into<IndividualId> + Copy>(&self, r: I) -> Option<IndividualTableRow> {
let ri = r.into().0;
let ri = r.into().into();
table_row_access!(ri, self, make_individual_table_row)
}

Expand Down

0 comments on commit e787d77

Please sign in to comment.