Skip to content

Commit

Permalink
Switch to a 'make_{reader, writer}' design instead of 'Channel::into_…
Browse files Browse the repository at this point in the history
…io_parts'
  • Loading branch information
lowlevl committed Sep 21, 2023
1 parent 9600bcb commit b06f23e
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions russh/src/channels/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl<S: From<(ChannelId, ChannelMsg)> + Send + 'static> Channel<S> {
ext: Option<u32>,
mut data: R,
) -> Result<(), Error> {
let (mut tx, _) = self.into_io_parts_ext(ext);
let mut tx = self.make_writer_ext(ext);

tokio::io::copy(&mut data, &mut tx).await?;

Expand Down Expand Up @@ -387,28 +387,33 @@ impl<S: From<(ChannelId, ChannelMsg)> + Send + 'static> Channel<S> {
stream
}

/// Setup the [`Channel`] to be able to send and receive [`ChannelMsg::Data`]
/// through [`io::ChannelTx`] and [`io::ChannelRx`].
pub fn into_io_parts(&mut self) -> (io::ChannelTx<S>, io::ChannelRx<'_, S>) {
self.into_io_parts_ext(None)
/// Make a reader for the [`Channel`] to receive [`ChannelMsg::Data`]
/// through the [`tokio::io::AsyncRead`] trait.
pub fn make_reader(&mut self) -> io::ChannelRx<'_, S> {
self.make_reader_ext(None)
}

/// Setup the [`Channel`] to be able to send and receive [`ChannelMsg::Data`]
/// or [`ChannelMsg::ExtendedData`] through [`io::ChannelTx`] and [`io::ChannelRx`]
/// depending on the `ext` parameter.
pub fn into_io_parts_ext(
&mut self,
ext: Option<u32>,
) -> (io::ChannelTx<S>, io::ChannelRx<'_, S>) {
(
io::ChannelTx::new(
self.sender.clone(),
self.id,
self.window_size.clone(),
self.max_packet_size,
ext,
),
io::ChannelRx::new(self, ext),
/// Make a reader for the [`Channel`] to receive [`ChannelMsg::Data`] or [`ChannelMsg::ExtendedData`]
/// depending on the `ext` parameter, through the [`tokio::io::AsyncRead`] trait.
pub fn make_reader_ext(&mut self, ext: Option<u32>) -> io::ChannelRx<'_, S> {
io::ChannelRx::new(self, ext)
}

/// Make a writer for the [`Channel`] to send [`ChannelMsg::Data`]
/// through the [`tokio::io::AsyncWrite`] trait.
pub fn make_writer(&self) -> io::ChannelTx<S> {
self.make_writer_ext(None)
}

/// Make a writer for the [`Channel`] to send [`ChannelMsg::Data`] or [`ChannelMsg::ExtendedData`]
/// depending on the `ext` parameter, through the [`tokio::io::AsyncWrite`] trait.
pub fn make_writer_ext(&self, ext: Option<u32>) -> io::ChannelTx<S> {
io::ChannelTx::new(
self.sender.clone(),
self.id,
self.window_size.clone(),
self.max_packet_size,
ext,
)
}
}

0 comments on commit b06f23e

Please sign in to comment.