transformable/impls/
bytes_array.rs1use super::*;
2
3impl<const N: usize> Transformable for [u8; N] {
4 type Error = BytesTransformError;
5
6 fn encode(&self, dst: &mut [u8]) -> Result<usize, Self::Error> {
7 if dst.len() < N {
8 return Err(BytesTransformError::EncodeBufferTooSmall);
9 }
10
11 dst[..N].copy_from_slice(self);
12 Ok(N)
13 }
14
15 #[cfg(feature = "std")]
16 fn encode_to_writer<W: std::io::Write>(&self, dst: &mut W) -> std::io::Result<usize> {
17 dst.write_all(self).map(|_| N)
18 }
19
20 #[cfg(feature = "async")]
21 async fn encode_to_async_writer<W: futures_util::io::AsyncWrite + Send + Unpin>(
22 &self,
23 dst: &mut W,
24 ) -> std::io::Result<usize> {
25 use futures_util::io::AsyncWriteExt;
26
27 dst.write_all(self).await.map(|_| N)
28 }
29
30 fn encoded_len(&self) -> usize {
31 N
32 }
33
34 fn decode(src: &[u8]) -> Result<(usize, Self), Self::Error>
35 where
36 Self: Sized,
37 {
38 let len = src.len();
39 if len < N {
40 return Err(BytesTransformError::NotEnoughBytes);
41 }
42
43 let mut buf = [0; N];
44 buf.copy_from_slice(&src[..N]);
45
46 Ok((N, buf))
47 }
48
49 #[cfg(feature = "std")]
50 fn decode_from_reader<R: std::io::Read>(src: &mut R) -> std::io::Result<(usize, Self)>
51 where
52 Self: Sized,
53 {
54 let mut buf = [0u8; N];
55 src.read_exact(&mut buf).map(|_| (N, buf))
56 }
57
58 #[cfg(feature = "async")]
59 async fn decode_from_async_reader<R: futures_util::io::AsyncRead + Send + Unpin>(
60 src: &mut R,
61 ) -> std::io::Result<(usize, Self)>
62 where
63 Self: Sized,
64 {
65 use futures_util::io::AsyncReadExt;
66
67 let mut buf = [0u8; N];
68 src.read_exact(&mut buf).await.map(|_| (N, buf))
69 }
70}