Skip to content

Commit 8f4e77e

Browse files
committed
add examples to README
1 parent 6dde6da commit 8f4e77e

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,70 @@
4141
</h3>
4242
</div>
4343

44+
Performant, portable, structured concurrency operations for async Rust. It
45+
works with any runtime, does not erase lifetimes, always handles
46+
cancellation, and always returns output to the caller.
47+
48+
`futures-concurrency` provides concurrency operations for both groups of futures
49+
and streams. Both for bounded and unbounded sets of futures and streams. In both
50+
cases performance should be on par with, if not exceed conventional executor
51+
implementations.
52+
53+
## Examples
54+
55+
**Await multiple futures of different types**
56+
```rust
57+
use futures_concurrency::prelude::*;
58+
use std::future;
59+
60+
let a = future::ready(1u8);
61+
let b = future::ready("hello");
62+
let c = future::ready(3u16);
63+
assert_eq!((a, b, c).join().await, (1, "hello", 3));
64+
```
65+
66+
**Concurrently process items in a stream**
67+
68+
```rust
69+
use futures_concurrency::prelude::*;
70+
use futures_lite::stream;
71+
72+
# futures::executor::block_on(async {
73+
let v: Vec<_> = vec!["chashu", "nori"]
74+
.into_co_stream()
75+
.map(|msg| async move { format!("hello {msg}") })
76+
.collect()
77+
.await;
78+
79+
assert_eq!(v, &["hello chashu", "hello nori"]);
80+
```
81+
82+
**Access stack data outside the futures' scope**
83+
84+
_Adapted from [`std::thread::scope`](https://doc.rust-lang.org/std/thread/fn.scope.html)._
85+
86+
```rust
87+
use futures_concurrency::prelude::*;
88+
89+
let mut container = vec![1, 2, 3];
90+
let mut num = 0;
91+
92+
let a = async {
93+
println!("hello from the first future");
94+
dbg!(&container);
95+
};
96+
97+
let b = async {
98+
println!("hello from the second future");
99+
num += container[0] + container[2];
100+
};
101+
102+
println!("hello from the main future");
103+
let _ = (a, b).join().await;
104+
container.push(4);
105+
assert_eq!(num, container.len());
106+
```
107+
44108
## Installation
45109
```sh
46110
$ cargo add futures-concurrency

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! cases performance should be on par with, if not exceed conventional executor
88
//! implementations.
99
//!
10-
//! ## Examples
10+
//! # Examples
1111
//!
1212
//! **Await multiple futures of different types**
1313
//! ```rust
@@ -67,9 +67,9 @@
6767
//! # });
6868
//! ```
6969
//!
70-
//! ## Operations
70+
//! # Operations
7171
//!
72-
//! ### Futures
72+
//! ## Futures
7373
//!
7474
//! For futures which return a regular type `T` only the `join` and `race`
7575
//! operations are available. `join` waits for all futures to complete, while `race`
@@ -88,7 +88,7 @@
8888
//! - `array`: [`join`][future::Join#impl-Join-for-\[Fut;+N\]], [`try_join`][future::TryJoin#impl-TryJoin-for-\[Fut;+N\]], [`race`][future::Race#impl-Race-for-\[Fut;+N\]], [`race_ok`][future::RaceOk#impl-RaceOk-for-\[Fut;+N\]]
8989
//! - `Vec`: [`join`][future::Join#impl-Join-for-Vec<Fut>], [`try_join`][future::TryJoin#impl-TryJoin-for-Vec<Fut>], [`race`][future::Race#impl-Race-for-Vec<Fut>], [`race_ok`][future::RaceOk#impl-RaceOk-for-Vec<Fut>]
9090
//!
91-
//! ### Streams
91+
//! ## Streams
9292
//!
9393
//! Streams yield outputs one-by-one, which means that deciding to stop iterating is
9494
//! the same for fallible and infallible streams. The operations provided for
@@ -116,15 +116,15 @@
116116
//! - `array`: [`chain`][stream::Chain#impl-Chain-for-\[Fut;+N\]], [`merge`][stream::Merge#impl-Merge-for-\[Fut;+N\]], [`zip`][stream::Zip#impl-Zip-for-\[Fut;+N\]]
117117
//! - `Vec`: [`chain`][stream::Chain#impl-Chain-for-Vec<Fut>], [`merge`][stream::Merge#impl-Merge-for-Vec<Fut>], [`zip`][stream::Zip#impl-Zip-for-Vec<Fut>]
118118
//!
119-
//! ## Runtime Support
119+
//! # Runtime Support
120120
//!
121121
//! `futures-concurrency` does not depend on any runtime executor being present.
122122
//! This enables it to work out of the box with any async runtime, including:
123123
//! `tokio`, `async-std`, `smol`, `glommio`, and `monoio`. It also supports
124124
//! `#[no_std]` environments, allowing it to be used with embedded async
125125
//! runtimes such as `embassy`.
126126
//!
127-
//! ## Feature Flags
127+
//! # Feature Flags
128128
//!
129129
//! The `std` feature flag is enabled by default. To target `alloc` or `no_std`
130130
//! environments, you can enable the following configuration:
@@ -138,7 +138,7 @@
138138
//! futures-concurrency = { version = "7.5.0", default-features = false, features = ["alloc"] }
139139
//! ```
140140
//!
141-
//! ## Further Reading
141+
//! # Further Reading
142142
//!
143143
//! `futures-concurrency` has been developed over the span of several years. It is
144144
//! primarily maintained by Yosh Wuyts, a member of the Rust Async WG. You can read

0 commit comments

Comments
 (0)