Skip to content

Commit d9e6b4a

Browse files
committed
convenience fns
1 parent 5c9e92d commit d9e6b4a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,27 @@ impl_size_type_comparisons_for_row_ids!(MigrationId);
263263
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
264264
pub struct SizeType(tsk_size_t);
265265

266+
impl SizeType {
267+
/// Convenience function to convert to usize.
268+
///
269+
/// Works via [`TryFrom`].
270+
///
271+
/// # Returns
272+
///
273+
/// * `None` if the underlying value is negative.
274+
/// * `Some(usize)` otherwise.
275+
pub fn to_usize(&self) -> Option<usize> {
276+
(*self).try_into().ok()
277+
}
278+
279+
/// Convenience function to convert to usize.
280+
/// Implemented via `as`.
281+
/// Negative values with therefore wrap.
282+
pub fn as_usize(&self) -> usize {
283+
self.0 as usize
284+
}
285+
}
286+
266287
impl std::fmt::Display for SizeType {
267288
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
268289
write!(f, "{}", self.0)

0 commit comments

Comments
 (0)