Rust crate for safely casting between primitive types - vendored from GitHub
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Jorge Aparicio 052288097d
Some checks failed
Continuous integration / MSRV check (push) Has been cancelled
Continuous integration / MSRV check-1 (push) Has been cancelled
Continuous integration / Test Suite (push) Has been cancelled
Continuous integration / Test Suite-1 (push) Has been cancelled
Merge pull request #44 from japaric/release
rm default feature & update CHANGELOG
2021-09-04 11:07:15 +00:00
.github/workflows remove default Cargo features 2021-09-04 13:04:51 +02:00
src fix promote and back tests 2021-09-04 12:40:55 +02:00
.gitignore prepare for 0.1.0 2016-02-07 14:33:11 -05:00
Cargo.toml remove default Cargo features 2021-09-04 13:04:51 +02:00
CHANGELOG.md update CHANGELOG for release 2021-09-04 13:05:20 +02:00
LICENSE-APACHE prepare for 0.1.0 2016-02-07 14:33:11 -05:00
LICENSE-MIT release v0.2.0 2017-02-08 20:10:36 -05:00
README.md release v0.2.0 2017-02-08 20:10:36 -05:00

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.