#primitive-integer #primitive #checked #integer #float

no-std dev cast

Ergonomic, checked cast functions for primitive types

9 releases

0.3.0 Sep 4, 2021
0.2.7 Jul 3, 2021
0.2.6 May 15, 2021
0.2.5 Apr 12, 2021
0.1.0 Feb 7, 2016

#517 in Rust patterns

Download history 657552/week @ 2024-07-21 634498/week @ 2024-07-28 623128/week @ 2024-08-04 675516/week @ 2024-08-11 749582/week @ 2024-08-18 704437/week @ 2024-08-25 688294/week @ 2024-09-01 702921/week @ 2024-09-08 626091/week @ 2024-09-15 694634/week @ 2024-09-22 696939/week @ 2024-09-29 761293/week @ 2024-10-06 708454/week @ 2024-10-13 739494/week @ 2024-10-20 728613/week @ 2024-10-27 728087/week @ 2024-11-03

2,947,899 downloads per month
Used in 6,252 crates (100 directly)

MIT/Apache

28KB
524 lines

crates.io crates.io

cast

Ergonomic, checked cast functions for primitive types

extern crate cast;

// `u8` and `u16` are checked cast functions, use them to cast from any numeric
// primitive to `u8`/`u16` respectively
use cast::{u8, u16, Error};

// Infallible operations, like integer promotion, are equivalent to a normal
// cast with `as`
assert_eq!(u16(0u8), 0u16);

// Everything else will return a `Result` depending on the success of the
// operation
assert_eq!(u8(0u16), Ok(0u8));
assert_eq!(u8(256u16), Err(Error::Overflow));
assert_eq!(u8(-1i8), Err(Error::Underflow));
assert_eq!(u8(1. / 0.), Err(Error::Infinite));
assert_eq!(u8(0. / 0.), Err(Error::NaN));

API docs

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps