1use crate::num::TryFromIntError;
2
3mod 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")]
14pub 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
20macro_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
37impl_float_to_int!(f16 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
38impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
39impl_float_to_int!(f64 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
40impl_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
45macro_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
85impl_from!(bool => u8);
86impl_from!(bool => u16);
87impl_from!(bool => u32);
88impl_from!(bool => u64);
89impl_from!(bool => u128);
90impl_from!(bool => usize);
91impl_from!(bool => i8);
92impl_from!(bool => i16);
93impl_from!(bool => i32);
94impl_from!(bool => i64);
95impl_from!(bool => i128);
96impl_from!(bool => isize);
97
98// unsigned integer -> unsigned integer
99impl_from!(u8 => u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
100impl_from!(u8 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
101impl_from!(u8 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
102impl_from!(u8 => u128, #[stable(feature = "i128", since = "1.26.0")]);
103impl_from!(u8 => usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
104impl_from!(u16 => u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
105impl_from!(u16 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
106impl_from!(u16 => u128, #[stable(feature = "i128", since = "1.26.0")]);
107impl_from!(u32 => u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
108impl_from!(u32 => u128, #[stable(feature = "i128", since = "1.26.0")]);
109impl_from!(u64 => u128, #[stable(feature = "i128", since = "1.26.0")]);
110
111// signed integer -> signed integer
112impl_from!(i8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
113impl_from!(i8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
114impl_from!(i8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
115impl_from!(i8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
116impl_from!(i8 => isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
117impl_from!(i16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
118impl_from!(i16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
119impl_from!(i16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
120impl_from!(i32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
121impl_from!(i32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
122impl_from!(i64 => i128, #[stable(feature = "i128", since = "1.26.0")]);
123
124// unsigned integer -> signed integer
125impl_from!(u8 => i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
126impl_from!(u8 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
127impl_from!(u8 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
128impl_from!(u8 => i128, #[stable(feature = "i128", since = "1.26.0")]);
129impl_from!(u16 => i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
130impl_from!(u16 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
131impl_from!(u16 => i128, #[stable(feature = "i128", since = "1.26.0")]);
132impl_from!(u32 => i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]);
133impl_from!(u32 => i128, #[stable(feature = "i128", since = "1.26.0")]);
134impl_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
139impl_from!(u16 => usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
140impl_from!(u8 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")]);
141impl_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
154impl_from!(i8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
155impl_from!(i8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
156impl_from!(i16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
157impl_from!(i16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
158impl_from!(i32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159
160// unsigned integer -> float
161impl_from!(u8 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
162impl_from!(u8 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
163impl_from!(u16 => f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
164impl_from!(u16 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
165impl_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>
170impl_from!(f16 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
171impl_from!(f32 => f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
172impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
173impl_from!(f64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
174
175macro_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
200impl_float_from_bool!(f32);
201impl_float_from_bool!(f64);
202
203// no possible bounds violation
204macro_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
222macro_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)
244macro_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
266macro_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
289macro_rules! rev {
290 ($mac:ident, $source:ty => $($target:ty),+) => {$(
291 $mac!($target => $source);
292 )*}
293}
294
295// unsigned integer -> unsigned integer
296impl_try_from_upper_bounded!(u16 => u8);
297impl_try_from_upper_bounded!(u32 => u8, u16);
298impl_try_from_upper_bounded!(u64 => u8, u16, u32);
299impl_try_from_upper_bounded!(u128 => u8, u16, u32, u64);
300
301// signed integer -> signed integer
302impl_try_from_both_bounded!(i16 => i8);
303impl_try_from_both_bounded!(i32 => i8, i16);
304impl_try_from_both_bounded!(i64 => i8, i16, i32);
305impl_try_from_both_bounded!(i128 => i8, i16, i32, i64);
306
307// unsigned integer -> signed integer
308impl_try_from_upper_bounded!(u8 => i8);
309impl_try_from_upper_bounded!(u16 => i8, i16);
310impl_try_from_upper_bounded!(u32 => i8, i16, i32);
311impl_try_from_upper_bounded!(u64 => i8, i16, i32, i64);
312impl_try_from_upper_bounded!(u128 => i8, i16, i32, i64, i128);
313
314// signed integer -> unsigned integer
315impl_try_from_lower_bounded!(i8 => u8, u16, u32, u64, u128);
316impl_try_from_both_bounded!(i16 => u8);
317impl_try_from_lower_bounded!(i16 => u16, u32, u64, u128);
318impl_try_from_both_bounded!(i32 => u8, u16);
319impl_try_from_lower_bounded!(i32 => u32, u64, u128);
320impl_try_from_both_bounded!(i64 => u8, u16, u32);
321impl_try_from_lower_bounded!(i64 => u64, u128);
322impl_try_from_both_bounded!(i128 => u8, u16, u32, u64);
323impl_try_from_lower_bounded!(i128 => u128);
324
325// usize/isize
326impl_try_from_upper_bounded!(usize => isize);
327impl_try_from_lower_bounded!(isize => usize);
328
329#[cfg(target_pointer_width = "16")]
330mod 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")]
352mod 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")]
377mod 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
402use crate::num::NonZero;
403
404macro_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
422impl_nonzero_int_from_nonzero_int!(u8 => u16);
423impl_nonzero_int_from_nonzero_int!(u8 => u32);
424impl_nonzero_int_from_nonzero_int!(u8 => u64);
425impl_nonzero_int_from_nonzero_int!(u8 => u128);
426impl_nonzero_int_from_nonzero_int!(u8 => usize);
427impl_nonzero_int_from_nonzero_int!(u16 => u32);
428impl_nonzero_int_from_nonzero_int!(u16 => u64);
429impl_nonzero_int_from_nonzero_int!(u16 => u128);
430impl_nonzero_int_from_nonzero_int!(u16 => usize);
431impl_nonzero_int_from_nonzero_int!(u32 => u64);
432impl_nonzero_int_from_nonzero_int!(u32 => u128);
433impl_nonzero_int_from_nonzero_int!(u64 => u128);
434
435// non-zero signed integer -> non-zero signed integer
436impl_nonzero_int_from_nonzero_int!(i8 => i16);
437impl_nonzero_int_from_nonzero_int!(i8 => i32);
438impl_nonzero_int_from_nonzero_int!(i8 => i64);
439impl_nonzero_int_from_nonzero_int!(i8 => i128);
440impl_nonzero_int_from_nonzero_int!(i8 => isize);
441impl_nonzero_int_from_nonzero_int!(i16 => i32);
442impl_nonzero_int_from_nonzero_int!(i16 => i64);
443impl_nonzero_int_from_nonzero_int!(i16 => i128);
444impl_nonzero_int_from_nonzero_int!(i16 => isize);
445impl_nonzero_int_from_nonzero_int!(i32 => i64);
446impl_nonzero_int_from_nonzero_int!(i32 => i128);
447impl_nonzero_int_from_nonzero_int!(i64 => i128);
448
449// non-zero unsigned -> non-zero signed integer
450impl_nonzero_int_from_nonzero_int!(u8 => i16);
451impl_nonzero_int_from_nonzero_int!(u8 => i32);
452impl_nonzero_int_from_nonzero_int!(u8 => i64);
453impl_nonzero_int_from_nonzero_int!(u8 => i128);
454impl_nonzero_int_from_nonzero_int!(u8 => isize);
455impl_nonzero_int_from_nonzero_int!(u16 => i32);
456impl_nonzero_int_from_nonzero_int!(u16 => i64);
457impl_nonzero_int_from_nonzero_int!(u16 => i128);
458impl_nonzero_int_from_nonzero_int!(u32 => i64);
459impl_nonzero_int_from_nonzero_int!(u32 => i128);
460impl_nonzero_int_from_nonzero_int!(u64 => i128);
461
462macro_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
481impl_nonzero_int_try_from_int!(u8);
482impl_nonzero_int_try_from_int!(u16);
483impl_nonzero_int_try_from_int!(u32);
484impl_nonzero_int_try_from_int!(u64);
485impl_nonzero_int_try_from_int!(u128);
486impl_nonzero_int_try_from_int!(usize);
487impl_nonzero_int_try_from_int!(i8);
488impl_nonzero_int_try_from_int!(i16);
489impl_nonzero_int_try_from_int!(i32);
490impl_nonzero_int_try_from_int!(i64);
491impl_nonzero_int_try_from_int!(i128);
492impl_nonzero_int_try_from_int!(isize);
493
494macro_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
514impl_nonzero_int_try_from_nonzero_int!(u16 => u8);
515impl_nonzero_int_try_from_nonzero_int!(u32 => u8, u16, usize);
516impl_nonzero_int_try_from_nonzero_int!(u64 => u8, u16, u32, usize);
517impl_nonzero_int_try_from_nonzero_int!(u128 => u8, u16, u32, u64, usize);
518impl_nonzero_int_try_from_nonzero_int!(usize => u8, u16, u32, u64, u128);
519
520// signed non-zero integer -> signed non-zero integer
521impl_nonzero_int_try_from_nonzero_int!(i16 => i8);
522impl_nonzero_int_try_from_nonzero_int!(i32 => i8, i16, isize);
523impl_nonzero_int_try_from_nonzero_int!(i64 => i8, i16, i32, isize);
524impl_nonzero_int_try_from_nonzero_int!(i128 => i8, i16, i32, i64, isize);
525impl_nonzero_int_try_from_nonzero_int!(isize => i8, i16, i32, i64, i128);
526
527// unsigned non-zero integer -> signed non-zero integer
528impl_nonzero_int_try_from_nonzero_int!(u8 => i8);
529impl_nonzero_int_try_from_nonzero_int!(u16 => i8, i16, isize);
530impl_nonzero_int_try_from_nonzero_int!(u32 => i8, i16, i32, isize);
531impl_nonzero_int_try_from_nonzero_int!(u64 => i8, i16, i32, i64, isize);
532impl_nonzero_int_try_from_nonzero_int!(u128 => i8, i16, i32, i64, i128, isize);
533impl_nonzero_int_try_from_nonzero_int!(usize => i8, i16, i32, i64, i128, isize);
534
535// signed non-zero integer -> unsigned non-zero integer
536impl_nonzero_int_try_from_nonzero_int!(i8 => u8, u16, u32, u64, u128, usize);
537impl_nonzero_int_try_from_nonzero_int!(i16 => u8, u16, u32, u64, u128, usize);
538impl_nonzero_int_try_from_nonzero_int!(i32 => u8, u16, u32, u64, u128, usize);
539impl_nonzero_int_try_from_nonzero_int!(i64 => u8, u16, u32, u64, u128, usize);
540impl_nonzero_int_try_from_nonzero_int!(i128 => u8, u16, u32, u64, u128, usize);
541impl_nonzero_int_try_from_nonzero_int!(isize => u8, u16, u32, u64, u128, usize);
542