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
- Convert to or from signed integers to unsigned in the full range of values.
- Convert to or from unsigned integers to signed in the full range of values.
- Supports generics types.
- Not all value types support conversions.
Trait TryToByAdd and TryFromByAdd
- Convert into or from signed integers to unsigned in the full range of values or possible values.
- Convert into or from unsigned integers to signed in the full range of values or possible values.
- Convert into or from signed integers to signed in the full range of values or possible values.
- Convert into or from unsigned integers to unsigned in the full range of values or possible values.
- Supports generics types.
Trait IntoAs and FromAs
- Signed and unsigned casts, similar to the as operator.
- Supports generics types.
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