my_ecs/ecs/ent/
storage.rs

1use super::{
2    component::{Component, ComponentKey},
3    entity::{
4        ContainEntity, Entity, EntityId, EntityIndex, EntityKey, EntityKeyKind, EntityKeyRef,
5        EntityName, EntityTag,
6    },
7};
8use crate::{
9    ds::{
10        BorrowResult, Borrowed, DescribeGroup, Getter, GetterMut, GroupDesc, GroupMap,
11        SimpleHolder, TypeInfo,
12    },
13    ecs::EcsError,
14    util::{macros::debug_format, With},
15};
16use std::{
17    any::TypeId,
18    collections::HashMap,
19    fmt,
20    hash::BuildHasher,
21    marker::PhantomData,
22    ops::{Deref, DerefMut},
23    ptr::NonNull,
24    sync::Arc,
25};
26
27/// A trait for generating [`EntityReg`].
28pub trait AsEntityReg {
29    fn entity_descriptor() -> EntityReg;
30}
31
32/// A storage where you can find both entity and component data and static
33/// information about them such as names, types, and their relationships.
34///
35/// Each container is basically identified by its component keys. In other
36/// words, unique combination of components is the key of an entity container.
37/// So you cannot register two entities that has the same components. Entity
38/// containers are also identified by their indices they get when they are
39/// registered to this storage. It's recommended accessing using entity index
40/// instead of component keys if possible because it would be faster.
41///
42/// Optionally, entity name or type may be able to be used as an identification,
43/// but it's not guaranteed because it must be provided by clients. If not so,
44/// searching an entity container via entity name or type fails. See
45/// [`EntityKeyRef`].
46//
47// TODO: Write this on ent module doc as well.
48// Why entities of the same component combination are not allowed?
49// - If it's allowed, something like below is possible.
50//   EntA: (CompA, CompB), EntB: (CompA), EntC: (CompA)
51// - Imagine clients are removing `CompB` from some items in EntA's container.
52//   In that case, they must be moved into `EntB` or `EntC`, but we cannot
53//   specify which container they should go.
54#[derive(Debug)]
55pub(crate) struct EntityStorage<S> {
56    /// A map holding entity containers and relationships to components.
57    ///
58    /// Key: Index or slice of [`ComponentKey`]s
59    map: GroupMap<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo, S>,
60
61    /// Optional mapping from [`EntityName`] to [`EntityIndex`].
62    name_to_index: HashMap<EntityName, EntityIndex, S>,
63
64    /// Generation of each entity container. The generation is when the
65    /// container is registered to this storage.
66    ent_gens: Vec<u64>,
67
68    /// Generation that will be assigned to the next registered entity
69    /// container.
70    generation: u64,
71}
72
73impl<S> EntityStorage<S>
74where
75    S: Default,
76{
77    pub(crate) fn new() -> Self {
78        Self {
79            map: GroupMap::new(),
80            name_to_index: HashMap::default(),
81            ent_gens: Vec::new(),
82            generation: 1,
83        }
84    }
85}
86
87impl<S> EntityStorage<S>
88where
89    S: BuildHasher + Default,
90{
91    /// Turns `ekey` into another type of it according to `to`.
92    pub(crate) fn convert_entity_key<'r, K>(&self, key: K, to: EntityKeyKind) -> Option<EntityKey>
93    where
94        K: Into<EntityKeyRef<'r>>,
95    {
96        return inner(self, key.into(), to);
97
98        // === Internal helper functions ===
99
100        fn inner<S>(
101            this: &EntityStorage<S>,
102            key: EntityKeyRef<'_>,
103            to: EntityKeyKind,
104        ) -> Option<EntityKey>
105        where
106            S: BuildHasher + Default,
107        {
108            let ei = this.entity_index(key)?;
109            let res = match to {
110                EntityKeyKind::Name => {
111                    // Safety: Infallible.
112                    let (cont, _) = unsafe { this.map.get_group(ei).unwrap_unchecked() };
113                    let name = cont.get_tag().get_name()?.clone();
114                    EntityKey::Name(name)
115                }
116                EntityKeyKind::Index => {
117                    let ei = EntityIndex::new(With::new(
118                        ei,
119                        // Safety: Infallible.
120                        unsafe { *this.ent_gens.get_unchecked(ei) },
121                    ));
122                    EntityKey::Index(ei)
123                }
124                EntityKeyKind::Ckeys => {
125                    // Safety: Infallible.
126                    let ckeys = unsafe { this.map.get_group_key(ei).unwrap_unchecked() };
127                    EntityKey::Ckeys(Arc::clone(ckeys))
128                }
129            };
130            Some(res)
131        }
132    }
133
134    pub(crate) fn get_component_keys<'r, K>(&self, key: K) -> Option<&Arc<[ComponentKey]>>
135    where
136        K: Into<EntityKeyRef<'r>>,
137    {
138        return inner(self, key.into());
139
140        // === Internal helper functions ===
141
142        fn inner<'this, S>(
143            this: &'this EntityStorage<S>,
144            key: EntityKeyRef<'_>,
145        ) -> Option<&'this Arc<[ComponentKey]>>
146        where
147            S: BuildHasher + Default,
148        {
149            let ei = this.entity_index(key)?;
150            // Safety: Infallible.
151            let ckeys = unsafe { this.map.get_group_key(ei).unwrap_unchecked() };
152            Some(ckeys)
153        }
154    }
155
156    pub(crate) fn get_entity_container<'r, K>(&self, key: K) -> Option<&EntityContainer>
157    where
158        K: Into<EntityKeyRef<'r>>,
159    {
160        return inner(self, key.into());
161
162        // === Internal helper functions ===
163
164        fn inner<'this, S>(
165            this: &'this EntityStorage<S>,
166            key: EntityKeyRef<'_>,
167        ) -> Option<&'this EntityContainer>
168        where
169            S: BuildHasher + Default,
170        {
171            let ei = this.entity_index(key)?;
172            // Safety: Infallible.
173            let cont = unsafe { this.map.get_group(ei).unwrap_unchecked().0 };
174            Some(cont)
175        }
176    }
177
178    pub(crate) fn get_entity_container_mut<'r, K>(&mut self, key: K) -> Option<&mut EntityContainer>
179    where
180        K: Into<EntityKeyRef<'r>>,
181    {
182        return inner(self, key.into());
183
184        // === Internal helper functions ===
185
186        fn inner<'this, S>(
187            this: &'this mut EntityStorage<S>,
188            key: EntityKeyRef<'_>,
189        ) -> Option<&'this mut EntityContainer>
190        where
191            S: BuildHasher + Default,
192        {
193            let ei = this.entity_index(key)?;
194            // Safety: Infallible.
195            let cont = unsafe { this.map.get_group_mut(ei).unwrap_unchecked().0 };
196            Some(cont)
197        }
198    }
199
200    pub(crate) fn iter_entity_container(
201        &self,
202    ) -> impl Iterator<Item = (&Arc<[ComponentKey]>, EntityIndex, &EntityContainer)> {
203        self.map.iter_group().map(|(ckeys, index, cont)| {
204            (
205                ckeys,
206                EntityIndex::new(With::new(index, self.ent_gens[index])),
207                cont,
208            )
209        })
210    }
211
212    /// Registers new entity and its components information and returns entity
213    /// container index. If you want to change entity information, you must
214    /// remove if first. See [`Self::unregister_entity`]. Also, this method
215    /// doesn't overwrite component information.
216    pub(crate) fn register(&mut self, mut desc: EntityReg) -> Result<EntityIndex, EcsError> {
217        if desc.is_empty() {
218            let reason = debug_format!(
219                "failed to register an entity: `{:?}` has no components",
220                desc.get_name()
221            );
222            return Err(EcsError::InvalidEntity(reason, ()));
223        }
224
225        let gkey = desc
226            .get_key_info_pairs()
227            .iter()
228            .map(|(ckey, _)| *ckey)
229            .collect::<Vec<_>>();
230        let index = self.map.next_index(&*gkey);
231        let ei = EntityIndex::new(With::new(index, self.generation));
232        desc.set_index(ei);
233
234        let ename = desc.get_name().cloned();
235        match self.map.add_group(desc) {
236            Ok(i) => {
237                debug_assert_eq!(i, index);
238                self.generation += 1;
239
240                // Adds mapping.
241                if let Some(name) = ename {
242                    self.name_to_index.insert(name, ei);
243                }
244
245                // Writes current generation on the slot.
246                while self.ent_gens.len() <= index {
247                    self.ent_gens.push(0);
248                }
249                self.ent_gens[index] = ei.generation();
250
251                Ok(ei)
252            }
253            Err(desc) => {
254                let (_cont, _) = self.map.get_group2(&desc.group_key).unwrap();
255                let reason = debug_format!(
256                    "failed to register an entity: two entities `{:?}` and `{:?}` are the same",
257                    ename,
258                    _cont.get_tag().get_name()
259                );
260                Err(EcsError::InvalidEntity(reason, ()))
261            }
262        }
263    }
264
265    /// Unregister entity and tries to unregister corresponding components as well.
266    /// But components that are linked to another entity won't be unregistered.
267    pub(crate) fn unregister<'r, K>(
268        &mut self,
269        key: K,
270    ) -> Option<(Arc<[ComponentKey]>, EntityContainer)>
271    where
272        K: Into<EntityKeyRef<'r>>,
273    {
274        return inner(self, key.into());
275
276        // === Internal helper functions ===
277
278        fn inner<S>(
279            this: &mut EntityStorage<S>,
280            key: EntityKeyRef<'_>,
281        ) -> Option<(Arc<[ComponentKey]>, EntityContainer)>
282        where
283            S: BuildHasher + Default,
284        {
285            let index = this.entity_index(key)?;
286            let ckeys_cont = this.map.remove_group(index);
287            debug_assert!(ckeys_cont.is_some());
288
289            // Removes mapping.
290            if let Some((_ckeys, cont)) = ckeys_cont.as_ref() {
291                if let Some(name) = cont.get_tag().get_name() {
292                    this.name_to_index.remove(name);
293                }
294            }
295
296            // In contrast to `register_entity()`, we don't need to reset generation in the slot
297            // because we know that it doesn't exist.
298
299            ckeys_cont
300        }
301    }
302
303    pub(crate) fn entity_index<'r, K>(&self, key: K) -> Option<usize>
304    where
305        K: Into<EntityKeyRef<'r>>,
306    {
307        return inner(self, key.into());
308
309        // === Internal helper functions ===
310
311        fn inner<S>(this: &EntityStorage<S>, key: EntityKeyRef<'_>) -> Option<usize>
312        where
313            S: BuildHasher + Default,
314        {
315            match key {
316                EntityKeyRef::Ckeys(ckeys) => this.map.get_group_index(ckeys),
317                EntityKeyRef::Index(ei) => this.is_valid_index(ei).then_some(ei.index()),
318                EntityKeyRef::Name(name) => {
319                    let ei = this.name_to_index.get(name)?;
320                    this.is_valid_index(ei).then_some(ei.index())
321                }
322            }
323        }
324    }
325
326    /// # Safety
327    ///
328    /// Undefined behavior if exclusive borrow happened before.
329    //
330    // Allows dead_code for test.
331    #[allow(dead_code)]
332    pub(crate) unsafe fn get_ptr(&self, ei: &EntityIndex) -> Option<NonNull<dyn ContainEntity>> {
333        let ekey = EntityKeyRef::from(ei);
334        let cont = self.get_entity_container(ekey)?;
335        let ptr = unsafe { cont.cont_ptr.get_unchecked() };
336        Some(*ptr)
337    }
338
339    fn is_valid_index(&self, ei: &EntityIndex) -> bool {
340        self.map.contains_group(ei.index()) && ei.generation() == self.ent_gens[ei.index()]
341    }
342}
343
344impl<S> Default for EntityStorage<S>
345where
346    S: Default,
347{
348    fn default() -> Self {
349        Self::new()
350    }
351}
352
353/// A descriptor for registration of an entity storage.
354pub struct EntityReg {
355    name: Option<EntityName>,
356    index: Option<EntityIndex>,
357    cont: Box<dyn ContainEntity>,
358    key_info_pairs: Vec<(ComponentKey, TypeInfo)>,
359}
360
361impl fmt::Debug for EntityReg {
362    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363        f.debug_struct("EntityReg")
364            .field("name", &self.get_name())
365            .field("index", &self.get_index())
366            .field("key_info_pairs", &self.get_key_info_pairs())
367            .finish_non_exhaustive()
368    }
369}
370
371impl EntityReg {
372    pub fn new(name: Option<EntityName>, mut cont: Box<dyn ContainEntity>) -> Self {
373        // Removes remaining columns.
374        for ci in (0..cont.num_columns()).rev() {
375            cont.remove_column(ci);
376        }
377
378        Self {
379            name,
380            index: None,
381            cont,
382            key_info_pairs: Vec::new(),
383        }
384    }
385
386    pub(crate) const fn get_name(&self) -> Option<&EntityName> {
387        self.name.as_ref()
388    }
389
390    pub(crate) const fn get_index(&self) -> Option<&EntityIndex> {
391        self.index.as_ref()
392    }
393
394    pub(crate) const fn get_key_info_pairs(&self) -> &Vec<(ComponentKey, TypeInfo)> {
395        &self.key_info_pairs
396    }
397
398    pub fn add_component_of<C: Component>(&mut self) {
399        self.add_component(C::type_info());
400    }
401
402    pub fn add_component(&mut self, tinfo: TypeInfo) {
403        let pair = (ComponentKey::from(&tinfo), tinfo);
404        self.key_info_pairs.push(pair);
405    }
406
407    fn is_empty(&self) -> bool {
408        self.key_info_pairs.is_empty()
409    }
410
411    fn set_index(&mut self, index: EntityIndex) {
412        self.index = Some(index);
413    }
414
415    fn finish(self) -> GroupDesc<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> {
416        let Self {
417            name,
418            index,
419            mut cont,
420            mut key_info_pairs,
421        } = self;
422        let index = index.unwrap();
423
424        // Sorts by component key.
425        let old_len = key_info_pairs.len();
426        key_info_pairs.sort_unstable_by_key(|(key, _)| *key);
427        key_info_pairs.dedup_by_key(|(key, _)| *key);
428        assert_eq!(
429            key_info_pairs.len(),
430            old_len,
431            "entity cannot have duplicated components"
432        );
433
434        // Puts sorted component columns in the container.
435        let mut ckeys = Vec::new();
436        let mut cnames = Vec::new();
437        for (ckey, tinfo) in key_info_pairs.iter() {
438            cnames.push(tinfo.name);
439            ckeys.push(*ckey);
440            cont.add_column(*tinfo);
441        }
442        let ckeys: Arc<[ComponentKey]> = ckeys.into();
443        let cnames: Box<[&'static str]> = cnames.into();
444
445        // Creates `EntityContainer` which is more info-rich.
446        let tag = EntityTag::new(index, name, Arc::clone(&ckeys), cnames);
447        let tag = Arc::new(tag);
448        let cont = EntityContainer::new(tag, cont);
449
450        GroupDesc {
451            group_key: ckeys,
452            group_value: cont,
453            items: key_info_pairs,
454        }
455    }
456}
457
458impl DescribeGroup<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> for EntityReg {
459    fn into_group_and_items(
460        self,
461    ) -> GroupDesc<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> {
462        self.finish()
463    }
464}
465
466/// A wrapper of an entity container.
467///
468/// The entity container is held as a trait object, and this wrapper provides
469/// access to the entity container with some information such as entity name.
470pub(crate) struct EntityContainer {
471    tag: Arc<EntityTag>,
472
473    /// Container that including components for the entity.
474    cont: Box<dyn ContainEntity>,
475
476    /// Pointer to the `dyn ContainEntity`.
477    cont_ptr: SimpleHolder<NonNull<dyn ContainEntity>>,
478}
479
480impl fmt::Debug for EntityContainer {
481    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
482        f.debug_struct("EntityContainer")
483            .field("tag", &self.get_tag())
484            .field("cont_ptr", &self.cont_ptr)
485            .finish_non_exhaustive()
486    }
487}
488
489impl EntityContainer {
490    pub(crate) fn new(tag: Arc<EntityTag>, mut cont: Box<dyn ContainEntity>) -> Self {
491        // Safety: Infallible
492        let cont_ptr = unsafe { NonNull::new_unchecked(&mut *cont as *mut _) };
493        let cont_ptr = SimpleHolder::new(cont_ptr);
494
495        Self {
496            tag,
497            cont,
498            cont_ptr,
499        }
500    }
501
502    pub(crate) fn into_inner(self) -> Box<dyn ContainEntity> {
503        self.cont
504    }
505
506    pub(crate) const fn get_tag(&self) -> &Arc<EntityTag> {
507        &self.tag
508    }
509
510    pub(crate) fn borrow(&self) -> BorrowResult<NonNull<dyn ContainEntity>> {
511        self.cont_ptr.borrow()
512    }
513}
514
515impl Deref for EntityContainer {
516    type Target = dyn ContainEntity;
517
518    fn deref(&self) -> &Self::Target {
519        &*self.cont
520    }
521}
522
523impl DerefMut for EntityContainer {
524    fn deref_mut(&mut self) -> &mut Self::Target {
525        &mut *self.cont
526    }
527}
528
529pub struct EntityContainerRef<'buf, T> {
530    etag: &'buf EntityTag,
531    cont: &'buf mut dyn ContainEntity,
532    _marker: PhantomData<&'buf mut T>,
533}
534
535impl<T> fmt::Debug for EntityContainerRef<'_, T> {
536    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
537        f.debug_struct("EntityContainerRef")
538            .field("etag", &self.get_entity_tag())
539            .finish_non_exhaustive()
540    }
541}
542
543impl<'buf, T> EntityContainerRef<'buf, T> {
544    pub(crate) fn new(etag: &'buf EntityTag, cont: &'buf mut dyn ContainEntity) -> Self {
545        Self {
546            etag,
547            cont,
548            _marker: PhantomData,
549        }
550    }
551
552    pub const fn get_entity_tag(&self) -> &EntityTag {
553        self.etag
554    }
555
556    /// Returns number of items.
557    pub fn len(&self) -> usize {
558        self.cont.len()
559    }
560
561    /// Returns `true` if the entity container is empty.
562    pub fn is_empty(&self) -> bool {
563        self.len() == 0
564    }
565
566    /// Returns capacity if the entity container supports gauging capacity.
567    /// Otherwise, returns number of items, which is equal to
568    /// [`EntityContainerRef::len`].
569    pub fn capacity(&self) -> usize {
570        self.cont.capacity()
571    }
572
573    /// Returns number of component columns.
574    pub fn num_columns(&self) -> usize {
575        self.cont.num_columns()
576    }
577
578    /// Returns `true` if all components in the entity container are [`Default`].
579    pub fn is_default(&self) -> bool {
580        (0..self.num_columns()).all(|ci| {
581            // Safety: Infallible.
582            let tinfo = unsafe { self.cont.get_column_info(ci).unwrap_unchecked() };
583            tinfo.is_default
584        })
585    }
586
587    /// Returns `true` if all components in the entity container are [`Clone`].
588    pub fn is_clone(&self) -> bool {
589        (0..self.num_columns()).all(|ci| {
590            // Safety: Infallible.
591            let tinfo = unsafe { self.cont.get_column_info(ci).unwrap_unchecked() };
592            tinfo.is_clone
593        })
594    }
595
596    /// Reserves extra `additional` capacity if the entity container supports to
597    /// do so. Otherwise, nothing takes place.
598    pub fn reserve(&mut self, additional: usize) {
599        self.cont.reserve(additional);
600    }
601
602    pub fn shrink_to_fit(&mut self) {
603        self.cont.shrink_to_fit();
604    }
605
606    /// Retrieves borrowed getter for a certain component column.
607    ///
608    /// If the component type doesn't belong to the entity or the column was
609    /// borrowed mutably in the past and not returned yet, returns None.
610    pub fn get_column_of<C: Component>(&self) -> Option<Borrowed<Getter<'_, C>>> {
611        let ci = self.cont.get_column_index(&TypeId::of::<C>())?;
612        self.cont.borrow_column(ci).ok().map(|col| {
613            // Safety: We got the column index from the type, so the type is correct.
614            col.map(|raw_getter| unsafe { Getter::from_raw(raw_getter) })
615        })
616    }
617
618    /// Retrieves borrowed mutable getter for a certain component column.
619    ///
620    /// If the component type doesn't belong to the entity or the column was
621    /// borrowed in the past and not returned yet, returns None.
622    pub fn get_column_mut_of<C: Component>(&mut self) -> Option<Borrowed<GetterMut<'_, C>>> {
623        let ci = self.cont.get_column_index(&TypeId::of::<C>())?;
624        self.cont.borrow_column_mut(ci).ok().map(|col| {
625            // Safety: We got the column index from the type, so the type is correct.
626            col.map(|raw_getter| unsafe { GetterMut::from_raw(raw_getter) })
627        })
628    }
629
630    /// Removes an entity for the given entity id from the entity container.
631    pub fn remove(&mut self, eid: &EntityId) {
632        if eid.container_index() == self.get_entity_tag().index() {
633            if let Some(vi) = self.cont.to_value_index(eid.row_index()) {
634                self.remove_by_value_index(vi);
635            }
636        }
637    }
638
639    /// Removes an entity for the given value index from the entity container.
640    ///
641    /// # Panics
642    ///
643    /// Panics if the given value index is out of bounds.
644    pub fn remove_by_value_index(&mut self, vi: usize) {
645        self.cont.remove_row_by_value_index(vi);
646    }
647}
648
649impl<T> EntityContainerRef<'_, T>
650where
651    T: Entity,
652{
653    /// Returns a struct holding shared references to components that belong
654    /// to an entity for the given entity id.
655    ///
656    /// If it failed to find an entity using the given entity id, returns
657    /// `None`. See [`EntityContainerRef::get_by_value_index`] for more
658    /// details.
659    ///
660    /// # Examples
661    ///
662    /// ```
663    /// # use my_ecs::prelude::*;
664    ///
665    /// #[derive(Entity)]
666    /// struct Ea {
667    ///     ca: Ca,
668    /// }
669    /// #[derive(Component, Debug)]
670    /// struct Ca(i32);
671    ///
672    /// fn system(ew: EntWrite<Ea>) {
673    ///     let mut ew = ew.take_recur();
674    ///     let eid = ew.add(Ea { ca: Ca(0) });
675    ///     println!("{:?}", ew.get(&eid).unwrap());
676    /// }
677    /// ```
678    pub fn get(&self, eid: &EntityId) -> Option<T::Ref<'_>> {
679        if eid.container_index() == self.get_entity_tag().index() {
680            let vi = self.cont.to_value_index(eid.row_index())?;
681            Some(self.get_by_value_index(vi))
682        } else {
683            None
684        }
685    }
686
687    /// Returns a struct holding mutable references to components that belong
688    /// to an entity for the given entity id.
689    ///
690    /// If it failed to find an entity using the given entity id, returns
691    /// `None`. See [`EntityContainerRef::get_mut_by_value_index`] for more
692    /// details.
693    ///
694    /// # Examples
695    ///
696    /// ```
697    /// # use my_ecs::prelude::*;
698    ///
699    /// #[derive(Entity)]
700    /// struct Ea {
701    ///     ca: Ca,
702    /// }
703    /// #[derive(Component, Debug)]
704    /// struct Ca(i32);
705    ///
706    /// fn system(ew: EntWrite<Ea>) {
707    ///     let mut ew = ew.take_recur();
708    ///     let eid = ew.add(Ea { ca: Ca(0) });
709    ///     let ea = ew.get_mut(&eid).unwrap();
710    ///     *ea.ca = Ca(42);
711    ///     println!("{:?}", ew.get(&eid).unwrap());
712    /// }
713    /// ```
714    pub fn get_mut(&mut self, eid: &EntityId) -> Option<T::Mut<'_>> {
715        if eid.container_index() == self.get_entity_tag().index() {
716            let vi = self.cont.to_value_index(eid.row_index())?;
717            Some(self.get_mut_by_value_index(vi))
718        } else {
719            None
720        }
721    }
722
723    /// Returns a struct holding shared references to components that belong
724    /// to an entity for the given value index.
725    ///
726    /// Note, however, that entity is not stored as it is. It is split up into
727    /// its components then stored in each component container. That means
728    /// collecting those references like this function is inefficient because it
729    /// requires random access to memory.
730    ///
731    /// # Panics
732    ///
733    /// Panics if the given value index is out of bounds.
734    ///
735    /// # Examples
736    ///
737    /// ```
738    /// # use my_ecs::prelude::*;
739    ///
740    /// #[derive(Entity)]
741    /// struct Ea {
742    ///     ca: Ca,
743    /// }
744    /// #[derive(Component, Debug)]
745    /// struct Ca(i32);
746    ///
747    /// fn system(ew: EntWrite<Ea>) {
748    ///     let ew = ew.take_recur();
749    ///     for vi in 0..ew.len() {
750    ///         println!("{:?}", ew.get_by_value_index(vi));
751    ///     }
752    /// }
753    /// ```
754    pub fn get_by_value_index(&self, vi: usize) -> T::Ref<'_> {
755        // Panics here.
756        T::get_ref_from(self.cont, vi)
757    }
758
759    /// Returns a struct holding mutable references to components that belong
760    /// to an entity for the given value index.
761    ///
762    /// Note, however, that entity is not stored as it is. It is split up into
763    /// its components then stored in each component container. That means
764    /// collecting those references like this function is inefficient because it
765    /// requires random access to memory.
766    ///
767    /// # Panics
768    ///
769    /// Panics if the given value index is out of bounds.
770    ///
771    /// # Examples
772    ///
773    /// ```
774    /// # use my_ecs::prelude::*;
775    ///
776    /// #[derive(Entity)]
777    /// struct Ea {
778    ///     ca: Ca,
779    /// }
780    /// #[derive(Component, Debug)]
781    /// struct Ca(i32);
782    ///
783    /// fn system(ew: EntWrite<Ea>) {
784    ///     let mut ew = ew.take_recur();
785    ///     for vi in 0..ew.len() {
786    ///         let ea = ew.get_mut_by_value_index(vi);
787    ///         *ea.ca = Ca(42);
788    ///         println!("{:?}", ea);
789    ///     }
790    /// }
791    /// ```
792    pub fn get_mut_by_value_index(&mut self, vi: usize) -> T::Mut<'_> {
793        // Panics here.
794        T::get_mut_from(self.cont, vi)
795    }
796
797    /// Inserts an entity to the entity container then returns entity id of the
798    /// inserted entity.
799    ///
800    /// It's encouraged to use combination of [`EntityContainerRef::resize`]
801    /// and [`EntityContainerRef::get_column_mut_of`] when you need to add
802    /// lots of entities at once. It's more cache-friendly so it would be faster
803    /// than calling to this method many times.
804    pub fn add(&mut self, value: T) -> EntityId {
805        let ei = self.get_entity_tag().index();
806        let ri = value.move_to(self.cont);
807        EntityId::new(ei, ri)
808    }
809
810    /// Removes an entity for the given entity id from the entity container
811    /// then returns the entity.
812    pub fn take(&mut self, eid: &EntityId) -> Option<T> {
813        if eid.container_index() == self.get_entity_tag().index() {
814            let vi = self.cont.to_value_index(eid.row_index())?;
815            Some(self.take_by_value_index(vi))
816        } else {
817            None
818        }
819    }
820
821    /// Removes an entity for the given value index from the entity container
822    /// then returns the entity.
823    ///
824    /// # Panics
825    ///
826    /// Panics if the given value index is out of bounds.
827    pub fn take_by_value_index(&mut self, vi: usize) -> T {
828        T::take_from(self.cont, vi)
829    }
830
831    /// Resizes the entity container to the new length by cloning the given
832    /// value.
833    ///
834    /// Resizing occurs column by column.
835    ///
836    /// # Panics
837    ///
838    /// Panics if any components of the entity are not [`Clone`].
839    pub fn resize(&mut self, new_len: usize, value: T) {
840        for ci in 0..T::num_components() {
841            // Safety: All columns are resized.
842            unsafe {
843                self.cont
844                    .resize_column(ci, new_len, value.component_ptr(ci));
845            }
846        }
847    }
848}