]> git.proxmox.com Git - rustc.git/blame - src/libcore/convert/num.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / libcore / convert / num.rs
CommitLineData
60c5eb7d
XL
1use super::{From, TryFrom};
2use crate::num::TryFromIntError;
3
4mod private {
5 /// This trait being unreachable from outside the crate
6 /// prevents other implementations of the `FloatToInt` trait,
7 /// which allows potentially adding more trait methods after the trait is `#[stable]`.
8 #[unstable(feature = "convert_float_to_int", issue = "67057")]
9 pub trait Sealed {}
10}
11
12/// Supporting trait for inherent methods of `f32` and `f64` such as `round_unchecked_to`.
13/// Typically doesn’t need to be used directly.
14#[unstable(feature = "convert_float_to_int", issue = "67057")]
15pub trait FloatToInt<Int>: private::Sealed + Sized {
ba9703b0 16 #[unstable(feature = "convert_float_to_int", issue = "67057")]
60c5eb7d 17 #[doc(hidden)]
ba9703b0 18 unsafe fn to_int_unchecked(self) -> Int;
60c5eb7d
XL
19}
20
21macro_rules! impl_float_to_int {
22 ( $Float: ident => $( $Int: ident )+ ) => {
23 #[unstable(feature = "convert_float_to_int", issue = "67057")]
24 impl private::Sealed for $Float {}
25 $(
26 #[unstable(feature = "convert_float_to_int", issue = "67057")]
27 impl FloatToInt<$Int> for $Float {
60c5eb7d
XL
28 #[doc(hidden)]
29 #[inline]
ba9703b0
XL
30 unsafe fn to_int_unchecked(self) -> $Int {
31 #[cfg(bootstrap)]
32 {
33 crate::intrinsics::float_to_int_approx_unchecked(self)
34 }
35 #[cfg(not(bootstrap))]
36 {
37 crate::intrinsics::float_to_int_unchecked(self)
38 }
60c5eb7d
XL
39 }
40 }
41 )+
42 }
43}
44
45impl_float_to_int!(f32 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
46impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
47
48// Conversion traits for primitive integer and float types
49// Conversions T -> T are covered by a blanket impl and therefore excluded
50// Some conversions from and to usize/isize are not implemented due to portability concerns
51macro_rules! impl_from {
52 ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
53 #[$attr]
54 #[doc = $doc]
55 impl From<$Small> for $Large {
56 #[inline]
dfeec247
XL
57 fn from(small: $Small) -> Self {
58 small as Self
60c5eb7d
XL
59 }
60 }
61 };
62 ($Small: ty, $Large: ty, #[$attr:meta]) => {
63 impl_from!($Small,
64 $Large,
65 #[$attr],
66 concat!("Converts `",
67 stringify!($Small),
68 "` to `",
69 stringify!($Large),
70 "` losslessly."));
71 }
72}
73
74macro_rules! impl_from_bool {
75 ($target: ty, #[$attr:meta]) => {
76 impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
77 stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
78values.
79
80# Examples
81
82```
83assert_eq!(", stringify!($target), "::from(true), 1);
84assert_eq!(", stringify!($target), "::from(false), 0);
85```"));
86 };
87}
88
89// Bool -> Any
90impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
91impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
92impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
93impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
94impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
95impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
96impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
97impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
98impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
99impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
100impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
101impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
102
103// Unsigned -> Unsigned
104impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
105impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
106impl_from! { u8, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
107impl_from! { u8, u128, #[stable(feature = "i128", since = "1.26.0")] }
108impl_from! { u8, usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
109impl_from! { u16, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
110impl_from! { u16, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
111impl_from! { u16, u128, #[stable(feature = "i128", since = "1.26.0")] }
112impl_from! { u32, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
113impl_from! { u32, u128, #[stable(feature = "i128", since = "1.26.0")] }
114impl_from! { u64, u128, #[stable(feature = "i128", since = "1.26.0")] }
115
116// Signed -> Signed
117impl_from! { i8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
118impl_from! { i8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
119impl_from! { i8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
120impl_from! { i8, i128, #[stable(feature = "i128", since = "1.26.0")] }
121impl_from! { i8, isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
122impl_from! { i16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
123impl_from! { i16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
124impl_from! { i16, i128, #[stable(feature = "i128", since = "1.26.0")] }
125impl_from! { i32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
126impl_from! { i32, i128, #[stable(feature = "i128", since = "1.26.0")] }
127impl_from! { i64, i128, #[stable(feature = "i128", since = "1.26.0")] }
128
129// Unsigned -> Signed
130impl_from! { u8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
131impl_from! { u8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
132impl_from! { u8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
133impl_from! { u8, i128, #[stable(feature = "i128", since = "1.26.0")] }
134impl_from! { u16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
135impl_from! { u16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
136impl_from! { u16, i128, #[stable(feature = "i128", since = "1.26.0")] }
137impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
138impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
139impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
140
141// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
142// which imply that pointer-sized integers must be at least 16 bits:
143// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
144impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
145impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
146impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
147
148// RISC-V defines the possibility of a 128-bit address space (RV128).
149
150// CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
151// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
152// http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf
153
154// Note: integers can only be represented with full precision in a float if
155// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
156// Lossy float conversions are not implemented at this time.
157
158// Signed -> Float
159impl_from! { i8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
160impl_from! { i8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
161impl_from! { i16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
162impl_from! { i16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
163impl_from! { i32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
164
165// Unsigned -> Float
166impl_from! { u8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
167impl_from! { u8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
168impl_from! { u16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
169impl_from! { u16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
170impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
171
172// Float -> Float
173impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
174
175// no possible bounds violation
176macro_rules! try_from_unbounded {
177 ($source:ty, $($target:ty),*) => {$(
178 #[stable(feature = "try_from", since = "1.34.0")]
179 impl TryFrom<$source> for $target {
180 type Error = TryFromIntError;
181
182 /// Try to create the target number type from a source
183 /// number type. This returns an error if the source value
184 /// is outside of the range of the target type.
185 #[inline]
186 fn try_from(value: $source) -> Result<Self, Self::Error> {
dfeec247 187 Ok(value as Self)
60c5eb7d
XL
188 }
189 }
190 )*}
191}
192
193// only negative bounds
194macro_rules! try_from_lower_bounded {
195 ($source:ty, $($target:ty),*) => {$(
196 #[stable(feature = "try_from", since = "1.34.0")]
197 impl TryFrom<$source> for $target {
198 type Error = TryFromIntError;
199
200 /// Try to create the target number type from a source
201 /// number type. This returns an error if the source value
202 /// is outside of the range of the target type.
203 #[inline]
dfeec247 204 fn try_from(u: $source) -> Result<Self, Self::Error> {
60c5eb7d 205 if u >= 0 {
dfeec247 206 Ok(u as Self)
60c5eb7d
XL
207 } else {
208 Err(TryFromIntError(()))
209 }
210 }
211 }
212 )*}
213}
214
215// unsigned to signed (only positive bound)
216macro_rules! try_from_upper_bounded {
217 ($source:ty, $($target:ty),*) => {$(
218 #[stable(feature = "try_from", since = "1.34.0")]
219 impl TryFrom<$source> for $target {
220 type Error = TryFromIntError;
221
222 /// Try to create the target number type from a source
223 /// number type. This returns an error if the source value
224 /// is outside of the range of the target type.
225 #[inline]
dfeec247
XL
226 fn try_from(u: $source) -> Result<Self, Self::Error> {
227 if u > (Self::max_value() as $source) {
60c5eb7d
XL
228 Err(TryFromIntError(()))
229 } else {
dfeec247 230 Ok(u as Self)
60c5eb7d
XL
231 }
232 }
233 }
234 )*}
235}
236
237// all other cases
238macro_rules! try_from_both_bounded {
239 ($source:ty, $($target:ty),*) => {$(
240 #[stable(feature = "try_from", since = "1.34.0")]
241 impl TryFrom<$source> for $target {
242 type Error = TryFromIntError;
243
244 /// Try to create the target number type from a source
245 /// number type. This returns an error if the source value
246 /// is outside of the range of the target type.
247 #[inline]
dfeec247
XL
248 fn try_from(u: $source) -> Result<Self, Self::Error> {
249 let min = Self::min_value() as $source;
250 let max = Self::max_value() as $source;
60c5eb7d
XL
251 if u < min || u > max {
252 Err(TryFromIntError(()))
253 } else {
dfeec247 254 Ok(u as Self)
60c5eb7d
XL
255 }
256 }
257 }
258 )*}
259}
260
261macro_rules! rev {
262 ($mac:ident, $source:ty, $($target:ty),*) => {$(
263 $mac!($target, $source);
264 )*}
265}
266
267// intra-sign conversions
268try_from_upper_bounded!(u16, u8);
269try_from_upper_bounded!(u32, u16, u8);
270try_from_upper_bounded!(u64, u32, u16, u8);
271try_from_upper_bounded!(u128, u64, u32, u16, u8);
272
273try_from_both_bounded!(i16, i8);
274try_from_both_bounded!(i32, i16, i8);
275try_from_both_bounded!(i64, i32, i16, i8);
276try_from_both_bounded!(i128, i64, i32, i16, i8);
277
278// unsigned-to-signed
279try_from_upper_bounded!(u8, i8);
280try_from_upper_bounded!(u16, i8, i16);
281try_from_upper_bounded!(u32, i8, i16, i32);
282try_from_upper_bounded!(u64, i8, i16, i32, i64);
283try_from_upper_bounded!(u128, i8, i16, i32, i64, i128);
284
285// signed-to-unsigned
286try_from_lower_bounded!(i8, u8, u16, u32, u64, u128);
287try_from_lower_bounded!(i16, u16, u32, u64, u128);
288try_from_lower_bounded!(i32, u32, u64, u128);
289try_from_lower_bounded!(i64, u64, u128);
290try_from_lower_bounded!(i128, u128);
291try_from_both_bounded!(i16, u8);
292try_from_both_bounded!(i32, u16, u8);
293try_from_both_bounded!(i64, u32, u16, u8);
294try_from_both_bounded!(i128, u64, u32, u16, u8);
295
296// usize/isize
297try_from_upper_bounded!(usize, isize);
298try_from_lower_bounded!(isize, usize);
299
300#[cfg(target_pointer_width = "16")]
301mod ptr_try_from_impls {
302 use super::TryFromIntError;
303 use crate::convert::TryFrom;
304
305 try_from_upper_bounded!(usize, u8);
306 try_from_unbounded!(usize, u16, u32, u64, u128);
307 try_from_upper_bounded!(usize, i8, i16);
308 try_from_unbounded!(usize, i32, i64, i128);
309
310 try_from_both_bounded!(isize, u8);
311 try_from_lower_bounded!(isize, u16, u32, u64, u128);
312 try_from_both_bounded!(isize, i8);
313 try_from_unbounded!(isize, i16, i32, i64, i128);
314
315 rev!(try_from_upper_bounded, usize, u32, u64, u128);
316 rev!(try_from_lower_bounded, usize, i8, i16);
317 rev!(try_from_both_bounded, usize, i32, i64, i128);
318
319 rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
320 rev!(try_from_both_bounded, isize, i32, i64, i128);
321}
322
323#[cfg(target_pointer_width = "32")]
324mod ptr_try_from_impls {
325 use super::TryFromIntError;
326 use crate::convert::TryFrom;
327
328 try_from_upper_bounded!(usize, u8, u16);
329 try_from_unbounded!(usize, u32, u64, u128);
330 try_from_upper_bounded!(usize, i8, i16, i32);
331 try_from_unbounded!(usize, i64, i128);
332
333 try_from_both_bounded!(isize, u8, u16);
334 try_from_lower_bounded!(isize, u32, u64, u128);
335 try_from_both_bounded!(isize, i8, i16);
336 try_from_unbounded!(isize, i32, i64, i128);
337
338 rev!(try_from_unbounded, usize, u32);
339 rev!(try_from_upper_bounded, usize, u64, u128);
340 rev!(try_from_lower_bounded, usize, i8, i16, i32);
341 rev!(try_from_both_bounded, usize, i64, i128);
342
343 rev!(try_from_unbounded, isize, u16);
344 rev!(try_from_upper_bounded, isize, u32, u64, u128);
345 rev!(try_from_unbounded, isize, i32);
346 rev!(try_from_both_bounded, isize, i64, i128);
347}
348
349#[cfg(target_pointer_width = "64")]
350mod ptr_try_from_impls {
351 use super::TryFromIntError;
352 use crate::convert::TryFrom;
353
354 try_from_upper_bounded!(usize, u8, u16, u32);
355 try_from_unbounded!(usize, u64, u128);
356 try_from_upper_bounded!(usize, i8, i16, i32, i64);
357 try_from_unbounded!(usize, i128);
358
359 try_from_both_bounded!(isize, u8, u16, u32);
360 try_from_lower_bounded!(isize, u64, u128);
361 try_from_both_bounded!(isize, i8, i16, i32);
362 try_from_unbounded!(isize, i64, i128);
363
364 rev!(try_from_unbounded, usize, u32, u64);
365 rev!(try_from_upper_bounded, usize, u128);
366 rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
367 rev!(try_from_both_bounded, usize, i128);
368
369 rev!(try_from_unbounded, isize, u16, u32);
370 rev!(try_from_upper_bounded, isize, u64, u128);
371 rev!(try_from_unbounded, isize, i32, i64);
372 rev!(try_from_both_bounded, isize, i128);
373}
374
375// Conversion traits for non-zero integer types
376use crate::num::NonZeroI128;
377use crate::num::NonZeroI16;
378use crate::num::NonZeroI32;
379use crate::num::NonZeroI64;
380use crate::num::NonZeroI8;
381use crate::num::NonZeroIsize;
382use crate::num::NonZeroU128;
383use crate::num::NonZeroU16;
384use crate::num::NonZeroU32;
385use crate::num::NonZeroU64;
386use crate::num::NonZeroU8;
387use crate::num::NonZeroUsize;
388
389macro_rules! nzint_impl_from {
390 ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
391 #[$attr]
392 #[doc = $doc]
393 impl From<$Small> for $Large {
394 #[inline]
dfeec247 395 fn from(small: $Small) -> Self {
60c5eb7d
XL
396 // SAFETY: input type guarantees the value is non-zero
397 unsafe {
dfeec247 398 Self::new_unchecked(small.get().into())
60c5eb7d
XL
399 }
400 }
401 }
402 };
403 ($Small: ty, $Large: ty, #[$attr:meta]) => {
404 nzint_impl_from!($Small,
405 $Large,
406 #[$attr],
407 concat!("Converts `",
408 stringify!($Small),
409 "` to `",
410 stringify!($Large),
411 "` losslessly."));
412 }
413}
414
415// Non-zero Unsigned -> Non-zero Unsigned
416nzint_impl_from! { NonZeroU8, NonZeroU16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
417nzint_impl_from! { NonZeroU8, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
418nzint_impl_from! { NonZeroU8, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
419nzint_impl_from! { NonZeroU8, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
420nzint_impl_from! { NonZeroU8, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
421nzint_impl_from! { NonZeroU16, NonZeroU32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
422nzint_impl_from! { NonZeroU16, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
423nzint_impl_from! { NonZeroU16, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
424nzint_impl_from! { NonZeroU16, NonZeroUsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
425nzint_impl_from! { NonZeroU32, NonZeroU64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
426nzint_impl_from! { NonZeroU32, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
427nzint_impl_from! { NonZeroU64, NonZeroU128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
428
429// Non-zero Signed -> Non-zero Signed
430nzint_impl_from! { NonZeroI8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
431nzint_impl_from! { NonZeroI8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
432nzint_impl_from! { NonZeroI8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
433nzint_impl_from! { NonZeroI8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
434nzint_impl_from! { NonZeroI8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
435nzint_impl_from! { NonZeroI16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
436nzint_impl_from! { NonZeroI16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
437nzint_impl_from! { NonZeroI16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
438nzint_impl_from! { NonZeroI16, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
439nzint_impl_from! { NonZeroI32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
440nzint_impl_from! { NonZeroI32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
441nzint_impl_from! { NonZeroI64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
442
443// NonZero UnSigned -> Non-zero Signed
444nzint_impl_from! { NonZeroU8, NonZeroI16, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
445nzint_impl_from! { NonZeroU8, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
446nzint_impl_from! { NonZeroU8, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
447nzint_impl_from! { NonZeroU8, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
448nzint_impl_from! { NonZeroU8, NonZeroIsize, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
449nzint_impl_from! { NonZeroU16, NonZeroI32, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
450nzint_impl_from! { NonZeroU16, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
451nzint_impl_from! { NonZeroU16, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
452nzint_impl_from! { NonZeroU32, NonZeroI64, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
453nzint_impl_from! { NonZeroU32, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }
454nzint_impl_from! { NonZeroU64, NonZeroI128, #[stable(feature = "nz_int_conv", since = "1.41.0")] }