#checked #primitive #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

#4 in #primitive-integer

Download history 427802/week @ 2023-11-03 462342/week @ 2023-11-10 438904/week @ 2023-11-17 419091/week @ 2023-11-24 474794/week @ 2023-12-01 430097/week @ 2023-12-08 393422/week @ 2023-12-15 232971/week @ 2023-12-22 317433/week @ 2023-12-29 439373/week @ 2024-01-05 454139/week @ 2024-01-12 521958/week @ 2024-01-19 466370/week @ 2024-01-26 463550/week @ 2024-02-02 475934/week @ 2024-02-09 382526/week @ 2024-02-16

1,881,306 downloads per month
Used in 5,179 crates (94 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