Struct ExecutionContext

Source
pub struct ExecutionContext { /* private fields */ }
Expand description

An execution context is a container for the current logical flow of execution.

This container holds all state that needs to be carried forward with the logical thread of execution.

The ExecutionContext class provides the functionality to capture and transfer the encapsulated context across asynchronous points such as threads or tasks.

An execution context can be captured, send and cloned. This permits a context to be carried to other threads.

Implementations§

Source§

impl ExecutionContext

Source

pub fn capture() -> ExecutionContext

Captures the current execution context and returns it.

If the current execution context is suppressed then this will instead capture an empty default scope. Capturing will always succeed.

Capturing the execution context means that the flow of data will branch off here. If a flow local is modified after the flow is captured it will not be reflected in the captured context.

§Example
let ec = ExecutionContext::capture();
ec.run(|| {
    // this code runs in the flow of the given execution context.
});
Examples found in repository?
examples/basic.rs (line 15)
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 suppress_flow() -> FlowGuard

Suppresses the flow.

This returns a clonable non-send guard that when dropped restores the flow. This can be used to spawn an operation that should not be considered to be part of the same logical flow. Once a new execution context has been created, that context will start its own flow again.

To permanently disable flow propagation use disable_flow.

§Example
{
    let _guard = ExecutionContext::suppress_flow();
    let ec = ExecutionContext::capture();
    ec.run(|| {
        // a new flow is started here because the captured flow was
        // suppressed.
    });
}
// the flow is resumed here
Source

pub fn disable_flow() -> FlowGuard

Permanently disables the flow.

This works similar to suppress_flow but instead of just starting a new flow this permanently disables the flow. The flow can be manually restored by a call to restore_flow.

Source

pub fn restore_flow()

Restores the flow.

In normal situations the flow is restored when the flow guard is dropped. However when for instance the flow is permanently disabled with disable_flow new branches will never have their flow restored. In those situations it might be useful to call into this function to restore the flow.

Source

pub fn is_flow_suppressed() -> bool

Checks if the flow is currently suppressed.

A caller cannot determine if the flow is just temporarily suppressed or permanently disabled.

Source

pub fn run<F: FnOnce() -> R, R>(&self, f: F) -> R

Runs a function in the context of the given execution context.

The captured execution flow will be carried forward. If the flow was suppressed a new flow is started. In case the flow was disabled then it’s also disabled here.

§Example
let ec = ExecutionContext::capture();
thread::spawn(move || {
    ec.run(|| {
        // the captured execution context is carried into
        // another thread.
    });
});
Examples found in repository?
examples/basic.rs (lines 17-21)
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 Clone for ExecutionContext

Source§

fn clone(&self) -> ExecutionContext

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ExecutionContext

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.