tokio-inherit-task-local

Crates.iotokio-inherit-task-local
lib.rstokio-inherit-task-local
version0.2.0
sourcesrc
created_at2024-05-08 17:48:07.384492
updated_at2024-05-08 19:04:23.981056
descriptionTask local variables for tokio that can be inherited across a spawn
homepage
repositoryhttps://github.com/Xaeroxe/tokio-inherit-task-local
max_upload_size
id1234004
size20,503
Jacob Kiesel (Xaeroxe)

documentation

https://docs.rs/tokio-inherit-task-local

README

tokio-inherit-task-local

Provides functionality very similar to tokio::task_local with one key difference. Any future annotated with .inherit_task_local() will inherit the task local values of the task which spawned it. This does not inherit values created by tokio::task_local, it will only inherit values created by inheritable_task_local.

Here's a simple example

use tokio_inherit_task_local::{inheritable_task_local, FutureInheritTaskLocal as _};

inheritable_task_local! {
    pub static DEMO_VALUE: u32;
}

async fn foo() {
    let out = DEMO_VALUE
        .scope(5, async {
            tokio::spawn(async { DEMO_VALUE.with(|&v| v) }.inherit_task_local()).await
        })
        .await
        .unwrap();
    assert_eq!(out, 5);
}

Even though DEMO_VALUE was not defined for the spawned future, it was still able to inherit the value defined in its parent. This happens thanks to the .inherit_task_local() method call. That method can be found in FutureInheritTaskLocal.

These inherited values DO NOT need to be Clone. Child tasks will inherit counted references to the original value.

Commit count: 28

cargo fmt