From 56e337d464236972928f0a26743ade1e0b49ee4b Mon Sep 17 00:00:00 2001 From: lixiang365 Date: Mon, 2 Sep 2024 21:16:29 +0800 Subject: [PATCH] fix doctest error --- tokio-util/Cargo.toml | 4 ++++ tokio-util/src/io/sync_bridge.rs | 33 ++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/tokio-util/Cargo.toml b/tokio-util/Cargo.toml index 23a577a37fe..b73b663a8f5 100644 --- a/tokio-util/Cargo.toml +++ b/tokio-util/Cargo.toml @@ -57,6 +57,10 @@ futures = "0.3.0" futures-test = "0.3.5" parking_lot = "0.12.0" tempfile = "3.1.0" +blake3 = "1.5.4" +async-compression = { version = "*", features = ["all-implementations", "zlib"] } +serde_json = "1.0" +serde = { version = "1.0", features = ["derive"] } [package.metadata.docs.rs] all-features = true diff --git a/tokio-util/src/io/sync_bridge.rs b/tokio-util/src/io/sync_bridge.rs index 67c2b2f8bed..cf62eedf481 100644 --- a/tokio-util/src/io/sync_bridge.rs +++ b/tokio-util/src/io/sync_bridge.rs @@ -168,7 +168,12 @@ impl SyncIoBridge { /// or uses [`AsyncReadExt::read`] to asynchronously read some bytes /// into a fixed-size buffer each time, looping until all data is read: /// - /// ```no_run + /// ```rust,no_run + /// # use tokio::io::{AsyncReadExt, BufReader}; + /// # async fn docs() -> std::io::Result<()> { + /// # let data = b"example"; + /// # let mut reader = BufReader::new(&data[..]); + /// let mut hasher = blake3::Hasher::new(); /// let mut data = vec![0; 64*1024]; /// loop { /// let len = reader.read(&mut data).await?; @@ -176,19 +181,31 @@ impl SyncIoBridge { /// hasher.update(&data[..len]); /// } /// let hash = hasher.finalize(); + /// # Ok(()) + /// # } /// ``` /// /// This example demonstrates how [`SyncIoBridge`] converts an asynchronous data stream /// into synchronous reading, using `block_on` internally to block and read the data. /// you should do not this: /// - /// ```no_run + /// ```rust,no_run + /// # use tokio::task; + /// # use tokio::io::BufReader; + /// # use tokio_util::io::SyncIoBridge; + /// # use tokio::task::JoinError; + /// # async fn docs() -> Result<(), JoinError> { + /// # let data = b"example"; + /// # let reader = BufReader::new(&data[..]); /// task::spawn_blocking(move || { - /// let hasher = blake3::Hasher::new(); - /// let reader = SyncIoBridge::new(reader); - /// std::io::copy(&mut reader, &mut hasher); + /// let mut hasher = blake3::Hasher::new(); + /// let mut reader = SyncIoBridge::new(reader); + /// let _ = std::io::copy(&mut reader, &mut hasher); /// let hash = hasher.finalize(); - /// }) + /// }).await?; + /// + /// # Ok(()) + /// # } /// ``` /// /// In the three examples above, the first two involve asynchronously reading data within the current runtime context. @@ -202,7 +219,7 @@ impl SyncIoBridge { /// How to compress a stream of data correctly. /// (use async-compression and don't pass a `SyncIoBridge` to a non-async compression library): /// - /// ```rust + /// ```ignore /// use std::io::Result; /// use async_compression::tokio::write::ZlibEncoder; /// use tokio::io::{AsyncWriteExt, BufReader, BufWriter}; // for `write_all` and `shutdown` @@ -264,7 +281,7 @@ impl SyncIoBridge { /// and `block_on` cannot drive I/O or timers, it must wait for other threads in the runtime to handle I/O. /// Therefore, using tokio::fs::File for file handling within `spawn_blocking` is inefficient: /// - /// ```rust + /// ```rust,no_run /// use tokio::task; /// use std::fs; /// use std::io::{self, Read, Write};