serde/de/
from_primitive.rs1use lib::*;
2
3macro_rules! int_to_int {
4 ($dst:ident, $n:ident) => {
5 if $dst::min_value() as i64 <= $n as i64 && $n as i64 <= $dst::max_value() as i64 {
6 Some($n as $dst)
7 } else {
8 None
9 }
10 };
11}
12
13macro_rules! int_to_uint {
14 ($dst:ident, $n:ident) => {
15 if 0 <= $n && $n as u64 <= $dst::max_value() as u64 {
16 Some($n as $dst)
17 } else {
18 None
19 }
20 };
21}
22
23macro_rules! uint_to {
24 ($dst:ident, $n:ident) => {
25 if $n as u64 <= $dst::max_value() as u64 {
26 Some($n as $dst)
27 } else {
28 None
29 }
30 };
31}
32
33pub trait FromPrimitive: Sized {
34 fn from_i8(n: i8) -> Option<Self>;
35 fn from_i16(n: i16) -> Option<Self>;
36 fn from_i32(n: i32) -> Option<Self>;
37 fn from_i64(n: i64) -> Option<Self>;
38 fn from_u8(n: u8) -> Option<Self>;
39 fn from_u16(n: u16) -> Option<Self>;
40 fn from_u32(n: u32) -> Option<Self>;
41 fn from_u64(n: u64) -> Option<Self>;
42}
43
44macro_rules! impl_from_primitive_for_int {
45 ($t:ident) => {
46 impl FromPrimitive for $t {
47 #[inline]
48 fn from_i8(n: i8) -> Option<Self> {
49 int_to_int!($t, n)
50 }
51 #[inline]
52 fn from_i16(n: i16) -> Option<Self> {
53 int_to_int!($t, n)
54 }
55 #[inline]
56 fn from_i32(n: i32) -> Option<Self> {
57 int_to_int!($t, n)
58 }
59 #[inline]
60 fn from_i64(n: i64) -> Option<Self> {
61 int_to_int!($t, n)
62 }
63 #[inline]
64 fn from_u8(n: u8) -> Option<Self> {
65 uint_to!($t, n)
66 }
67 #[inline]
68 fn from_u16(n: u16) -> Option<Self> {
69 uint_to!($t, n)
70 }
71 #[inline]
72 fn from_u32(n: u32) -> Option<Self> {
73 uint_to!($t, n)
74 }
75 #[inline]
76 fn from_u64(n: u64) -> Option<Self> {
77 uint_to!($t, n)
78 }
79 }
80 };
81}
82
83macro_rules! impl_from_primitive_for_uint {
84 ($t:ident) => {
85 impl FromPrimitive for $t {
86 #[inline]
87 fn from_i8(n: i8) -> Option<Self> {
88 int_to_uint!($t, n)
89 }
90 #[inline]
91 fn from_i16(n: i16) -> Option<Self> {
92 int_to_uint!($t, n)
93 }
94 #[inline]
95 fn from_i32(n: i32) -> Option<Self> {
96 int_to_uint!($t, n)
97 }
98 #[inline]
99 fn from_i64(n: i64) -> Option<Self> {
100 int_to_uint!($t, n)
101 }
102 #[inline]
103 fn from_u8(n: u8) -> Option<Self> {
104 uint_to!($t, n)
105 }
106 #[inline]
107 fn from_u16(n: u16) -> Option<Self> {
108 uint_to!($t, n)
109 }
110 #[inline]
111 fn from_u32(n: u32) -> Option<Self> {
112 uint_to!($t, n)
113 }
114 #[inline]
115 fn from_u64(n: u64) -> Option<Self> {
116 uint_to!($t, n)
117 }
118 }
119 };
120}
121
122macro_rules! impl_from_primitive_for_float {
123 ($t:ident) => {
124 impl FromPrimitive for $t {
125 #[inline]
126 fn from_i8(n: i8) -> Option<Self> {
127 Some(n as Self)
128 }
129 #[inline]
130 fn from_i16(n: i16) -> Option<Self> {
131 Some(n as Self)
132 }
133 #[inline]
134 fn from_i32(n: i32) -> Option<Self> {
135 Some(n as Self)
136 }
137 #[inline]
138 fn from_i64(n: i64) -> Option<Self> {
139 Some(n as Self)
140 }
141 #[inline]
142 fn from_u8(n: u8) -> Option<Self> {
143 Some(n as Self)
144 }
145 #[inline]
146 fn from_u16(n: u16) -> Option<Self> {
147 Some(n as Self)
148 }
149 #[inline]
150 fn from_u32(n: u32) -> Option<Self> {
151 Some(n as Self)
152 }
153 #[inline]
154 fn from_u64(n: u64) -> Option<Self> {
155 Some(n as Self)
156 }
157 }
158 };
159}
160
161impl_from_primitive_for_int!(isize);
162impl_from_primitive_for_int!(i8);
163impl_from_primitive_for_int!(i16);
164impl_from_primitive_for_int!(i32);
165impl_from_primitive_for_int!(i64);
166impl_from_primitive_for_uint!(usize);
167impl_from_primitive_for_uint!(u8);
168impl_from_primitive_for_uint!(u16);
169impl_from_primitive_for_uint!(u32);
170impl_from_primitive_for_uint!(u64);
171impl_from_primitive_for_float!(f32);
172impl_from_primitive_for_float!(f64);
173
174serde_if_integer128! {
175 impl FromPrimitive for i128 {
176 #[inline]
177 fn from_i8(n: i8) -> Option<Self> {
178 Some(n as i128)
179 }
180 #[inline]
181 fn from_i16(n: i16) -> Option<Self> {
182 Some(n as i128)
183 }
184 #[inline]
185 fn from_i32(n: i32) -> Option<Self> {
186 Some(n as i128)
187 }
188 #[inline]
189 fn from_i64(n: i64) -> Option<Self> {
190 Some(n as i128)
191 }
192 #[inline]
193 fn from_u8(n: u8) -> Option<Self> {
194 Some(n as i128)
195 }
196 #[inline]
197 fn from_u16(n: u16) -> Option<Self> {
198 Some(n as i128)
199 }
200 #[inline]
201 fn from_u32(n: u32) -> Option<Self> {
202 Some(n as i128)
203 }
204 #[inline]
205 fn from_u64(n: u64) -> Option<Self> {
206 Some(n as i128)
207 }
208 }
209
210 impl FromPrimitive for u128 {
211 #[inline]
212 fn from_i8(n: i8) -> Option<Self> {
213 if n >= 0 {
214 Some(n as u128)
215 } else {
216 None
217 }
218 }
219 #[inline]
220 fn from_i16(n: i16) -> Option<Self> {
221 if n >= 0 {
222 Some(n as u128)
223 } else {
224 None
225 }
226 }
227 #[inline]
228 fn from_i32(n: i32) -> Option<Self> {
229 if n >= 0 {
230 Some(n as u128)
231 } else {
232 None
233 }
234 }
235 #[inline]
236 fn from_i64(n: i64) -> Option<Self> {
237 if n >= 0 {
238 Some(n as u128)
239 } else {
240 None
241 }
242 }
243 #[inline]
244 fn from_u8(n: u8) -> Option<Self> {
245 Some(n as u128)
246 }
247 #[inline]
248 fn from_u16(n: u16) -> Option<Self> {
249 Some(n as u128)
250 }
251 #[inline]
252 fn from_u32(n: u32) -> Option<Self> {
253 Some(n as u128)
254 }
255 #[inline]
256 fn from_u64(n: u64) -> Option<Self> {
257 Some(n as u128)
258 }
259 }
260}