Add the entity trait and implement for models, views, subscriptions, and observations
This commit is contained in:
parent
58446c2715
commit
327a2f9967
13 changed files with 183 additions and 101 deletions
|
@ -24,6 +24,12 @@ mod util;
|
|||
mod view;
|
||||
mod window;
|
||||
|
||||
mod private {
|
||||
/// A mechanism for restricting implementations of a trait to only those in GPUI.
|
||||
/// See: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/
|
||||
pub trait Sealed {}
|
||||
}
|
||||
|
||||
pub use action::*;
|
||||
pub use anyhow::Result;
|
||||
pub use app::*;
|
||||
|
@ -39,6 +45,7 @@ pub use image_cache::*;
|
|||
pub use interactive::*;
|
||||
pub use keymap::*;
|
||||
pub use platform::*;
|
||||
use private::Sealed;
|
||||
pub use refineable::*;
|
||||
pub use scene::*;
|
||||
pub use serde;
|
||||
|
@ -80,7 +87,7 @@ pub trait Context {
|
|||
where
|
||||
T: 'static + Send;
|
||||
|
||||
fn update_entity<T: 'static, R>(
|
||||
fn update_model<T: 'static, R>(
|
||||
&mut self,
|
||||
handle: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
|
||||
|
@ -104,6 +111,16 @@ pub trait VisualContext: Context {
|
|||
) -> Self::Result<R>;
|
||||
}
|
||||
|
||||
pub trait Entity<T>: Sealed {
|
||||
type Weak: 'static + Send;
|
||||
|
||||
fn entity_id(&self) -> EntityId;
|
||||
fn downgrade(&self) -> Self::Weak;
|
||||
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub enum GlobalKey {
|
||||
Numeric(usize),
|
||||
View(EntityId),
|
||||
|
@ -149,12 +166,12 @@ impl<C: Context> Context for MainThread<C> {
|
|||
})
|
||||
}
|
||||
|
||||
fn update_entity<T: 'static, R>(
|
||||
fn update_model<T: 'static, R>(
|
||||
&mut self,
|
||||
handle: &Model<T>,
|
||||
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
|
||||
) -> Self::Result<R> {
|
||||
self.0.update_entity(handle, |entity, cx| {
|
||||
self.0.update_model(handle, |entity, cx| {
|
||||
let cx = unsafe {
|
||||
mem::transmute::<
|
||||
&mut C::ModelContext<'_, T>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue