bssh_russh/channels/
channel_stream.rs1use std::io;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use tokio::io::{AsyncRead, AsyncWrite};
6
7use super::io::{ChannelCloseOnDrop, ChannelRx, ChannelTx};
8use super::{ChannelId, ChannelMsg};
9
10pub struct ChannelStream<S>
12where
13 S: From<(ChannelId, ChannelMsg)> + Send + 'static,
14{
15 tx: ChannelTx<S>,
16 rx: ChannelRx<ChannelCloseOnDrop<S>>,
17}
18
19impl<S> ChannelStream<S>
20where
21 S: From<(ChannelId, ChannelMsg)> + Send,
22{
23 pub(super) fn new(tx: ChannelTx<S>, rx: ChannelRx<ChannelCloseOnDrop<S>>) -> Self {
24 Self { tx, rx }
25 }
26}
27
28impl<S> AsyncRead for ChannelStream<S>
29where
30 S: From<(ChannelId, ChannelMsg)> + Send,
31{
32 fn poll_read(
33 mut self: Pin<&mut Self>,
34 cx: &mut Context<'_>,
35 buf: &mut tokio::io::ReadBuf<'_>,
36 ) -> Poll<io::Result<()>> {
37 Pin::new(&mut self.rx).poll_read(cx, buf)
38 }
39}
40
41impl<S> AsyncWrite for ChannelStream<S>
42where
43 S: From<(ChannelId, ChannelMsg)> + 'static + Send + Sync,
44{
45 fn poll_write(
46 mut self: Pin<&mut Self>,
47 cx: &mut Context<'_>,
48 buf: &[u8],
49 ) -> Poll<Result<usize, io::Error>> {
50 Pin::new(&mut self.tx).poll_write(cx, buf)
51 }
52
53 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
54 Pin::new(&mut self.tx).poll_flush(cx)
55 }
56
57 fn poll_shutdown(
58 mut self: Pin<&mut Self>,
59 cx: &mut Context<'_>,
60 ) -> Poll<Result<(), io::Error>> {
61 Pin::new(&mut self.tx).poll_shutdown(cx)
62 }
63}