Crate tokio_inherit_task_local

Source
Expand description

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::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.

Macros§

inheritable_task_local
Declares a new inheritable task-local key of type InheritableLocalKey.

Structs§

InheritableLocalKey
A key for inheritable task-local data.
TaskLocalInheritableTable
This is mostly an implementation detail. It stores references to all of the inheritable task local values that are available to a given task. You are not meant to use this directly.

Enums§

InheritableAccessError
Returned when the requested inheritable task local did not have a value set.

Traits§

FutureInheritTaskLocal
Extends any Future with a 'static lifetime. Provides a method that copies references to the current inheritable task local values into this Future.

Functions§

inherit_task_local
Returns a closure which has its own copy of the current table for inheritable task locals. Intended for use with tokio::task::spawn_blocking.