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>
impl<T: Send + 'static> FlowLocal<T>
Sourcepub fn get(&self) -> FlowBox<T>
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?
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}
Sourcepub fn set(&self, value: T)
pub fn set(&self, value: T)
Sets a new value for the flow-local.
Examples found in repository?
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}