channels_io/impls/
tokio.rs

1use super::prelude::*;
2
3/// Wrapper IO type for [`tokio::io::AsyncRead`] and [`tokio::io::AsyncWrite`].
4///
5/// [`tokio::io::AsyncRead`]: ::tokio::io::AsyncRead
6/// [`tokio::io::AsyncWrite`]: ::tokio::io::AsyncWrite
7#[derive(Debug)]
8#[pin_project]
9pub struct Tokio<T>(#[pin] pub T);
10
11impl_newtype! { Tokio }
12
13impl_newtype_read! { Tokio: ::tokio::io::AsyncRead }
14
15impl<T> AsyncRead for Tokio<T>
16where
17	T: ::tokio::io::AsyncRead,
18{
19	type Error = ::tokio::io::Error;
20
21	fn poll_read_slice(
22		self: Pin<&mut Self>,
23		cx: &mut Context,
24		buf: &mut [u8],
25	) -> Poll<Result<usize, Self::Error>> {
26		let this = self.project();
27
28		let mut read_buf = ::tokio::io::ReadBuf::new(buf);
29		ready!(this.0.poll_read(cx, &mut read_buf))?;
30		let n = read_buf.filled().len();
31		Poll::Ready(Ok(n))
32	}
33}
34
35impl_newtype_write! { Tokio: ::tokio::io::AsyncWrite }
36
37impl<T> AsyncWrite for Tokio<T>
38where
39	T: ::tokio::io::AsyncWrite,
40{
41	type Error = ::tokio::io::Error;
42
43	fn poll_write_slice(
44		self: Pin<&mut Self>,
45		cx: &mut Context,
46		buf: &[u8],
47	) -> Poll<Result<usize, Self::Error>> {
48		let this = self.project();
49		this.0.poll_write(cx, buf)
50	}
51
52	fn poll_flush_once(
53		self: Pin<&mut Self>,
54		cx: &mut Context,
55	) -> Poll<Result<(), Self::Error>> {
56		let this = self.project();
57		this.0.poll_flush(cx)
58	}
59}