pub struct FutureOnceCell<T>(/* private fields */);
Expand description

An init-once-per-future cell for thread-local values.

It uses thread local storage to ensure that the each polled future has its own local storage key. Unlike the std::thread::LocalKey this cell will not lazily initialize the value on first access. Instead, the value is first initialized when the future containing the future-local is first polled by an executor.

After the execution finished the value moves from the future local cell to the future output.

Implementations§

source§

impl<T> FutureOnceCell<T>

source

pub const fn new() -> Self

Creates an empty future once cell.

source§

impl<T: Send + 'static> FutureOnceCell<T>

source

pub fn with<F, R>(&'static self, f: F) -> R
where F: FnOnce(&T) -> R,

Acquires a reference to the value in this future local storage.

Unlike the std::thread::LocalKey::with this method does not initialize the value when called.

Panics
  • This method will panic if the future local doesn’t have a value set.

  • If you the returned future inside the a call to Self::with on the same cell, then the call to poll will panic.

source

pub fn get(&'static self) -> T
where T: Copy,

Returns a copy of the contained value.

Panics

This method will panic if the future local doesn’t have a value set.

source

pub fn scope<F>( &'static self, value: T, future: F ) -> ScopedFutureWithValue<T, F>
where F: Future,

Sets a value T as the future-local value for the future F.

On completion of scope, the future-local value will be returned by the scoped future.

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
    }).await;
}

Trait Implementations§

source§

impl<T: Debug + Send + 'static> Debug for FutureOnceCell<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for FutureOnceCell<T>

§

impl<T> Send for FutureOnceCell<T>

§

impl<T> Sync for FutureOnceCell<T>
where T: Send,

§

impl<T> Unpin for FutureOnceCell<T>

§

impl<T> !UnwindSafe for FutureOnceCell<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.