-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit fe2b997. We are avoiding adding poll_read_buf to tokio itself for now. The patch is reverted now in order to not block the v0.3.2 release (#3059).
- Loading branch information
1 parent
38605c5
commit d786553
Showing
11 changed files
with
232 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ name = "tokio-util" | |
# - Cargo.toml | ||
# - Update CHANGELOG.md. | ||
# - Create "v0.2.x" git tag. | ||
version = "0.5.0" | ||
version = "0.4.0" | ||
edition = "2018" | ||
authors = ["Tokio Contributors <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -27,15 +27,15 @@ default = [] | |
full = ["codec", "compat", "io", "time"] | ||
|
||
compat = ["futures-io",] | ||
codec = ["tokio/io-util", "tokio/stream"] | ||
codec = ["tokio/stream"] | ||
time = ["tokio/time","slab"] | ||
io = ["tokio/io-util"] | ||
io = [] | ||
rt = ["tokio/rt"] | ||
|
||
[dependencies] | ||
tokio = { version = "0.3.0", path = "../tokio" } | ||
|
||
bytes = "0.6.0" | ||
bytes = "0.5.0" | ||
futures-core = "0.3.0" | ||
futures-sink = "0.3.0" | ||
futures-io = { version = "0.3.0", optional = true } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use bytes::BufMut; | ||
use futures_core::ready; | ||
use std::io; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
use tokio::io::{AsyncRead, ReadBuf}; | ||
|
||
/// Try to read data from an `AsyncRead` into an implementer of the [`Buf`] trait. | ||
/// | ||
/// [`Buf`]: bytes::Buf | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use bytes::{Bytes, BytesMut}; | ||
/// use tokio::stream; | ||
/// use tokio::io::Result; | ||
/// use tokio_util::io::{StreamReader, poll_read_buf}; | ||
/// use futures::future::poll_fn; | ||
/// use std::pin::Pin; | ||
/// # #[tokio::main] | ||
/// # async fn main() -> std::io::Result<()> { | ||
/// | ||
/// // Create a reader from an iterator. This particular reader will always be | ||
/// // ready. | ||
/// let mut read = StreamReader::new(stream::iter(vec![Result::Ok(Bytes::from_static(&[0, 1, 2, 3]))])); | ||
/// | ||
/// let mut buf = BytesMut::new(); | ||
/// let mut reads = 0; | ||
/// | ||
/// loop { | ||
/// reads += 1; | ||
/// let n = poll_fn(|cx| poll_read_buf(Pin::new(&mut read), cx, &mut buf)).await?; | ||
/// | ||
/// if n == 0 { | ||
/// break; | ||
/// } | ||
/// } | ||
/// | ||
/// // one or more reads might be necessary. | ||
/// assert!(reads >= 1); | ||
/// assert_eq!(&buf[..], &[0, 1, 2, 3]); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn poll_read_buf<R, B>( | ||
read: Pin<&mut R>, | ||
cx: &mut Context<'_>, | ||
buf: &mut B, | ||
) -> Poll<io::Result<usize>> | ||
where | ||
R: AsyncRead, | ||
B: BufMut, | ||
{ | ||
if !buf.has_remaining_mut() { | ||
return Poll::Ready(Ok(0)); | ||
} | ||
|
||
let n = { | ||
let mut buf = ReadBuf::uninit(buf.bytes_mut()); | ||
let before = buf.filled().as_ptr(); | ||
|
||
ready!(read.poll_read(cx, &mut buf)?); | ||
|
||
// This prevents a malicious read implementation from swapping out the | ||
// buffer being read, which would allow `filled` to be advanced without | ||
// actually initializing the provided buffer. | ||
// | ||
// We avoid this by asserting that the `ReadBuf` instance wraps the same | ||
// memory address both before and after the poll. Which will panic in | ||
// case its swapped. | ||
// | ||
// See https://github.com/tokio-rs/tokio/issues/2827 for more info. | ||
assert! { | ||
std::ptr::eq(before, buf.filled().as_ptr()), | ||
"Read buffer must not be changed during a read poll. \ | ||
See https://github.com/tokio-rs/tokio/issues/2827 for more info." | ||
}; | ||
|
||
buf.filled().len() | ||
}; | ||
|
||
// Safety: This is guaranteed to be the number of initialized (and read) | ||
// bytes due to the invariants provided by `ReadBuf::filled`. | ||
unsafe { | ||
buf.advance_mut(n); | ||
} | ||
|
||
Poll::Ready(Ok(n)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use bytes::BufMut; | ||
use std::future::Future; | ||
use std::io; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
use tokio::io::AsyncRead; | ||
|
||
/// Read data from an `AsyncRead` into an implementer of the [`Buf`] trait. | ||
/// | ||
/// [`Buf`]: bytes::Buf | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use bytes::{Bytes, BytesMut}; | ||
/// use tokio::stream; | ||
/// use tokio::io::Result; | ||
/// use tokio_util::io::{StreamReader, read_buf}; | ||
/// # #[tokio::main] | ||
/// # async fn main() -> std::io::Result<()> { | ||
/// | ||
/// // Create a reader from an iterator. This particular reader will always be | ||
/// // ready. | ||
/// let mut read = StreamReader::new(stream::iter(vec![Result::Ok(Bytes::from_static(&[0, 1, 2, 3]))])); | ||
/// | ||
/// let mut buf = BytesMut::new(); | ||
/// let mut reads = 0; | ||
/// | ||
/// loop { | ||
/// reads += 1; | ||
/// let n = read_buf(&mut read, &mut buf).await?; | ||
/// | ||
/// if n == 0 { | ||
/// break; | ||
/// } | ||
/// } | ||
/// | ||
/// // one or more reads might be necessary. | ||
/// assert!(reads >= 1); | ||
/// assert_eq!(&buf[..], &[0, 1, 2, 3]); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub async fn read_buf<R, B>(read: &mut R, buf: &mut B) -> io::Result<usize> | ||
where | ||
R: AsyncRead + Unpin, | ||
B: BufMut, | ||
{ | ||
return ReadBufFn(read, buf).await; | ||
|
||
struct ReadBufFn<'a, R, B>(&'a mut R, &'a mut B); | ||
|
||
impl<'a, R, B> Future for ReadBufFn<'a, R, B> | ||
where | ||
R: AsyncRead + Unpin, | ||
B: BufMut, | ||
{ | ||
type Output = io::Result<usize>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = &mut *self; | ||
super::poll_read_buf(Pin::new(this.0), cx, this.1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.