Skip to content

Commit

Permalink
fix doctest error
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiang365 committed Sep 2, 2024
1 parent 42f8ed8 commit 56e337d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions tokio-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 25 additions & 8 deletions tokio-util/src/io/sync_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,44 @@ impl<T: Unpin> SyncIoBridge<T> {
/// 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?;
/// if len == 0 { break; }
/// 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.
Expand All @@ -202,7 +219,7 @@ impl<T: Unpin> SyncIoBridge<T> {
/// 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`
Expand Down Expand Up @@ -264,7 +281,7 @@ impl<T: Unpin> SyncIoBridge<T> {
/// 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};
Expand Down

0 comments on commit 56e337d

Please sign in to comment.