entity_map.rs

  1use crate::Context;
  2use anyhow::{anyhow, Result};
  3use derive_more::{Deref, DerefMut};
  4use parking_lot::{RwLock, RwLockUpgradableReadGuard};
  5use slotmap::{SecondaryMap, SlotMap};
  6use std::{
  7    any::Any,
  8    marker::PhantomData,
  9    mem,
 10    sync::{
 11        atomic::{AtomicUsize, Ordering::SeqCst},
 12        Arc, Weak,
 13    },
 14};
 15
 16slotmap::new_key_type! { pub struct EntityId; }
 17
 18pub(crate) struct EntityMap(Arc<RwLock<EntityMapState>>);
 19
 20struct EntityMapState {
 21    ref_counts: SlotMap<EntityId, AtomicUsize>,
 22    entities: SecondaryMap<EntityId, Box<dyn Any + Send + Sync>>,
 23    dropped_entities: Vec<(EntityId, Box<dyn Any + Send + Sync>)>,
 24}
 25
 26impl EntityMap {
 27    pub fn new() -> Self {
 28        Self(Arc::new(RwLock::new(EntityMapState {
 29            ref_counts: SlotMap::with_key(),
 30            entities: SecondaryMap::new(),
 31            dropped_entities: Vec::new(),
 32        })))
 33    }
 34
 35    /// Reserve a slot for an entity, which you can subsequently use with `insert`.
 36    pub fn reserve<T: 'static + Send + Sync>(&self) -> Slot<T> {
 37        let id = self.0.write().ref_counts.insert(1.into());
 38        Slot(Handle::new(id, Arc::downgrade(&self.0)))
 39    }
 40
 41    /// Insert an entity into a slot obtained by calling `reserve`.
 42    pub fn insert<T: 'static + Any + Send + Sync>(&self, slot: Slot<T>, entity: T) -> Handle<T> {
 43        let handle = slot.0;
 44        self.0.write().entities.insert(handle.id, Box::new(entity));
 45        handle
 46    }
 47
 48    /// Move an entity to the stack.
 49    pub fn lease<T: 'static + Send + Sync>(&self, handle: &Handle<T>) -> Lease<T> {
 50        let id = handle.id;
 51        let entity = Some(
 52            self.0
 53                .write()
 54                .entities
 55                .remove(id)
 56                .expect("Circular entity lease. Is the entity already being updated?")
 57                .downcast::<T>()
 58                .unwrap(),
 59        );
 60        Lease { id, entity }
 61    }
 62
 63    /// Return an entity after moving it to the stack.
 64    pub fn end_lease<T: 'static + Send + Sync>(&mut self, mut lease: Lease<T>) {
 65        self.0
 66            .write()
 67            .entities
 68            .insert(lease.id, lease.entity.take().unwrap());
 69    }
 70
 71    pub fn weak_handle<T: 'static + Send + Sync>(&self, id: EntityId) -> WeakHandle<T> {
 72        WeakHandle {
 73            id,
 74            entity_type: PhantomData,
 75            entity_map: Arc::downgrade(&self.0),
 76        }
 77    }
 78
 79    pub fn take_dropped(&self) -> Vec<(EntityId, Box<dyn Any + Send + Sync>)> {
 80        mem::take(&mut self.0.write().dropped_entities)
 81    }
 82}
 83
 84pub struct Lease<T> {
 85    entity: Option<Box<T>>,
 86    pub id: EntityId,
 87}
 88
 89impl<T> core::ops::Deref for Lease<T> {
 90    type Target = T;
 91
 92    fn deref(&self) -> &Self::Target {
 93        self.entity.as_ref().unwrap()
 94    }
 95}
 96
 97impl<T> core::ops::DerefMut for Lease<T> {
 98    fn deref_mut(&mut self) -> &mut Self::Target {
 99        self.entity.as_mut().unwrap()
100    }
101}
102
103impl<T> Drop for Lease<T> {
104    fn drop(&mut self) {
105        if self.entity.is_some() {
106            // We don't panic here, because other panics can cause us to drop the lease without ending it cleanly.
107            log::error!("Leases must be ended with EntityMap::end_lease")
108        }
109    }
110}
111
112#[derive(Deref, DerefMut)]
113pub struct Slot<T: Send + Sync + 'static>(Handle<T>);
114
115pub struct Handle<T: Send + Sync> {
116    pub(crate) id: EntityId,
117    entity_type: PhantomData<T>,
118    entity_map: Weak<RwLock<EntityMapState>>,
119}
120
121impl<T: 'static + Send + Sync> Handle<T> {
122    fn new(id: EntityId, entity_map: Weak<RwLock<EntityMapState>>) -> Self {
123        Self {
124            id,
125            entity_type: PhantomData,
126            entity_map,
127        }
128    }
129
130    pub fn downgrade(&self) -> WeakHandle<T> {
131        WeakHandle {
132            id: self.id,
133            entity_type: self.entity_type,
134            entity_map: self.entity_map.clone(),
135        }
136    }
137
138    /// Update the entity referenced by this handle with the given function.
139    ///
140    /// The update function receives a context appropriate for its environment.
141    /// When updating in an `AppContext`, it receives a `ModelContext`.
142    /// When updating an a `WindowContext`, it receives a `ViewContext`.
143    pub fn update<C: Context, R>(
144        &self,
145        cx: &mut C,
146        update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
147    ) -> C::Result<R> {
148        cx.update_entity(self, update)
149    }
150}
151
152impl<T: Send + Sync> Clone for Handle<T> {
153    fn clone(&self) -> Self {
154        if let Some(entity_map) = self.entity_map.upgrade() {
155            let entity_map = entity_map.read();
156            let count = entity_map
157                .ref_counts
158                .get(self.id)
159                .expect("detected over-release of a handle");
160            let prev_count = count.fetch_add(1, SeqCst);
161            assert_ne!(prev_count, 0, "Detected over-release of a handle.");
162        }
163
164        Self {
165            id: self.id,
166            entity_type: PhantomData,
167            entity_map: self.entity_map.clone(),
168        }
169    }
170}
171
172impl<T: Send + Sync> Drop for Handle<T> {
173    fn drop(&mut self) {
174        if let Some(entity_map) = self.entity_map.upgrade() {
175            let entity_map = entity_map.upgradable_read();
176            let count = entity_map
177                .ref_counts
178                .get(self.id)
179                .expect("Detected over-release of a handle.");
180            let prev_count = count.fetch_sub(1, SeqCst);
181            assert_ne!(prev_count, 0, "Detected over-release of a handle.");
182            if prev_count == 1 {
183                // We were the last reference to this entity, so we can remove it.
184                let mut entity_map = RwLockUpgradableReadGuard::upgrade(entity_map);
185                let entity = entity_map
186                    .entities
187                    .remove(self.id)
188                    .expect("entity was removed twice");
189                entity_map.ref_counts.remove(self.id);
190                entity_map.dropped_entities.push((self.id, entity));
191            }
192        }
193    }
194}
195
196pub struct WeakHandle<T> {
197    pub(crate) id: EntityId,
198    entity_type: PhantomData<T>,
199    entity_map: Weak<RwLock<EntityMapState>>,
200}
201
202impl<T: 'static + Send + Sync> Clone for WeakHandle<T> {
203    fn clone(&self) -> Self {
204        Self {
205            id: self.id,
206            entity_type: self.entity_type,
207            entity_map: self.entity_map.clone(),
208        }
209    }
210}
211
212impl<T: Send + Sync + 'static> WeakHandle<T> {
213    pub fn upgrade(&self, _: &impl Context) -> Option<Handle<T>> {
214        let entity_map = &self.entity_map.upgrade()?;
215        entity_map
216            .read()
217            .ref_counts
218            .get(self.id)?
219            .fetch_add(1, SeqCst);
220        Some(Handle {
221            id: self.id,
222            entity_type: self.entity_type,
223            entity_map: self.entity_map.clone(),
224        })
225    }
226
227    /// Update the entity referenced by this handle with the given function if
228    /// the referenced entity still exists. Returns an error if the entity has
229    /// been released.
230    ///
231    /// The update function receives a context appropriate for its environment.
232    /// When updating in an `AppContext`, it receives a `ModelContext`.
233    /// When updating an a `WindowContext`, it receives a `ViewContext`.
234    pub fn update<C: Context, R>(
235        &self,
236        cx: &mut C,
237        update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
238    ) -> Result<R>
239    where
240        Result<C::Result<R>>: crate::Flatten<R>,
241    {
242        crate::Flatten::flatten(
243            self.upgrade(cx)
244                .ok_or_else(|| anyhow!("entity release"))
245                .map(|this| cx.update_entity(&this, update)),
246        )
247    }
248}