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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! 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 bytes array.
/// ```
/// use num_bytes::FromBytes;
/// let a = [8,0,0,0];
/// let b = i32::from_le_bytes(a);
/// assert_eq!(b, 8);
/// ```
/// `isize` and `usize` implementations compile to implement `FromBytes<2>`, `FromBytes<4>` or `FromBytes<8>` depending on your system.
///
/// It simply says `FromBytes<8>` in documentation because the machine which has compiled the documentation is 64 bit.
pub trait FromBytes<const T: usize>: Sized {
    fn from_le_bytes(bytes: [u8; T]) -> Self;
    fn from_be_bytes(bytes: [u8; T]) -> Self;
}

/// 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]);
/// ```
/// `isize` and `usize` implementations compile to implement `FromBytes<2>`, `FromBytes<4>` or `FromBytes<8>` depending on your system.
///
/// It simply says `FromBytes<8>` in documentation because the machine which has compiled the documentation is 64 bit.
pub trait IntoBytes<const T: usize>: Sized {
    fn into_le_bytes(self) -> [u8; T];
    fn into_be_bytes(self) -> [u8; T];
}

macro_rules! impl_from_bytes {
    ($T: ty, $N:tt) => {
        impl FromBytes<$N> for $T {
            fn from_le_bytes(bytes: [u8; $N]) -> Self {
                Self::from_le_bytes(bytes)
            }
            fn from_be_bytes(bytes: [u8; $N]) -> Self {
                Self::from_be_bytes(bytes)
            }
        }
    };
}
macro_rules! impl_try_from_bytes {
    ($T: ty) => {
        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: ty, $N:tt) => {
        impl IntoBytes<$N> for $T {
            fn into_le_bytes(self) -> [u8; $N] {
                self.to_le_bytes()
            }
            fn into_be_bytes(self) -> [u8; $N] {
                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, 1);
impl_into_bytes!(i16, 2);
impl_into_bytes!(i32, 4);
impl_into_bytes!(i64, 8);
impl_into_bytes!(u8, 1);
impl_into_bytes!(u16, 2);
impl_into_bytes!(u32, 4);
impl_into_bytes!(u64, 8);

impl_into_bytes!(f32, 4);
impl_into_bytes!(f64, 8);

#[cfg(target_pointer_width = "16")]
impl_into_bytes!(isize, 2);
#[cfg(target_pointer_width = "32")]
impl_into_bytes!(isize, 4);
#[cfg(target_pointer_width = "64")]
impl_into_bytes!(isize, 8);

#[cfg(target_pointer_width = "16")]
impl_into_bytes!(usize, 2);
#[cfg(target_pointer_width = "32")]
impl_into_bytes!(usize, 4);
#[cfg(target_pointer_width = "64")]
impl_into_bytes!(usize, 8);

// From implementations
impl_from_bytes!(i8, 1);
impl_from_bytes!(i16, 2);
impl_from_bytes!(i32, 4);
impl_from_bytes!(i64, 8);
impl_from_bytes!(u8, 1);
impl_from_bytes!(u16, 2);
impl_from_bytes!(u32, 4);
impl_from_bytes!(u64, 8);

impl_from_bytes!(f32, 4);
impl_from_bytes!(f64, 8);

#[cfg(target_pointer_width = "16")]
impl_from_bytes!(isize, 2);
#[cfg(target_pointer_width = "32")]
impl_from_bytes!(isize, 4);
#[cfg(target_pointer_width = "64")]
impl_from_bytes!(isize, 8);

#[cfg(target_pointer_width = "16")]
impl_from_bytes!(usize, 2);
#[cfg(target_pointer_width = "32")]
impl_from_bytes!(usize, 4);
#[cfg(target_pointer_width = "64")]
impl_from_bytes!(usize, 8);