tokio_util/codec/
bytes_codec.rs

1use crate::codec::decoder::Decoder;
2use crate::codec::encoder::Encoder;
3
4use bytes::{BufMut, Bytes, BytesMut};
5use std::io;
6
7/// A simple [`Decoder`] and [`Encoder`] implementation that just ships bytes around.
8///
9/// [`Decoder`]: crate::codec::Decoder
10/// [`Encoder`]: crate::codec::Encoder
11///
12/// # Example
13///
14/// Turn an [`AsyncRead`] into a stream of `Result<`[`BytesMut`]`, `[`Error`]`>`.
15///
16/// [`AsyncRead`]: tokio::io::AsyncRead
17/// [`BytesMut`]: bytes::BytesMut
18/// [`Error`]: std::io::Error
19///
20/// ```
21/// # mod hidden {
22/// # #[allow(unused_imports)]
23/// use tokio::fs::File;
24/// # }
25/// use tokio::io::AsyncRead;
26/// use tokio_util::codec::{FramedRead, BytesCodec};
27///
28/// # enum File {}
29/// # impl File {
30/// #     async fn open(_name: &str) -> Result<impl AsyncRead, std::io::Error> {
31/// #         use std::io::Cursor;
32/// #         Ok(Cursor::new(vec![0, 1, 2, 3, 4, 5]))
33/// #     }
34/// # }
35/// #
36/// # #[tokio::main(flavor = "current_thread")]
37/// # async fn main() -> Result<(), std::io::Error> {
38/// let my_async_read = File::open("filename.txt").await?;
39/// let my_stream_of_bytes = FramedRead::new(my_async_read, BytesCodec::new());
40/// # Ok(())
41/// # }
42/// ```
43///
44#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
45pub struct BytesCodec(());
46
47impl BytesCodec {
48    /// Creates a new `BytesCodec` for shipping around raw bytes.
49    pub fn new() -> BytesCodec {
50        BytesCodec(())
51    }
52}
53
54impl Decoder for BytesCodec {
55    type Item = BytesMut;
56    type Error = io::Error;
57
58    fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<BytesMut>, io::Error> {
59        if !buf.is_empty() {
60            let len = buf.len();
61            Ok(Some(buf.split_to(len)))
62        } else {
63            Ok(None)
64        }
65    }
66}
67
68impl Encoder<Bytes> for BytesCodec {
69    type Error = io::Error;
70
71    fn encode(&mut self, data: Bytes, buf: &mut BytesMut) -> Result<(), io::Error> {
72        buf.reserve(data.len());
73        buf.put(data);
74        Ok(())
75    }
76}
77
78impl Encoder<BytesMut> for BytesCodec {
79    type Error = io::Error;
80
81    fn encode(&mut self, data: BytesMut, buf: &mut BytesMut) -> Result<(), io::Error> {
82        buf.reserve(data.len());
83        buf.put(data);
84        Ok(())
85    }
86}