Skip to content

Commit 5c9e92d

Browse files
committed
tryfrom
1 parent 8aeb85b commit 5c9e92d

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/_macros.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,13 @@ macro_rules! node_table_add_row_with_metadata {
676676
M: $crate::metadata::NodeMetadata,
677677
{
678678
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
679+
let mdlen = md.len()?;
679680
node_table_add_row_details!(flags,
680681
time,
681682
population,
682683
individual,
683684
md.as_ptr(),
684-
md.len().into(),
685+
mdlen.into(),
685686
$table)
686687
}
687688
};
@@ -761,7 +762,7 @@ macro_rules! edge_table_add_row_with_metadata {
761762
parent,
762763
child,
763764
md.as_ptr(),
764-
md.len().into(),
765+
md.len()?.into(),
765766
$table)
766767
}
767768
};
@@ -791,7 +792,7 @@ macro_rules! population_table_add_row_with_metadata {
791792
pub fn $name<M>(&mut $self, metadata: &M) -> Result<$crate::PopulationId, $crate::TskitError>
792793
where M: $crate::metadata::PopulationMetadata {
793794
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
794-
population_table_add_row_details!(md.as_ptr(), md.len().into(), $table)
795+
population_table_add_row_details!(md.as_ptr(), md.len()?.into(), $table)
795796
}
796797
};
797798
}
@@ -865,7 +866,7 @@ macro_rules! individual_table_add_row_with_metadata {
865866
location,
866867
parents,
867868
md.as_ptr(),
868-
md.len().into(),
869+
md.len()?.into(),
869870
$table)
870871
}
871872
};
@@ -946,7 +947,7 @@ macro_rules! mutation_table_add_row_with_metadata {
946947
time,
947948
derived_state,
948949
md.as_ptr(),
949-
md.len().into(),
950+
md.len()?.into(),
950951
$table)
951952
}
952953
};
@@ -1002,7 +1003,7 @@ macro_rules! site_table_add_row_with_metadata {
10021003
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
10031004
site_table_add_row_details!(position, ancestral_state,
10041005
md.as_ptr(),
1005-
md.len().into(),
1006+
md.len()?.into(),
10061007
$table)
10071008
}
10081009
};
@@ -1076,7 +1077,7 @@ macro_rules! migration_table_add_row_with_metadata {
10761077
{
10771078
let md = $crate::metadata::EncodedMetadata::new(metadata)?;
10781079
migration_table_add_row_details!(span, node, source_dest, time,
1079-
md.as_ptr(), md.len().into(), $table)
1080+
md.as_ptr(), md.len()?.into(), $table)
10801081
}
10811082
};
10821083
}

src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,17 @@ impl TryFrom<SizeType> for usize {
297297
}
298298
}
299299

300-
impl From<usize> for SizeType {
301-
fn from(value: usize) -> Self {
302-
Self(value as tsk_size_t)
300+
impl TryFrom<usize> for SizeType {
301+
type Error = TskitError;
302+
303+
fn try_from(value: usize) -> Result<Self, Self::Error> {
304+
match tsk_size_t::try_from(value) {
305+
Ok(x) => Ok(Self(x)),
306+
Err(_) => Err(TskitError::RangeError(format!(
307+
"could not convert usize {} to SizeType",
308+
value
309+
))),
310+
}
303311
}
304312
}
305313

@@ -542,17 +550,17 @@ mod tests {
542550
#[test]
543551
fn test_usize_to_size_type() {
544552
let x = usize::MAX;
545-
let s = SizeType::from(x);
553+
let s = SizeType::try_from(x).ok();
546554

547555
#[cfg(target_pointer_width = "64")]
548-
assert_eq!(s, bindings::tsk_size_t::MAX);
556+
assert_eq!(s, Some(bindings::tsk_size_t::MAX.into()));
549557

550558
#[cfg(target_pointer_width = "32")]
551-
assert_eq!(s, usize::MAX as bindings::tsk_size_t);
559+
assert_eq!(s, Some((usize::MAX as bindings::tsk_size_t).into()));
552560

553561
let x = usize::MIN;
554-
let s = SizeType::from(x);
555-
assert_eq!(s, 0);
562+
let s = SizeType::try_from(x).ok();
563+
assert_eq!(s, Some(0.into()));
556564
}
557565

558566
// Testing modules

src/metadata.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ impl EncodedMetadata {
234234
}
235235
}
236236

237-
pub(crate) fn len(&self) -> SizeType {
238-
self.encoded.len().into()
237+
pub(crate) fn len(&self) -> Result<SizeType, crate::TskitError> {
238+
SizeType::try_from(self.encoded.len())
239239
}
240240
}
241241

@@ -365,7 +365,7 @@ mod tests {
365365
let enc = EncodedMetadata::new(&f).unwrap();
366366
let p = enc.as_ptr();
367367
let mut d = vec![];
368-
for i in 0..usize::try_from(enc.len()).unwrap() {
368+
for i in 0..usize::try_from(enc.len().unwrap()).unwrap() {
369369
d.push(unsafe { *p.add(i) as u8 });
370370
}
371371
let df = F::decode(&d).unwrap();
@@ -399,7 +399,7 @@ mod test_serde {
399399
let enc = EncodedMetadata::new(&f).unwrap();
400400
let p = enc.as_ptr();
401401
let mut d = vec![];
402-
for i in 0..usize::try_from(enc.len()).unwrap() {
402+
for i in 0..usize::try_from(enc.len().unwrap()).unwrap() {
403403
d.push(unsafe { *p.add(i) as u8 });
404404
}
405405
let df = F::decode(&d).unwrap();

0 commit comments

Comments
 (0)