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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use super::*;

#[cfg(any(feature = "alloc", feature = "std"))]
mod bytes;

#[cfg(any(feature = "alloc", feature = "std"))]
mod string;
#[cfg(any(feature = "alloc", feature = "std"))]
pub use string::*;

#[cfg(any(feature = "alloc", feature = "std"))]
mod vec;

#[cfg(feature = "std")]
mod net;
#[cfg(feature = "std")]
pub use net::*;

mod time;
pub use time::*;

mod numbers;
pub use numbers::*;

#[cfg(feature = "smallvec")]
mod smallvec;

mod bytes_array;

use byteorder::{ByteOrder, NetworkEndian};

trait ToNetworkEndian {
  fn to_network_endian(&self) -> impl AsRef<[u8]>;

  fn from_network_endian(src: &[u8]) -> Self;
}

macro_rules! impl_network_endian {
  ($($ty:ident), + $(,)?) => {
    $(
      impl ToNetworkEndian for $ty {
        fn to_network_endian(&self) -> impl AsRef<[u8]> {
          let mut buf = [0; core::mem::size_of::<$ty>()];
          paste::paste! {
            NetworkEndian::[< write_ $ty >](&mut buf, *self);
          }
          buf
        }

        fn from_network_endian(src: &[u8]) -> Self {
          paste::paste! {
            NetworkEndian::[< read_ $ty >](src)
          }
        }
      }
    )*
  };
}

impl ToNetworkEndian for u8 {
  fn to_network_endian(&self) -> impl AsRef<[u8]> {
    [*self]
  }

  fn from_network_endian(src: &[u8]) -> Self {
    src[0]
  }
}

impl ToNetworkEndian for i8 {
  fn to_network_endian(&self) -> impl AsRef<[u8]> {
    [*self as u8]
  }

  fn from_network_endian(src: &[u8]) -> Self {
    src[0] as i8
  }
}

impl_network_endian!(u16, u32, u64, u128, i16, i32, i64, i128,);

/// The error type for errors that get returned when encoding or decoding fails.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum BytesTransformError {
  /// Returned when the buffer is too small to encode.
  #[cfg_attr(feature = "std", error(
    "buffer is too small, use `Transformable::encoded_len` to pre-allocate a buffer with enough space"
  ))]
  EncodeBufferTooSmall,
  /// Returned when the bytes are corrupted.
  #[cfg_attr(feature = "std", error("not enough bytes to decode"))]
  NotEnoughBytes,
}

#[cfg(not(feature = "std"))]
impl core::fmt::Display for BytesTransformError {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self {
      Self::EncodeBufferTooSmall => write!(
        f,
        "buffer is too small, use `Transformable::encoded_len` to pre-allocate a buffer with enough space"
      ),
      Self::NotEnoughBytes => write!(f, "not enough bytes to decode"),
    }
  }
}

#[cfg(all(feature = "std", feature = "async"))]
async fn decode_bytes_from_async<R: futures_util::io::AsyncRead + Unpin>(
  src: &mut R,
) -> std::io::Result<(usize, Vec<u8>)> {
  use futures_util::io::AsyncReadExt;

  let mut len_buf = [0u8; MESSAGE_SIZE_LEN];
  src.read_exact(&mut len_buf).await?;
  let len = u32::from_network_endian(&len_buf) as usize;
  let mut buf = vec![0u8; len];
  src
    .read_exact(&mut buf)
    .await
    .map(|_| (len + MESSAGE_SIZE_LEN, buf))
}

#[cfg(feature = "std")]
fn decode_bytes_from<R: std::io::Read>(src: &mut R) -> std::io::Result<(usize, Vec<u8>)> {
  let mut len_buf = [0u8; MESSAGE_SIZE_LEN];
  src.read_exact(&mut len_buf)?;
  let len = u32::from_network_endian(&len_buf) as usize;
  let mut buf = vec![0u8; len];
  src
    .read_exact(&mut buf)
    .map(|_| (MESSAGE_SIZE_LEN + len, buf))
}

#[cfg(any(feature = "alloc", feature = "std"))]
fn decode_bytes(src: &[u8]) -> Result<(usize, Vec<u8>), ()> {
  let len = src.len();
  if len < MESSAGE_SIZE_LEN {
    return Err(());
  }

  let data_len = u32::from_network_endian(&src[..MESSAGE_SIZE_LEN]) as usize;
  if data_len > len - MESSAGE_SIZE_LEN {
    return Err(());
  }

  let total_len = MESSAGE_SIZE_LEN + data_len;
  Ok((
    total_len,
    src[MESSAGE_SIZE_LEN..MESSAGE_SIZE_LEN + data_len].to_vec(),
  ))
}

#[cfg(any(feature = "alloc", feature = "std"))]
fn encode_bytes(src: &[u8], dst: &mut [u8]) -> Result<usize, ()> {
  let encoded_len = encoded_bytes_len(src);
  if dst.len() < encoded_len {
    return Err(());
  }
  let src_len = src.len();
  NetworkEndian::write_u32(&mut dst[..MESSAGE_SIZE_LEN], src_len as u32);
  dst[MESSAGE_SIZE_LEN..MESSAGE_SIZE_LEN + src_len].copy_from_slice(src);
  Ok(MESSAGE_SIZE_LEN + src_len)
}

#[cfg(feature = "std")]
fn encode_bytes_to<W: std::io::Write>(src: &[u8], dst: &mut W) -> std::io::Result<usize> {
  let len = src.len();
  if len + MESSAGE_SIZE_LEN <= MAX_INLINED_BYTES {
    let mut buf = [0u8; MAX_INLINED_BYTES];
    NetworkEndian::write_u32(&mut buf[..MESSAGE_SIZE_LEN], len as u32);
    buf[MESSAGE_SIZE_LEN..MESSAGE_SIZE_LEN + len].copy_from_slice(src);
    dst
      .write_all(&buf[..MESSAGE_SIZE_LEN + len])
      .map(|_| MESSAGE_SIZE_LEN + len)
  } else {
    let mut buf = std::vec![0; MESSAGE_SIZE_LEN + len];
    NetworkEndian::write_u32(&mut buf[..MESSAGE_SIZE_LEN], len as u32);
    buf[MESSAGE_SIZE_LEN..].copy_from_slice(src);
    dst.write_all(&buf).map(|_| MESSAGE_SIZE_LEN + len)
  }
}

#[cfg(all(feature = "std", feature = "async"))]
async fn encode_bytes_to_async<W: futures_util::io::AsyncWrite + Unpin>(
  src: &[u8],
  dst: &mut W,
) -> std::io::Result<usize> {
  use futures_util::io::AsyncWriteExt;

  let len = src.len();
  if len + MESSAGE_SIZE_LEN <= MAX_INLINED_BYTES {
    let mut buf = [0u8; MAX_INLINED_BYTES];
    NetworkEndian::write_u32(&mut buf[..MESSAGE_SIZE_LEN], len as u32);
    buf[MESSAGE_SIZE_LEN..MESSAGE_SIZE_LEN + len].copy_from_slice(src);
    dst
      .write_all(&buf[..MESSAGE_SIZE_LEN + len])
      .await
      .map(|_| MESSAGE_SIZE_LEN + len)
  } else {
    let mut buf = std::vec![0; MESSAGE_SIZE_LEN + len];
    NetworkEndian::write_u32(&mut buf[..MESSAGE_SIZE_LEN], len as u32);
    buf[MESSAGE_SIZE_LEN..].copy_from_slice(src);
    dst.write_all(&buf).await.map(|_| MESSAGE_SIZE_LEN + len)
  }
}

#[cfg(any(feature = "alloc", feature = "std"))]
fn encoded_bytes_len(src: &[u8]) -> usize {
  MESSAGE_SIZE_LEN + src.len()
}