Skip to main content

UpdateGlobal

Trait UpdateGlobal 

Source
pub trait UpdateGlobal {
    // Required methods
    fn update_global<C, F, R>(cx: &mut C, update: F) -> R
       where C: BorrowAppContext,
             F: FnOnce(&mut Self, &mut C) -> R;
    fn set_global<C>(cx: &mut C, global: Self)
       where C: BorrowAppContext;
}
Expand description

Changes or sets the global of the implementing type through a context.

Every Global type implements this trait. Theme::update_global(cx, ...) is the same as cx.update_global::<Theme, _>(...), and Theme::set_global(cx, ...) is the same as cx.set_global(...). Both accept any context that implements BorrowAppContext, such as &mut App.

§Examples

struct Theme {
    dark: bool,
}

impl Global for Theme {}

app.update(|cx| {
    Theme::set_global(cx, Theme { dark: false });
    Theme::update_global(cx, |theme, _cx| theme.dark = true);
});
assert!(app.read(|cx| cx.global::<Theme>().dark));

Required Methods§

Source

fn update_global<C, F, R>(cx: &mut C, update: F) -> R
where C: BorrowAppContext, F: FnOnce(&mut Self, &mut C) -> R,

Runs update with mutable access to the global and context.

The global is taken out of the app while update runs, so update can’t read it through the context. Observers of the global are notified when the update finishes.

§Panics

Panics if no global of this type has been set, or if this global is already being updated.

Source

fn set_global<C>(cx: &mut C, global: Self)

Sets the global of this type to global, replacing any previous value.

Observers of the global are notified when the update finishes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§