4 releases (breaking)

✓ Uses Rust 2018 edition

0.3.0 Oct 1, 2019
0.2.0 Sep 10, 2019
0.1.0 Sep 9, 2019
0.0.0 Sep 7, 2019
Download history 17/week @ 2019-10-07 26/week @ 2019-10-14 28/week @ 2019-10-21 10/week @ 2019-10-28 10/week @ 2019-11-04 7/week @ 2019-11-11 17/week @ 2019-11-18 10/week @ 2019-11-25 24/week @ 2019-12-02 73/week @ 2019-12-09 81/week @ 2019-12-16 208/week @ 2019-12-23 28/week @ 2019-12-30 51/week @ 2020-01-06 40/week @ 2020-01-13

114 downloads per month
Used in 19 crates (1 directly)


3.5K SLoC

Numeric casts

This crate provides checked, overflowing and static casts.

What’s new

Version 0.3.0 news (2019-10-01)

  • The behavior of static casts was changed: now they return Option, but an implementation should either always return Some or always return None.
  • Bug fix: checked casts from floating-point to wrapped integers were panicking for infinite or NaN.

Quick examples

use az::{Az, OverflowingAs, WrappingAs};
use core::num::Wrapping;

// Panics on overflow with `debug_assertions`, otherwise wraps
assert_eq!(12i32.az::<u32>(), 12u32);

// Always wraps
let wrapped = 1u32.wrapping_neg();
assert_eq!((-1).wrapping_as::<u32>(), wrapped);
assert_eq!((-1).overflowing_as::<u32>(), (wrapped, true));

// Wrapping can also be obtained using `Wrapping`
assert_eq!((-1).az::<Wrapping<u32>>().0, wrapped);

Conversions from floating-point to integers are also supported. Numbers are rounded towards zero, but the Round wrapper can be used to convert floating-point numbers to integers with rounding to the nearest, with ties rounded to even.

use az::{Az, CheckedAs, Round, SaturatingAs};
use core::f32;

assert_eq!(15.7.az::<i32>(), 15);
assert_eq!(Round(15.5).az::<i32>(), 16);
assert_eq!(1.5e20.saturating_as::<i32>(), i32::max_value());
assert_eq!(f32::NAN.checked_as::<i32>(), None);

Using the az crate

The az crate is available on crates.io. To use it in your crate, add it as a dependency inside Cargo.toml:

az = "0.3.0"


This crate is free software: you can redistribute it and/or modify it under the terms of either

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache License, Version 2.0, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps