diff options
author | Sean McArthur <sean@seanmonstar.com> | 2020-08-13 20:15:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-13 20:15:01 -0700 |
commit | c393236dfd12c13e82badd631d3a3a90481c6f95 (patch) | |
tree | 47e7e70b7a58fb968870d5d44e95f6c45192e114 /tokio/tests/io_read.rs | |
parent | 71da06097bf9aa851ebdde79d7b01a3e38174db9 (diff) |
io: change AsyncRead to use a ReadBuf (#2758)
Works towards #2716. Changes the argument to `AsyncRead::poll_read` to
take a `ReadBuf` struct that safely manages writes to uninitialized memory.
Diffstat (limited to 'tokio/tests/io_read.rs')
-rw-r--r-- | tokio/tests/io_read.rs | 32 |
1 files changed, 5 insertions, 27 deletions
diff --git a/tokio/tests/io_read.rs b/tokio/tests/io_read.rs index 4791c9a6..0a717cf5 100644 --- a/tokio/tests/io_read.rs +++ b/tokio/tests/io_read.rs @@ -1,7 +1,7 @@ #![warn(rust_2018_idioms)] #![cfg(feature = "full")] -use tokio::io::{AsyncRead, AsyncReadExt}; +use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf}; use tokio_test::assert_ok; use std::io; @@ -19,13 +19,13 @@ async fn read() { fn poll_read( mut self: Pin<&mut Self>, _cx: &mut Context<'_>, - buf: &mut [u8], - ) -> Poll<io::Result<usize>> { + buf: &mut ReadBuf<'_>, + ) -> Poll<io::Result<()>> { assert_eq!(0, self.poll_cnt); self.poll_cnt += 1; - buf[0..11].copy_from_slice(b"hello world"); - Poll::Ready(Ok(11)) + buf.append(b"hello world"); + Poll::Ready(Ok(())) } } @@ -36,25 +36,3 @@ async fn read() { assert_eq!(n, 11); assert_eq!(buf[..], b"hello world"[..]); } - -struct BadAsyncRead; - -impl AsyncRead for BadAsyncRead { - fn poll_read( - self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &mut [u8], - ) -> Poll<io::Result<usize>> { - for b in &mut *buf { - *b = b'a'; - } - Poll::Ready(Ok(buf.len() * 2)) - } -} - -#[tokio::test] -#[should_panic] -async fn read_buf_bad_async_read() { - let mut buf = Vec::with_capacity(10); - BadAsyncRead.read_buf(&mut buf).await.unwrap(); -} |