1#![doc = include_str!("../README.md")]
2
3pub mod bridge;
4
5use std::pin::Pin;
6use std::task::{Context, Poll};
7use tokio::io::{AsyncRead, ReadBuf, AsyncWrite};
8
9pub struct AsyncStream<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> {
11 reader: R,
12 writer: W,
13}
14
15impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> AsyncStream<R, W> {
16 pub fn new(reader: R, writer: W) -> Self {
17 Self { reader, writer }
18 }
19
20 pub fn split<'a>(&'a mut self) -> (AsyncReadHalf<'a, R>, AsyncWriteHalf<'a, W>) {
22 (AsyncReadHalf { inner: &mut self.reader }, AsyncWriteHalf { inner: &mut self.writer })
23 }
24
25 pub fn into_split(self) -> (OwnedAsyncReadHalf<R>, OwnedAsyncWriteHalf<W>) {
27 (OwnedAsyncReadHalf { inner: self.reader }, OwnedAsyncWriteHalf { inner: self.writer })
28 }
29}
30
31impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> AsyncRead for AsyncStream<R, W> {
32 fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
33 Pin::new(&mut self.get_mut().reader).poll_read(cx, buf)
34 }
35}
36
37impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> AsyncWrite for AsyncStream<R, W> {
38 fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
39 Pin::new(&mut self.get_mut().writer).poll_write(cx, buf)
40 }
41
42 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
43 Pin::new(&mut self.get_mut().writer).poll_flush(cx)
44 }
45
46 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
47 Pin::new(&mut self.get_mut().writer).poll_shutdown(cx)
48 }
49}
50
51pub struct AsyncReadHalf<'a, T: AsyncRead + Unpin> {
52 inner: &'a mut T,
53}
54
55impl<'a, T: AsyncRead + Unpin> AsyncRead for AsyncReadHalf<'a, T> {
56 fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
57 Pin::new(&mut self.get_mut().inner).poll_read(cx, buf)
58 }
59}
60
61pub struct AsyncWriteHalf<'a, T: AsyncWrite + Unpin> {
62 inner: &'a mut T,
63}
64
65impl<'a, T: AsyncWrite + Unpin> AsyncWrite for AsyncWriteHalf<'a, T> {
66 fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
67 Pin::new(&mut self.get_mut().inner).poll_write(cx, buf)
68 }
69
70 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
71 Pin::new(&mut self.get_mut().inner).poll_flush(cx)
72 }
73
74 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
75 Pin::new(&mut self.get_mut().inner).poll_shutdown(cx)
76 }
77}
78
79pub struct OwnedAsyncReadHalf<T: AsyncRead + Unpin> {
80 inner: T,
81}
82
83impl<T: AsyncRead + Unpin> AsyncRead for OwnedAsyncReadHalf<T> {
84 fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
85 Pin::new(&mut self.get_mut().inner).poll_read(cx, buf)
86 }
87}
88
89pub struct OwnedAsyncWriteHalf<T: AsyncWrite + Unpin> {
90 inner: T,
91}
92
93impl<T: AsyncWrite + Unpin> AsyncWrite for OwnedAsyncWriteHalf<T> {
94 fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
95 Pin::new(&mut self.get_mut().inner).poll_write(cx, buf)
96 }
97
98 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
99 Pin::new(&mut self.get_mut().inner).poll_flush(cx)
100 }
101
102 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
103 Pin::new(&mut self.get_mut().inner).poll_shutdown(cx)
104 }
105}