Skip to content

Commit

Permalink
codec: move into tokio-util
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carllerche committed Oct 21, 2019
1 parent 6aa6ebb commit 48e61fc
Show file tree
Hide file tree
Showing 33 changed files with 127 additions and 168 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

members = [
"tokio",
"tokio-codec",
"tokio-executor",
"tokio-fs",
"tokio-io",
Expand All @@ -12,5 +11,8 @@ members = [
"tokio-test",
"tokio-timer",
"tokio-tls",
"tokio-util",

# Crates used by tests
"build-tests",
]
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ jobs:
displayName: Test sub crates -
rust: beta
crates:
tokio-codec: []
tokio-executor:
- current-thread
- thread-pool
Expand All @@ -74,6 +73,7 @@ jobs:
tokio-timer:
- async-traits
tokio-test: []
tokio-util: []

# Test compilation failure
- template: ci/azure-test-stable.yml
Expand Down
2 changes: 1 addition & 1 deletion ci/patch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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" }
35 changes: 0 additions & 35 deletions tokio-codec/CHANGELOG.md

This file was deleted.

1 change: 0 additions & 1 deletion tokio-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 0 additions & 2 deletions tokio-net/src/udp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
//!
//! [`UdpSocket`]: struct.UdpSocket

mod frame;
mod socket;
pub mod split;

pub use self::frame::UdpFramed;
pub use self::socket::UdpSocket;
29 changes: 13 additions & 16 deletions tokio-net/src/udp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl UdpSocket {
///
/// [`connect`]: #method.connect
pub async fn send(&mut self, buf: &[u8]) -> io::Result<usize> {
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.
Expand All @@ -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<io::Result<usize>> {
#[doc(hidden)]
pub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
ready!(self.io.poll_write_ready(cx))?;

match self.io.get_ref().send(buf) {
Expand All @@ -152,14 +149,11 @@ impl UdpSocket {
///
/// [`connect`]: #method.connect
pub async fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize> {
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<io::Result<usize>> {
#[doc(hidden)]
pub fn poll_recv(&self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
ready!(self.io.poll_read_ready(cx, mio::Ready::readable()))?;

match self.io.get_ref().recv(buf) {
Expand All @@ -180,15 +174,17 @@ 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",
)),
}
}

pub(crate) fn poll_send_to_priv(
// TODO: Public or not?
#[doc(hidden)]
pub fn poll_send_to(
&self,
cx: &mut Context<'_>,
buf: &[u8],
Expand All @@ -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],
Expand Down
8 changes: 4 additions & 4 deletions tokio-net/src/udp/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -102,7 +102,7 @@ impl UdpSocketRecvHalf {
///
/// [`connect`]: super::UdpSocket::connect
pub async fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize> {
poll_fn(|cx| self.0.poll_recv_priv(cx, buf)).await
poll_fn(|cx| self.0.poll_recv(cx, buf)).await
}
}

Expand All @@ -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<usize> {
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.
Expand All @@ -131,7 +131,7 @@ impl UdpSocketSendHalf {
///
/// [`connect`]: super::UdpSocket::connect
pub async fn send(&mut self, buf: &[u8]) -> io::Result<usize> {
poll_fn(|cx| self.0.poll_send_priv(cx, buf)).await
poll_fn(|cx| self.0.poll_send(cx, buf)).await
}
}

Expand Down
Empty file added tokio-util/CHANGELOG.md
Empty file.
7 changes: 4 additions & 3 deletions tokio-codec/Cargo.toml → tokio-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "tokio-codec"
name = "tokio-util"
# When releasing to crates.io:
# - Remove path dependencies
# - Update html_root_url.
Expand All @@ -13,14 +13,15 @@ authors = ["Tokio Contributors <[email protected]>"]
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"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tokio-codec/README.md → tokio-util/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tokio-codec
# tokio-util

Utilities for encoding and decoding frames.

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
12 changes: 6 additions & 6 deletions tokio-codec/src/decoder.rs → tokio-util/src/codec/decoder.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -75,7 +75,7 @@ pub trait Decoder {
/// # use std::io;
/// #
/// # use bytes::BytesMut;
/// # use tokio_codec::Decoder;
/// # use tokio_util::codec::Decoder;
/// #
/// # struct MyCodec;
/// #
Expand Down
File renamed without changes.
10 changes: 4 additions & 6 deletions tokio-codec/src/framed.rs → tokio-util/src/codec/framed.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::framed::Fuse;
use super::Decoder;
use crate::codec::framed::Fuse;
use crate::codec::Decoder;

use tokio_io::AsyncRead;

Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
Loading

0 comments on commit 48e61fc

Please sign in to comment.