10 releases (5 stable)

new 1.2.0 Nov 24, 2021
1.1.1 Mar 25, 2021
1.0.0 Apr 18, 2020
0.3.1 Apr 17, 2020
0.3.0 Oct 1, 2019

#37 in Rust patterns

Download history 4525/week @ 2021-08-10 2529/week @ 2021-08-17 4126/week @ 2021-08-24 3490/week @ 2021-08-31 3681/week @ 2021-09-07 4124/week @ 2021-09-14 5476/week @ 2021-09-21 4011/week @ 2021-09-28 4824/week @ 2021-10-05 4817/week @ 2021-10-12 5148/week @ 2021-10-19 5824/week @ 2021-10-26 5068/week @ 2021-11-02 5610/week @ 2021-11-09 5341/week @ 2021-11-16 5130/week @ 2021-11-23

22,317 downloads per month
Used in 186 crates (9 directly)

MIT/Apache

190KB
4K SLoC

Numeric casts

This crate provides casts and checked casts.

What’s new

Version 1.2.0 news (2021-11-24)

Other releases

Details on other releases can be found in RELEASES.md.

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);

Implementing casts for other types

To provide casts for another type, you should implement the Cast trait and if necessary the CheckedCast, SaturatingCast, WrappingCast, OverflowingCast and UnwrappedCast traits. The Az, CheckedAs, SaturatingAs, WrappingAs, OverflowingAs and UnwrappedAs traits are already implemented for all types using blanket implementations that make use of the former traits.

The cast traits can also be implemented for references. This can be useful for expensive types that are not Copy. For example if you have your own integer type that does not implement Copy, you could implement casts like in the following example. (I could be an expensive type, for example a bignum integer, but for the example it is only a wrapped i32.)

use az::{Az, Cast};
use core::borrow::Borrow;

struct I(i32);
impl Cast<i64> for &'_ I {
    fn cast(self) -> i64 { self.0.cast() }
}

let owned = I(12);
assert_eq!(owned.borrow().az::<i64>(), 12);

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:

[dependencies]
az = "1.2"

The crate requires rustc version 1.31.0 or later.

License

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

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 License, Version 2.0, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps