Skip to content

Commit

Permalink
Add as_usize()
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lehmann authored and danlehmann committed Dec 3, 2024
1 parent 8e84b27 commit 2405504
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_<T: Number>(self) -> T {
Expand Down Expand Up @@ -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 }
}
)+
};
Expand Down Expand Up @@ -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 }
}
)+
};
Expand Down Expand Up @@ -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
}
}
)+
};
Expand Down Expand Up @@ -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")]
Expand Down
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,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"))]
Expand Down

0 comments on commit 2405504

Please sign in to comment.