#cast #primitive-integer #primitive #truncate #no-alloc #widen

no-std explicit_cast

A crate for explicit widening, truncating, and sign casting of primitive integers

1 stable release

1.0.0 Dec 7, 2023

#2982 in Rust patterns

Apache-2.0 OR MIT

10KB
118 lines

explicit_cast

A crate for explicitly widening, truncating, and sign casting integers.
This crate is very simple, but the functionality it provides is universal, as such, it is distributed under Apache-2.0 OR MIT.

Usage

use explict_cast::prelude::*;

let my_value: u8 = u16::MAX.truncate();
let next: i16 = my_value.widen().sign_cast();

assert_eq!(next, 0xffi16);

lib.rs:

A crate that provides a simple interface to primitive ints to allow explicit widening, truncating, and sign casting.

To get started, import the prelude:

use explicit_cast::prelude::*;

assert_eq!(5u8.widen::<u16>().sign_cast().widen::<i32>().truncate::<i8>(), 5i8);

Stability

This crate is 1.0 as in being stable and or finished, as there is no other functionality to be had than allowing explicit casting of integers. As such, a prelude has been included that imports Widen, Truncate, and SignCast for you. No new methods will be added to these traits, and no new traits will be added to the prelude, without a 2.0 release, that theoretically should never happen.

Documentation updates may be published under a 1.0.X patch release, but no new functionality is planned. The prelude to this crate, includes SignCast, Truncate, and Widen imported for you

No runtime deps