gpui: Improve Global ergonomics (#11923)

This PR adds some ergonomic improvements when working with GPUI
`Global`s.

Two new traits have been added—`ReadGlobal` and `UpdateGlobal`—that
provide associated functions on any type that implements `Global` for
accessing and updating the global without needing to call the methods on
the `cx` directly (which generally involves qualifying the type).

I looked into adding `ObserveGlobal` as well, but this seems a bit
trickier to implement as the signatures of `cx.observe_global` vary
slightly between the different contexts.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-05-16 12:47:43 -04:00 committed by GitHub
parent 1b261608c6
commit c1e291bc96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 81 additions and 33 deletions

62
crates/gpui/src/global.rs Normal file
View file

@ -0,0 +1,62 @@
use crate::{AppContext, BorrowAppContext};
/// 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 ensuring only
/// types that implement [`Global`] can be used with the accessor methods. For
/// example, trying to access a global with a type that does not implement
/// [`Global`] will result in a compile-time error.
///
/// Implement this on types you want to store in the context as a global.
///
/// ## 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.
pub trait Global: 'static {
// This trait is intentionally left empty, by virtue of being a marker trait.
//
// Use additional traits with blanket implementations to attach functionality
// to types that implement `Global`.
}
/// A trait for reading a global value from the context.
pub trait ReadGlobal {
/// Returns the global instance of the implementing type.
///
/// Panics if a global for that type has not been assigned.
fn global(cx: &AppContext) -> &Self;
}
impl<T: Global> ReadGlobal for T {
fn global(cx: &AppContext) -> &Self {
cx.global::<T>()
}
}
/// A trait for updating a global value in the context.
pub trait UpdateGlobal {
/// Updates the global instance of the implementing type using the provided closure.
///
/// This method provides the closure with mutable access to the context and the global simultaneously.
fn update_global<C, F, R>(cx: &mut C, update: F) -> R
where
C: BorrowAppContext,
F: FnOnce(&mut Self, &mut C) -> R;
}
impl<T: Global> UpdateGlobal for T {
fn update_global<C, F, R>(cx: &mut C, update: F) -> R
where
C: BorrowAppContext,
F: FnOnce(&mut Self, &mut C) -> R,
{
cx.update_global(update)
}
}

View file

@ -77,6 +77,7 @@ mod element;
mod elements;
mod executor;
mod geometry;
mod global;
mod input;
mod interactive;
mod key_dispatch;
@ -125,6 +126,7 @@ pub use element::*;
pub use elements::*;
pub use executor::*;
pub use geometry::*;
pub use global::*;
pub use gpui_macros::{register_action, test, IntoElement, Render};
pub use input::*;
pub use interactive::*;
@ -327,15 +329,3 @@ impl<T> Flatten<T> for Result<T> {
self
}
}
/// 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 {
// This trait is intentionally left empty, by virtue of being a marker trait.
}