Skip to content

Commit e00c1b0

Browse files
committed
misc(docs): update async usage in readme
1 parent e7eda3d commit e00c1b0

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Works seamlessly with Tokio:
8181
```rust
8282
use std::{iter::repeat_n, time::Duration};
8383

84-
use compact_waitgroup::{WaitGroup, WithWorkerHandle};
84+
use compact_waitgroup::{GroupTokenExt, WaitGroup};
8585
use tokio::time::sleep;
8686

8787
#[tokio::main]
@@ -101,8 +101,17 @@ async fn main() {
101101
}
102102
});
103103

104-
// Wait for the task to complete
105-
wg.await;
104+
tokio::pin!(wg);
105+
loop {
106+
tokio::select! {
107+
_ = sleep(Duration::from_millis(200)) => {
108+
println!("Running...");
109+
}
110+
_ = &mut wg => {
111+
break;
112+
}
113+
};
114+
}
106115
println!("All done!");
107116
}
108117
```

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@
4545
//! # });
4646
//! ```
4747
//!
48+
//! ## With `async` Runtime
49+
//!
50+
//! ```rust
51+
//! # use core::{iter::repeat_n, mem::drop as spawn, time::Duration};
52+
//! # use compact_waitgroup::{GroupTokenExt, WaitGroup};
53+
//! # let sleep = |_| async {};
54+
//! # futures_executor::block_on(async {
55+
//! let (wg, factory) = WaitGroup::new();
56+
//! for (i, token) in repeat_n(factory.into_token(), 8).enumerate() {
57+
//! let task = async move {
58+
//! println!("Task {i} started");
59+
//! // Long-running task...
60+
//! sleep(Duration::from_secs(1)).await;
61+
//! println!("Task {i} finished");
62+
//! }
63+
//! .release_on_ready(token);
64+
//! spawn(task);
65+
//! }
66+
//! // Wait for all tasks to complete
67+
//! wg.await;
68+
//! # });
69+
//! ```
70+
//!
4871
//! # Memory Layout
4972
//!
5073
//! This crate is designed to be extremely lightweight. The memory footprint

0 commit comments

Comments
 (0)