From fbf1a6d8d4ae89c24defc11b2642dc530f0732d0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 6 Apr 2023 11:02:03 +0100 Subject: [PATCH] JoinReadWrite: Move the example to the struct This makes it more prominent on the rustdoc page. --- crates/tor-async-utils/src/join_read_write.rs | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/crates/tor-async-utils/src/join_read_write.rs b/crates/tor-async-utils/src/join_read_write.rs index 3da4107e1..a95a17a90 100644 --- a/crates/tor-async-utils/src/join_read_write.rs +++ b/crates/tor-async-utils/src/join_read_write.rs @@ -19,6 +19,31 @@ use pin_project::pin_project; /// you probably want the `reunite` or `unsplit` method, instead of `JoinReadWrite`. /// /// Does *not* implement any kind of flushing behaviour when switching between reading and writing. +/// +/// # Example +/// +/// ``` +/// # #[tokio::main] +/// # async fn main() { +/// use tor_async_utils::JoinReadWrite; +/// use futures::{AsyncReadExt as _, AsyncWriteExt as _}; +/// +/// let read = b"hello\n"; +/// let mut read = &read[..]; +/// let mut write = Vec::::new(); +/// +/// let mut joined = JoinReadWrite::new(read, write); +/// +/// let mut got = String::new(); +/// let _: usize = joined.read_to_string(&mut got).await.unwrap(); +/// assert_eq!(got, "hello\n"); +/// +/// let () = joined.write_all(b"some data").await.unwrap(); +/// +/// let (r, w) = joined.into_parts(); +/// assert_eq!(w, b"some data"); +/// # } +/// ``` #[pin_project] pub struct JoinReadWrite { /// readable @@ -31,29 +56,6 @@ pub struct JoinReadWrite { impl JoinReadWrite { /// Join an `AsyncRead` and an `AsyncWrite` into a single `impl AsyncRead + AsyncWrite` - /// - /// ``` - /// # #[tokio::main] - /// # async fn main() { - /// use tor_async_utils::JoinReadWrite; - /// use futures::{AsyncReadExt as _, AsyncWriteExt as _}; - /// - /// let read = b"hello\n"; - /// let mut read = &read[..]; - /// let mut write = Vec::::new(); - /// - /// let mut joined = JoinReadWrite::new(read, write); - /// - /// let mut got = String::new(); - /// let _: usize = joined.read_to_string(&mut got).await.unwrap(); - /// assert_eq!(got, "hello\n"); - /// - /// let () = joined.write_all(b"some data").await.unwrap(); - /// - /// let (r, w) = joined.into_parts(); - /// assert_eq!(w, b"some data"); - /// # } - /// ``` pub fn new(r: R, w: W) -> Self { JoinReadWrite { r, w } }