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
use super::*;

impl<const N: usize> Transformable for [u8; N] {
  type Error = BytesTransformError;

  fn encode(&self, dst: &mut [u8]) -> Result<(), Self::Error> {
    if dst.len() < N {
      return Err(BytesTransformError::EncodeBufferTooSmall);
    }

    dst[..N].copy_from_slice(self);
    Ok(())
  }

  #[cfg(feature = "std")]
  #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
  fn encode_to_writer<W: std::io::Write>(&self, dst: &mut W) -> std::io::Result<()> {
    dst.write_all(self)
  }

  #[cfg(feature = "async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
  async fn encode_to_async_writer<W: futures_util::io::AsyncWrite + Send + Unpin>(
    &self,
    dst: &mut W,
  ) -> std::io::Result<()> {
    use futures_util::io::AsyncWriteExt;

    dst.write_all(self).await
  }

  fn encoded_len(&self) -> usize {
    N
  }

  fn decode(src: &[u8]) -> Result<(usize, Self), Self::Error>
  where
    Self: Sized,
  {
    let len = src.len();
    if len < N {
      return Err(BytesTransformError::Corrupted);
    }

    let mut buf = [0; N];
    buf.copy_from_slice(&src[..N]);

    Ok((N, buf))
  }

  #[cfg(feature = "std")]
  #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
  fn decode_from_reader<R: std::io::Read>(src: &mut R) -> std::io::Result<(usize, Self)>
  where
    Self: Sized,
  {
    let mut buf = [0u8; N];
    src.read_exact(&mut buf).map(|_| (N, buf))
  }

  #[cfg(feature = "async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
  async fn decode_from_async_reader<R: futures_util::io::AsyncRead + Send + Unpin>(
    src: &mut R,
  ) -> std::io::Result<(usize, Self)>
  where
    Self: Sized,
  {
    use futures_util::io::AsyncReadExt;

    let mut buf = [0u8; N];
    src.read_exact(&mut buf).await.map(|_| (N, buf))
  }
}