hypercore_protocol/
duplex.rs1use futures_lite::{AsyncRead, AsyncWrite};
2use std::io;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6#[derive(Clone, Debug, PartialEq)]
7pub struct Duplex<R, W>
12where
13 R: AsyncRead + Send + Unpin + 'static,
14 W: AsyncWrite + Send + Unpin + 'static,
15{
16 reader: R,
17 writer: W,
18}
19
20impl<R, W> Duplex<R, W>
21where
22 R: AsyncRead + Send + Unpin + 'static,
23 W: AsyncWrite + Send + Unpin + 'static,
24{
25 pub fn new(reader: R, writer: W) -> Self {
27 Self { reader, writer }
28 }
29}
30
31impl<R, W> AsyncRead for Duplex<R, W>
32where
33 R: AsyncRead + Send + Unpin + 'static,
34 W: AsyncWrite + Send + Unpin + 'static,
35{
36 fn poll_read(
37 mut self: Pin<&mut Self>,
38 cx: &mut Context<'_>,
39 buf: &mut [u8],
40 ) -> Poll<io::Result<usize>> {
41 Pin::new(&mut self.reader).poll_read(cx, buf)
42 }
43}
44
45impl<R, W> AsyncWrite for Duplex<R, W>
46where
47 R: AsyncRead + Send + Unpin + 'static,
48 W: AsyncWrite + Send + Unpin + 'static,
49{
50 fn poll_write(
51 mut self: Pin<&mut Self>,
52 cx: &mut Context<'_>,
53 buf: &[u8],
54 ) -> Poll<io::Result<usize>> {
55 Pin::new(&mut self.writer).poll_write(cx, buf)
56 }
57
58 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
59 Pin::new(&mut self.writer).poll_flush(cx)
60 }
61
62 fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
63 Pin::new(&mut self.writer).poll_close(cx)
64 }
65}