4 releases (breaking)
0.4.0 | Aug 22, 2021 |
---|---|
0.4.0-beta |
|
0.3.0 | Aug 6, 2021 |
0.2.0 | Aug 6, 2021 |
0.1.0 | Aug 6, 2021 |
#12 in #le
32 downloads per month
Used in 2 crates
6KB
76 lines
num-bytes
Traits for converting primitives into bytes.
Kinda like a bad extension to num.
Usage is a little tricky:
let a = [8,0];
let b: i16 = from_generic(a);
assert_eq!(b,8);
fn from_generic<T: num_bytes::FromBytes<N>, const N: usize>(x: [u8;N]) -> T {
T::from_le_bytes(x)
}
and:
let a = 8i16;
let b = into_generic(a);
assert_eq!(b,[8,0]);
fn into_generic<T: num_bytes::IntoBytes<N>, const N: usize>(x: T) -> [u8;N] {
x.into_le_bytes()
}
lib.rs
:
A crate to allow converting generic from/into bytes.
Defines FromBytes
and IntoBytes
traits.
Implements them on all numerical primitives.
Usage is a little tricky:
let a = [8,0];
let b: i16 = from_generic(a);
assert_eq!(b,8);
fn from_generic<T: num_bytes::FromBytes<N>, const N: usize>(x: [u8;N]) -> T {
T::from_le_bytes(x)
}
let a = 8i16;
let b = into_generic(a);
assert_eq!(b,[8,0]);
fn into_generic<T: num_bytes::IntoBytes<N>, const N: usize>(x: T) -> [u8;N] {
x.into_le_bytes()
}