Skip to content

Commit

Permalink
Merge pull request #120 from ankane/non-zero
Browse files Browse the repository at this point in the history
Add `TryConvert` for `NonZero` types
  • Loading branch information
matsadler authored Oct 6, 2024
2 parents 96a9cc8 + 0cea06e commit faad142
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/try_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ impl TryConvert for usize {
}
unsafe impl TryConvertOwned for usize {}

macro_rules! impl_non_zero_try_convert {
($type:ty, $prim:ty) => {
impl TryConvert for $type {
#[inline]
fn try_convert(val: Value) -> Result<Self, Error> {
<$type>::new(<$prim>::try_convert(val)?).ok_or(Error::new(
Ruby::get_with(val).exception_arg_error(),
"value must be non-zero",
))
}
}
unsafe impl TryConvertOwned for $type {}
};
}

impl_non_zero_try_convert!(std::num::NonZeroI8, i8);
impl_non_zero_try_convert!(std::num::NonZeroI16, i16);
impl_non_zero_try_convert!(std::num::NonZeroI32, i32);
impl_non_zero_try_convert!(std::num::NonZeroI64, i64);
impl_non_zero_try_convert!(std::num::NonZeroIsize, isize);
impl_non_zero_try_convert!(std::num::NonZeroU8, u8);
impl_non_zero_try_convert!(std::num::NonZeroU16, u16);
impl_non_zero_try_convert!(std::num::NonZeroU32, u32);
impl_non_zero_try_convert!(std::num::NonZeroU64, u64);
impl_non_zero_try_convert!(std::num::NonZeroUsize, usize);

impl TryConvert for f32 {
#[inline]
fn try_convert(val: Value) -> Result<Self, Error> {
Expand Down

0 comments on commit faad142

Please sign in to comment.