Debian Codegraph @

src/lib.rs

raw symbols SCIP
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 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{i8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/i8/, i16rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/i16/, i32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/i32/, i64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/i64/, isizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/isize/};
24 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{u8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/u8/, u16rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/u16/, u32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/u32/, u64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/u64/, usizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/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_unsignedrust-analyzer cargo checked_int_cast 1.0.0 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_signedrust-analyzer cargo checked_int_cast 1.0.0 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_anyrust-analyzer cargo checked_int_cast 1.0.0 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 CheckedIntCastrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast# {
88 fn as_isize_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_isize_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<isize>;
89 fn as_i8_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_i8_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<i8>;
90 fn as_i16_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_i16_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<i16>;
91 fn as_i32_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_i32_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<i32>;
92 fn as_i64_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_i64_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<i64>;
93 fn as_usize_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_usize_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>;
94 fn as_u8_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_u8_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u8>;
95 fn as_u16_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_u16_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16>;
96 fn as_u32_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_u32_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u32>;
97 fn as_u64_checkedrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#as_u64_checked().(self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u64>;
98 }
99
100 macro_rules! impl_signed_to_allrust-analyzer cargo checked_int_cast 1.0.0 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_allrust-analyzer cargo checked_int_cast 1.0.0 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_allrust-analyzer cargo checked_int_cast 1.0.0 impl_signed_to_all!!(isize);
131 impl_signed_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_signed_to_all!!(i8);
132 impl_signed_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_signed_to_all!!(i16);
133 impl_signed_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_signed_to_all!!(i32);
134 impl_signed_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_signed_to_all!!(i64);
135
136 impl_unsigned_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_unsigned_to_all!!(usize);
137 impl_unsigned_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_unsigned_to_all!!(u8);
138 impl_unsigned_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_unsigned_to_all!!(u16);
139 impl_unsigned_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_unsigned_to_all!!(u32);
140 impl_unsigned_to_allrust-analyzer cargo checked_int_cast 1.0.0 impl_unsigned_to_all!!(u64);
141
142
143 #[cfg(test)]
144 mod testrust-analyzer cargo checked_int_cast 1.0.0 test/ {
145 use superrust-analyzer cargo checked_int_cast 1.0.0 crate/::CheckedIntCastrust-analyzer cargo checked_int_cast 1.0.0 CheckedIntCast#;
146
147 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
148 fn basicrust-analyzer cargo checked_int_cast 1.0.0 test/basic().() {
149 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0u64.as_i8_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[u64][CheckedIntCast]as_i8_checked().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0i8));
150 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().40i64).as_i8_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[i64][CheckedIntCast]as_i8_checked().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().40));
151 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().321i64).as_i8_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[i64][CheckedIntCast]as_i8_checked().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
152 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(40000000u64.as_u16_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[u64][CheckedIntCast]as_u16_checked().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
153 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(40000000u64.as_i32_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[u64][CheckedIntCast]as_i32_checked().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(40000000i32));
154 }
155
156 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
157 fn negative_to_unsignedrust-analyzer cargo checked_int_cast 1.0.0 test/negative_to_unsigned().() {
158 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().4i8).as_u64_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[i8][CheckedIntCast]as_u64_checked().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
159 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1i32).as_usize_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[i32][CheckedIntCast]as_usize_checked().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
160 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::i32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/i32/::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/i32/MIN..as_usize_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[i32][CheckedIntCast]as_usize_checked().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
161 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().3053i64).as_u32_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[i64][CheckedIntCast]as_u32_checked().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
162 }
163
164 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
165 fn unsigned_as_unsignedrust-analyzer cargo checked_int_cast 1.0.0 test/unsigned_as_unsigned().() {
166 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(256u32.as_u8_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[u32][CheckedIntCast]as_u8_checked().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
167 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(255u32.as_u8_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[u32][CheckedIntCast]as_u8_checked().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(255));
168 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(256u32.as_u16_checkedrust-analyzer cargo checked_int_cast 1.0.0 impl#[u32][CheckedIntCast]as_u16_checked().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(256));
169 }
170 }