Skip to main content

Global

Trait Global 

Source
pub trait Global: 'static { }
Expand description

Marks a type whose value is stored once per app.

Implement it for a type, then store one value of that type with App::set_global. Read it with App::global, and change it with UpdateGlobal::update_global. Use a global for state that exists once for the whole app, such as settings or a theme. Use an entity for state that can have several instances, or that several parts of the app change independently.

The trait is a marker: it adds no methods of its own. Helper traits with blanket implementations, such as ReadGlobal and UpdateGlobal, add methods to every Global type.

§Restricting access to globals

In some situations you may need to store some global state, but want to restrict access to reading it or writing to it.

In these cases, Rust’s visibility system can be used to restrict access to a global value. For example, you can create a private struct that implements Global and holds the global state. Then create a newtype struct that wraps the global type and create custom accessor methods to expose the desired subset of operations.

§Examples

struct Theme {
    dark: bool,
}

impl Global for Theme {}

app.set_global(Theme { dark: false });
assert!(!app.read(|cx| cx.global::<Theme>().dark));

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Global for DebugBelow

Available on debug-assertions enabled only.
Source§

impl Global for GlobalColors

Source§

impl Global for SystemWindowTabController