Skip to content

Commit

Permalink
util: upgrade tokio-util to bytes 0.6 (#3052)
Browse files Browse the repository at this point in the history
  • Loading branch information
djc authored Oct 27, 2020
1 parent 6d0ba19 commit fe2b997
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 233 deletions.
4 changes: 2 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ edition = "2018"
tokio = { version = "0.3.0", path = "../tokio", features = ["full", "tracing"] }
tracing = "0.1"
tracing-subscriber = { version = "0.2.7", default-features = false, features = ["fmt", "ansi", "env-filter", "chrono", "tracing-log"] }
tokio-util = { version = "0.4.0", path = "../tokio-util", features = ["full"] }
bytes = "0.5"
tokio-util = { version = "0.5.0", path = "../tokio-util", features = ["full"] }
bytes = "0.6"
futures = "0.3.0"
http = "0.2"
serde = "1.0"
Expand Down
8 changes: 4 additions & 4 deletions tokio-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "tokio-util"
# - Cargo.toml
# - Update CHANGELOG.md.
# - Create "v0.2.x" git tag.
version = "0.4.0"
version = "0.5.0"
edition = "2018"
authors = ["Tokio Contributors <[email protected]>"]
license = "MIT"
Expand All @@ -27,15 +27,15 @@ default = []
full = ["codec", "compat", "io", "time"]

compat = ["futures-io",]
codec = ["tokio/stream"]
codec = ["tokio/io-util", "tokio/stream"]
time = ["tokio/time","slab"]
io = []
io = ["tokio/io-util"]
rt = ["tokio/rt"]

[dependencies]
tokio = { version = "0.3.0", path = "../tokio" }

bytes = "0.5.0"
bytes = "0.6.0"
futures-core = "0.3.0"
futures-sink = "0.3.0"
futures-io = { version = "0.3.0", optional = true }
Expand Down
6 changes: 2 additions & 4 deletions tokio-util/src/codec/framed_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::codec::decoder::Decoder;
use crate::codec::encoder::Encoder;

use tokio::{
io::{AsyncRead, AsyncWrite},
io::{AsyncRead, AsyncReadExt, AsyncWrite},
stream::Stream,
};

Expand Down Expand Up @@ -118,8 +118,6 @@ where
type Item = Result<U::Item, U::Error>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
use crate::util::poll_read_buf;

let mut pinned = self.project();
let state: &mut ReadFrame = pinned.state.borrow_mut();
loop {
Expand Down Expand Up @@ -150,7 +148,7 @@ where
// got room for at least one byte to read to ensure that we don't
// get a spurious 0 that looks like EOF
state.buffer.reserve(1);
let bytect = match poll_read_buf(cx, pinned.inner.as_mut(), &mut state.buffer)? {
let bytect = match pinned.inner.as_mut().poll_read_buf(&mut state.buffer, cx)? {
Poll::Ready(ct) => ct,
Poll::Pending => return Poll::Pending,
};
Expand Down
4 changes: 0 additions & 4 deletions tokio-util/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
//! [`Body`]: https://docs.rs/hyper/0.13/hyper/struct.Body.html
//! [`AsyncRead`]: tokio::io::AsyncRead

mod poll_read_buf;
mod read_buf;
mod reader_stream;
mod stream_reader;

pub use self::poll_read_buf::poll_read_buf;
pub use self::read_buf::read_buf;
pub use self::reader_stream::ReaderStream;
pub use self::stream_reader::StreamReader;
90 changes: 0 additions & 90 deletions tokio-util/src/io/poll_read_buf.rs

This file was deleted.

65 changes: 0 additions & 65 deletions tokio-util/src/io/read_buf.rs

This file was deleted.

8 changes: 3 additions & 5 deletions tokio-util/src/io/reader_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use futures_core::stream::Stream;
use pin_project_lite::pin_project;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::AsyncRead;
use tokio::io::{AsyncRead, AsyncReadExt};

const CAPACITY: usize = 4096;

Expand Down Expand Up @@ -70,11 +70,9 @@ impl<R: AsyncRead> ReaderStream<R> {
impl<R: AsyncRead> Stream for ReaderStream<R> {
type Item = std::io::Result<Bytes>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
use crate::util::poll_read_buf;

let mut this = self.as_mut().project();

let reader = match this.reader.as_pin_mut() {
let mut reader = match this.reader.as_pin_mut() {
Some(r) => r,
None => return Poll::Ready(None),
};
Expand All @@ -83,7 +81,7 @@ impl<R: AsyncRead> Stream for ReaderStream<R> {
this.buf.reserve(CAPACITY);
}

match poll_read_buf(cx, reader, &mut this.buf) {
match reader.poll_read_buf(&mut this.buf, cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Err(err)) => {
self.project().reader.set(None);
Expand Down
34 changes: 0 additions & 34 deletions tokio-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,3 @@ pub mod either;

#[cfg(feature = "time")]
pub mod time;

#[cfg(any(feature = "io", feature = "codec"))]
mod util {
use tokio::io::{AsyncRead, ReadBuf};

use bytes::BufMut;
use futures_core::ready;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

pub(crate) fn poll_read_buf<T: AsyncRead>(
cx: &mut Context<'_>,
io: Pin<&mut T>,
buf: &mut impl BufMut,
) -> Poll<io::Result<usize>> {
if !buf.has_remaining_mut() {
return Poll::Ready(Ok(0));
}

let orig = buf.bytes_mut().as_ptr() as *const u8;
let mut b = ReadBuf::uninit(buf.bytes_mut());

ready!(io.poll_read(cx, &mut b))?;
let n = b.filled().len();

// Safety: we can assume `n` bytes were read, since they are in`filled`.
assert_eq!(orig, b.filled().as_ptr());
unsafe {
buf.advance_mut(n);
}
Poll::Ready(Ok(n))
}
}
28 changes: 26 additions & 2 deletions tokio/src/io/util/async_read_ext.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::io::util::chain::{chain, Chain};
use crate::io::util::read::{read, Read};
use crate::io::util::read_buf::{read_buf, ReadBuf};
use crate::io::util::read_buf::{poll_read_buf, read_buf, ReadBuf};
use crate::io::util::read_exact::{read_exact, ReadExact};
use crate::io::util::read_int::{
ReadI128, ReadI128Le, ReadI16, ReadI16Le, ReadI32, ReadI32Le, ReadI64, ReadI64Le, ReadI8,
Expand All @@ -14,6 +14,8 @@ use crate::io::util::take::{take, Take};
use crate::io::AsyncRead;

use bytes::BufMut;
use std::io;
use std::task::{Context, Poll};

cfg_io_util! {
/// Defines numeric reader
Expand Down Expand Up @@ -185,7 +187,7 @@ cfg_io_util! {
///
/// On a successful read, the number of read bytes is returned. If the
/// supplied buffer is not empty and the function returns `Ok(0)` then
/// the source as reached an "end-of-file" event.
/// the source has reached an "end-of-file" event.
///
/// # Errors
///
Expand Down Expand Up @@ -231,6 +233,28 @@ cfg_io_util! {
read_buf(self, buf)
}

/// Attempts to pull some bytes from this source into the specified buffer,
/// advancing the buffer's internal cursor if the underlying reader is ready.
///
/// Usually, only a single `read` syscall is issued, even if there is
/// more space in the supplied buffer.
///
/// # Return
///
/// On a successful read, the number of read bytes is returned. If the
/// supplied buffer is not empty and the function returns `Ok(0)` then
/// the source has reached an "end-of-file" event.
///
/// # Errors
///
/// If this function encounters any form of I/O or other error, an error
/// variant will be returned. If an error is returned then it must be
/// guaranteed that no bytes were read.
/// ```
fn poll_read_buf<'a, B>(&'a mut self, buf: &'a mut B, cx: &mut Context<'_>) -> Poll<io::Result<usize>> where Self: Unpin + Sized, B: BufMut {
poll_read_buf(self, buf, cx)
}

/// Reads the exact number of bytes required to fill `buf`.
///
/// Equivalent to:
Expand Down
Loading

0 comments on commit fe2b997

Please sign in to comment.