Initial import of cast-0.2.3. am: 80f5cd0831 am: 46353a3021 am: f8edbc5cc4

Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/cast/+/1620970

Change-Id: Ib90648fbe61f665b761be044237510ef3540c6fd
tree: 3c131e9a6e96d63c9d59a16721bcf780f03ee20e
  1. ci/
  2. src/
  3. .cargo_vcs_info.json
  4. .gitignore
  5. .travis.yml
  6. build.rs
  7. Cargo.toml
  8. Cargo.toml.orig
  9. CHANGELOG.md
  10. README.md
README.md

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.