File: io_join.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (83 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]

use tokio::io::{join, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, Join, ReadBuf};

use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

struct R;

impl AsyncRead for R {
    fn poll_read(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        buf.put_slice(&[b'z']);
        Poll::Ready(Ok(()))
    }
}

struct W;

impl AsyncWrite for W {
    fn poll_write(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        _buf: &[u8],
    ) -> Poll<Result<usize, io::Error>> {
        Poll::Ready(Ok(1))
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
        Poll::Ready(Ok(()))
    }

    fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
        Poll::Ready(Ok(()))
    }

    fn poll_write_vectored(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        _bufs: &[io::IoSlice<'_>],
    ) -> Poll<Result<usize, io::Error>> {
        Poll::Ready(Ok(2))
    }

    fn is_write_vectored(&self) -> bool {
        true
    }
}

#[test]
fn is_send_and_sync() {
    fn assert_bound<T: Send + Sync>() {}

    assert_bound::<Join<W, R>>();
}

#[test]
fn method_delegation() {
    let mut rw = join(R, W);
    let mut buf = [0; 1];

    tokio_test::block_on(async move {
        assert_eq!(1, rw.read(&mut buf).await.unwrap());
        assert_eq!(b'z', buf[0]);

        assert_eq!(1, rw.write(&[b'x']).await.unwrap());
        assert_eq!(
            2,
            rw.write_vectored(&[io::IoSlice::new(&[b'x'])])
                .await
                .unwrap()
        );
        assert!(rw.is_write_vectored());

        assert!(rw.flush().await.is_ok());
        assert!(rw.shutdown().await.is_ok());
    });
}