| 1 |
#![warn(rust_2018_idioms)] |
| 2 |
#![cfg(feature = "full")] |
| 3 |
|
| 4 |
use tokio::io::{join, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, Join, ReadBuf}; |
| 5 |
|
| 6 |
use std::io; |
| 7 |
use std::pin::Pin; |
| 8 |
use std::task::{Context, Poll}; |
| 9 |
|
| 10 |
struct R; |
| 11 |
|
| 12 |
impl AsyncRead for R { |
| 13 |
fn poll_read( |
| 14 |
self: Pin<&mut Self>, |
| 15 |
_cx: &mut Context<'_>, |
| 16 |
buf: &mut ReadBuf<'_>, |
| 17 |
) -> Poll<io::Result<()>> { |
| 18 |
buf.put_slice(&[b'z']); |
| 19 |
Poll::Ready(Ok(())) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
struct W; |
| 24 |
|
| 25 |
impl AsyncWrite for W { |
| 26 |
fn poll_write( |
| 27 |
self: Pin<&mut Self>, |
| 28 |
_cx: &mut Context<'_>, |
| 29 |
_buf: &[u8], |
| 30 |
) -> Poll<Result<usize, io::Error>> { |
| 31 |
Poll::Ready(Ok(1)) |
| 32 |
} |
| 33 |
|
| 34 |
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { |
| 35 |
Poll::Ready(Ok(())) |
| 36 |
} |
| 37 |
|
| 38 |
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { |
| 39 |
Poll::Ready(Ok(())) |
| 40 |
} |
| 41 |
|
| 42 |
fn poll_write_vectored( |
| 43 |
self: Pin<&mut Self>, |
| 44 |
_cx: &mut Context<'_>, |
| 45 |
_bufs: &[io::IoSlice<'_>], |
| 46 |
) -> Poll<Result<usize, io::Error>> { |
| 47 |
Poll::Ready(Ok(2)) |
| 48 |
} |
| 49 |
|
| 50 |
fn is_write_vectored(&self) -> bool { |
| 51 |
true |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn is_send_and_sync() { |
| 57 |
fn assert_bound<T: Send + Sync>() {} |
| 58 |
|
| 59 |
assert_bound::<Join<W, R>>(); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn method_delegation() { |
| 64 |
let mut rw = join(R, W); |
| 65 |
let mut buf = [0; 1]; |
| 66 |
|
| 67 |
tokio_test::block_on(async move { |
| 68 |
assert_eq!(1, rw.read(&mut buf).await.unwrap()); |
| 69 |
assert_eq!(b'z', buf[0]); |
| 70 |
|
| 71 |
assert_eq!(1, rw.write(&[b'x']).await.unwrap()); |
| 72 |
assert_eq!( |
| 73 |
2, |
| 74 |
rw.write_vectored(&[io::IoSlice::new(&[b'x'])]) |
| 75 |
.await |
| 76 |
.unwrap() |
| 77 |
); |
| 78 |
assert!(rw.is_write_vectored()); |
| 79 |
|
| 80 |
assert!(rw.flush().await.is_ok()); |
| 81 |
assert!(rw.shutdown().await.is_ok()); |
| 82 |
}); |
| 83 |
} |