num_convert

Rust library for converting integers.

Description

Type converting library.

This library provide a way to convert from one type to another type.

Trait ToByAdd and FromByAdd

Trait TryToByAdd and TryFromByAdd

Trait IntoAs and FromAs

Usage

Add this to your Cargo.toml

[dependencies] num_convert = { git = "https://github.com/pic16f877ccs/num_convert", version = "0.3.2" }

Or using cargo

``` cargo add numconvert --git "https://github.com/pic16f877ccs/numconvert"

```

Examples

``` use num_convert::ToByAdd;

fn convertintou8(min: T, max: T) -> (u8, u8) { (min.intou8(), max.intou8()) } asserteq!((u8::MIN, u8::MAX), convertinto_u8(i8::MIN, i8::MAX));

asserteq!(i128::MIN, ToByAdd::intoi128(u128::MIN)); asserteq!(u8::MAX as u64, ToByAdd::intou64(i8::MAX)); ```

``` use num_convert::TryToByAdd;

fn convertintou8(min: T, max: T) -> (u8, u8) where T: TryToByAdd, {

 (min.try_into_u8().unwrap(), max.try_into_u8().unwrap())

} asserteq!((u8::MIN, u8::MAX), convertintou8(i8::MIN, i8::MAX)); asserteq!((u8::MIN, u8::MAX), convertintou8(i8::MIN as i64, i8::MAX as i64));

asserteq!(u8::MAX, TryToByAdd::tryintou8(i8::MAX).unwrap()); asserteq!(i8::MIN, TryToByAdd::tryintoi8(u8::MIN).unwrap()); asserteq!(u8::MIN, TryToByAdd::tryinto_u8(u8::MIN).unwrap()); ```

``` use num_convert:: { FromAs, IntoAs };

asserteq!(>::fromas(258u16), 2u8);
asserteq!(>::intoas(258u16), 2u8); ```

License

GNU General Public License v3.0