Struct FlowLocal

Source
pub struct FlowLocal<T> { /* private fields */ }
Expand description

A key for flow-local data stored in the execution context.

This type is generated by the flow_local! macro and performs very similarly to the thread_local! macro and std::thread::FlowLocal types. Data associated with a FlowLocal<T> is stored inside the current execution context and the data is destroyed when the execution context is destroyed.

Flow-local data can migrate between threads and hence requires a Send bound. Additionally, flow-local data also requires the 'static bound to ensure it lives long enough. When a key is accessed for the first time the flow’s data is initialized with the provided initialization expression to the macro.

Implementations§

Source§

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

Source

pub fn get(&self) -> FlowBox<T>

Access this flow-local.

This function will access this flow-local key to retrieve the data associated with the current flow and this key. If this is the first time this key has been accessed on this flow, then the key will be initialized with the initialization expression provided at the time the flow_local! macro was called.

Examples found in repository?
examples/basic.rs (line 11)
10fn main() {
11    println!("the current locale is {}", LOCALE.get());
12    LOCALE.set("de_DE".into());
13    println!("changing locale to {}", LOCALE.get());
14
15    let ec = ExecutionContext::capture();
16    thread::spawn(move || {
17        ec.run(|| {
18            println!("the locale in the child thread is {}", LOCALE.get());
19            LOCALE.set("fr_FR".into());
20            println!("the new locale in the child thread is {}", LOCALE.get());
21        });
22    }).join().unwrap();
23
24    println!("the locale of the parent thread is again {}", LOCALE.get());
25}
Source

pub fn set(&self, value: T)

Sets a new value for the flow-local.

Examples found in repository?
examples/basic.rs (line 12)
10fn main() {
11    println!("the current locale is {}", LOCALE.get());
12    LOCALE.set("de_DE".into());
13    println!("changing locale to {}", LOCALE.get());
14
15    let ec = ExecutionContext::capture();
16    thread::spawn(move || {
17        ec.run(|| {
18            println!("the locale in the child thread is {}", LOCALE.get());
19            LOCALE.set("fr_FR".into());
20            println!("the new locale in the child thread is {}", LOCALE.get());
21        });
22    }).join().unwrap();
23
24    println!("the locale of the parent thread is again {}", LOCALE.get());
25}

Trait Implementations§

Source§

impl<T> Debug for FlowLocal<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for FlowLocal<T>

§

impl<T> RefUnwindSafe for FlowLocal<T>

§

impl<T> Send for FlowLocal<T>

§

impl<T> Sync for FlowLocal<T>

§

impl<T> Unpin for FlowLocal<T>

§

impl<T> UnwindSafe for FlowLocal<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<A> Shared<A> for A

Source§

fn shared(self) -> Arc<A>

Source§

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

Source§

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>,

Source§

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.