transformable/impls/vec.rs
1use super::*;
2
3impl Transformable for ::std::vec::Vec<u8> {
4 type Error = BytesTransformError;
5
6 fn encode(&self, dst: &mut [u8]) -> Result<usize, Self::Error> {
7 encode_bytes(self.as_ref(), dst).map_err(|_| Self::Error::EncodeBufferTooSmall)
8 }
9
10 /// Encodes the value into the given writer.
11 ///
12 /// # Note
13 /// The implementation of this method is not optimized, which means
14 /// if your writer is expensive (e.g. [`TcpStream`](std::net::TcpStream), [`File`](std::fs::File)),
15 /// it is better to use a [`BufWriter`](std::io::BufWriter)
16 /// to wrap your orginal writer to cut down the number of I/O times.
17 #[cfg(feature = "std")]
18 fn encode_to_writer<W: std::io::Write>(&self, dst: &mut W) -> std::io::Result<usize> {
19 encode_bytes_to(self.as_ref(), dst)
20 }
21
22 /// Encodes the value into the given async writer.
23 ///
24 /// # Note
25 /// The implementation of this method is not optimized, which means
26 /// if your writer is expensive (e.g. `TcpStream`, `File`),
27 /// it is better to use a [`BufWriter`](futures_util::io::BufWriter)
28 /// to wrap your orginal writer to cut down the number of I/O times.
29 #[cfg(feature = "async")]
30 async fn encode_to_async_writer<W: futures_util::io::AsyncWrite + Send + Unpin>(
31 &self,
32 dst: &mut W,
33 ) -> std::io::Result<usize> {
34 encode_bytes_to_async(self.as_ref(), dst).await
35 }
36
37 fn encoded_len(&self) -> usize {
38 encoded_bytes_len(self.as_ref())
39 }
40
41 fn decode(src: &[u8]) -> Result<(usize, Self), Self::Error>
42 where
43 Self: Sized,
44 {
45 decode_bytes(src).map_err(|_| Self::Error::NotEnoughBytes)
46 }
47
48 /// Decodes the value from the given reader.
49 ///
50 /// # Note
51 /// The implementation of this method is not optimized, which means
52 /// if your reader is expensive (e.g. [`TcpStream`](std::net::TcpStream), [`File`](std::fs::File)),
53 /// it is better to use a [`BufReader`](std::io::BufReader)
54 /// to wrap your orginal reader to cut down the number of I/O times.
55 #[cfg(feature = "std")]
56 fn decode_from_reader<R: std::io::Read>(src: &mut R) -> std::io::Result<(usize, Self)>
57 where
58 Self: Sized,
59 {
60 decode_bytes_from(src)
61 }
62
63 /// Decodes the value from the given async reader.
64 ///
65 /// # Note
66 /// The implementation of this method is not optimized, which means
67 /// if your reader is expensive (e.g. `TcpStream`, `File`),
68 /// it is better to use a [`BufReader`](futures_util::io::BufReader)
69 /// to wrap your orginal reader to cut down the number of I/O times.
70 #[cfg(feature = "async")]
71 async fn decode_from_async_reader<R: futures_util::io::AsyncRead + Send + Unpin>(
72 src: &mut R,
73 ) -> std::io::Result<(usize, Self)>
74 where
75 Self: Sized,
76 {
77 decode_bytes_from_async(src).await
78 }
79}
80
81test_transformable!(Vec<u8> => test_vec_transformable(std::vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0]));