codec: move length delimited codec to tokio-codec (#1401)

This commit is contained in:
Gurwinder Singh
2019-08-07 12:10:05 -07:00
committed by Carl Lerche
parent cb2336ff3d
commit 7174c63bf9
3 changed files with 86 additions and 83 deletions
@@ -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<T: AsyncRead + AsyncWrite>(io: T)
//! -> Framed<T, LengthDelimitedCodec>
@@ -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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncWrite>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncRead>(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<T: AsyncWrite>(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<T: AsyncRead + AsyncWrite>(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 {}
+2
View File
@@ -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};
+2 -5
View File
@@ -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,
};