sj/number/impls/
non_zeros.rs1use {
30 core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8},
31 crate::{Error, Number, Result},
32};
33
34#[cfg(any(target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64"))]
35use core::num::{NonZeroIsize, NonZeroUsize};
36
37macro_rules! impl_from_non_zeros_for_number { ($($ty: ty),+$(,)?) => {
38 $(
39 impl From<&$ty> for Number {
40
41 fn from(n: &$ty) -> Self {
42 Self::from(n.get())
43 }
44
45 }
46
47 impl From<$ty> for Number {
48
49 fn from(n: $ty) -> Self {
50 Self::from(n.get())
51 }
52
53 }
54 )+
55}}
56
57impl_from_non_zeros_for_number!(
58 NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
59 NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
60);
61
62#[cfg(any(target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64"))]
63impl_from_non_zeros_for_number!(NonZeroUsize, NonZeroIsize);
64
65macro_rules! impl_try_from_number_for_non_zeros { ($(($non_zero: ty, $ty: ty)),+$(,)?) => {
66 $(
67 impl TryFrom<&Number> for $non_zero {
68
69 type Error = Error;
70
71 fn try_from(n: &Number) -> Result<Self> {
72 Self::try_from(<$ty>::try_from(n)?).map_err(|e| err!("{e}"))
73 }
74
75 }
76
77 impl TryFrom<Number> for $non_zero {
78
79 type Error = Error;
80
81 fn try_from(n: Number) -> Result<Self> {
82 Self::try_from(&n)
83 }
84
85 }
86 )+
87}}
88
89impl_try_from_number_for_non_zeros!(
90 (NonZeroI8, i8), (NonZeroI16, i16), (NonZeroI32, i32), (NonZeroI64, i64), (NonZeroI128, i128), (NonZeroIsize, isize),
91 (NonZeroU8, u8), (NonZeroU16, u16), (NonZeroU32, u32), (NonZeroU64, u64), (NonZeroU128, u128), (NonZeroUsize, usize),
92);