|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use fmt; |
| 12 | + |
| 13 | +/// Internal trait for APIs like `i32::cast`. |
| 14 | +#[unstable(feature = "num_cast_internals", issue = "0")] |
| 15 | +pub trait Cast<T>: Sized { |
| 16 | + /// Internal implementation detail of this trait. |
| 17 | + fn cast(t: T) -> Result<Self, CastError>; |
| 18 | +} |
| 19 | + |
| 20 | +/// Error type returned from APIs like `i32::cast`, indicates that a cast could |
| 21 | +/// not be performed losslessly. |
| 22 | +#[unstable(feature = "num_cast", issue = "0")] |
| 23 | +#[derive(Debug)] |
| 24 | +pub struct CastError(()); |
| 25 | + |
| 26 | +#[unstable(feature = "num_cast", issue = "0")] |
| 27 | +impl fmt::Display for CastError { |
| 28 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 29 | + "failed to losslessly cast integral types".fmt(f) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +macro_rules! same_sign_cast_int_impl { |
| 34 | + ($storage:ty, $target:ty, $($source:ty),*) => {$( |
| 35 | + #[unstable(feature = "num_cast", issue = "0")] |
| 36 | + impl Cast<$source> for $target { |
| 37 | + #[inline] |
| 38 | + fn cast(u: $source) -> Result<$target, CastError> { |
| 39 | + let min = <$target>::min_value() as $storage; |
| 40 | + let max = <$target>::max_value() as $storage; |
| 41 | + if u as $storage < min || u as $storage > max { |
| 42 | + Err(CastError(())) |
| 43 | + } else { |
| 44 | + Ok(u as $target) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + )*} |
| 49 | +} |
| 50 | + |
| 51 | +same_sign_cast_int_impl!(u128, u8, u8, u16, u32, u64, u128, usize); |
| 52 | +same_sign_cast_int_impl!(i128, i8, i8, i16, i32, i64, i128, isize); |
| 53 | +same_sign_cast_int_impl!(u128, u16, u8, u16, u32, u64, u128, usize); |
| 54 | +same_sign_cast_int_impl!(i128, i16, i8, i16, i32, i64, i128, isize); |
| 55 | +same_sign_cast_int_impl!(u128, u32, u8, u16, u32, u64, u128, usize); |
| 56 | +same_sign_cast_int_impl!(i128, i32, i8, i16, i32, i64, i128, isize); |
| 57 | +same_sign_cast_int_impl!(u128, u64, u8, u16, u32, u64, u128, usize); |
| 58 | +same_sign_cast_int_impl!(i128, i64, i8, i16, i32, i64, i128, isize); |
| 59 | +same_sign_cast_int_impl!(u128, u128, u8, u16, u32, u64, u128, usize); |
| 60 | +same_sign_cast_int_impl!(i128, i128, i8, i16, i32, i64, i128, isize); |
| 61 | +same_sign_cast_int_impl!(u128, usize, u8, u16, u32, u64, u128, usize); |
| 62 | +same_sign_cast_int_impl!(i128, isize, i8, i16, i32, i64, i128, isize); |
| 63 | + |
| 64 | +macro_rules! cross_sign_cast_int_impl { |
| 65 | + ($unsigned:ty, $($signed:ty),*) => {$( |
| 66 | + #[unstable(feature = "num_cast", issue = "0")] |
| 67 | + impl Cast<$unsigned> for $signed { |
| 68 | + #[inline] |
| 69 | + fn cast(u: $unsigned) -> Result<$signed, CastError> { |
| 70 | + let max = <$signed>::max_value() as u128; |
| 71 | + if u as u128 > max { |
| 72 | + Err(CastError(())) |
| 73 | + } else { |
| 74 | + Ok(u as $signed) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + #[unstable(feature = "num_cast", issue = "0")] |
| 80 | + impl Cast<$signed> for $unsigned { |
| 81 | + #[inline] |
| 82 | + fn cast(u: $signed) -> Result<$unsigned, CastError> { |
| 83 | + let max = <$unsigned>::max_value() as u128; |
| 84 | + if u < 0 || u as u128 > max { |
| 85 | + Err(CastError(())) |
| 86 | + } else { |
| 87 | + Ok(u as $unsigned) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + )*} |
| 92 | +} |
| 93 | + |
| 94 | +cross_sign_cast_int_impl!(u8, i8, i16, i32, i64, i128, isize); |
| 95 | +cross_sign_cast_int_impl!(u16, i8, i16, i32, i64, i128, isize); |
| 96 | +cross_sign_cast_int_impl!(u32, i8, i16, i32, i64, i128, isize); |
| 97 | +cross_sign_cast_int_impl!(u64, i8, i16, i32, i64, i128, isize); |
| 98 | +cross_sign_cast_int_impl!(u128, i8, i16, i32, i64, i128, isize); |
| 99 | +cross_sign_cast_int_impl!(usize, i8, i16, i32, i64, i128, isize); |
0 commit comments