1 | use crate::num::TryFromIntError; |
2 | |
3 | mod private { |
4 | /// This trait being unreachable from outside the crate |
5 | /// prevents other implementations of the `FloatToInt` trait, |
6 | /// which allows potentially adding more trait methods after the trait is `#[stable]`. |
7 | #[unstable (feature = "convert_float_to_int" , issue = "67057" )] |
8 | pub trait Sealed {} |
9 | } |
10 | |
11 | /// Supporting trait for inherent methods of `f32` and `f64` such as `to_int_unchecked`. |
12 | /// Typically doesn’t need to be used directly. |
13 | #[unstable (feature = "convert_float_to_int" , issue = "67057" )] |
14 | pub trait FloatToInt<Int>: private::Sealed + Sized { |
15 | #[unstable (feature = "convert_float_to_int" , issue = "67057" )] |
16 | #[doc (hidden)] |
17 | unsafe fn to_int_unchecked(self) -> Int; |
18 | } |
19 | |
20 | macro_rules! impl_float_to_int { |
21 | ($Float:ty => $($Int:ty),+) => { |
22 | #[unstable(feature = "convert_float_to_int" , issue = "67057" )] |
23 | impl private::Sealed for $Float {} |
24 | $( |
25 | #[unstable(feature = "convert_float_to_int" , issue = "67057" )] |
26 | impl FloatToInt<$Int> for $Float { |
27 | #[inline] |
28 | unsafe fn to_int_unchecked(self) -> $Int { |
29 | // SAFETY: the safety contract must be upheld by the caller. |
30 | unsafe { crate::intrinsics::float_to_int_unchecked(self) } |
31 | } |
32 | } |
33 | )+ |
34 | } |
35 | } |
36 | |
37 | impl_float_to_int!(f16 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
38 | impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
39 | impl_float_to_int!(f64 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
40 | impl_float_to_int!(f128 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
41 | |
42 | // Conversion traits for primitive integer and float types |
43 | // Conversions T -> T are covered by a blanket impl and therefore excluded |
44 | // Some conversions from and to usize/isize are not implemented due to portability concerns |
45 | macro_rules! impl_from { |
46 | (bool => $Int:ty $(,)?) => { |
47 | impl_from!( |
48 | bool => $Int, |
49 | #[stable(feature = "from_bool" , since = "1.28.0" )], |
50 | concat!( |
51 | "Converts a [`bool`] to [`" , stringify!($Int), "`] losslessly. \n" , |
52 | "The resulting value is `0` for `false` and `1` for `true` values. \n" , |
53 | " \n" , |
54 | "# Examples \n" , |
55 | " \n" , |
56 | "``` \n" , |
57 | "assert_eq!(" , stringify!($Int), "::from(true), 1); \n" , |
58 | "assert_eq!(" , stringify!($Int), "::from(false), 0); \n" , |
59 | "``` \n" , |
60 | ), |
61 | ); |
62 | }; |
63 | ($Small:ty => $Large:ty, #[$attr:meta] $(,)?) => { |
64 | impl_from!( |
65 | $Small => $Large, |
66 | #[$attr], |
67 | concat!("Converts [`" , stringify!($Small), "`] to [`" , stringify!($Large), "`] losslessly." ), |
68 | ); |
69 | }; |
70 | ($Small:ty => $Large:ty, #[$attr:meta], $doc:expr $(,)?) => { |
71 | #[$attr] |
72 | impl From<$Small> for $Large { |
73 | // Rustdocs on the impl block show a "[+] show undocumented items" toggle. |
74 | // Rustdocs on functions do not. |
75 | #[doc = $doc] |
76 | #[inline(always)] |
77 | fn from(small: $Small) -> Self { |
78 | small as Self |
79 | } |
80 | } |
81 | }; |
82 | } |
83 | |
84 | // boolean -> integer |
85 | impl_from!(bool => u8); |
86 | impl_from!(bool => u16); |
87 | impl_from!(bool => u32); |
88 | impl_from!(bool => u64); |
89 | impl_from!(bool => u128); |
90 | impl_from!(bool => usize); |
91 | impl_from!(bool => i8); |
92 | impl_from!(bool => i16); |
93 | impl_from!(bool => i32); |
94 | impl_from!(bool => i64); |
95 | impl_from!(bool => i128); |
96 | impl_from!(bool => isize); |
97 | |
98 | // unsigned integer -> unsigned integer |
99 | impl_from!(u8 => u16, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
100 | impl_from!(u8 => u32, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
101 | impl_from!(u8 => u64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
102 | impl_from!(u8 => u128, #[stable (feature = "i128" , since = "1.26.0" )]); |
103 | impl_from!(u8 => usize, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
104 | impl_from!(u16 => u32, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
105 | impl_from!(u16 => u64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
106 | impl_from!(u16 => u128, #[stable (feature = "i128" , since = "1.26.0" )]); |
107 | impl_from!(u32 => u64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
108 | impl_from!(u32 => u128, #[stable (feature = "i128" , since = "1.26.0" )]); |
109 | impl_from!(u64 => u128, #[stable (feature = "i128" , since = "1.26.0" )]); |
110 | |
111 | // signed integer -> signed integer |
112 | impl_from!(i8 => i16, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
113 | impl_from!(i8 => i32, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
114 | impl_from!(i8 => i64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
115 | impl_from!(i8 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
116 | impl_from!(i8 => isize, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
117 | impl_from!(i16 => i32, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
118 | impl_from!(i16 => i64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
119 | impl_from!(i16 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
120 | impl_from!(i32 => i64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
121 | impl_from!(i32 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
122 | impl_from!(i64 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
123 | |
124 | // unsigned integer -> signed integer |
125 | impl_from!(u8 => i16, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
126 | impl_from!(u8 => i32, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
127 | impl_from!(u8 => i64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
128 | impl_from!(u8 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
129 | impl_from!(u16 => i32, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
130 | impl_from!(u16 => i64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
131 | impl_from!(u16 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
132 | impl_from!(u32 => i64, #[stable (feature = "lossless_int_conv" , since = "1.5.0" )]); |
133 | impl_from!(u32 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
134 | impl_from!(u64 => i128, #[stable (feature = "i128" , since = "1.26.0" )]); |
135 | |
136 | // The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX |
137 | // which imply that pointer-sized integers must be at least 16 bits: |
138 | // https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4 |
139 | impl_from!(u16 => usize, #[stable (feature = "lossless_iusize_conv" , since = "1.26.0" )]); |
140 | impl_from!(u8 => isize, #[stable (feature = "lossless_iusize_conv" , since = "1.26.0" )]); |
141 | impl_from!(i16 => isize, #[stable (feature = "lossless_iusize_conv" , since = "1.26.0" )]); |
142 | |
143 | // RISC-V defines the possibility of a 128-bit address space (RV128). |
144 | |
145 | // CHERI proposes 128-bit “capabilities”. Unclear if this would be relevant to usize/isize. |
146 | // https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf |
147 | // https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf |
148 | |
149 | // Note: integers can only be represented with full precision in a float if |
150 | // they fit in the significand, which is 24 bits in f32 and 53 bits in f64. |
151 | // Lossy float conversions are not implemented at this time. |
152 | |
153 | // signed integer -> float |
154 | impl_from!(i8 => f32, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
155 | impl_from!(i8 => f64, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
156 | impl_from!(i16 => f32, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
157 | impl_from!(i16 => f64, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
158 | impl_from!(i32 => f64, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
159 | |
160 | // unsigned integer -> float |
161 | impl_from!(u8 => f32, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
162 | impl_from!(u8 => f64, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
163 | impl_from!(u16 => f32, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
164 | impl_from!(u16 => f64, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
165 | impl_from!(u32 => f64, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
166 | |
167 | // float -> float |
168 | // FIXME(f16_f128): adding additional `From` impls for existing types breaks inference. See |
169 | // <https://github.com/rust-lang/rust/issues/123824> |
170 | impl_from!(f16 => f128, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
171 | impl_from!(f32 => f64, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
172 | impl_from!(f32 => f128, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
173 | impl_from!(f64 => f128, #[stable (feature = "lossless_float_conv" , since = "1.6.0" )]); |
174 | |
175 | macro_rules! impl_float_from_bool { |
176 | ($float:ty) => { |
177 | #[stable(feature = "float_from_bool" , since = "1.68.0" )] |
178 | impl From<bool> for $float { |
179 | #[doc = concat!("Converts a [`bool`] to [`" , stringify!($float),"`] losslessly." )] |
180 | /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values. |
181 | /// |
182 | /// # Examples |
183 | /// ``` |
184 | #[doc = concat!("let x: " , stringify!($float)," = false.into();" )] |
185 | /// assert_eq!(x, 0.0); |
186 | /// assert!(x.is_sign_positive()); |
187 | /// |
188 | #[doc = concat!("let y: " , stringify!($float)," = true.into();" )] |
189 | /// assert_eq!(y, 1.0); |
190 | /// ``` |
191 | #[inline] |
192 | fn from(small: bool) -> Self { |
193 | small as u8 as Self |
194 | } |
195 | } |
196 | }; |
197 | } |
198 | |
199 | // boolean -> float |
200 | impl_float_from_bool!(f32); |
201 | impl_float_from_bool!(f64); |
202 | |
203 | // no possible bounds violation |
204 | macro_rules! impl_try_from_unbounded { |
205 | ($source:ty => $($target:ty),+) => {$( |
206 | #[stable(feature = "try_from" , since = "1.34.0" )] |
207 | impl TryFrom<$source> for $target { |
208 | type Error = TryFromIntError; |
209 | |
210 | /// Try to create the target number type from a source |
211 | /// number type. This returns an error if the source value |
212 | /// is outside of the range of the target type. |
213 | #[inline] |
214 | fn try_from(value: $source) -> Result<Self, Self::Error> { |
215 | Ok(value as Self) |
216 | } |
217 | } |
218 | )*} |
219 | } |
220 | |
221 | // only negative bounds |
222 | macro_rules! impl_try_from_lower_bounded { |
223 | ($source:ty => $($target:ty),+) => {$( |
224 | #[stable(feature = "try_from" , since = "1.34.0" )] |
225 | impl TryFrom<$source> for $target { |
226 | type Error = TryFromIntError; |
227 | |
228 | /// Try to create the target number type from a source |
229 | /// number type. This returns an error if the source value |
230 | /// is outside of the range of the target type. |
231 | #[inline] |
232 | fn try_from(u: $source) -> Result<Self, Self::Error> { |
233 | if u >= 0 { |
234 | Ok(u as Self) |
235 | } else { |
236 | Err(TryFromIntError(())) |
237 | } |
238 | } |
239 | } |
240 | )*} |
241 | } |
242 | |
243 | // unsigned to signed (only positive bound) |
244 | macro_rules! impl_try_from_upper_bounded { |
245 | ($source:ty => $($target:ty),+) => {$( |
246 | #[stable(feature = "try_from" , since = "1.34.0" )] |
247 | impl TryFrom<$source> for $target { |
248 | type Error = TryFromIntError; |
249 | |
250 | /// Try to create the target number type from a source |
251 | /// number type. This returns an error if the source value |
252 | /// is outside of the range of the target type. |
253 | #[inline] |
254 | fn try_from(u: $source) -> Result<Self, Self::Error> { |
255 | if u > (Self::MAX as $source) { |
256 | Err(TryFromIntError(())) |
257 | } else { |
258 | Ok(u as Self) |
259 | } |
260 | } |
261 | } |
262 | )*} |
263 | } |
264 | |
265 | // all other cases |
266 | macro_rules! impl_try_from_both_bounded { |
267 | ($source:ty => $($target:ty),+) => {$( |
268 | #[stable(feature = "try_from" , since = "1.34.0" )] |
269 | impl TryFrom<$source> for $target { |
270 | type Error = TryFromIntError; |
271 | |
272 | /// Try to create the target number type from a source |
273 | /// number type. This returns an error if the source value |
274 | /// is outside of the range of the target type. |
275 | #[inline] |
276 | fn try_from(u: $source) -> Result<Self, Self::Error> { |
277 | let min = Self::MIN as $source; |
278 | let max = Self::MAX as $source; |
279 | if u < min || u > max { |
280 | Err(TryFromIntError(())) |
281 | } else { |
282 | Ok(u as Self) |
283 | } |
284 | } |
285 | } |
286 | )*} |
287 | } |
288 | |
289 | macro_rules! rev { |
290 | ($mac:ident, $source:ty => $($target:ty),+) => {$( |
291 | $mac!($target => $source); |
292 | )*} |
293 | } |
294 | |
295 | // unsigned integer -> unsigned integer |
296 | impl_try_from_upper_bounded!(u16 => u8); |
297 | impl_try_from_upper_bounded!(u32 => u8, u16); |
298 | impl_try_from_upper_bounded!(u64 => u8, u16, u32); |
299 | impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64); |
300 | |
301 | // signed integer -> signed integer |
302 | impl_try_from_both_bounded!(i16 => i8); |
303 | impl_try_from_both_bounded!(i32 => i8, i16); |
304 | impl_try_from_both_bounded!(i64 => i8, i16, i32); |
305 | impl_try_from_both_bounded!(i128 => i8, i16, i32, i64); |
306 | |
307 | // unsigned integer -> signed integer |
308 | impl_try_from_upper_bounded!(u8 => i8); |
309 | impl_try_from_upper_bounded!(u16 => i8, i16); |
310 | impl_try_from_upper_bounded!(u32 => i8, i16, i32); |
311 | impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64); |
312 | impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128); |
313 | |
314 | // signed integer -> unsigned integer |
315 | impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128); |
316 | impl_try_from_both_bounded!(i16 => u8); |
317 | impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128); |
318 | impl_try_from_both_bounded!(i32 => u8, u16); |
319 | impl_try_from_lower_bounded!(i32 => u32, u64, u128); |
320 | impl_try_from_both_bounded!(i64 => u8, u16, u32); |
321 | impl_try_from_lower_bounded!(i64 => u64, u128); |
322 | impl_try_from_both_bounded!(i128 => u8, u16, u32, u64); |
323 | impl_try_from_lower_bounded!(i128 => u128); |
324 | |
325 | // usize/isize |
326 | impl_try_from_upper_bounded!(usize => isize); |
327 | impl_try_from_lower_bounded!(isize => usize); |
328 | |
329 | #[cfg (target_pointer_width = "16" )] |
330 | mod ptr_try_from_impls { |
331 | use super::TryFromIntError; |
332 | |
333 | impl_try_from_upper_bounded!(usize => u8); |
334 | impl_try_from_unbounded!(usize => u16, u32, u64, u128); |
335 | impl_try_from_upper_bounded!(usize => i8, i16); |
336 | impl_try_from_unbounded!(usize => i32, i64, i128); |
337 | |
338 | impl_try_from_both_bounded!(isize => u8); |
339 | impl_try_from_lower_bounded!(isize => u16, u32, u64, u128); |
340 | impl_try_from_both_bounded!(isize => i8); |
341 | impl_try_from_unbounded!(isize => i16, i32, i64, i128); |
342 | |
343 | rev!(impl_try_from_upper_bounded, usize => u32, u64, u128); |
344 | rev!(impl_try_from_lower_bounded, usize => i8, i16); |
345 | rev!(impl_try_from_both_bounded, usize => i32, i64, i128); |
346 | |
347 | rev!(impl_try_from_upper_bounded, isize => u16, u32, u64, u128); |
348 | rev!(impl_try_from_both_bounded, isize => i32, i64, i128); |
349 | } |
350 | |
351 | #[cfg (target_pointer_width = "32" )] |
352 | mod ptr_try_from_impls { |
353 | use super::TryFromIntError; |
354 | |
355 | impl_try_from_upper_bounded!(usize => u8, u16); |
356 | impl_try_from_unbounded!(usize => u32, u64, u128); |
357 | impl_try_from_upper_bounded!(usize => i8, i16, i32); |
358 | impl_try_from_unbounded!(usize => i64, i128); |
359 | |
360 | impl_try_from_both_bounded!(isize => u8, u16); |
361 | impl_try_from_lower_bounded!(isize => u32, u64, u128); |
362 | impl_try_from_both_bounded!(isize => i8, i16); |
363 | impl_try_from_unbounded!(isize => i32, i64, i128); |
364 | |
365 | rev!(impl_try_from_unbounded, usize => u32); |
366 | rev!(impl_try_from_upper_bounded, usize => u64, u128); |
367 | rev!(impl_try_from_lower_bounded, usize => i8, i16, i32); |
368 | rev!(impl_try_from_both_bounded, usize => i64, i128); |
369 | |
370 | rev!(impl_try_from_unbounded, isize => u16); |
371 | rev!(impl_try_from_upper_bounded, isize => u32, u64, u128); |
372 | rev!(impl_try_from_unbounded, isize => i32); |
373 | rev!(impl_try_from_both_bounded, isize => i64, i128); |
374 | } |
375 | |
376 | #[cfg (target_pointer_width = "64" )] |
377 | mod ptr_try_from_impls { |
378 | use super::TryFromIntError; |
379 | |
380 | impl_try_from_upper_bounded!(usize => u8, u16, u32); |
381 | impl_try_from_unbounded!(usize => u64, u128); |
382 | impl_try_from_upper_bounded!(usize => i8, i16, i32, i64); |
383 | impl_try_from_unbounded!(usize => i128); |
384 | |
385 | impl_try_from_both_bounded!(isize => u8, u16, u32); |
386 | impl_try_from_lower_bounded!(isize => u64, u128); |
387 | impl_try_from_both_bounded!(isize => i8, i16, i32); |
388 | impl_try_from_unbounded!(isize => i64, i128); |
389 | |
390 | rev!(impl_try_from_unbounded, usize => u32, u64); |
391 | rev!(impl_try_from_upper_bounded, usize => u128); |
392 | rev!(impl_try_from_lower_bounded, usize => i8, i16, i32, i64); |
393 | rev!(impl_try_from_both_bounded, usize => i128); |
394 | |
395 | rev!(impl_try_from_unbounded, isize => u16, u32); |
396 | rev!(impl_try_from_upper_bounded, isize => u64, u128); |
397 | rev!(impl_try_from_unbounded, isize => i32, i64); |
398 | rev!(impl_try_from_both_bounded, isize => i128); |
399 | } |
400 | |
401 | // Conversion traits for non-zero integer types |
402 | use crate::num::NonZero; |
403 | |
404 | macro_rules! impl_nonzero_int_from_nonzero_int { |
405 | ($Small:ty => $Large:ty) => { |
406 | #[stable(feature = "nz_int_conv" , since = "1.41.0" )] |
407 | impl From<NonZero<$Small>> for NonZero<$Large> { |
408 | // Rustdocs on the impl block show a "[+] show undocumented items" toggle. |
409 | // Rustdocs on functions do not. |
410 | #[doc = concat!("Converts <code>[NonZero] \\<[" , stringify!($Small), "]></code> " )] |
411 | #[doc = concat!("to <code>[NonZero] \\<[" , stringify!($Large), "]></code> losslessly." )] |
412 | #[inline] |
413 | fn from(small: NonZero<$Small>) -> Self { |
414 | // SAFETY: input type guarantees the value is non-zero |
415 | unsafe { Self::new_unchecked(From::from(small.get())) } |
416 | } |
417 | } |
418 | }; |
419 | } |
420 | |
421 | // non-zero unsigned integer -> non-zero unsigned integer |
422 | impl_nonzero_int_from_nonzero_int!(u8 => u16); |
423 | impl_nonzero_int_from_nonzero_int!(u8 => u32); |
424 | impl_nonzero_int_from_nonzero_int!(u8 => u64); |
425 | impl_nonzero_int_from_nonzero_int!(u8 => u128); |
426 | impl_nonzero_int_from_nonzero_int!(u8 => usize); |
427 | impl_nonzero_int_from_nonzero_int!(u16 => u32); |
428 | impl_nonzero_int_from_nonzero_int!(u16 => u64); |
429 | impl_nonzero_int_from_nonzero_int!(u16 => u128); |
430 | impl_nonzero_int_from_nonzero_int!(u16 => usize); |
431 | impl_nonzero_int_from_nonzero_int!(u32 => u64); |
432 | impl_nonzero_int_from_nonzero_int!(u32 => u128); |
433 | impl_nonzero_int_from_nonzero_int!(u64 => u128); |
434 | |
435 | // non-zero signed integer -> non-zero signed integer |
436 | impl_nonzero_int_from_nonzero_int!(i8 => i16); |
437 | impl_nonzero_int_from_nonzero_int!(i8 => i32); |
438 | impl_nonzero_int_from_nonzero_int!(i8 => i64); |
439 | impl_nonzero_int_from_nonzero_int!(i8 => i128); |
440 | impl_nonzero_int_from_nonzero_int!(i8 => isize); |
441 | impl_nonzero_int_from_nonzero_int!(i16 => i32); |
442 | impl_nonzero_int_from_nonzero_int!(i16 => i64); |
443 | impl_nonzero_int_from_nonzero_int!(i16 => i128); |
444 | impl_nonzero_int_from_nonzero_int!(i16 => isize); |
445 | impl_nonzero_int_from_nonzero_int!(i32 => i64); |
446 | impl_nonzero_int_from_nonzero_int!(i32 => i128); |
447 | impl_nonzero_int_from_nonzero_int!(i64 => i128); |
448 | |
449 | // non-zero unsigned -> non-zero signed integer |
450 | impl_nonzero_int_from_nonzero_int!(u8 => i16); |
451 | impl_nonzero_int_from_nonzero_int!(u8 => i32); |
452 | impl_nonzero_int_from_nonzero_int!(u8 => i64); |
453 | impl_nonzero_int_from_nonzero_int!(u8 => i128); |
454 | impl_nonzero_int_from_nonzero_int!(u8 => isize); |
455 | impl_nonzero_int_from_nonzero_int!(u16 => i32); |
456 | impl_nonzero_int_from_nonzero_int!(u16 => i64); |
457 | impl_nonzero_int_from_nonzero_int!(u16 => i128); |
458 | impl_nonzero_int_from_nonzero_int!(u32 => i64); |
459 | impl_nonzero_int_from_nonzero_int!(u32 => i128); |
460 | impl_nonzero_int_from_nonzero_int!(u64 => i128); |
461 | |
462 | macro_rules! impl_nonzero_int_try_from_int { |
463 | ($Int:ty) => { |
464 | #[stable(feature = "nzint_try_from_int_conv" , since = "1.46.0" )] |
465 | impl TryFrom<$Int> for NonZero<$Int> { |
466 | type Error = TryFromIntError; |
467 | |
468 | // Rustdocs on the impl block show a "[+] show undocumented items" toggle. |
469 | // Rustdocs on functions do not. |
470 | #[doc = concat!("Attempts to convert [`" , stringify!($Int), "`] " )] |
471 | #[doc = concat!("to <code>[NonZero] \\<[" , stringify!($Int), "]></code>." )] |
472 | #[inline] |
473 | fn try_from(value: $Int) -> Result<Self, Self::Error> { |
474 | Self::new(value).ok_or(TryFromIntError(())) |
475 | } |
476 | } |
477 | }; |
478 | } |
479 | |
480 | // integer -> non-zero integer |
481 | impl_nonzero_int_try_from_int!(u8); |
482 | impl_nonzero_int_try_from_int!(u16); |
483 | impl_nonzero_int_try_from_int!(u32); |
484 | impl_nonzero_int_try_from_int!(u64); |
485 | impl_nonzero_int_try_from_int!(u128); |
486 | impl_nonzero_int_try_from_int!(usize); |
487 | impl_nonzero_int_try_from_int!(i8); |
488 | impl_nonzero_int_try_from_int!(i16); |
489 | impl_nonzero_int_try_from_int!(i32); |
490 | impl_nonzero_int_try_from_int!(i64); |
491 | impl_nonzero_int_try_from_int!(i128); |
492 | impl_nonzero_int_try_from_int!(isize); |
493 | |
494 | macro_rules! impl_nonzero_int_try_from_nonzero_int { |
495 | ($source:ty => $($target:ty),+) => {$( |
496 | #[stable(feature = "nzint_try_from_nzint_conv" , since = "1.49.0" )] |
497 | impl TryFrom<NonZero<$source>> for NonZero<$target> { |
498 | type Error = TryFromIntError; |
499 | |
500 | // Rustdocs on the impl block show a "[+] show undocumented items" toggle. |
501 | // Rustdocs on functions do not. |
502 | #[doc = concat!("Attempts to convert <code>[NonZero] \\<[" , stringify!($source), "]></code> " )] |
503 | #[doc = concat!("to <code>[NonZero] \\<[" , stringify!($target), "]></code>." )] |
504 | #[inline] |
505 | fn try_from(value: NonZero<$source>) -> Result<Self, Self::Error> { |
506 | // SAFETY: Input is guaranteed to be non-zero. |
507 | Ok(unsafe { Self::new_unchecked(<$target>::try_from(value.get())?) }) |
508 | } |
509 | } |
510 | )*}; |
511 | } |
512 | |
513 | // unsigned non-zero integer -> unsigned non-zero integer |
514 | impl_nonzero_int_try_from_nonzero_int!(u16 => u8); |
515 | impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize); |
516 | impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize); |
517 | impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize); |
518 | impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128); |
519 | |
520 | // signed non-zero integer -> signed non-zero integer |
521 | impl_nonzero_int_try_from_nonzero_int!(i16 => i8); |
522 | impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize); |
523 | impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize); |
524 | impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize); |
525 | impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128); |
526 | |
527 | // unsigned non-zero integer -> signed non-zero integer |
528 | impl_nonzero_int_try_from_nonzero_int!(u8 => i8); |
529 | impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize); |
530 | impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize); |
531 | impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize); |
532 | impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize); |
533 | impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize); |
534 | |
535 | // signed non-zero integer -> unsigned non-zero integer |
536 | impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize); |
537 | impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize); |
538 | impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize); |
539 | impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize); |
540 | impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize); |
541 | impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize); |
542 | |