#async #collection #stack #mutex #snippets #sync #tomt

tomt_async

Primarily a dumping ground for personal async snippets that may be used in other ToMT projects. If this crate is useful to others please let us know.

4 releases

0.1.3 Jan 31, 2024
0.1.2 Jan 23, 2024
0.1.1 Jan 23, 2024
0.1.0 Jan 23, 2024

#1158 in Algorithms

36 downloads per month

MIT/Apache

17KB
450 lines

Brief

This crate is primarily a dumping ground for personal async snippets that may be used in other ToMT projects.

However should this crate prove useful to others please let us know.

Usage

tomt_async::sync::Mutex

use std::sync::Arc;
use tomt_async::sync::Mutex;

async fn main()
{
    let shared_mutex = Arc::new(Mutex::new(0));

    do_something(shared_mutex.clone()).await;
}

async do_something(
    shared_mutex: Arc<Mutex<i32>>
) {
    let mut lock = shared_mutex.lock().await;
    *lock = *lock + 1;

    // lock is released on drop, though it's highly recommended to avoid
    // calling any async funcs while the lock is held
}

tomt_async::collections::stack

use tomt_async::collections::Stack;

async fn main()
{
    let stack = Stack::<i32>::new();

    stack.push(5).await;
    stack.push(8).await;

    do_something(stack).await;
}

async fn do_something<T: std::fmt::Debug>(
    stack: Stack<T>
) {
    while let Some(item) = stack.pop().await {
        println!("Item = {item:?}");
    }
}

No runtime deps

Features