libp2p_swarm/
stream.rs

1use futures::{AsyncRead, AsyncWrite};
2use libp2p_core::muxing::SubstreamBox;
3use libp2p_core::Negotiated;
4use std::io::{IoSlice, IoSliceMut};
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8#[derive(Debug)]
9pub struct Stream(Negotiated<SubstreamBox>);
10
11impl Stream {
12    pub(crate) fn new(stream: Negotiated<SubstreamBox>) -> Self {
13        Self(stream)
14    }
15}
16
17impl AsyncRead for Stream {
18    fn poll_read(
19        self: Pin<&mut Self>,
20        cx: &mut Context<'_>,
21        buf: &mut [u8],
22    ) -> Poll<std::io::Result<usize>> {
23        Pin::new(&mut self.get_mut().0).poll_read(cx, buf)
24    }
25
26    fn poll_read_vectored(
27        self: Pin<&mut Self>,
28        cx: &mut Context<'_>,
29        bufs: &mut [IoSliceMut<'_>],
30    ) -> Poll<std::io::Result<usize>> {
31        Pin::new(&mut self.get_mut().0).poll_read_vectored(cx, bufs)
32    }
33}
34
35impl AsyncWrite for Stream {
36    fn poll_write(
37        self: Pin<&mut Self>,
38        cx: &mut Context<'_>,
39        buf: &[u8],
40    ) -> Poll<std::io::Result<usize>> {
41        Pin::new(&mut self.get_mut().0).poll_write(cx, buf)
42    }
43
44    fn poll_write_vectored(
45        self: Pin<&mut Self>,
46        cx: &mut Context<'_>,
47        bufs: &[IoSlice<'_>],
48    ) -> Poll<std::io::Result<usize>> {
49        Pin::new(&mut self.get_mut().0).poll_write_vectored(cx, bufs)
50    }
51
52    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
53        Pin::new(&mut self.get_mut().0).poll_flush(cx)
54    }
55
56    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
57        Pin::new(&mut self.get_mut().0).poll_close(cx)
58    }
59}