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 6570241 commit 44d0500
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions russh/src/channels/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,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 @@ -381,28 +381,35 @@ 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 [`io::ChannelRx`] 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 [`io::ChannelRx`] 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 [`io::ChannelTx`] 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 [`io::ChannelTx`] reader 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 44d0500

Please sign in to comment.