gpui: Fix Global
trait (#11187)
This PR restores the `Global` trait's status as a marker trait. This was the original intent from #7095, when it was added, that had been lost in #9777. The purpose of the `Global` trait is to statically convey what types can and can't be accessed as `Global` state, as well as provide a way of restricting access to said globals. For example, in the case of the `ThemeRegistry` we have a private `GlobalThemeRegistry` that is marked as `Global`:91b3c24ed3/crates/theme/src/registry.rs (L25-L34)
We're then able to permit reading the `ThemeRegistry` from the `GlobalThemeRegistry` via a custom getter, while still restricting which callers are able to mutate the global:91b3c24ed3/crates/theme/src/registry.rs (L46-L61)
Release Notes: - N/A
This commit is contained in:
parent
91b3c24ed3
commit
d633a0da78
8 changed files with 29 additions and 21 deletions
|
@ -330,19 +330,12 @@ impl<T> Flatten<T> for Result<T> {
|
|||
|
||||
/// A marker trait for types that can be stored in GPUI's global state.
|
||||
///
|
||||
/// This trait exists to provide type-safe access to globals by restricting
|
||||
/// the scope from which they can be accessed. For instance, the actual type
|
||||
/// that implements [`Global`] can be private, with public accessor functions
|
||||
/// that enforce correct usage.
|
||||
///
|
||||
/// Implement this on types you want to store in the context as a global.
|
||||
pub trait Global: 'static + Sized {
|
||||
/// Access the global of the implementing type. Panics if a global for that type has not been assigned.
|
||||
fn get(cx: &AppContext) -> &Self {
|
||||
cx.global()
|
||||
}
|
||||
|
||||
/// Updates the global of the implementing type with a closure. Unlike `global_mut`, this method provides
|
||||
/// your closure with mutable access to the `AppContext` and the global simultaneously.
|
||||
fn update<C, R>(cx: &mut C, f: impl FnOnce(&mut Self, &mut C) -> R) -> R
|
||||
where
|
||||
C: BorrowAppContext,
|
||||
{
|
||||
cx.update_global(f)
|
||||
}
|
||||
pub trait Global: 'static {
|
||||
// This trait is intentionally left empty, by virtue of being a marker trait.
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue