Skip to content

Commit

Permalink
Added TryConvert for NonZero types
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 4, 2024
1 parent 65e35e5 commit d7440c1
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/try_convert.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Traits for converting from Ruby [`Value`]s to Rust types.

use std::num::NonZero;
use std::path::PathBuf;

use rb_sys::{rb_get_path, rb_num2dbl};
Expand Down Expand Up @@ -134,6 +135,32 @@ impl TryConvert for usize {
}
unsafe impl TryConvertOwned for usize {}

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

impl_non_zero_try_convert!(i8);
impl_non_zero_try_convert!(i16);
impl_non_zero_try_convert!(i32);
impl_non_zero_try_convert!(i64);
impl_non_zero_try_convert!(isize);
impl_non_zero_try_convert!(u8);
impl_non_zero_try_convert!(u16);
impl_non_zero_try_convert!(u32);
impl_non_zero_try_convert!(u64);
impl_non_zero_try_convert!(usize);

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

0 comments on commit d7440c1

Please sign in to comment.