Skip to content

Commit 4c2c949

Browse files
authored
refactor: move newtypes into module (#421)
1 parent 4984aca commit 4c2c949

14 files changed

+476
-531
lines changed

src/_macros.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,32 @@ macro_rules! panic_on_tskit_error {
2929

3030
macro_rules! unsafe_tsk_column_access {
3131
($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident) => {{
32-
if $i < $lo || ($i as $crate::tsk_size_t) >= $hi {
32+
let x = $crate::tsk_id_t::from($i);
33+
if x < $lo || (x as $crate::tsk_size_t) >= $hi {
3334
None
3435
} else {
3536
debug_assert!(!($owner).$array.is_null());
3637
if !$owner.$array.is_null() {
3738
// SAFETY: array is not null
3839
// and we did our best effort
3940
// on bounds checking
40-
Some(unsafe { *$owner.$array.offset($i as isize) })
41+
Some(unsafe { *$owner.$array.offset(x as isize) })
4142
} else {
4243
None
4344
}
4445
}
4546
}};
46-
($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident, $output_id_type: expr) => {{
47-
if $i < $lo || ($i as $crate::tsk_size_t) >= $hi {
47+
($i: expr, $lo: expr, $hi: expr, $owner: expr, $array: ident, $output_id_type: ty) => {{
48+
let x = $crate::tsk_id_t::from($i);
49+
if x < $lo || (x as $crate::tsk_size_t) >= $hi {
4850
None
4951
} else {
5052
debug_assert!(!($owner).$array.is_null());
5153
if !$owner.$array.is_null() {
5254
// SAFETY: array is not null
5355
// and we did our best effort
5456
// on bounds checking
55-
unsafe { Some($output_id_type(*($owner.$array.offset($i as isize)))) }
57+
unsafe { Some(<$output_id_type>::from(*($owner.$array.offset(x as isize)))) }
5658
} else {
5759
None
5860
}
@@ -78,10 +80,11 @@ macro_rules! unsafe_tsk_ragged_column_access {
7880
if $owner.$array.is_null() {
7981
return None;
8082
}
83+
let offset = i.as_usize() as isize;
8184
// SAFETY: we have checked bounds and ensured not null
82-
let start = unsafe { *$owner.$offset_array.offset($i as isize) };
85+
let start = unsafe { *$owner.$offset_array.offset(offset) };
8386
let stop = if i < $hi {
84-
unsafe { *$owner.$offset_array.offset(($i + 1) as isize) }
87+
unsafe { *$owner.$offset_array.offset((offset + 1) as isize) }
8588
} else {
8689
$owner.$offset_array_len as tsk_size_t
8790
};
@@ -145,9 +148,10 @@ macro_rules! unsafe_tsk_ragged_char_column_access_to_slice_u8 {
145148
} else {
146149
assert!(!$owner.$array.is_null());
147150
assert!(!$owner.$offset_array.is_null());
148-
let start = unsafe { *$owner.$offset_array.offset($i as isize) };
151+
let offset = i.as_usize() as isize;
152+
let start = unsafe { *$owner.$offset_array.offset(offset) };
149153
let stop = if i < $hi {
150-
unsafe { *$owner.$offset_array.offset(($i + 1) as isize) }
154+
unsafe { *$owner.$offset_array.offset((offset + 1) as isize) }
151155
} else {
152156
$owner.$offset_array_len as tsk_size_t
153157
};

src/edge_table.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl EdgeTable {
181181
/// * `None` otherwise.
182182
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
183183
unsafe_tsk_column_access!(
184-
row.into().0,
184+
row.into(),
185185
0,
186186
self.num_rows(),
187187
self.as_ref(),
@@ -197,14 +197,7 @@ impl EdgeTable {
197197
/// * `Some(child)` if `u` is valid.
198198
/// * `None` otherwise.
199199
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
200-
unsafe_tsk_column_access!(
201-
row.into().0,
202-
0,
203-
self.num_rows(),
204-
self.as_ref(),
205-
child,
206-
NodeId
207-
)
200+
unsafe_tsk_column_access!(row.into(), 0, self.num_rows(), self.as_ref(), child, NodeId)
208201
}
209202

210203
/// Return the ``left`` value from row ``row`` of the table.
@@ -215,7 +208,7 @@ impl EdgeTable {
215208
/// * `None` otherwise.
216209
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
217210
unsafe_tsk_column_access!(
218-
row.into().0,
211+
row.into(),
219212
0,
220213
self.num_rows(),
221214
self.as_ref(),
@@ -231,13 +224,7 @@ impl EdgeTable {
231224
/// * `Some(position)` if `u` is valid.
232225
/// * `None` otherwise.
233226
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
234-
unsafe_tsk_column_access_and_map_into!(
235-
row.into().0,
236-
0,
237-
self.num_rows(),
238-
self.as_ref(),
239-
right
240-
)
227+
unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), right)
241228
}
242229

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

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

293280
/// Return a view of row `r` of the table.

src/individual_table.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,7 @@ impl IndividualTable {
171171
/// * `Some(flags)` if `row` is valid.
172172
/// * `None` otherwise.
173173
pub fn flags<I: Into<IndividualId> + Copy>(&self, row: I) -> Option<IndividualFlags> {
174-
unsafe_tsk_column_access_and_map_into!(
175-
row.into().0,
176-
0,
177-
self.num_rows(),
178-
self.as_ref(),
179-
flags
180-
)
174+
unsafe_tsk_column_access_and_map_into!(row.into(), 0, self.num_rows(), self.as_ref(), flags)
181175
}
182176

183177
/// Return the locations for a given row.
@@ -188,7 +182,7 @@ impl IndividualTable {
188182
/// * `None` otherwise.
189183
pub fn location<I: Into<IndividualId> + Copy>(&self, row: I) -> Option<&[Location]> {
190184
unsafe_tsk_ragged_column_access!(
191-
row.into().0,
185+
row.into(),
192186
0,
193187
self.num_rows(),
194188
self.as_ref(),
@@ -207,7 +201,7 @@ impl IndividualTable {
207201
/// * `None` otherwise.
208202
pub fn parents<I: Into<IndividualId> + Copy>(&self, row: I) -> Option<&[IndividualId]> {
209203
unsafe_tsk_ragged_column_access!(
210-
row.into().0,
204+
row.into(),
211205
0,
212206
self.num_rows(),
213207
self.as_ref(),
@@ -393,7 +387,7 @@ match tables.individuals().metadata::<MutationMetadata>(0.into())
393387
row: IndividualId,
394388
) -> Option<Result<T, TskitError>> {
395389
let table_ref = self.as_ref();
396-
let buffer = metadata_to_vector!(self, table_ref, row.0)?;
390+
let buffer = metadata_to_vector!(self, table_ref, row.into())?;
397391
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
398392
}
399393

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

0 commit comments

Comments
 (0)