README.md
1[![crates.io](https://img.shields.io/crates/d/cast.svg)](https://crates.io/crates/cast)
2[![crates.io](https://img.shields.io/crates/v/cast.svg)](https://crates.io/crates/cast)
3
4# `cast`
5
6> Ergonomic, checked cast functions for primitive types
7
8``` rust
9extern crate cast;
10
11// `u8` and `u16` are checked cast functions, use them to cast from any numeric
12// primitive to `u8`/`u16` respectively
13use cast::{u8, u16, Error};
14
15// Infallible operations, like integer promotion, are equivalent to a normal
16// cast with `as`
17assert_eq!(u16(0u8), 0u16);
18
19// Everything else will return a `Result` depending on the success of the
20// operation
21assert_eq!(u8(0u16), Ok(0u8));
22assert_eq!(u8(256u16), Err(Error::Overflow));
23assert_eq!(u8(-1i8), Err(Error::Underflow));
24assert_eq!(u8(1. / 0.), Err(Error::Infinite));
25assert_eq!(u8(0. / 0.), Err(Error::NaN));
26```
27
28## [API docs](https://docs.rs/cast)
29
30## License
31
32Licensed under either of
33
34- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
35 http://www.apache.org/licenses/LICENSE-2.0)
36- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
37
38at your option.
39
40### Contribution
41
42Unless you explicitly state otherwise, any contribution intentionally submitted
43for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
44dual licensed as above, without any additional terms or conditions.
45