Global

Struct Global 

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

Ergonomic global variables.

No more Mutex<Option<...>> shenanigans with lazy initialization on each use site, or OnceLock which limits to immutable access.

This type is very similar to once_cell::Lazy in its nature, with a minimalistic implementation. Unlike Lazy, it is only designed for global variables, not for local lazy initialization (following “do one thing and do it well”).

Global<T> features:

  • const constructors, allowing to be used in static variables without Option.
  • Initialization function provided in constructor, not in each use site separately.
  • Ergonomic access through guards to both &T and &mut T.
  • Completely safe usage. Little use of unsafe in the implementation (for performance reasons).

There are two const methods for construction: new() and default(). For access, you should primarily use lock(). There is also try_lock() for special cases.

Implementations§

Source§

impl<T> Global<T>

Source

pub const fn new(init_fn: fn() -> T) -> Self

Create Global<T>, providing a lazy initialization function.

The initialization function is only called once, when the global is first accessed through lock().

Source

pub const fn default() -> Self
where T: Default,

Create Global<T> with T::default() as initialization function.

This is inherent rather than implementing the Default trait, because the latter is not const and thus useless in static contexts.

Source

pub fn lock(&self) -> GlobalGuard<'_, T>

Returns a guard that gives shared or mutable access to the value.

Blocks until the internal mutex is available.

§Panics

If the initialization function panics. Once that happens, the global is considered poisoned and all future calls to lock() will panic. This can currently not be recovered from.

Source

pub fn try_lock(&self) -> Result<GlobalGuard<'_, T>, GlobalLockError<'_, T>>

Non-blocking access with error introspection.

Auto Trait Implementations§

§

impl<T> !Freeze for Global<T>

§

impl<T> RefUnwindSafe for Global<T>

§

impl<T> Send for Global<T>
where T: Send,

§

impl<T> Sync for Global<T>
where T: Send,

§

impl<T> Unpin for Global<T>
where T: Unpin,

§

impl<T> UnwindSafe for Global<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<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.