1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! A crate to allow converting generic primitives into bytes.
//!
//! Defines `TryFromBytes` and `IntoBytes` traits and implements them on all numerical primitives.

use std::{array::TryFromSliceError, convert::TryInto, u8};

/// Defines a type can be converted from a byte slice.
/// ```
/// use num_bytes::TryFromBytes;
/// let a = [8,0,0,0];
/// let b = u32::try_from_le_bytes(&a).unwrap();
/// assert_eq!(b,8);
/// ```
pub trait TryFromBytes: Sized {
    fn try_from_le_bytes(bytes: &[u8]) -> Result<Self, TryFromSliceError>;
    fn try_from_be_bytes(bytes: &[u8]) -> Result<Self, TryFromSliceError>;
}
/// Defines a type can be converted into a byte vector.
/// ```
/// use num_bytes::IntoBytes;
/// let a = 8u32;
/// let b = a.into_le_bytes();
/// assert_eq!(b,[8,0,0,0]);
/// ```
pub trait IntoBytes: Sized {
    fn into_le_bytes(self) -> Vec<u8>;
    fn into_be_bytes(self) -> Vec<u8>;
}
macro_rules! impl_try_from_bytes {
    ($T: ident) => {
        impl TryFromBytes for $T {
            fn try_from_le_bytes(bytes: &[u8]) -> Result<Self, TryFromSliceError> {
                Ok(Self::from_le_bytes(bytes.try_into()?))
            }
            fn try_from_be_bytes(bytes: &[u8]) -> Result<Self, TryFromSliceError> {
                Ok(Self::from_be_bytes(bytes.try_into()?))
            }
        }
    };
}
macro_rules! impl_into_bytes {
    ($T: ident) => {
        impl IntoBytes for $T {
            fn into_le_bytes(self) -> Vec<u8> {
                Vec::from(self.to_le_bytes())
            }
            fn into_be_bytes(self) -> Vec<u8> {
                Vec::from(self.to_be_bytes())
            }
        }
    };
}

// Try from implementations.
impl_try_from_bytes!(i8);
impl_try_from_bytes!(i16);
impl_try_from_bytes!(i32);
impl_try_from_bytes!(i64);
impl_try_from_bytes!(u8);
impl_try_from_bytes!(u16);
impl_try_from_bytes!(u32);
impl_try_from_bytes!(u64);

impl_try_from_bytes!(f32);
impl_try_from_bytes!(f64);

impl_try_from_bytes!(isize);
impl_try_from_bytes!(usize);

// Into implementations.
impl_into_bytes!(i8);
impl_into_bytes!(i16);
impl_into_bytes!(i32);
impl_into_bytes!(i64);
impl_into_bytes!(u8);
impl_into_bytes!(u16);
impl_into_bytes!(u32);
impl_into_bytes!(u64);

impl_into_bytes!(f32);
impl_into_bytes!(f64);

impl_into_bytes!(isize);
impl_into_bytes!(usize);