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;
}

Object Safety§

This trait is not object safe.

Implementors§