diff --git a/src/lib.rs b/src/lib.rs index fd72585..2b4ad6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,6 +87,8 @@ pub trait Number: Sized + Copy + Clone + PartialOrd + Ord + PartialEq + Eq { fn as_u128(&self) -> u128; + fn as_usize(&self) -> usize; + #[cfg(not(feature = "const_convert_and_const_trait_impl"))] #[inline] fn as_(self) -> T { @@ -127,6 +129,9 @@ macro_rules! impl_number_native { #[inline] fn as_u128(&self) -> u128 { *self as u128 } + + #[inline] + fn as_usize(&self) -> usize { *self as usize } } )+ }; @@ -186,6 +191,9 @@ macro_rules! impl_number_native { #[inline] fn as_u128(&self) -> u128 { *self as u128 } + + #[inline] + fn as_usize(&self) -> usize { *self as usize } } )+ }; @@ -296,6 +304,11 @@ macro_rules! uint_impl_num { fn as_u128(&self) -> u128 { self.value() as u128 } + + #[inline] + fn as_usize(&self) -> usize { + self.value() as usize + } } )+ }; @@ -368,6 +381,10 @@ macro_rules! uint_impl_num { self.value() as _ } + fn as_usize(&self) -> usize { + self.value() as _ + } + #[inline] fn value(self) -> $type { #[cfg(feature = "hint")] diff --git a/tests/tests.rs b/tests/tests.rs index f7caf1b..712fdd4 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2067,6 +2067,7 @@ fn new_and_as_specific_types() { assert_eq!(d.as_u128(), 42); assert_eq!(e.as_u128(), 42); assert_eq!(f.as_u128(), 42); + assert_eq!(f.as_usize(), 42); } #[cfg(not(feature = "const_convert_and_const_trait_impl"))]