-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_ext.rs
53 lines (46 loc) · 1.23 KB
/
io_ext.rs
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
use std::{
pin::Pin,
task::{self, Poll},
};
use pin_project_lite::pin_project;
use tokio::io::{AsyncRead, AsyncWrite};
pin_project! {
pub struct Unsplit<A, B> {
#[pin]
reader: A,
#[pin]
writer: B,
}
}
impl<A, B> Unsplit<A, B> {
pub fn new(reader: A, writer: B) -> Self {
Self { reader, writer }
}
}
impl<A: AsyncRead, B: AsyncWrite> AsyncWrite for Unsplit<A, B> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &[u8],
) -> Poll<tokio::io::Result<usize>> {
self.project().writer.poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<tokio::io::Result<()>> {
self.project().writer.poll_flush(cx)
}
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<tokio::io::Result<()>> {
self.project().writer.poll_shutdown(cx)
}
}
impl<A: AsyncRead, B: AsyncWrite> AsyncRead for Unsplit<A, B> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<tokio::io::Result<()>> {
self.project().reader.poll_read(cx, buf)
}
}