Skip to content

Commit

Permalink
Add documentation for SyncIoBridge with examples and alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathy-bajo committed Sep 3, 2024
1 parent 30d38d5 commit 881d3fa
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tokio-util/src/io/sync_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ use tokio::io::{
/// When hashing data, using `SyncIoBridge` can lead to suboptimal performance and might not fully leverage the async capabilities of the system.
/// Instead, consider reading the data into memory and then hashing it, or processing the data in chunks.
///
/// ```rust
/// ```rust, no_run
/// let mut data = Vec::new();
/// reader.read_to_end(&mut data).await?;
/// let hash = blake3::hash(&data);
/// ```
///
/// Or, for more complex cases:
///
/// ```rust
/// ```rust, no_run
/// let mut data = vec![0; 64 * 1024];
/// loop {
/// let len = reader.read(&mut data).await?;
Expand All @@ -40,7 +40,7 @@ use tokio::io::{
/// When compressing data, avoid using `SyncIoBridge`` with non-async compression libraries, as it may lead to inefficient and blocking code.
/// Instead, use `async-compression`, which is designed to work with asynchronous data streams.
///
/// ```rust
/// ```rust, no_run
/// use async_compression::tokio::write::GzipEncoder;
///
/// let mut encoder = GzipEncoder::new(writer);
Expand All @@ -52,7 +52,7 @@ use tokio::io::{
/// When parsing `JSON` data, avoid using `SyncIoBridge` with `serde_json::from_reader` as it may cause blocking operations.
/// Instead, read the data into a `Vec<u8>` and parse it using `serde_json::from_slice`.
///
/// ```rust
/// ```rust, no_run
/// let mut data = Vec::new();
/// reader.read_to_end(&mut data).await?;
/// let value: MyStruct = serde_json::from_slice(&data)?;
Expand All @@ -63,7 +63,7 @@ use tokio::io::{
/// `SyncIoBridge` is mainly useful when you need to interface with synchronous libraries from an asynchronous context.
/// Here is how you can do it correctly:
///
/// ```rust
/// ```rust, no_run
/// use tokio::task::spawn_blocking;
/// use tokio::io::AsyncReadExt;
/// use tokio_util::io::SyncIoBridge;
Expand Down

0 comments on commit 881d3fa

Please sign in to comment.