Crates.io | spawn_groups |
lib.rs | spawn_groups |
version | 1.0.0 |
source | src |
created_at | 2023-09-24 16:55:36.451847 |
updated_at | 2023-09-24 17:48:06.546001 |
description | Structured concurrency construct written in Rust, for Rustaceans |
homepage | |
repository | https://github.com/Genaro-Chris/spawn_groups/ |
max_upload_size | |
id | 982035 |
size | 66,681 |
A structured concurrency construct which provides a way to spawn and run an arbitrary number of child tasks,
possibly await the results of each child task immediately after it has finish executing, cancel all running child tasks or wait for all child tasks to finish their execution.
This was heavily influenced by the Swift language's TaskGroup
.
Add to your code
$ cargo add spawn_groups
use async_std::io::{self};
use async_std::net::{TcpListener, TcpStream};
use async_std::prelude::*;
use spawn_groups::{with_err_spawn_group, GetType, Priority};
async fn process(stream: TcpStream) -> io::Result<()> {
println!("Accepted from local: {}", stream.local_addr()?);
println!("Accepted from: {}", stream.peer_addr()?);
let mut reader = stream.clone();
let mut writer = stream;
io::copy(&mut reader, &mut writer).await?;
Ok(())
}
type Void = ();
#[async_std::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
println!("Listening on {}", listener.local_addr()?);
with_err_spawn_group(Void::TYPE, io::Error::TYPE, |mut group| async move {
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let Ok(stream) = stream else {
return Err(stream.expect_err("Expected an error"));
};
group.spawn_task(Priority::default(), async move { process(stream).await });
}
Ok(())
})
.await?;
Ok(())
}
For a better documentation of this rust crate. Visit here