From 7174c63bf941e1b882e2da10833e8ed5a4f72d3e Mon Sep 17 00:00:00 2001 From: Gurwinder Singh Date: Thu, 8 Aug 2019 00:40:05 +0530 Subject: [PATCH] codec: move length delimited codec to tokio-codec (#1401) --- .../src}/length_delimited.rs | 160 +++++++++--------- tokio-codec/src/lib.rs | 2 + tokio/src/codec/mod.rs | 7 +- 3 files changed, 86 insertions(+), 83 deletions(-) rename {tokio/src/codec => tokio-codec/src}/length_delimited.rs (88%) diff --git a/tokio/src/codec/length_delimited.rs b/tokio-codec/src/length_delimited.rs similarity index 88% rename from tokio/src/codec/length_delimited.rs rename to tokio-codec/src/length_delimited.rs index 565c7607f..f08f93176 100644 --- a/tokio/src/codec/length_delimited.rs +++ b/tokio-codec/src/length_delimited.rs @@ -15,8 +15,8 @@ //! byte stream into a stream of frames. //! //! ``` -//! use tokio::io::{AsyncRead, AsyncWrite}; -//! use tokio::codec::*; +//! use tokio_io::{AsyncRead, AsyncWrite}; +//! use tokio_codec::{Framed, LengthDelimitedCodec}; //! //! fn bind_transport(io: T) //! -> Framed @@ -41,9 +41,9 @@ //! ``` //! #![feature(async_await)] //! -//! use tokio::io::{AsyncRead, AsyncWrite}; -//! use tokio::codec::*; -//! use tokio::prelude::*; +//! use tokio_io::{AsyncRead, AsyncWrite}; +//! use tokio_codec::{Framed, LengthDelimitedCodec}; +//! use futures::SinkExt; //! //! use bytes::Bytes; //! @@ -81,10 +81,10 @@ //! frame head in the yielded `BytesMut`. //! //! ``` -//! # use tokio::io::AsyncRead; -//! # use tokio::codec::length_delimited; +//! # use tokio_io::AsyncRead; +//! # use tokio_codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { -//! length_delimited::Builder::new() +//! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value //! .length_field_length(2) //! .length_adjustment(0) // default value @@ -115,10 +115,10 @@ //! frame head in the yielded `BytesMut`. //! //! ``` -//! # use tokio::io::AsyncRead; -//! # use tokio::codec::length_delimited; +//! # use tokio_io::AsyncRead; +//! # use tokio_codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { -//! length_delimited::Builder::new() +//! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value //! .length_field_length(2) //! .length_adjustment(0) // default value @@ -147,10 +147,10 @@ //! **includes** the frame head length. //! //! ``` -//! # use tokio::io::AsyncRead; -//! # use tokio::codec::length_delimited; +//! # use tokio_io::AsyncRead; +//! # use tokio_codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { -//! length_delimited::Builder::new() +//! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value //! .length_field_length(2) //! .length_adjustment(-2) // size of head @@ -181,10 +181,10 @@ //! frame head, including the frame head in the yielded `BytesMut`. //! //! ``` -//! # use tokio::io::AsyncRead; -//! # use tokio::codec::length_delimited; +//! # use tokio_io::AsyncRead; +//! # use tokio_codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { -//! length_delimited::Builder::new() +//! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value //! .length_field_length(3) //! .length_adjustment(2) // remaining head @@ -225,10 +225,10 @@ //! included. //! //! ``` -//! # use tokio::io::AsyncRead; -//! # use tokio::codec::length_delimited; +//! # use tokio_io::AsyncRead; +//! # use tokio_codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { -//! length_delimited::Builder::new() +//! LengthDelimitedCodec::builder() //! .length_field_offset(1) // length of hdr1 //! .length_field_length(2) //! .length_adjustment(1) // length of hdr2 @@ -271,10 +271,10 @@ //! length. //! //! ``` -//! # use tokio::io::AsyncRead; -//! # use tokio::codec::length_delimited; +//! # use tokio_io::AsyncRead; +//! # use tokio_codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { -//! length_delimited::Builder::new() +//! LengthDelimitedCodec::builder() //! .length_field_offset(1) // length of hdr1 //! .length_field_length(2) //! .length_adjustment(-3) // length of hdr1 + LEN, negative @@ -316,12 +316,12 @@ //! configuration: //! //! ``` -//! # use tokio::io::AsyncWrite; -//! # use tokio::codec::length_delimited; +//! # use tokio_io::AsyncWrite; +//! # use tokio_codec::LengthDelimitedCodec; //! # use bytes::BytesMut; //! # fn write_frame(io: T) { //! # let _ = -//! length_delimited::Builder::new() +//! LengthDelimitedCodec::builder() //! .length_field_length(2) //! .new_write(io); //! # } @@ -344,14 +344,12 @@ //! [`Encoder`]: ../trait.Encoder.html //! [`BytesMut`]: https://docs.rs/bytes/0.4/bytes/struct.BytesMut.html -use crate::{ - codec::{Decoder, Encoder, Framed, FramedRead, FramedWrite}, - io::{AsyncRead, AsyncWrite}, -}; +use crate::{Decoder, Encoder, Framed, FramedRead, FramedWrite}; use bytes::{Buf, BufMut, Bytes, BytesMut, IntoBuf}; use std::error::Error as StdError; use std::io::{self, Cursor}; use std::{cmp, fmt}; +use tokio_io::{AsyncRead, AsyncWrite}; /// Configure length delimited `LengthDelimitedCodec`s. /// @@ -381,7 +379,7 @@ pub struct Builder { } /// An error when the number of bytes read is more than max frame length. -pub struct FrameTooBig { +pub struct LengthDelimitedCodecError { _priv: (), } @@ -419,6 +417,12 @@ impl LengthDelimitedCodec { } } + /// Creates a new length delimited codec builder with default configuration + /// values. + pub fn builder() -> Builder { + Builder::new() + } + /// Returns the current max frame setting /// /// This is the largest size this codec will accept from the wire. Larger @@ -462,7 +466,7 @@ impl LengthDelimitedCodec { if n > self.builder.max_frame_len as u64 { return Err(io::Error::new( io::ErrorKind::InvalidData, - FrameTooBig { _priv: () }, + LengthDelimitedCodecError { _priv: () }, )); } @@ -553,7 +557,7 @@ impl Encoder for LengthDelimitedCodec { if n > self.builder.max_frame_len { return Err(io::Error::new( io::ErrorKind::InvalidInput, - FrameTooBig { _priv: () }, + LengthDelimitedCodecError { _priv: () }, )); } @@ -603,11 +607,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .length_field_offset(0) /// .length_field_length(2) /// .length_adjustment(0) @@ -647,11 +651,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .big_endian() /// .new_read(io); /// # } @@ -671,11 +675,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .little_endian() /// .new_read(io); /// # } @@ -695,11 +699,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .native_endian() /// .new_read(io); /// # } @@ -724,16 +728,16 @@ impl Builder { /// setting. /// /// When frames exceed the max length, an `io::Error` with the custom value - /// of the `FrameTooBig` type will be returned. + /// of the `LengthDelimitedCodecError` type will be returned. /// /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .max_frame_length(8 * 1024) /// .new_read(io); /// # } @@ -753,11 +757,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .length_field_length(4) /// .new_read(io); /// # } @@ -776,11 +780,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .length_field_offset(1) /// .new_read(io); /// # } @@ -797,11 +801,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .length_adjustment(-2) /// .new_read(io); /// # } @@ -821,11 +825,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .num_skip(4) /// .new_read(io); /// # } @@ -841,10 +845,10 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// # pub fn main() { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .length_field_offset(0) /// .length_field_length(2) /// .length_adjustment(0) @@ -864,11 +868,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncRead; - /// use tokio::codec::length_delimited::Builder; + /// # use tokio_io::AsyncRead; + /// use tokio_codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { - /// Builder::new() + /// LengthDelimitedCodec::builder() /// .length_field_offset(0) /// .length_field_length(2) /// .length_adjustment(0) @@ -889,11 +893,11 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::AsyncWrite; - /// # use tokio::codec::length_delimited; + /// # use tokio_io::AsyncWrite; + /// # use tokio_codec::LengthDelimitedCodec; /// # use bytes::BytesMut; /// # fn write_frame(io: T) { - /// length_delimited::Builder::new() + /// LengthDelimitedCodec::builder() /// .length_field_length(2) /// .new_write(io); /// # } @@ -911,12 +915,12 @@ impl Builder { /// # Examples /// /// ``` - /// # use tokio::io::{AsyncRead, AsyncWrite}; - /// # use tokio::codec::length_delimited; + /// # use tokio_io::{AsyncRead, AsyncWrite}; + /// # use tokio_codec::LengthDelimitedCodec; /// # use bytes::BytesMut; /// # fn write_frame(io: T) { /// # let _ = - /// length_delimited::Builder::new() + /// LengthDelimitedCodec::builder() /// .length_field_length(2) /// .new_framed(io); /// # } @@ -946,18 +950,18 @@ impl Default for Builder { } } -// ===== impl FrameTooBig ===== +// ===== impl LengthDelimitedCodecError ===== -impl fmt::Debug for FrameTooBig { +impl fmt::Debug for LengthDelimitedCodecError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("FrameTooBig").finish() + f.debug_struct("LengthDelimitedCodecError").finish() } } -impl fmt::Display for FrameTooBig { +impl fmt::Display for LengthDelimitedCodecError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("frame size too big") } } -impl StdError for FrameTooBig {} +impl StdError for LengthDelimitedCodecError {} diff --git a/tokio-codec/src/lib.rs b/tokio-codec/src/lib.rs index 437e4d286..956fc362a 100644 --- a/tokio-codec/src/lib.rs +++ b/tokio-codec/src/lib.rs @@ -23,6 +23,7 @@ mod encoder; mod framed; mod framed_read; mod framed_write; +pub mod length_delimited; mod lines_codec; pub use crate::bytes_codec::BytesCodec; @@ -31,4 +32,5 @@ pub use crate::encoder::Encoder; pub use crate::framed::{Framed, FramedParts}; pub use crate::framed_read::FramedRead; pub use crate::framed_write::FramedWrite; +pub use crate::length_delimited::{LengthDelimitedCodec, LengthDelimitedCodecError}; pub use crate::lines_codec::{LinesCodec, LinesCodecError}; diff --git a/tokio/src/codec/mod.rs b/tokio/src/codec/mod.rs index 30109bf36..1f4a33a5f 100644 --- a/tokio/src/codec/mod.rs +++ b/tokio/src/codec/mod.rs @@ -10,10 +10,7 @@ //! [`Stream`]: https://docs.rs/futures/0.1/futures/stream/trait.Stream.html //! [transports]: https://tokio.rs/docs/going-deeper/frames/ -pub mod length_delimited; - -pub use self::length_delimited::LengthDelimitedCodec; pub use tokio_codec::{ - BytesCodec, Decoder, Encoder, Framed, FramedParts, FramedRead, FramedWrite, LinesCodec, - LinesCodecError, + length_delimited, BytesCodec, Decoder, Encoder, Framed, FramedParts, FramedRead, FramedWrite, + LengthDelimitedCodec, LengthDelimitedCodecError, LinesCodec, LinesCodecError, };