Create tokio-codec (#360)

Create a new tokio-codec crate with many of the contents of
`tokio_io::codec`.
This commit is contained in:
Bryan Burgers
2018-06-04 20:36:06 -07:00
committed by Carl Lerche
parent 3d7263d3a0
commit f723d10087
42 changed files with 1100 additions and 23 deletions
+3
View File
@@ -1,9 +1,12 @@
#![allow(deprecated)]
use bytes::{Bytes, BufMut, BytesMut};
use codec::{Encoder, Decoder};
use std::io;
/// A simple `Codec` implementation that just ships bytes around.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[deprecated(since = "0.1.7", note = "Moved to tokio-codec")]
pub struct BytesCodec(());
impl BytesCodec {
+31
View File
@@ -1,6 +1,11 @@
use std::io;
use bytes::BytesMut;
use {AsyncWrite, AsyncRead};
use super::encoder::Encoder;
use ::_tokio_codec::{framed, Framed};
/// Decoding of frames via buffers.
///
/// This trait is used when constructing an instance of `Framed` or
@@ -11,6 +16,9 @@ use bytes::BytesMut;
/// Implementations are able to track state on `self`, which enables
/// implementing stateful streaming parsers. In many cases, though, this type
/// will simply be a unit struct (e.g. `struct HttpDecoder`).
// Note: We can't deprecate this trait, because the deprecation carries through to tokio-codec, and
// there doesn't seem to be a way to un-deprecate the re-export.
pub trait Decoder {
/// The type of decoded frames.
type Item;
@@ -83,4 +91,27 @@ pub trait Decoder {
}
}
}
/// Provides a `Stream` and `Sink` interface for reading and writing to this
/// `Io` object, using `Decode` and `Encode` to read and write the raw data.
///
/// Raw I/O objects work with byte sequences, but higher-level code usually
/// wants to batch these into meaningful chunks, called "frames". This
/// method layers framing on top of an I/O object, by using the `Codec`
/// traits to handle encoding and decoding of messages frames. Note that
/// the incoming and outgoing frame types may be distinct.
///
/// This function returns a *single* object that is both `Stream` and
/// `Sink`; grouping this into a single object is often useful for layering
/// things like gzip or TLS, which require both read and write access to the
/// underlying object.
///
/// If you want to work more directly with the streams and sink, consider
/// calling `split` on the `Framed` returned by this method, which will
/// break them into separate objects, allowing them to interact more easily.
fn framed<T: AsyncRead + AsyncWrite + Sized>(self, io: T) -> Framed<T, Self>
where Self: Encoder + Sized,
{
framed(io, self)
}
}
+3
View File
@@ -3,6 +3,9 @@ use bytes::BytesMut;
/// Trait of helper objects to write out messages as bytes, for use with
/// `FramedWrite`.
// Note: We can't deprecate this trait, because the deprecation carries through to tokio-codec, and
// there doesn't seem to be a way to un-deprecate the re-export.
pub trait Encoder {
/// The type of items consumed by the `Encoder`
type Item;
+3
View File
@@ -1,9 +1,12 @@
#![allow(deprecated)]
use bytes::{BufMut, BytesMut};
use codec::{Encoder, Decoder};
use std::{io, str};
/// A simple `Codec` implementation that splits up data into lines.
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[deprecated(since = "0.1.7", note = "Moved to tokio-codec")]
pub struct LinesCodec {
// Stored index of the next index to examine for a `\n` character.
// This is used to optimize searching.
+8
View File
@@ -10,6 +10,14 @@
//! [`Stream`]: #
//! [transports]: #
// tokio_io::codec originally held all codec-related helpers. This is now intended to be in
// tokio_codec instead. However, for backward compatibility, this remains here. When the next major
// breaking change comes, `Encoder` and `Decoder` need to be moved to `tokio_codec`, and the rest
// of this module should be removed.
#![doc(hidden)]
#![allow(deprecated)]
mod decoder;
mod encoder;
mod bytes_codec;