#checked #int #integer #cast #convert #conversion

checked_int_cast

Conversions between primitive integers with overflow and underflow checking

1 stable release

Uses old Rust 2015

1.0.0 Sep 7, 2015

#487 in Algorithms

Download history 16991/week @ 2021-09-29 16446/week @ 2021-10-06 17108/week @ 2021-10-13 17163/week @ 2021-10-20 14267/week @ 2021-10-27 14303/week @ 2021-11-03 15794/week @ 2021-11-10 19414/week @ 2021-11-17 13511/week @ 2021-11-24 18837/week @ 2021-12-01 17242/week @ 2021-12-08 18573/week @ 2021-12-15 5602/week @ 2021-12-22 6777/week @ 2021-12-29 13050/week @ 2022-01-05 12748/week @ 2022-01-12

41,111 downloads per month
Used in 67 crates (2 directly)

MIT license

7KB
116 lines

Documentation

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);

No runtime deps

U