Trait LocalKeyExt

Source
pub trait LocalKeyExt<T> {
    // Required methods
    fn with_borrow<F, R>(&'static self, f: F) -> R
       where F: FnOnce(&T) -> R;
    fn with_borrow_mut<F, R>(&'static self, f: F) -> R
       where F: FnOnce(&mut T) -> R;
    fn set(&'static self, value: T);
    fn take(&'static self) -> T
       where T: Default;
    fn replace(&'static self, value: T) -> T;
}

Required Methods§

Source

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

Acquires a reference to the contained value.

This will lazily initialize the value if this thread has not referenced this key yet.

§Panics

Panics if the value is currently mutably borrowed.

Panics if the key currently has its destructor running, and it may panic if the destructor has previously been run for this thread.

§Example
use interrupt_ref_cell::{InterruptRefCell, LocalKeyExt};

thread_local! {
    static X: InterruptRefCell<Vec<i32>> = InterruptRefCell::new(Vec::new());
}

X.with_borrow(|v| assert!(v.is_empty()));
Source

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

Acquires a mutable reference to the contained value.

This will lazily initialize the value if this thread has not referenced this key yet.

§Panics

Panics if the value is currently borrowed.

Panics if the key currently has its destructor running, and it may panic if the destructor has previously been run for this thread.

§Example
use interrupt_ref_cell::{InterruptRefCell, LocalKeyExt};

thread_local! {
    static X: InterruptRefCell<Vec<i32>> = InterruptRefCell::new(Vec::new());
}

X.with_borrow_mut(|v| v.push(1));

X.with_borrow(|v| assert_eq!(*v, vec![1]));
Source

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

Sets the contained value.

This will run the lazy initializer.

Unlike LocalKey<RefCell<T>>::set, this method does run the lazy initializer of the thread local. The required API to avoid that is not not public.

§Panics

Panics if the value is currently borrowed.

Panics if the key currently has its destructor running, and it may panic if the destructor has previously been run for this thread.

§Examples
use interrupt_ref_cell::{InterruptRefCell, LocalKeyExt};

thread_local! {
    static X: InterruptRefCell<Vec<i32>> = InterruptRefCell::new(Vec::new());
}

// Calling X.with() here would result in a panic.

X.set(vec![1, 2, 3]); // But X.set() is fine, as it skips the initializer above.

X.with_borrow(|v| assert_eq!(*v, vec![1, 2, 3]));
Source

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

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

This will lazily initialize the value if this thread has not referenced this key yet.

§Panics

Panics if the value is currently borrowed.

Panics if the key currently has its destructor running, and it may panic if the destructor has previously been run for this thread.

§Examples
use interrupt_ref_cell::{InterruptRefCell, LocalKeyExt};

thread_local! {
    static X: InterruptRefCell<Vec<i32>> = InterruptRefCell::new(Vec::new());
}

X.with_borrow_mut(|v| v.push(1));

let a = X.take();

assert_eq!(a, vec![1]);

X.with_borrow(|v| assert!(v.is_empty()));
Source

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

Replaces the contained value, returning the old value.

§Panics

Panics if the value is currently borrowed.

Panics if the key currently has its destructor running, and it may panic if the destructor has previously been run for this thread.

§Examples
use interrupt_ref_cell::{InterruptRefCell, LocalKeyExt};

thread_local! {
    static X: InterruptRefCell<Vec<i32>> = InterruptRefCell::new(Vec::new());
}

let prev = X.replace(vec![1, 2, 3]);
assert!(prev.is_empty());

X.with_borrow(|v| assert_eq!(*v, vec![1, 2, 3]));

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.

Implementations on Foreign Types§

Source§

impl<T: 'static> LocalKeyExt<T> for LocalKey<InterruptRefCell<T>>

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Implementors§