channels_io/impls/
native.rs

1use super::prelude::*;
2
3/// Wrapper IO type for [`Read`] and [`Write`].
4#[derive(Debug)]
5pub struct Native<T>(pub T);
6
7impl_newtype! { Native }
8
9impl_newtype_read! { Native: Read }
10
11impl<T> Read for Native<T>
12where
13	T: Read,
14{
15	type Error = T::Error;
16
17	fn read_slice(
18		&mut self,
19		buf: &mut [u8],
20	) -> Result<usize, Self::Error> {
21		self.0.read_slice(buf)
22	}
23}
24
25impl_newtype_write! { Native: Write }
26
27impl<T> Write for Native<T>
28where
29	T: Write,
30{
31	type Error = T::Error;
32
33	fn write_slice(
34		&mut self,
35		buf: &[u8],
36	) -> Result<usize, Self::Error> {
37		self.0.write_slice(buf)
38	}
39
40	fn flush_once(&mut self) -> Result<(), Self::Error> {
41		self.0.flush_once()
42	}
43}
44
45/// Wrapper IO type for [`AsyncRead`] and [`AsyncWrite`].
46#[derive(Debug)]
47#[pin_project]
48pub struct NativeAsync<T>(#[pin] pub T);
49
50impl_newtype! { NativeAsync }
51
52impl_newtype_read! { NativeAsync: AsyncRead }
53
54impl<T> AsyncRead for NativeAsync<T>
55where
56	T: AsyncRead,
57{
58	type Error = T::Error;
59
60	fn poll_read_slice(
61		self: Pin<&mut Self>,
62		cx: &mut Context,
63		buf: &mut [u8],
64	) -> Poll<Result<usize, Self::Error>> {
65		let this = self.project();
66		this.0.poll_read_slice(cx, buf)
67	}
68}
69
70impl_newtype_write! { NativeAsync: AsyncWrite }
71
72impl<T> AsyncWrite for NativeAsync<T>
73where
74	T: AsyncWrite,
75{
76	type Error = T::Error;
77
78	fn poll_write_slice(
79		self: Pin<&mut Self>,
80		cx: &mut Context,
81		buf: &[u8],
82	) -> Poll<Result<usize, Self::Error>> {
83		let this = self.project();
84		this.0.poll_write_slice(cx, buf)
85	}
86
87	fn poll_flush_once(
88		self: Pin<&mut Self>,
89		cx: &mut Context,
90	) -> Poll<Result<(), Self::Error>> {
91		let this = self.project();
92		this.0.poll_flush_once(cx)
93	}
94}