• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

patches/25-Apr-2025-2016

src/25-Apr-2025-752526

.cargo-checksum.jsonD25-Apr-2025655 11

Android.bpD25-Apr-20251.1 KiB4742

CHANGELOG.mdD25-Apr-20253 KiB11168

Cargo.tomlD25-Apr-2025954 2825

LICENSED25-Apr-202510.6 KiB202169

LICENSE-APACHED25-Apr-202510.6 KiB202169

LICENSE-MITD25-Apr-20251 KiB2622

METADATAD25-Apr-2025377 1817

MODULE_LICENSE_APACHE2D25-Apr-20250

README.mdD25-Apr-20251.4 KiB4531

TEST_MAPPINGD25-Apr-2025475 2726

cargo_embargo.jsonD25-Apr-202542 54

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