#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

#2439 in Algorithms

Download history 14451/week @ 2024-03-15 14302/week @ 2024-03-22 12250/week @ 2024-03-29 11590/week @ 2024-04-05 13114/week @ 2024-04-12 14564/week @ 2024-04-19 11393/week @ 2024-04-26 10883/week @ 2024-05-03 13478/week @ 2024-05-10 17942/week @ 2024-05-17 13349/week @ 2024-05-24 12736/week @ 2024-05-31 12699/week @ 2024-06-07 12773/week @ 2024-06-14 12349/week @ 2024-06-21 5190/week @ 2024-06-28

46,004 downloads per month
Used in 110 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