async-task-group

Crates.ioasync-task-group
lib.rsasync-task-group
version0.2.1
sourcesrc
created_at2021-10-25 10:06:47.75597
updated_at2021-10-25 10:06:47.75597
descriptionManage groups of `async-std` tasks as a single unit.
homepage
repositoryhttps://github.com/yoshuawuyts/async-task-group
max_upload_size
id470896
size35,392
Yosh (yoshuawuyts)

documentation

README

async-task-group

Manage groups of `async-std` tasks as a single unit.

Crates.io version Download docs.rs docs

API Docs | Releases | Contributing

Credit

This codebase is based on the task-group project by Pat Hickey.

Examples

Create an echo tcp server which processes incoming connections in a loop without ever creating any dangling tasks:

use async_std::io;
use async_std::net::{TcpListener, TcpStream};
use async_std::prelude::*;
use async_std::task;

async fn process(stream: TcpStream) -> io::Result<()> {
    println!("Accepted from: {}", stream.peer_addr()?);

    let mut reader = stream.clone();
    let mut writer = stream;
    io::copy(&mut reader, &mut writer).await?;

    Ok(())
}

#[async_std::main]
async fn main() -> io::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:8080").await?;
    println!("Listening on {}", listener.local_addr()?);

    let handle = async_task_group::group(|group| async move {
        let mut incoming = listener.incoming();
        while let Some(stream) = incoming.next().await {
            let stream = stream?;
            group.spawn(async move { process(stream).await });
        }
        Ok(group)
    });
    handle.await?;
    Ok(())
}

Installation

$ cargo add async-task-group

Safety

This crate uses #![deny(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

Contributing

Want to join us? Check out our "Contributing" guide and take a look at some of these issues:

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Commit count: 30

cargo fmt