LocalKey

Struct LocalKey 

Source
pub struct LocalKey<T: 'static> { /* private fields */ }
Expand description

A variable stored in task-local storage.

§Usage

The primary mode of accessing this is through the LocalKey::with method. For LocalKey<RefCell<T>> and LocalKey<Cell<T>>, additional convenience methods are added that mirror the underlying [RefCell<T>] or [Cell<T>]’s methods.

§Examples

use std::cell::{Cell, RefCell};

use vexide::prelude::*;

task_local! {
    static PHI: f64 = 1.61803;
    static COUNTER: Cell<u32> = Cell::new(0);
    static NAMES: RefCell<Vec<String>> = RefCell::new(Vec::new());
}

#[vexide::main]
async fn main(_peripherals: Peripherals) {
    // LocalKey::with accepts a function and applies it to a reference, returning whatever value
    // the function returned
    let double_phi = PHI.with(|&phi| phi * 2.0);
    assert_eq!(double_phi, 1.61803 * 2.0);

    // We can use interior mutability
    COUNTER.set(1);
    assert_eq!(COUNTER.get(), 1);

    NAMES.with_borrow_mut(|names| names.push(String::from("Johnny")));
    NAMES.with_borrow(|names| assert_eq!(names.len(), 1));

    // Creating another task
    spawn(async {
        // The locals of the previous task are completely different.
        assert_eq!(COUNTER.get(), 0);
        NAMES.with_borrow(|names| assert_eq!(names.len(), 0));
    })
    .await;
}

Implementations§

Source§

impl<T: 'static> LocalKey<T>

Source

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

Obtains a reference to the local and applies it to the function f, returning whatever f returned.

§Examples
use vexide::task::task_local;

task_local! {
    static PHI: f64 = 1.61803;
}

let double_phi = PHI.with(|&phi| phi * 2.0);
assert_eq!(double_phi, 1.61803 * 2.0);
Source§

impl<T: 'static> LocalKey<Cell<T>>

Source

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

Returns a copy of the contained value.

Source

pub fn set(&'static self, value: T)

Sets the contained value.

Source

pub fn take(&'static self) -> T
where T: Default,

Takes the value of contained value, leaving [Default::default()] in its place.

Source

pub fn replace(&'static self, value: T) -> T

Replaces the contained value with value, returning the old contained value.

Source§

impl<T: 'static> LocalKey<RefCell<T>>

Source

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

Immutably borrows from the [RefCell] and applies the obtained reference to f.

§Panics

Panics if the value is currently mutably borrowed. For a non-panicking variant, use LocalKey::try_with_borrow.

Source

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

Mutably borrows from the [RefCell] and applies the obtained reference to f.

§Panics

Panics if the value is currently borrowed. For a non-panicking variant, use LocalKey::try_with_borrow_mut.

Source

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

Tries to immutably borrow the contained value, returning an error if it is currently mutably borrowed, and applies the obtained reference to f.

This is the non-panicking variant of LocalKey::with_borrow.

§Errors

Returns [BorrowError] if the contained value is currently mutably borrowed.

Source

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

Tries to mutably borrow the contained value, returning an error if it is currently borrowed, and applies the obtained reference to f.

This is the non-panicking variant of LocalKey::with_borrow_mut.

§Errors

Returns [BorrowMutError] if the contained value is currently borrowed.

Source

pub fn set(&'static self, value: T)

Sets the contained value.

§Panics

Panics if the value is currently borrowed.

Source

pub fn take(&'static self) -> T
where T: Default,

Takes the contained value, leaving [Default::default()] in its place.

§Panics

Panics if the value is currently borrowed.

Source

pub fn replace(&'static self, value: T) -> T

Replaces the contained value with value, returning the old contained value.

§Panics

Panics if the value is currently borrowed.

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for LocalKey<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Send for LocalKey<T>

Source§

impl<T> Sync for LocalKey<T>

Auto Trait Implementations§

§

impl<T> !Freeze for LocalKey<T>

§

impl<T> RefUnwindSafe for LocalKey<T>

§

impl<T> Unpin for LocalKey<T>

§

impl<T> UnwindSafe for LocalKey<T>

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

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

Performs the conversion.