|
41 | 41 | </h3>
|
42 | 42 | </div>
|
43 | 43 |
|
| 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 | + |
44 | 108 | ## Installation
|
45 | 109 | ```sh
|
46 | 110 | $ cargo add futures-concurrency
|
|
0 commit comments