| 1 | #![warn (rust_2018_idioms)] |
| 2 | #![cfg (all(feature = "full" , not(target_os = "wasi" )))] // Wasi does not support panic recovery |
| 3 | |
| 4 | use tokio::io::{ |
| 5 | split, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf, ReadHalf, WriteHalf, |
| 6 | }; |
| 7 | |
| 8 | use std::io; |
| 9 | use std::pin::Pin; |
| 10 | use std::task::{Context, Poll}; |
| 11 | |
| 12 | struct RW; |
| 13 | |
| 14 | impl AsyncRead for RW { |
| 15 | fn poll_read( |
| 16 | self: Pin<&mut Self>, |
| 17 | _cx: &mut Context<'_>, |
| 18 | buf: &mut ReadBuf<'_>, |
| 19 | ) -> Poll<io::Result<()>> { |
| 20 | buf.put_slice(&[b'z' ]); |
| 21 | Poll::Ready(Ok(())) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl AsyncWrite for RW { |
| 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::<ReadHalf<RW>>(); |
| 60 | assert_bound::<WriteHalf<RW>>(); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn split_stream_id() { |
| 65 | let (r1, w1) = split(RW); |
| 66 | let (r2, w2) = split(RW); |
| 67 | assert!(r1.is_pair_of(&w1)); |
| 68 | assert!(!r1.is_pair_of(&w2)); |
| 69 | assert!(r2.is_pair_of(&w2)); |
| 70 | assert!(!r2.is_pair_of(&w1)); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn unsplit_ok() { |
| 75 | let (r, w) = split(RW); |
| 76 | r.unsplit(w); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | #[should_panic ] |
| 81 | fn unsplit_err1() { |
| 82 | let (r, _) = split(RW); |
| 83 | let (_, w) = split(RW); |
| 84 | r.unsplit(w); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | #[should_panic ] |
| 89 | fn unsplit_err2() { |
| 90 | let (_, w) = split(RW); |
| 91 | let (r, _) = split(RW); |
| 92 | r.unsplit(w); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn method_delegation() { |
| 97 | let (mut r, mut w) = split(RW); |
| 98 | let mut buf = [0; 1]; |
| 99 | |
| 100 | tokio_test::block_on(async move { |
| 101 | assert_eq!(1, r.read(&mut buf).await.unwrap()); |
| 102 | assert_eq!(b'z' , buf[0]); |
| 103 | |
| 104 | assert_eq!(1, w.write(&[b'x' ]).await.unwrap()); |
| 105 | assert_eq!( |
| 106 | 2, |
| 107 | w.write_vectored(&[io::IoSlice::new(&[b'x' ])]) |
| 108 | .await |
| 109 | .unwrap() |
| 110 | ); |
| 111 | assert!(w.is_write_vectored()); |
| 112 | |
| 113 | assert!(w.flush().await.is_ok()); |
| 114 | assert!(w.shutdown().await.is_ok()); |
| 115 | }); |
| 116 | } |
| 117 | |