| 1 | //! Conversions between integer types that check for overflow and underflow |
| 2 | //! |
| 3 | //! The functions that this module exposes attempt to cast from one primitive |
| 4 | //! integer type to another, returning `None` on overflow or underflow. |
| 5 | //! |
| 6 | //! # Examples |
| 7 | //! ``` |
| 8 | //! use checked_int_cast::CheckedIntCast; |
| 9 | //! |
| 10 | //! // Returns None if usize has 32 or fewer bits |
| 11 | //! (2u64 << 33).as_usize_checked(); |
| 12 | //! |
| 13 | //! // Successful cast |
| 14 | //! assert_eq!(127u8.as_i8_checked(), Some(127i8)); |
| 15 | //! |
| 16 | //! // Overflow |
| 17 | //! assert_eq!(255u8.as_i8_checked(), None); |
| 18 | //! |
| 19 | //! // Underflow |
| 20 | //! assert_eq!((-1i8).as_u32_checked(), None); |
| 21 | //! ``` |
| 22 | |
| 23 | use std::{i8, i16, i32, i64, isize}; |
| 24 | use std::{u8, u16, u32, u64, usize}; |
| 25 | |
| 26 | // The only subtle thing about these implementations is that we need to skip |
| 27 | // the comparison with MAX if we might get overflow in that comparison. |
| 28 | // Conveniently, these are also the cases where the comparison is not |
| 29 | // necessary. |
| 30 | // |
| 31 | // For example, checking the conversion from u32 to usize with the code |
| 32 | // x > (u32::MAX as usize) |
| 33 | // would work fine with 32-bit or 64-bit usizes. For a 16-bit usize, though, |
| 34 | // u32::MAX would be truncated to 0usize and the comparison would not work |
| 35 | // like we want. |
| 36 | // |
| 37 | // So, to check whether or not the comparison is necesary, we convert both of |
| 38 | // the type's MAXes to f64. Although that conversion won't always be precise, |
| 39 | // it is good enough to tell which type has a larger range. The compiler |
| 40 | // optimizes away this check in release mode. |
| 41 | // |
| 42 | |
| 43 | macro_rules! impl_signed_as_unsigned(($fn_name:ident, $from:ident, $to:ident) => ( |
| 44 | #[inline] |
| 45 | fn $fn_name(self) -> Option<$to> { |
| 46 | if self < 0 { |
| 47 | return None; |
| 48 | } |
| 49 | if $from::MAX as f64 > $to::MAX as f64 { |
| 50 | if self > $to::MAX as $from { |
| 51 | return None; |
| 52 | } |
| 53 | } |
| 54 | return Some(self as $to); |
| 55 | } |
| 56 | )); |
| 57 | |
| 58 | macro_rules! impl_signed_as_signed(($fn_name:ident, $from:ident, $to:ident) => ( |
| 59 | #[inline] |
| 60 | fn $fn_name(self) -> Option<$to> { |
| 61 | if $from::MAX as f64 > $to::MAX as f64 { |
| 62 | if self > $to::MAX as $from { |
| 63 | return None; |
| 64 | } |
| 65 | if self < $to::MIN as $from { |
| 66 | return None; |
| 67 | } |
| 68 | } |
| 69 | return Some(self as $to); |
| 70 | } |
| 71 | )); |
| 72 | |
| 73 | macro_rules! impl_unsigned_as_any(($fn_name:ident, $from:ident, $to:ident) => ( |
| 74 | #[inline] |
| 75 | fn $fn_name(self) -> Option<$to> { |
| 76 | if $from::MAX as f64 > $to::MAX as f64 { |
| 77 | if self > $to::MAX as $from { |
| 78 | return None; |
| 79 | } |
| 80 | } |
| 81 | return Some(self as $to); |
| 82 | } |
| 83 | )); |
| 84 | |
| 85 | /// This trait allows a value to be cast to the various primitive integer types. |
| 86 | /// If the conversion overflows or underflows, the functions return `None`. |
| 87 | pub trait CheckedIntCast { |
| 88 | fn as_isize_checked(self) -> Option<isize>; |
| 89 | fn as_i8_checked(self) -> Option<i8>; |
| 90 | fn as_i16_checked(self) -> Option<i16>; |
| 91 | fn as_i32_checked(self) -> Option<i32>; |
| 92 | fn as_i64_checked(self) -> Option<i64>; |
| 93 | fn as_usize_checked(self) -> Option<usize>; |
| 94 | fn as_u8_checked(self) -> Option<u8>; |
| 95 | fn as_u16_checked(self) -> Option<u16>; |
| 96 | fn as_u32_checked(self) -> Option<u32>; |
| 97 | fn as_u64_checked(self) -> Option<u64>; |
| 98 | } |
| 99 | |
| 100 | macro_rules! impl_signed_to_all(($from:ident) => ( |
| 101 | impl CheckedIntCast for $from { |
| 102 | impl_signed_as_signed!(as_isize_checked, $from, isize); |
| 103 | impl_signed_as_signed!(as_i8_checked, $from, i8); |
| 104 | impl_signed_as_signed!(as_i16_checked, $from, i16); |
| 105 | impl_signed_as_signed!(as_i32_checked, $from, i32); |
| 106 | impl_signed_as_signed!(as_i64_checked, $from, i64); |
| 107 | impl_signed_as_unsigned!(as_usize_checked, $from, usize); |
| 108 | impl_signed_as_unsigned!(as_u8_checked, $from, u8); |
| 109 | impl_signed_as_unsigned!(as_u16_checked, $from, u16); |
| 110 | impl_signed_as_unsigned!(as_u32_checked, $from, u32); |
| 111 | impl_signed_as_unsigned!(as_u64_checked, $from, u64); |
| 112 | } |
| 113 | )); |
| 114 | |
| 115 | macro_rules! impl_unsigned_to_all(($from:ident) => ( |
| 116 | impl CheckedIntCast for $from { |
| 117 | impl_unsigned_as_any!(as_isize_checked, $from, isize); |
| 118 | impl_unsigned_as_any!(as_i8_checked, $from, i8); |
| 119 | impl_unsigned_as_any!(as_i16_checked, $from, i16); |
| 120 | impl_unsigned_as_any!(as_i32_checked, $from, i32); |
| 121 | impl_unsigned_as_any!(as_i64_checked, $from, i64); |
| 122 | impl_unsigned_as_any!(as_usize_checked, $from, usize); |
| 123 | impl_unsigned_as_any!(as_u8_checked, $from, u8); |
| 124 | impl_unsigned_as_any!(as_u16_checked, $from, u16); |
| 125 | impl_unsigned_as_any!(as_u32_checked, $from, u32); |
| 126 | impl_unsigned_as_any!(as_u64_checked, $from, u64); |
| 127 | } |
| 128 | )); |
| 129 | |
| 130 | impl_signed_to_all!(isize); |
| 131 | impl_signed_to_all!(i8); |
| 132 | impl_signed_to_all!(i16); |
| 133 | impl_signed_to_all!(i32); |
| 134 | impl_signed_to_all!(i64); |
| 135 | |
| 136 | impl_unsigned_to_all!(usize); |
| 137 | impl_unsigned_to_all!(u8); |
| 138 | impl_unsigned_to_all!(u16); |
| 139 | impl_unsigned_to_all!(u32); |
| 140 | impl_unsigned_to_all!(u64); |
| 141 | |
| 142 | |
| 143 | #[cfg(test)] |
| 144 | mod test { |
| 145 | use super::CheckedIntCast; |
| 146 | |
| 147 | #[test] |
| 148 | fn basic() { |
| 149 | assert_eq!(0u64.as_i8_checked(), Some(0i8)); |
| 150 | assert_eq!((-40i64).as_i8_checked(), Some(-40)); |
| 151 | assert_eq!((-321i64).as_i8_checked(), None); |
| 152 | assert_eq!(40000000u64.as_u16_checked(), None); |
| 153 | assert_eq!(40000000u64.as_i32_checked(), Some(40000000i32)); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn negative_to_unsigned() { |
| 158 | assert_eq!((-4i8).as_u64_checked(), None); |
| 159 | assert_eq!((-1i32).as_usize_checked(), None); |
| 160 | assert_eq!(::std::i32::MIN.as_usize_checked(), None); |
| 161 | assert_eq!((-3053i64).as_u32_checked(), None); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn unsigned_as_unsigned() { |
| 166 | assert_eq!(256u32.as_u8_checked(), None); |
| 167 | assert_eq!(255u32.as_u8_checked(), Some(255)); |
| 168 | assert_eq!(256u32.as_u16_checked(), Some(256)); |
| 169 | } |
| 170 | } |