1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
3
4 use tokio::io::{split, AsyncRead, AsyncWrite, ReadBuf, ReadHalf, WriteHalf};
5
6 use std::io;
7 use std::pin::Pin;
8 use std::task::{Context, Poll};
9
10 struct RW;
11
12 impl AsyncRead for RW {
poll_read( self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<io::Result<()>>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 impl AsyncWrite for RW {
poll_write( self: Pin<&mut Self>, _cx: &mut Context<'_>, _buf: &[u8], ) -> Poll<Result<usize, io::Error>>24 fn poll_write(
25 self: Pin<&mut Self>,
26 _cx: &mut Context<'_>,
27 _buf: &[u8],
28 ) -> Poll<Result<usize, io::Error>> {
29 Poll::Ready(Ok(1))
30 }
31
poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>>32 fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
33 Poll::Ready(Ok(()))
34 }
35
poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>>36 fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
37 Poll::Ready(Ok(()))
38 }
39 }
40
41 #[test]
is_send_and_sync()42 fn is_send_and_sync() {
43 fn assert_bound<T: Send + Sync>() {}
44
45 assert_bound::<ReadHalf<RW>>();
46 assert_bound::<WriteHalf<RW>>();
47 }
48
49 #[test]
split_stream_id()50 fn split_stream_id() {
51 let (r1, w1) = split(RW);
52 let (r2, w2) = split(RW);
53 assert!(r1.is_pair_of(&w1));
54 assert!(!r1.is_pair_of(&w2));
55 assert!(r2.is_pair_of(&w2));
56 assert!(!r2.is_pair_of(&w1));
57 }
58
59 #[test]
unsplit_ok()60 fn unsplit_ok() {
61 let (r, w) = split(RW);
62 r.unsplit(w);
63 }
64
65 #[test]
66 #[should_panic]
unsplit_err1()67 fn unsplit_err1() {
68 let (r, _) = split(RW);
69 let (_, w) = split(RW);
70 r.unsplit(w);
71 }
72
73 #[test]
74 #[should_panic]
unsplit_err2()75 fn unsplit_err2() {
76 let (_, w) = split(RW);
77 let (r, _) = split(RW);
78 r.unsplit(w);
79 }
80