|
7 | 7 |
|
8 | 8 | #![cfg_attr(feature = "unstable", feature(nonzero))] |
9 | 9 | #![cfg_attr(feature = "unstable", feature(const_fn))] |
10 | | -#![cfg_attr(feature = "unstable", feature(const_nonzero_new))] |
11 | 10 |
|
12 | | -#[cfg_attr(not(feature = "unstable"), macro_use)] |
13 | 11 | extern crate serde; |
14 | 12 |
|
15 | | -pub use imp::*; |
| 13 | +use std::fmt; |
16 | 14 |
|
17 | | -#[cfg(feature = "unstable")] |
18 | | -mod imp { |
19 | | - extern crate core; |
20 | | - use self::core::nonzero::NonZero as CoreNonZero; |
21 | | - use serde::{Serialize, Serializer, Deserialize, Deserializer}; |
22 | | - |
23 | | - pub use self::core::nonzero::Zeroable; |
24 | | - |
25 | | - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
26 | | - pub struct NonZero<T: Zeroable>(CoreNonZero<T>); |
| 15 | +macro_rules! impl_nonzero_fmt { |
| 16 | + ( ( $( $Trait: ident ),+ ) for $Ty: ident ) => { |
| 17 | + $( |
| 18 | + impl fmt::$Trait for $Ty { |
| 19 | + #[inline] |
| 20 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 21 | + self.get().fmt(f) |
| 22 | + } |
| 23 | + } |
| 24 | + )+ |
| 25 | + } |
| 26 | +} |
27 | 27 |
|
28 | | - impl<T: Zeroable> NonZero<T> { |
29 | | - #[inline] |
30 | | - pub const unsafe fn new_unchecked(x: T) -> Self { |
31 | | - NonZero(CoreNonZero::new_unchecked(x)) |
32 | | - } |
| 28 | +macro_rules! nonzero_integers { |
| 29 | + ( $( $Ty: ident($Int: ty); )+ ) => { |
| 30 | + $( |
| 31 | + #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 32 | + pub struct $Ty( |
| 33 | + #[cfg(feature = "unstable")] std::num::$Ty, |
| 34 | + #[cfg(not(feature = "unstable"))] $Int, |
| 35 | + ); |
| 36 | + |
| 37 | + impl $Ty { |
| 38 | + #[cfg(feature = "unstable")] |
| 39 | + #[inline] |
| 40 | + pub const unsafe fn new_unchecked(n: $Int) -> Self { |
| 41 | + $Ty(std::num::$Ty::new_unchecked(n)) |
| 42 | + } |
33 | 43 |
|
34 | | - #[inline] |
35 | | - pub fn new(x: T) -> Option<Self> { |
36 | | - CoreNonZero::new(x).map(NonZero) |
37 | | - } |
| 44 | + #[cfg(not(feature = "unstable"))] |
| 45 | + #[inline] |
| 46 | + pub unsafe fn new_unchecked(n: $Int) -> Self { |
| 47 | + $Ty(n) |
| 48 | + } |
38 | 49 |
|
39 | | - #[inline] |
40 | | - pub fn get(self) -> T { |
41 | | - self.0.get() |
42 | | - } |
43 | | - } |
| 50 | + #[cfg(feature = "unstable")] |
| 51 | + #[inline] |
| 52 | + pub fn new(n: $Int) -> Option<Self> { |
| 53 | + std::num::$Ty::new(n).map($Ty) |
| 54 | + } |
44 | 55 |
|
45 | | - // Not using derive because of the additional Clone bound required by the inner impl |
46 | | - |
47 | | - impl<T> Serialize for NonZero<T> |
48 | | - where |
49 | | - T: Serialize + Zeroable + Clone, |
50 | | - { |
51 | | - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
52 | | - where |
53 | | - S: Serializer, |
54 | | - { |
55 | | - self.0.serialize(serializer) |
56 | | - } |
57 | | - } |
| 56 | + #[cfg(not(feature = "unstable"))] |
| 57 | + #[inline] |
| 58 | + pub fn new(n: $Int) -> Option<Self> { |
| 59 | + if n != 0 { |
| 60 | + Some($Ty(NonZero(n))) |
| 61 | + } else { |
| 62 | + None |
| 63 | + } |
| 64 | + } |
58 | 65 |
|
59 | | - impl<'de, T> Deserialize<'de> for NonZero<T> |
60 | | - where |
61 | | - T: Deserialize<'de> + Zeroable, |
62 | | - { |
63 | | - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
64 | | - where |
65 | | - D: Deserializer<'de>, |
66 | | - { |
67 | | - CoreNonZero::deserialize(deserializer).map(NonZero) |
68 | | - } |
69 | | - } |
70 | | -} |
| 66 | + #[cfg(feature = "unstable")] |
| 67 | + #[inline] |
| 68 | + pub fn get(self) -> $Int { |
| 69 | + self.0.get() |
| 70 | + } |
71 | 71 |
|
72 | | -#[cfg(not(feature = "unstable"))] |
73 | | -mod imp { |
74 | | - #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] |
75 | | - pub struct NonZero<T: Zeroable>(T); |
76 | | - |
77 | | - impl<T: Zeroable> NonZero<T> { |
78 | | - #[inline] |
79 | | - pub unsafe fn new_unchecked(x: T) -> Self { |
80 | | - NonZero(x) |
81 | | - } |
82 | | - |
83 | | - #[inline] |
84 | | - pub fn new(x: T) -> Option<Self> { |
85 | | - if x.is_zero() { |
86 | | - None |
87 | | - } else { |
88 | | - Some(NonZero(x)) |
| 72 | + #[cfg(not(feature = "unstable"))] |
| 73 | + #[inline] |
| 74 | + pub fn get(self) -> $Int { |
| 75 | + self.0 |
| 76 | + } |
89 | 77 | } |
90 | | - } |
91 | | - |
92 | | - #[inline] |
93 | | - pub fn get(self) -> T { |
94 | | - self.0 |
95 | | - } |
96 | | - } |
97 | 78 |
|
98 | | - /// Unsafe trait to indicate what types are usable with the NonZero struct |
99 | | - pub unsafe trait Zeroable { |
100 | | - /// Whether this value is zero |
101 | | - fn is_zero(&self) -> bool; |
102 | | - } |
| 79 | + impl_nonzero_fmt! { |
| 80 | + (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty |
| 81 | + } |
103 | 82 |
|
104 | | - macro_rules! impl_zeroable_for_pointer_types { |
105 | | - ( $( $Ptr: ty )+ ) => { |
106 | | - $( |
107 | | - /// For fat pointers to be considered "zero", only the "data" part needs to be null. |
108 | | - unsafe impl<T: ?Sized> Zeroable for $Ptr { |
109 | | - #[inline] |
110 | | - fn is_zero(&self) -> bool { |
111 | | - // Cast because `is_null` is only available on thin pointers |
112 | | - (*self as *mut u8).is_null() |
113 | | - } |
| 83 | + impl serde::Serialize for $Ty { |
| 84 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 85 | + where S: serde::Serializer |
| 86 | + { |
| 87 | + self.get().serialize(serializer) |
114 | 88 | } |
115 | | - )+ |
116 | | - } |
117 | | - } |
| 89 | + } |
118 | 90 |
|
119 | | - macro_rules! impl_zeroable_for_integer_types { |
120 | | - ( $( $Int: ty )+ ) => { |
121 | | - $( |
122 | | - unsafe impl Zeroable for $Int { |
123 | | - #[inline] |
124 | | - fn is_zero(&self) -> bool { |
125 | | - *self == 0 |
| 91 | + impl<'de> serde::Deserialize<'de> for $Ty { |
| 92 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 93 | + where D: serde::Deserializer<'de> |
| 94 | + { |
| 95 | + let value = <$Int>::deserialize(deserializer)?; |
| 96 | + match <$Ty>::new(value) { |
| 97 | + Some(nonzero) => Ok(nonzero), |
| 98 | + None => Err(serde::de::Error::custom("expected a non-zero value")), |
126 | 99 | } |
127 | 100 | } |
128 | | - )+ |
129 | | - } |
130 | | - } |
131 | | - |
132 | | - impl_zeroable_for_pointer_types! { |
133 | | - *const T |
134 | | - *mut T |
| 101 | + } |
| 102 | + )+ |
135 | 103 | } |
| 104 | +} |
136 | 105 |
|
137 | | - impl_zeroable_for_integer_types! { |
138 | | - usize u8 u16 u32 u64 |
139 | | - isize i8 i16 i32 i64 |
140 | | - } |
| 106 | +nonzero_integers! { |
| 107 | + NonZeroU8(u8); NonZeroI8(i8); |
| 108 | + NonZeroU16(u16); NonZeroI16(i16); |
| 109 | + NonZeroU32(u32); NonZeroI32(i32); |
| 110 | + NonZeroU64(u64); NonZeroI64(i64); |
| 111 | + NonZeroUsize(usize); NonZeroIsize(isize); |
141 | 112 | } |
0 commit comments