3 releases

0.1.1 Dec 23, 2023
0.1.0 Dec 23, 2023
0.1.0-preview1 Dec 18, 2023

#668 in Asynchronous

MIT license

17KB
278 lines

Overview

This crate provides an FutureOnceCell cell-like type, which provides the similar API as the tokio::task_local but without using any macros.

Future local storage associates a value to the context of a given future. After the future finished it returns this value back to the caller. That meaning that the values is passed through the context of the executed future. This functionality can be useful for tracing async code or adding metrics to it.

Usage

use std::cell::Cell;

use future_local_storage::FutureOnceCell;

static VALUE: FutureOnceCell<Cell<u64>> = FutureOnceCell::new();

#[tokio::main]
async fn main() {
    let (output, answer) = VALUE.scope(Cell::from(0), async {
        VALUE.with(|x| {
            let value = x.get();
            x.set(value + 1);
        });

        "42".to_owned()
    }).await;

    assert_eq!(output.into_inner(), 1);
    assert_eq!(answer, "42");
}

lib.rs:

Examples

Tracing spans


// Usage example

async fn some_method(mut a: u64) -> u64 {
    TracerContext::on_enter(format!("`some_method` with params: a={a}"));
   
    // Some async computation
    
    TracerContext::on_exit("`some_method`");
    a * 32
}

#[tokio::main]
async fn main() {
    let (trace, result) = TracerContext::in_scope(some_method(45)).await;

    println!("answer: {result}");
    println!("trace: {trace:#?}");
}

Dependencies

~0.4–27MB
~349K SLoC