mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-21 00:00:10 +02:00
util: add links to tokio-util + example to BytesCodec (#2207)
This commit is contained in:
@@ -4,7 +4,43 @@ use crate::codec::encoder::Encoder;
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use std::io;
|
||||
|
||||
/// A simple `Codec` implementation that just ships bytes around.
|
||||
/// A simple [`Decoder`] and [`Encoder`] implementation that just ships bytes around.
|
||||
///
|
||||
/// [`Decoder`]: crate::codec::Decoder
|
||||
/// [`Encoder`]: crate::codec::Encoder
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// Turn an [`AsyncRead`] into a stream of `Result<`[`BytesMut`]`, `[`Error`]`>`.
|
||||
///
|
||||
/// [`AsyncRead`]: tokio::io::AsyncRead
|
||||
/// [`BytesMut`]: bytes::BytesMut
|
||||
/// [`Error`]: std::io::Error
|
||||
///
|
||||
/// ```
|
||||
/// # mod hidden {
|
||||
/// # #[allow(unused_imports)]
|
||||
/// use tokio::fs::File;
|
||||
/// # }
|
||||
/// use tokio::io::AsyncRead;
|
||||
/// use tokio_util::codec::{FramedRead, BytesCodec};
|
||||
///
|
||||
/// # enum File {}
|
||||
/// # impl File {
|
||||
/// # async fn open(_name: &str) -> Result<impl AsyncRead, std::io::Error> {
|
||||
/// # use std::io::Cursor;
|
||||
/// # Ok(Cursor::new(vec![0, 1, 2, 3, 4, 5]))
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// # #[tokio::main(core_threads = 1)]
|
||||
/// # async fn main() -> Result<(), std::io::Error> {
|
||||
/// let my_async_read = File::open("filename.txt").await?;
|
||||
/// let my_stream_of_bytes = FramedRead::new(my_async_read, BytesCodec::new());
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
|
||||
pub struct BytesCodec(());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user