1 stable release
Uses old Rust 2015
1.0.0 | Sep 7, 2015 |
---|
#2387 in Algorithms
52,347 downloads per month
Used in 111 crates
(4 directly)
7KB
116 lines
The functions that this module exposes attempt to cast from one primitive
integer type to another, returning None
on overflow or underflow.
Examples
use checked_int_cast::CheckedIntCast;
// Returns None if usize has 32 or fewer bits
(2u64 << 33).as_usize_checked();
// Successful cast
assert_eq!(127u8.as_i8_checked(), Some(127i8));
// Overflow
assert_eq!(255u8.as_i8_checked(), None);
// Underflow
assert_eq!((-1i8).as_u32_checked(), None);
lib.rs
:
Conversions between integer types that check for overflow and underflow
The functions that this module exposes attempt to cast from one primitive
integer type to another, returning None
on overflow or underflow.
Examples
use checked_int_cast::CheckedIntCast;
// Returns None if usize has 32 or fewer bits
(2u64 << 33).as_usize_checked();
// Successful cast
assert_eq!(127u8.as_i8_checked(), Some(127i8));
// Overflow
assert_eq!(255u8.as_i8_checked(), None);
// Underflow
assert_eq!((-1i8).as_u32_checked(), None);