#integer #cast #primitive-integer #checked #convert #conversion #int

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

#2387 in Algorithms

Download history 15146/week @ 2024-03-06 12503/week @ 2024-03-13 15459/week @ 2024-03-20 12674/week @ 2024-03-27 12170/week @ 2024-04-03 12645/week @ 2024-04-10 14269/week @ 2024-04-17 12858/week @ 2024-04-24 11295/week @ 2024-05-01 10881/week @ 2024-05-08 17177/week @ 2024-05-15 15362/week @ 2024-05-22 11916/week @ 2024-05-29 13199/week @ 2024-06-05 11891/week @ 2024-06-12 11542/week @ 2024-06-19

52,347 downloads per month
Used in 111 crates (4 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