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, TypeId},
  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            any_handle: AnyWeakHandle {
 74                id,
 75                entity_type: TypeId::of::<T>(),
 76                entity_map: Arc::downgrade(&self.0),
 77            },
 78            entity_type: PhantomData,
 79        }
 80    }
 81
 82    pub fn take_dropped(&self) -> Vec<(EntityId, Box<dyn Any + Send + Sync>)> {
 83        mem::take(&mut self.0.write().dropped_entities)
 84    }
 85}
 86
 87pub struct Lease<T> {
 88    entity: Option<Box<T>>,
 89    pub id: EntityId,
 90}
 91
 92impl<T> core::ops::Deref for Lease<T> {
 93    type Target = T;
 94
 95    fn deref(&self) -> &Self::Target {
 96        self.entity.as_ref().unwrap()
 97    }
 98}
 99
100impl<T> core::ops::DerefMut for Lease<T> {
101    fn deref_mut(&mut self) -> &mut Self::Target {
102        self.entity.as_mut().unwrap()
103    }
104}
105
106impl<T> Drop for Lease<T> {
107    fn drop(&mut self) {
108        if self.entity.is_some() {
109            // We don't panic here, because other panics can cause us to drop the lease without ending it cleanly.
110            log::error!("Leases must be ended with EntityMap::end_lease")
111        }
112    }
113}
114
115#[derive(Deref, DerefMut)]
116pub struct Slot<T: Send + Sync + 'static>(Handle<T>);
117
118pub struct AnyHandle {
119    pub(crate) id: EntityId,
120    entity_type: TypeId,
121    entity_map: Weak<RwLock<EntityMapState>>,
122}
123
124impl AnyHandle {
125    fn new(id: EntityId, entity_type: TypeId, entity_map: Weak<RwLock<EntityMapState>>) -> Self {
126        Self {
127            id,
128            entity_type,
129            entity_map,
130        }
131    }
132
133    pub fn downgrade(&self) -> AnyWeakHandle {
134        AnyWeakHandle {
135            id: self.id,
136            entity_type: self.entity_type,
137            entity_map: self.entity_map.clone(),
138        }
139    }
140
141    pub fn downcast<T>(&self) -> Option<Handle<T>>
142    where
143        T: 'static + Send + Sync,
144    {
145        if TypeId::of::<T>() == self.entity_type {
146            Some(Handle {
147                any_handle: self.clone(),
148                entity_type: PhantomData,
149            })
150        } else {
151            None
152        }
153    }
154}
155
156impl Clone for AnyHandle {
157    fn clone(&self) -> Self {
158        if let Some(entity_map) = self.entity_map.upgrade() {
159            let entity_map = entity_map.read();
160            let count = entity_map
161                .ref_counts
162                .get(self.id)
163                .expect("detected over-release of a handle");
164            let prev_count = count.fetch_add(1, SeqCst);
165            assert_ne!(prev_count, 0, "Detected over-release of a handle.");
166        }
167
168        Self {
169            id: self.id,
170            entity_type: self.entity_type,
171            entity_map: self.entity_map.clone(),
172        }
173    }
174}
175
176impl Drop for AnyHandle {
177    fn drop(&mut self) {
178        if let Some(entity_map) = self.entity_map.upgrade() {
179            let entity_map = entity_map.upgradable_read();
180            let count = entity_map
181                .ref_counts
182                .get(self.id)
183                .expect("Detected over-release of a handle.");
184            let prev_count = count.fetch_sub(1, SeqCst);
185            assert_ne!(prev_count, 0, "Detected over-release of a handle.");
186            if prev_count == 1 {
187                // We were the last reference to this entity, so we can remove it.
188                let mut entity_map = RwLockUpgradableReadGuard::upgrade(entity_map);
189                let entity = entity_map
190                    .entities
191                    .remove(self.id)
192                    .expect("entity was removed twice");
193                entity_map.ref_counts.remove(self.id);
194                entity_map.dropped_entities.push((self.id, entity));
195            }
196        }
197    }
198}
199
200impl<T> From<Handle<T>> for AnyHandle
201where
202    T: 'static + Send + Sync,
203{
204    fn from(handle: Handle<T>) -> Self {
205        handle.any_handle
206    }
207}
208
209#[derive(Deref, DerefMut)]
210pub struct Handle<T: Send + Sync> {
211    #[deref]
212    #[deref_mut]
213    any_handle: AnyHandle,
214    entity_type: PhantomData<T>,
215}
216
217impl<T: 'static + Send + Sync> Handle<T> {
218    fn new(id: EntityId, entity_map: Weak<RwLock<EntityMapState>>) -> Self {
219        Self {
220            any_handle: AnyHandle::new(id, TypeId::of::<T>(), entity_map),
221            entity_type: PhantomData,
222        }
223    }
224
225    pub fn downgrade(&self) -> WeakHandle<T> {
226        WeakHandle {
227            any_handle: self.any_handle.downgrade(),
228            entity_type: self.entity_type,
229        }
230    }
231
232    /// Update the entity referenced by this handle with the given function.
233    ///
234    /// The update function receives a context appropriate for its environment.
235    /// When updating in an `AppContext`, it receives a `ModelContext`.
236    /// When updating an a `WindowContext`, it receives a `ViewContext`.
237    pub fn update<C: Context, R>(
238        &self,
239        cx: &mut C,
240        update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
241    ) -> C::Result<R> {
242        cx.update_entity(self, update)
243    }
244}
245
246impl<T: Send + Sync> Clone for Handle<T> {
247    fn clone(&self) -> Self {
248        Self {
249            any_handle: self.any_handle.clone(),
250            entity_type: self.entity_type,
251        }
252    }
253}
254
255#[derive(Clone)]
256pub struct AnyWeakHandle {
257    pub(crate) id: EntityId,
258    entity_type: TypeId,
259    entity_map: Weak<RwLock<EntityMapState>>,
260}
261
262impl AnyWeakHandle {
263    pub fn upgrade(&self) -> Option<AnyHandle> {
264        let entity_map = &self.entity_map.upgrade()?;
265        entity_map
266            .read()
267            .ref_counts
268            .get(self.id)?
269            .fetch_add(1, SeqCst);
270        Some(AnyHandle {
271            id: self.id,
272            entity_type: self.entity_type,
273            entity_map: self.entity_map.clone(),
274        })
275    }
276}
277
278impl<T> From<WeakHandle<T>> for AnyWeakHandle
279where
280    T: 'static + Send + Sync,
281{
282    fn from(handle: WeakHandle<T>) -> Self {
283        handle.any_handle
284    }
285}
286
287#[derive(Deref, DerefMut)]
288pub struct WeakHandle<T> {
289    #[deref]
290    #[deref_mut]
291    any_handle: AnyWeakHandle,
292    entity_type: PhantomData<T>,
293}
294
295impl<T: 'static + Send + Sync> Clone for WeakHandle<T> {
296    fn clone(&self) -> Self {
297        Self {
298            any_handle: self.any_handle.clone(),
299            entity_type: self.entity_type,
300        }
301    }
302}
303
304impl<T: Send + Sync + 'static> WeakHandle<T> {
305    pub fn upgrade(&self) -> Option<Handle<T>> {
306        Some(Handle {
307            any_handle: self.any_handle.upgrade()?,
308            entity_type: self.entity_type,
309        })
310    }
311
312    /// Update the entity referenced by this handle with the given function if
313    /// the referenced entity still exists. Returns an error if the entity has
314    /// been released.
315    ///
316    /// The update function receives a context appropriate for its environment.
317    /// When updating in an `AppContext`, it receives a `ModelContext`.
318    /// When updating an a `WindowContext`, it receives a `ViewContext`.
319    pub fn update<C: Context, R>(
320        &self,
321        cx: &mut C,
322        update: impl FnOnce(&mut T, &mut C::EntityContext<'_, '_, T>) -> R,
323    ) -> Result<R>
324    where
325        Result<C::Result<R>>: crate::Flatten<R>,
326    {
327        crate::Flatten::flatten(
328            self.upgrade()
329                .ok_or_else(|| anyhow!("entity release"))
330                .map(|this| cx.update_entity(&this, update)),
331        )
332    }
333}