From 48e61fcbe84bf3e35589c2acab66714bbe2aa628 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Mon, 21 Oct 2019 11:44:02 -0700 Subject: [PATCH] codec: move into tokio-util Related to #1318, Tokio APIs that are "less stable" are moved into a new `tokio-util` crate. This crate will mirror `tokio` and provide additional APIs that may require a greater rate of breaking changes. --- Cargo.toml | 4 +- azure-pipelines.yml | 2 +- ci/patch.toml | 2 +- tokio-codec/CHANGELOG.md | 35 ------------- tokio-net/Cargo.toml | 1 - tokio-net/src/udp/mod.rs | 2 - tokio-net/src/udp/socket.rs | 29 +++++------ tokio-net/src/udp/split.rs | 8 +-- tokio-util/CHANGELOG.md | 0 {tokio-codec => tokio-util}/Cargo.toml | 7 +-- {tokio-codec => tokio-util}/LICENSE | 0 {tokio-codec => tokio-util}/README.md | 2 +- .../src/codec}/bytes_codec.rs | 5 +- .../src => tokio-util/src/codec}/decoder.rs | 12 ++--- .../src => tokio-util/src/codec}/encoder.rs | 0 .../src => tokio-util/src/codec}/framed.rs | 10 ++-- .../src/codec}/framed_read.rs | 4 +- .../src/codec}/framed_write.rs | 8 ++- .../src/codec}/length_delimited.rs | 50 ++++++++++--------- .../src/codec}/lines_codec.rs | 9 ++-- .../src => tokio-util/src/codec}/macros.rs | 0 .../src/lib.rs => tokio-util/src/codec/mod.rs | 37 ++++++-------- tokio-util/src/lib.rs | 17 +++++++ {tokio-net => tokio-util}/src/udp/frame.rs | 26 ++-------- tokio-util/src/udp/mod.rs | 4 ++ {tokio-codec => tokio-util}/tests/codecs.rs | 3 +- {tokio-codec => tokio-util}/tests/framed.rs | 2 +- .../tests/framed_read.rs | 2 +- .../tests/framed_write.rs | 2 +- .../tests/length_delimited.rs | 2 +- tokio-util/tests/udp.rs | 1 + tokio/Cargo.toml | 5 +- tokio/src/net.rs | 4 +- 33 files changed, 127 insertions(+), 168 deletions(-) delete mode 100644 tokio-codec/CHANGELOG.md create mode 100644 tokio-util/CHANGELOG.md rename {tokio-codec => tokio-util}/Cargo.toml (81%) rename {tokio-codec => tokio-util}/LICENSE (100%) rename {tokio-codec => tokio-util}/README.md (95%) rename {tokio-codec/src => tokio-util/src/codec}/bytes_codec.rs (92%) rename {tokio-codec/src => tokio-util/src/codec}/decoder.rs (98%) rename {tokio-codec/src => tokio-util/src/codec}/encoder.rs (100%) rename {tokio-codec/src => tokio-util/src/codec}/framed.rs (97%) rename {tokio-codec/src => tokio-util/src/codec}/framed_read.rs (99%) rename {tokio-codec/src => tokio-util/src/codec}/framed_write.rs (98%) rename {tokio-codec/src => tokio-util/src/codec}/length_delimited.rs (95%) rename {tokio-codec/src => tokio-util/src/codec}/lines_codec.rs (97%) rename {tokio-codec/src => tokio-util/src/codec}/macros.rs (100%) rename tokio-codec/src/lib.rs => tokio-util/src/codec/mod.rs (50%) create mode 100644 tokio-util/src/lib.rs rename {tokio-net => tokio-util}/src/udp/frame.rs (86%) create mode 100644 tokio-util/src/udp/mod.rs rename {tokio-codec => tokio-util}/tests/codecs.rs (98%) rename {tokio-codec => tokio-util}/tests/framed.rs (97%) rename {tokio-codec => tokio-util}/tests/framed_read.rs (99%) rename {tokio-codec => tokio-util}/tests/framed_write.rs (99%) rename {tokio-codec => tokio-util}/tests/length_delimited.rs (99%) create mode 100644 tokio-util/tests/udp.rs diff --git a/Cargo.toml b/Cargo.toml index 05e3de22c00..95a87226248 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,6 @@ members = [ "tokio", - "tokio-codec", "tokio-executor", "tokio-fs", "tokio-io", @@ -12,5 +11,8 @@ members = [ "tokio-test", "tokio-timer", "tokio-tls", + "tokio-util", + + # Crates used by tests "build-tests", ] diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 0d7c1077a06..275f9493b88 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -62,7 +62,6 @@ jobs: displayName: Test sub crates - rust: beta crates: - tokio-codec: [] tokio-executor: - current-thread - thread-pool @@ -74,6 +73,7 @@ jobs: tokio-timer: - async-traits tokio-test: [] + tokio-util: [] # Test compilation failure - template: ci/azure-test-stable.yml diff --git a/ci/patch.toml b/ci/patch.toml index e615fab9bd1..e7ae4d34ba4 100644 --- a/ci/patch.toml +++ b/ci/patch.toml @@ -2,7 +2,6 @@ # repository. [patch.crates-io] tokio = { path = "tokio" } -tokio-codec = { path = "tokio-codec" } tokio-executor = { path = "tokio-executor" } tokio-fs = { path = "tokio-fs" } tokio-io = { path = "tokio-io" } @@ -11,3 +10,4 @@ tokio-net = { path = "tokio-net" } tokio-sync = { path = "tokio-sync" } tokio-timer = { path = "tokio-timer" } tokio-tls = { path = "tokio-tls" } +tokio-util = { path = "tokio-util" } diff --git a/tokio-codec/CHANGELOG.md b/tokio-codec/CHANGELOG.md deleted file mode 100644 index 2f0ca5f59b2..00000000000 --- a/tokio-codec/CHANGELOG.md +++ /dev/null @@ -1,35 +0,0 @@ -# 0.2.0-alpha.6 (September 30, 2019) - -- Move to `futures-*-preview 0.3.0-alpha.19` -- Move to `pin-project 0.4` - -# 0.2.0-alpha.5 (September 19, 2019) - -- Track tokio release - -# 0.2.0-alpha.4 (August 29, 2019) - -- Track tokio release. - -# 0.2.0-alpha.3 (August 28, 2019) - -### Fix -- Infinite loop in `LinesCodec` (#1489). - -# 0.2.0-alpha.2 (August 17, 2019) - -### Changed -- Update `futures` dependency to 0.3.0-alpha.18. - -# 0.2.0-alpha.1 (August 8, 2019) - -### Changed -- Switch to `async`, `await`, and `std::future`. - -# 0.1.1 (September 26, 2018) - -* Allow setting max line length with `LinesCodec` (#632) - -# 0.1.0 (June 13, 2018) - -* Initial release (#353) diff --git a/tokio-net/Cargo.toml b/tokio-net/Cargo.toml index d7df33973f9..f19fd7598ec 100644 --- a/tokio-net/Cargo.toml +++ b/tokio-net/Cargo.toml @@ -62,7 +62,6 @@ uds = [ log = ["tracing/log"] [dependencies] -tokio-codec = { version = "=0.2.0-alpha.6", path = "../tokio-codec" } tokio-executor = { version = "=0.2.0-alpha.6", features = ["blocking"], path = "../tokio-executor" } tokio-io = { version = "=0.2.0-alpha.6", path = "../tokio-io" } tokio-sync = { version = "=0.2.0-alpha.6", path = "../tokio-sync" } diff --git a/tokio-net/src/udp/mod.rs b/tokio-net/src/udp/mod.rs index e5c06585422..45656773c25 100644 --- a/tokio-net/src/udp/mod.rs +++ b/tokio-net/src/udp/mod.rs @@ -7,9 +7,7 @@ //! //! [`UdpSocket`]: struct.UdpSocket -mod frame; mod socket; pub mod split; -pub use self::frame::UdpFramed; pub use self::socket::UdpSocket; diff --git a/tokio-net/src/udp/socket.rs b/tokio-net/src/udp/socket.rs index 00a42469ad2..8fd08339097 100644 --- a/tokio-net/src/udp/socket.rs +++ b/tokio-net/src/udp/socket.rs @@ -110,7 +110,7 @@ impl UdpSocket { /// /// [`connect`]: #method.connect pub async fn send(&mut self, buf: &[u8]) -> io::Result { - poll_fn(|cx| self.poll_send_priv(cx, buf)).await + poll_fn(|cx| self.poll_send(cx, buf)).await } // Poll IO functions that takes `&self` are provided for the split API. @@ -123,11 +123,8 @@ impl UdpSocket { // While violating this requirement is "safe" from a Rust memory model point // of view, it will result in unexpected behavior in the form of lost // notifications and tasks hanging. - pub(crate) fn poll_send_priv( - &self, - cx: &mut Context<'_>, - buf: &[u8], - ) -> Poll> { + #[doc(hidden)] + pub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll> { ready!(self.io.poll_write_ready(cx))?; match self.io.get_ref().send(buf) { @@ -152,14 +149,11 @@ impl UdpSocket { /// /// [`connect`]: #method.connect pub async fn recv(&mut self, buf: &mut [u8]) -> io::Result { - poll_fn(|cx| self.poll_recv_priv(cx, buf)).await + poll_fn(|cx| self.poll_recv(cx, buf)).await } - pub(crate) fn poll_recv_priv( - &self, - cx: &mut Context<'_>, - buf: &mut [u8], - ) -> Poll> { + #[doc(hidden)] + pub fn poll_recv(&self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll> { ready!(self.io.poll_read_ready(cx, mio::Ready::readable()))?; match self.io.get_ref().recv(buf) { @@ -180,7 +174,7 @@ impl UdpSocket { let mut addrs = target.to_socket_addrs().await?; match addrs.next() { - Some(target) => poll_fn(|cx| self.poll_send_to_priv(cx, buf, &target)).await, + Some(target) => poll_fn(|cx| self.poll_send_to(cx, buf, &target)).await, None => Err(io::Error::new( io::ErrorKind::InvalidInput, "no addresses to send data to", @@ -188,7 +182,9 @@ impl UdpSocket { } } - pub(crate) fn poll_send_to_priv( + // TODO: Public or not? + #[doc(hidden)] + pub fn poll_send_to( &self, cx: &mut Context<'_>, buf: &[u8], @@ -212,10 +208,11 @@ impl UdpSocket { /// to hold the message bytes. If a message is too long to fit in the supplied /// buffer, excess bytes may be discarded. pub async fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - poll_fn(|cx| self.poll_recv_from_priv(cx, buf)).await + poll_fn(|cx| self.poll_recv_from(cx, buf)).await } - pub(crate) fn poll_recv_from_priv( + #[doc(hidden)] + pub fn poll_recv_from( &self, cx: &mut Context<'_>, buf: &mut [u8], diff --git a/tokio-net/src/udp/split.rs b/tokio-net/src/udp/split.rs index e58f9276977..ad8ce061494 100644 --- a/tokio-net/src/udp/split.rs +++ b/tokio-net/src/udp/split.rs @@ -86,7 +86,7 @@ impl UdpSocketRecvHalf { /// to hold the message bytes. If a message is too long to fit in the supplied /// buffer, excess bytes may be discarded. pub async fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - poll_fn(|cx| self.0.poll_recv_from_priv(cx, buf)).await + poll_fn(|cx| self.0.poll_recv_from(cx, buf)).await } /// Returns a future that receives a single datagram message on the socket from @@ -102,7 +102,7 @@ impl UdpSocketRecvHalf { /// /// [`connect`]: super::UdpSocket::connect pub async fn recv(&mut self, buf: &mut [u8]) -> io::Result { - poll_fn(|cx| self.0.poll_recv_priv(cx, buf)).await + poll_fn(|cx| self.0.poll_recv(cx, buf)).await } } @@ -120,7 +120,7 @@ impl UdpSocketSendHalf { /// The future will resolve to an error if the IP version of the socket does /// not match that of `target`. pub async fn send_to(&mut self, buf: &[u8], target: &SocketAddr) -> io::Result { - poll_fn(|cx| self.0.poll_send_to_priv(cx, buf, target)).await + poll_fn(|cx| self.0.poll_send_to(cx, buf, target)).await } /// Returns a future that sends data on the socket to the remote address to which it is connected. @@ -131,7 +131,7 @@ impl UdpSocketSendHalf { /// /// [`connect`]: super::UdpSocket::connect pub async fn send(&mut self, buf: &[u8]) -> io::Result { - poll_fn(|cx| self.0.poll_send_priv(cx, buf)).await + poll_fn(|cx| self.0.poll_send(cx, buf)).await } } diff --git a/tokio-util/CHANGELOG.md b/tokio-util/CHANGELOG.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tokio-codec/Cargo.toml b/tokio-util/Cargo.toml similarity index 81% rename from tokio-codec/Cargo.toml rename to tokio-util/Cargo.toml index 6c0d61f909f..1ac3a1c877d 100644 --- a/tokio-codec/Cargo.toml +++ b/tokio-util/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "tokio-codec" +name = "tokio-util" # When releasing to crates.io: # - Remove path dependencies # - Update html_root_url. @@ -13,14 +13,15 @@ authors = ["Tokio Contributors "] license = "MIT" repository = "https://github.com/tokio-rs/tokio" homepage = "https://tokio.rs" -documentation = "https://docs.rs/tokio-codec/0.2.0-alpha.6/tokio_codec" +documentation = "https://docs.rs/tokio-util/0.2.0-alpha.6/tokio_util" description = """ -Utilities for encoding and decoding frames. +Additional utilities for working with Tokio. """ categories = ["asynchronous"] [dependencies] tokio-io = { version = "=0.2.0-alpha.6", path = "../tokio-io" } +tokio = { version = "=0.2.0-alpha.6", path = "../tokio" } bytes = "0.4.7" futures-core-preview = "=0.3.0-alpha.19" diff --git a/tokio-codec/LICENSE b/tokio-util/LICENSE similarity index 100% rename from tokio-codec/LICENSE rename to tokio-util/LICENSE diff --git a/tokio-codec/README.md b/tokio-util/README.md similarity index 95% rename from tokio-codec/README.md rename to tokio-util/README.md index 359070597fb..11b2b1841a7 100644 --- a/tokio-codec/README.md +++ b/tokio-util/README.md @@ -1,4 +1,4 @@ -# tokio-codec +# tokio-util Utilities for encoding and decoding frames. diff --git a/tokio-codec/src/bytes_codec.rs b/tokio-util/src/codec/bytes_codec.rs similarity index 92% rename from tokio-codec/src/bytes_codec.rs rename to tokio-util/src/codec/bytes_codec.rs index c5bf62b8007..a7d424e9e6a 100644 --- a/tokio-codec/src/bytes_codec.rs +++ b/tokio-util/src/codec/bytes_codec.rs @@ -1,5 +1,6 @@ -use crate::decoder::Decoder; -use crate::encoder::Encoder; +use crate::codec::decoder::Decoder; +use crate::codec::encoder::Encoder; + use bytes::{BufMut, Bytes, BytesMut}; use std::io; diff --git a/tokio-codec/src/decoder.rs b/tokio-util/src/codec/decoder.rs similarity index 98% rename from tokio-codec/src/decoder.rs rename to tokio-util/src/codec/decoder.rs index 41b95079000..720e0b6ef05 100644 --- a/tokio-codec/src/decoder.rs +++ b/tokio-util/src/codec/decoder.rs @@ -1,10 +1,10 @@ -use bytes::BytesMut; -use std::io; -use tokio_io::{AsyncRead, AsyncWrite}; +use crate::codec::encoder::Encoder; +use crate::codec::Framed; -use super::encoder::Encoder; +use tokio_io::{AsyncRead, AsyncWrite}; -use super::Framed; +use bytes::BytesMut; +use std::io; /// Decoding of frames via buffers. /// @@ -75,7 +75,7 @@ pub trait Decoder { /// # use std::io; /// # /// # use bytes::BytesMut; - /// # use tokio_codec::Decoder; + /// # use tokio_util::codec::Decoder; /// # /// # struct MyCodec; /// # diff --git a/tokio-codec/src/encoder.rs b/tokio-util/src/codec/encoder.rs similarity index 100% rename from tokio-codec/src/encoder.rs rename to tokio-util/src/codec/encoder.rs diff --git a/tokio-codec/src/framed.rs b/tokio-util/src/codec/framed.rs similarity index 97% rename from tokio-codec/src/framed.rs rename to tokio-util/src/codec/framed.rs index ac733d59fd2..e2eb82cb7d0 100644 --- a/tokio-codec/src/framed.rs +++ b/tokio-util/src/codec/framed.rs @@ -1,9 +1,7 @@ -#![allow(deprecated)] - -use crate::decoder::Decoder; -use crate::encoder::Encoder; -use crate::framed_read::{framed_read2, framed_read2_with_buffer, FramedRead2}; -use crate::framed_write::{framed_write2, framed_write2_with_buffer, FramedWrite2}; +use crate::codec::decoder::Decoder; +use crate::codec::encoder::Encoder; +use crate::codec::framed_read::{framed_read2, framed_read2_with_buffer, FramedRead2}; +use crate::codec::framed_write::{framed_write2, framed_write2_with_buffer, FramedWrite2}; use tokio_io::{AsyncBufRead, AsyncRead, AsyncWrite}; diff --git a/tokio-codec/src/framed_read.rs b/tokio-util/src/codec/framed_read.rs similarity index 99% rename from tokio-codec/src/framed_read.rs rename to tokio-util/src/codec/framed_read.rs index 85f221b9a68..71f22150d2b 100644 --- a/tokio-codec/src/framed_read.rs +++ b/tokio-util/src/codec/framed_read.rs @@ -1,5 +1,5 @@ -use super::framed::Fuse; -use super::Decoder; +use crate::codec::framed::Fuse; +use crate::codec::Decoder; use tokio_io::AsyncRead; diff --git a/tokio-codec/src/framed_write.rs b/tokio-util/src/codec/framed_write.rs similarity index 98% rename from tokio-codec/src/framed_write.rs rename to tokio-util/src/codec/framed_write.rs index f89e461120d..fa8aba175f5 100644 --- a/tokio-codec/src/framed_write.rs +++ b/tokio-util/src/codec/framed_write.rs @@ -1,8 +1,6 @@ -#![allow(deprecated)] - -use super::framed::Fuse; -use crate::decoder::Decoder; -use crate::encoder::Encoder; +use crate::codec::decoder::Decoder; +use crate::codec::encoder::Encoder; +use crate::codec::framed::Fuse; use tokio_io::{AsyncBufRead, AsyncRead, AsyncWrite}; diff --git a/tokio-codec/src/length_delimited.rs b/tokio-util/src/codec/length_delimited.rs similarity index 95% rename from tokio-codec/src/length_delimited.rs rename to tokio-util/src/codec/length_delimited.rs index ff3400de396..579417a2eb1 100644 --- a/tokio-codec/src/length_delimited.rs +++ b/tokio-util/src/codec/length_delimited.rs @@ -16,7 +16,7 @@ //! //! ``` //! use tokio_io::{AsyncRead, AsyncWrite}; -//! use tokio_codec::{Framed, LengthDelimitedCodec}; +//! use tokio_util::codec::{Framed, LengthDelimitedCodec}; //! //! fn bind_transport(io: T) //! -> Framed @@ -39,8 +39,8 @@ //! Specifically, given the following: //! //! ``` -//! use tokio::codec::{Framed, LengthDelimitedCodec}; //! use tokio::prelude::*; +//! use tokio_util::codec::{Framed, LengthDelimitedCodec}; //! //! use bytes::Bytes; //! @@ -79,7 +79,7 @@ //! //! ``` //! # use tokio_io::AsyncRead; -//! # use tokio_codec::LengthDelimitedCodec; +//! # use tokio_util::codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { //! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value @@ -113,7 +113,7 @@ //! //! ``` //! # use tokio_io::AsyncRead; -//! # use tokio_codec::LengthDelimitedCodec; +//! # use tokio_util::codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { //! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value @@ -145,7 +145,7 @@ //! //! ``` //! # use tokio_io::AsyncRead; -//! # use tokio_codec::LengthDelimitedCodec; +//! # use tokio_util::codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { //! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value @@ -179,7 +179,7 @@ //! //! ``` //! # use tokio_io::AsyncRead; -//! # use tokio_codec::LengthDelimitedCodec; +//! # use tokio_util::codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { //! LengthDelimitedCodec::builder() //! .length_field_offset(0) // default value @@ -223,7 +223,7 @@ //! //! ``` //! # use tokio_io::AsyncRead; -//! # use tokio_codec::LengthDelimitedCodec; +//! # use tokio_util::codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { //! LengthDelimitedCodec::builder() //! .length_field_offset(1) // length of hdr1 @@ -269,7 +269,7 @@ //! //! ``` //! # use tokio_io::AsyncRead; -//! # use tokio_codec::LengthDelimitedCodec; +//! # use tokio_util::codec::LengthDelimitedCodec; //! # fn bind_read(io: T) { //! LengthDelimitedCodec::builder() //! .length_field_offset(1) // length of hdr1 @@ -314,7 +314,7 @@ //! //! ``` //! # use tokio_io::AsyncWrite; -//! # use tokio_codec::LengthDelimitedCodec; +//! # use tokio_util::codec::LengthDelimitedCodec; //! # fn write_frame(io: T) { //! # let _ = //! LengthDelimitedCodec::builder() @@ -340,12 +340,14 @@ //! [`Encoder`]: ../trait.Encoder.html //! [`BytesMut`]: https://docs.rs/bytes/0.4/bytes/struct.BytesMut.html -use crate::{Decoder, Encoder, Framed, FramedRead, FramedWrite}; +use crate::codec::{Decoder, Encoder, Framed, FramedRead, FramedWrite}; + +use tokio_io::{AsyncRead, AsyncWrite}; + 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. /// @@ -604,7 +606,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -648,7 +650,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -672,7 +674,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -696,7 +698,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -730,7 +732,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -754,7 +756,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -777,7 +779,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -798,7 +800,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -822,7 +824,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -841,7 +843,7 @@ impl Builder { /// # Examples /// /// ``` - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// # pub fn main() { /// LengthDelimitedCodec::builder() /// .length_field_offset(0) @@ -864,7 +866,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncRead; - /// use tokio_codec::LengthDelimitedCodec; + /// use tokio_util::codec::LengthDelimitedCodec; /// /// # fn bind_read(io: T) { /// LengthDelimitedCodec::builder() @@ -889,7 +891,7 @@ impl Builder { /// /// ``` /// # use tokio_io::AsyncWrite; - /// # use tokio_codec::LengthDelimitedCodec; + /// # use tokio_util::codec::LengthDelimitedCodec; /// # fn write_frame(io: T) { /// LengthDelimitedCodec::builder() /// .length_field_length(2) @@ -910,7 +912,7 @@ impl Builder { /// /// ``` /// # use tokio_io::{AsyncRead, AsyncWrite}; - /// # use tokio_codec::LengthDelimitedCodec; + /// # use tokio_util::codec::LengthDelimitedCodec; /// # fn write_frame(io: T) { /// # let _ = /// LengthDelimitedCodec::builder() diff --git a/tokio-codec/src/lines_codec.rs b/tokio-util/src/codec/lines_codec.rs similarity index 97% rename from tokio-codec/src/lines_codec.rs rename to tokio-util/src/codec/lines_codec.rs index 19741c184d6..bca697237e6 100644 --- a/tokio-codec/src/lines_codec.rs +++ b/tokio-util/src/codec/lines_codec.rs @@ -1,5 +1,6 @@ -use crate::decoder::Decoder; -use crate::encoder::Encoder; +use crate::codec::decoder::Decoder; +use crate::codec::encoder::Encoder; + use bytes::{BufMut, BytesMut}; use std::{cmp, fmt, io, str, usize}; @@ -70,13 +71,13 @@ impl LinesCodec { /// /// ``` /// use std::usize; - /// use tokio_codec::LinesCodec; + /// use tokio_util::codec::LinesCodec; /// /// let codec = LinesCodec::new(); /// assert_eq!(codec.max_length(), usize::MAX); /// ``` /// ``` - /// use tokio_codec::LinesCodec; + /// use tokio_util::codec::LinesCodec; /// /// let codec = LinesCodec::new_with_max_length(256); /// assert_eq!(codec.max_length(), 256); diff --git a/tokio-codec/src/macros.rs b/tokio-util/src/codec/macros.rs similarity index 100% rename from tokio-codec/src/macros.rs rename to tokio-util/src/codec/macros.rs diff --git a/tokio-codec/src/lib.rs b/tokio-util/src/codec/mod.rs similarity index 50% rename from tokio-codec/src/lib.rs rename to tokio-util/src/codec/mod.rs index 6aaa15a2bed..fd080dc1805 100644 --- a/tokio-codec/src/lib.rs +++ b/tokio-util/src/codec/mod.rs @@ -1,16 +1,3 @@ -#![doc(html_root_url = "https://docs.rs/tokio-codec/0.2.0-alpha.6")] -#![warn( - missing_debug_implementations, - missing_docs, - rust_2018_idioms, - unreachable_pub -)] -#![deny(intra_doc_link_resolution_failure)] -#![doc(test( - no_crate_inject, - attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) -))] - //! Utilities for encoding and decoding frames. //! //! Contains adapters to go from streams of bytes, [`AsyncRead`] and @@ -26,19 +13,25 @@ mod macros; mod bytes_codec; +pub use self::bytes_codec::BytesCodec; + mod decoder; +pub use self::decoder::Decoder; + mod encoder; +pub use self::encoder::Encoder; + mod framed; +pub use self::framed::{Framed, FramedParts}; + mod framed_read; +pub use self::framed_read::FramedRead; + mod framed_write; +pub use self::framed_write::FramedWrite; + pub mod length_delimited; -mod lines_codec; +pub use self::length_delimited::{LengthDelimitedCodec, LengthDelimitedCodecError}; -pub use crate::bytes_codec::BytesCodec; -pub use crate::decoder::Decoder; -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}; +mod lines_codec; +pub use self::lines_codec::{LinesCodec, LinesCodecError}; diff --git a/tokio-util/src/lib.rs b/tokio-util/src/lib.rs new file mode 100644 index 00000000000..5a64673c8d1 --- /dev/null +++ b/tokio-util/src/lib.rs @@ -0,0 +1,17 @@ +#![doc(html_root_url = "https://docs.rs/tokio-util/0.2.0-alpha.6")] +#![warn( + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + unreachable_pub +)] +#![deny(intra_doc_link_resolution_failure)] +#![doc(test( + no_crate_inject, + attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) +))] + +//! Utilities for working with Tokio. + +pub mod codec; +pub mod udp; diff --git a/tokio-net/src/udp/frame.rs b/tokio-util/src/udp/frame.rs similarity index 86% rename from tokio-net/src/udp/frame.rs rename to tokio-util/src/udp/frame.rs index d694e2fed67..4a8c3c0b1aa 100644 --- a/tokio-net/src/udp/frame.rs +++ b/tokio-util/src/udp/frame.rs @@ -1,14 +1,14 @@ -use super::UdpSocket; +use crate::codec::{Decoder, Encoder}; -use tokio_codec::{Decoder, Encoder}; +use tokio::net::UdpSocket; use bytes::{BufMut, BytesMut}; -use core::task::{Context, Poll}; use futures_core::{ready, Stream}; use futures_sink::Sink; use std::io; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::pin::Pin; +use std::task::{Context, Poll}; /// A unified `Stream` and `Sink` interface to an underlying `UdpSocket`, using /// the `Encoder` and `Decoder` traits to encode and decode frames. @@ -47,22 +47,17 @@ impl Stream for UdpFramed { let (_n, addr) = unsafe { // Read into the buffer without having to initialize the memory. - let res = ready!(Pin::new(&mut pin.socket).poll_recv_from_priv(cx, pin.rd.bytes_mut())); + let res = ready!(Pin::new(&mut pin.socket).poll_recv_from(cx, pin.rd.bytes_mut())); let (n, addr) = res?; pin.rd.advance_mut(n); (n, addr) }; - let span = trace_span!("decoding", from.addr = %addr, dgram.length = _n); - let _e = span.enter(); - trace!("trying to decode a frame..."); - let frame_res = pin.codec.decode(&mut pin.rd); pin.rd.clear(); let frame = frame_res?; let result = frame.map(|frame| Ok((frame, addr))); // frame -> (frame, addr) - trace!("frame decoded from buffer"); Poll::Ready(result) } } @@ -84,16 +79,11 @@ impl Sink<(C::Item, SocketAddr)> for UdpFramed { fn start_send(self: Pin<&mut Self>, item: (C::Item, SocketAddr)) -> Result<(), Self::Error> { let (frame, out_addr) = item; - let span = trace_span!("sending", to.addr = %out_addr); - let _e = span.enter(); - trace!("encoding frame..."); - let pin = self.get_mut(); pin.codec.encode(frame, &mut pin.wr)?; pin.out_addr = out_addr; pin.flushed = false; - trace!(message = "frame encoded", frame.length = pin.wr.len()); Ok(()) } @@ -110,18 +100,12 @@ impl Sink<(C::Item, SocketAddr)> for UdpFramed { .. } = *self; - let span = trace_span!("flushing", to.addr = %out_addr, frame.length = wr.len()); - let _e = span.enter(); - trace!("flushing frame..."); - - let n = ready!(socket.poll_send_to_priv(cx, &wr, &out_addr))?; + let n = ready!(socket.poll_send_to(cx, &wr, &out_addr))?; let wrote_all = n == self.wr.len(); self.wr.clear(); self.flushed = true; - trace!(written.length = n, written.complete = wrote_all); - let res = if wrote_all { Ok(()) } else { diff --git a/tokio-util/src/udp/mod.rs b/tokio-util/src/udp/mod.rs new file mode 100644 index 00000000000..7c4bb2b3cb5 --- /dev/null +++ b/tokio-util/src/udp/mod.rs @@ -0,0 +1,4 @@ +//! UDP framing + +mod frame; +pub use self::frame::UdpFramed; diff --git a/tokio-codec/tests/codecs.rs b/tokio-util/tests/codecs.rs similarity index 98% rename from tokio-codec/tests/codecs.rs rename to tokio-util/tests/codecs.rs index 2c227a181f5..4709854ab14 100644 --- a/tokio-codec/tests/codecs.rs +++ b/tokio-util/tests/codecs.rs @@ -1,7 +1,8 @@ #![warn(rust_2018_idioms)] +use tokio_util::codec::{BytesCodec, Decoder, Encoder, LinesCodec}; + use bytes::{BufMut, Bytes, BytesMut}; -use tokio_codec::{BytesCodec, Decoder, Encoder, LinesCodec}; #[test] fn bytes_decoder() { diff --git a/tokio-codec/tests/framed.rs b/tokio-util/tests/framed.rs similarity index 97% rename from tokio-codec/tests/framed.rs rename to tokio-util/tests/framed.rs index 53582fb3d21..7aa40d8814b 100644 --- a/tokio-codec/tests/framed.rs +++ b/tokio-util/tests/framed.rs @@ -1,8 +1,8 @@ #![warn(rust_2018_idioms)] use tokio::prelude::*; -use tokio_codec::{Decoder, Encoder, Framed, FramedParts}; use tokio_test::assert_ok; +use tokio_util::codec::{Decoder, Encoder, Framed, FramedParts}; use bytes::{Buf, BufMut, BytesMut, IntoBuf}; use std::io::{self, Read}; diff --git a/tokio-codec/tests/framed_read.rs b/tokio-util/tests/framed_read.rs similarity index 99% rename from tokio-codec/tests/framed_read.rs rename to tokio-util/tests/framed_read.rs index 72c910ba437..2064b9fb102 100644 --- a/tokio-codec/tests/framed_read.rs +++ b/tokio-util/tests/framed_read.rs @@ -1,9 +1,9 @@ #![warn(rust_2018_idioms)] use tokio::prelude::*; -use tokio_codec::{Decoder, FramedRead}; use tokio_test::assert_ready; use tokio_test::task::MockTask; +use tokio_util::codec::{Decoder, FramedRead}; use bytes::{Buf, BytesMut, IntoBuf}; use std::collections::VecDeque; diff --git a/tokio-codec/tests/framed_write.rs b/tokio-util/tests/framed_write.rs similarity index 99% rename from tokio-codec/tests/framed_write.rs rename to tokio-util/tests/framed_write.rs index 9d85c4d835e..90a54af1655 100644 --- a/tokio-codec/tests/framed_write.rs +++ b/tokio-util/tests/framed_write.rs @@ -1,9 +1,9 @@ #![warn(rust_2018_idioms)] -use tokio_codec::{Encoder, FramedWrite}; use tokio_io::AsyncWrite; use tokio_test::assert_ready; use tokio_test::task::MockTask; +use tokio_util::codec::{Encoder, FramedWrite}; use bytes::{BufMut, BytesMut}; use futures_sink::Sink; diff --git a/tokio-codec/tests/length_delimited.rs b/tokio-util/tests/length_delimited.rs similarity index 99% rename from tokio-codec/tests/length_delimited.rs rename to tokio-util/tests/length_delimited.rs index 404786e31c2..c78dfac8dcb 100644 --- a/tokio-codec/tests/length_delimited.rs +++ b/tokio-util/tests/length_delimited.rs @@ -1,12 +1,12 @@ #![warn(rust_2018_idioms)] -use tokio::codec::*; use tokio::io::{AsyncRead, AsyncWrite}; use tokio::prelude::*; use tokio_test::task::MockTask; use tokio_test::{ assert_err, assert_ok, assert_pending, assert_ready, assert_ready_err, assert_ready_ok, }; +use tokio_util::codec::*; use bytes::{BufMut, Bytes, BytesMut}; use futures_util::pin_mut; diff --git a/tokio-util/tests/udp.rs b/tokio-util/tests/udp.rs new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/tokio-util/tests/udp.rs @@ -0,0 +1 @@ + diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 6ed8002c714..12ef517cb0a 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -25,7 +25,6 @@ keywords = ["io", "async", "non-blocking", "futures"] [features] default = [ - "codec", "fs", "io", "net", @@ -36,7 +35,6 @@ default = [ "timer", ] -codec = ["io", "tokio-codec", "bytes"] fs = ["tokio-fs"] io = ["tokio-io"] macros = ["tokio-macros"] @@ -71,9 +69,7 @@ futures-sink-preview = "=0.3.0-alpha.19" futures-util-preview = { version = "=0.3.0-alpha.19", features = ["sink"] } # Everything else is optional... -bytes = { version = "0.4", optional = true } num_cpus = { version = "1.8.0", optional = true } -tokio-codec = { version = "=0.2.0-alpha.6", optional = true, path = "../tokio-codec" } tokio-fs = { version = "=0.2.0-alpha.6", optional = true, path = "../tokio-fs" } tokio-io = { version = "=0.2.0-alpha.6", optional = true, features = ["util"], path = "../tokio-io" } tokio-executor = { version = "=0.2.0-alpha.6", optional = true, path = "../tokio-executor" } @@ -89,6 +85,7 @@ tokio-executor = { version = "=0.2.0-alpha.6", optional = true, path = "../tokio [dev-dependencies] tokio-test = { version = "=0.2.0-alpha.6", path = "../tokio-test" } +tokio-util = { version = "=0.2.0-alpha.6", path = "../tokio-util" } futures-preview = "=0.3.0-alpha.19" futures-util-preview = "=0.3.0-alpha.19" diff --git a/tokio/src/net.rs b/tokio/src/net.rs index d412b465ec5..6aa1e499c3b 100644 --- a/tokio/src/net.rs +++ b/tokio/src/net.rs @@ -52,10 +52,10 @@ pub mod udp { //! The main struct for UDP is the [`UdpSocket`], which represents a UDP socket. //! //! [`UdpSocket`]: struct.UdpSocket.html - pub use tokio_net::udp::{split, UdpFramed, UdpSocket}; + pub use tokio_net::udp::{split, UdpSocket}; } #[cfg(feature = "udp")] -pub use self::udp::{UdpFramed, UdpSocket}; +pub use self::udp::UdpSocket; #[cfg(all(unix, feature = "uds"))] pub mod unix {