FutureLocalStorage

Trait FutureLocalStorage 

Source
pub trait FutureLocalStorage:
    Future
    + Sized
    + Sealed {
    // Required method
    fn with_scope<T, S>(
        self,
        scope: &'static S,
        value: T,
    ) -> ScopedFutureWithValue<T, Self> 
       where T: Send,
             S: AsRef<FutureLocalKey<T>>;
}
Expand description

Attaches future local storage values to a Future.

Extension trait allowing futures to have their own static variables.

Required Methods§

Source

fn with_scope<T, S>( self, scope: &'static S, value: T, ) -> ScopedFutureWithValue<T, Self>
where T: Send, S: AsRef<FutureLocalKey<T>>,

Sets a given value as the future local value of this future.

Each future instance will have its own state of the attached value.

use std::cell::Cell;

use future_local_storage::{FutureOnceCell, FutureLocalStorage};

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

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

        42
    }.with_scope(&VALUE, Cell::from(0)).await;
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§