my-ecs 0.1.1

An Entity Component System (ECS) library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
use super::{
    component::{Component, ComponentKey},
    entity::{
        ContainEntity, Entity, EntityId, EntityIndex, EntityKey, EntityKeyKind, EntityKeyRef,
        EntityName, EntityTag,
    },
};
use crate::{ecs::EcsError, util, FxBuildHasher};
use my_utils::{
    debug_format,
    ds::{
        BorrowResult, Borrowed, DescribeGroup, Getter, GetterMut, GroupDesc, GroupMap,
        SimpleHolder, TypeInfo,
    },
    With,
};
use std::{
    any::TypeId,
    collections::HashMap,
    fmt,
    hash::BuildHasher,
    marker::PhantomData,
    ops::{Deref, DerefMut},
    ptr::NonNull,
    sync::Arc,
};

/// A trait for generating [`EntityReg`].
pub trait AsEntityReg {
    fn entity_descriptor() -> EntityReg;
}

/// A storage where you can find both entity and component data and static information about them
/// such as names, types, and their relationships.
///
/// Each container is basically identified by its component keys. In other words, unique combination
/// of components is the key of an entity container. So you cannot register two entities that has
/// the same components. Entity containers are also identified by their indices they get when they
/// are registered to this storage. It's recommended accessing using entity index instead of
/// component keys if possible because it would be faster.
///
/// Optionally, entity name or type may be able to be used as an identification, but it's not
/// guaranteed because it must be provided by clients. If not so, searching an entity container via
/// entity name or type fails. See [`EntityKeyRef`].
//
// TODO: Write this on ent module doc as well.
// Why entities of the same component combination are not allowed?
// - If it's allowed, something like below is possible. EntA: (CompA, CompB), EntB: (CompA), EntC:
//   (CompA)
// - Imagine clients are removing `CompB` from some items in EntA's container. In that case, they
//   must be moved into `EntB` or `EntC`, but we cannot specify which container they should go.
#[derive(Debug)]
pub(crate) struct EntityStorage<S = FxBuildHasher> {
    /// A map holding entity containers and relationships to components.
    ///
    /// Key: Index or slice of [`ComponentKey`]s
    map: GroupMap<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo, S>,

    /// Optional mapping from [`EntityName`] to [`EntityIndex`].
    name_to_index: HashMap<EntityName, EntityIndex, S>,

    /// Generation of each entity container. The generation is when the container is registered to
    /// this storage.
    ent_gens: Vec<u64>,

    /// Generation that will be assigned to the next registered entity container.
    generation: u64,
}

impl EntityStorage {
    #[cfg(test)]
    pub(crate) fn new() -> Self {
        Self {
            map: GroupMap::new(),
            name_to_index: HashMap::with_hasher(FxBuildHasher::default()),
            ent_gens: Vec::new(),
            generation: 1,
        }
    }
}

impl<S> EntityStorage<S> {
    pub(crate) fn with_hasher<F: FnMut() -> S>(mut hasher: F) -> Self {
        Self {
            map: GroupMap::with_hasher(&mut hasher),
            name_to_index: HashMap::with_hasher(hasher()),
            ent_gens: Vec::new(),
            generation: 1,
        }
    }
}

impl<S> EntityStorage<S>
where
    S: BuildHasher + Default,
{
    /// Turns `ekey` into another type of it according to `to`.
    #[inline]
    pub(crate) fn convert_entity_key<'r, K>(&self, key: K, to: EntityKeyKind) -> Option<EntityKey>
    where
        K: Into<EntityKeyRef<'r>>,
    {
        fn inner<S>(
            this: &EntityStorage<S>,
            key: EntityKeyRef<'_>,
            to: EntityKeyKind,
        ) -> Option<EntityKey>
        where
            S: BuildHasher + Default,
        {
            let ei = this.entity_index(key)?;
            let res = match to {
                EntityKeyKind::Name => {
                    // Safety: Infallible.
                    let cont = unsafe { this.map.get_group(ei).unwrap_unchecked() };
                    let name = cont.get_tag().get_name()?.clone();
                    EntityKey::Name(name)
                }
                EntityKeyKind::Index => {
                    let ei = EntityIndex::new(With::new(
                        ei,
                        // Safety: Infallible.
                        unsafe { *this.ent_gens.get_unchecked(ei) },
                    ));
                    EntityKey::Index(ei)
                }
                EntityKeyKind::Ckeys => {
                    // Safety: Infallible.
                    let ckeys = unsafe { this.map.get_group_key(ei).unwrap_unchecked() };
                    EntityKey::Ckeys(Arc::clone(ckeys))
                }
            };
            Some(res)
        }
        inner(self, key.into(), to)
    }

    pub(crate) fn get_component_keys<'r, K>(&self, key: K) -> Option<&Arc<[ComponentKey]>>
    where
        K: Into<EntityKeyRef<'r>>,
    {
        let ei = self.entity_index(key)?;
        // Safety: Infallible.
        let ckeys = unsafe { self.map.get_group_key(ei).unwrap_unchecked() };
        Some(ckeys)
    }

    pub(crate) fn get_entity_container<'r, K>(&self, key: K) -> Option<&EntityContainer>
    where
        K: Into<EntityKeyRef<'r>>,
    {
        let ei = self.entity_index(key)?;
        // Safety: Infallible.
        let cont = unsafe { self.map.get_group(ei).unwrap_unchecked() };
        Some(cont)
    }

    pub(crate) fn get_entity_container_mut<'r, K>(&mut self, key: K) -> Option<&mut EntityContainer>
    where
        K: Into<EntityKeyRef<'r>>,
    {
        let ei = self.entity_index(key)?;
        // Safety: Infallible.
        let cont = unsafe { self.map.get_group_mut(ei).unwrap_unchecked() };
        Some(cont)
    }

    pub(crate) fn get_two_entity_container_mut<'r, K1, K2>(
        &mut self,
        key1: K1,
        key2: K2,
    ) -> Option<(&mut EntityContainer, &mut EntityContainer)>
    where
        K1: Into<EntityKeyRef<'r>>,
        K2: Into<EntityKeyRef<'r>>,
    {
        let ei1 = self.entity_index(key1)?;
        let ei2 = self.entity_index(key2)?;

        // Safety: `groups` is of [Option<T>] type. We must do not change Option's state. That is
        // guaranteed because we're returning T instead of Option<T>.
        let groups = unsafe { self.map.as_mut_groups() };

        // Safety: Infallible.
        unsafe {
            let (slot1, slot2) = util::get_two_mut(groups, ei1, ei2).unwrap_unchecked();
            let cont1 = slot1.as_mut().unwrap_unchecked();
            let cont2 = slot2.as_mut().unwrap_unchecked();
            Some((cont1, cont2))
        }
    }

    // TODO: Implement
    #[allow(unused)]
    pub(crate) fn get_two_enitty_containers_mut<'r, K1, K2>(
        &mut self,
        key1: K1,
        key2: K2,
    ) -> Option<(&mut EntityContainer, &mut EntityContainer)>
    where
        K1: Into<EntityKeyRef<'r>>,
        K2: Into<EntityKeyRef<'r>>,
    {
        let ei1 = self.entity_index(key1)?;
        let ei2 = self.entity_index(key2)?;

        todo!()
    }

    pub(crate) fn iter_entity_container(
        &self,
    ) -> impl Iterator<Item = (&Arc<[ComponentKey]>, EntityIndex, &EntityContainer)> {
        self.map.iter_group().map(|(ckeys, index, cont)| {
            (
                ckeys,
                EntityIndex::new(With::new(index, self.ent_gens[index])),
                cont,
            )
        })
    }

    /// Registers new entity and its components information and returns entity container index. If
    /// you want to change entity information, you must remove if first. See
    /// [`Self::unregister_entity`]. Also, this method doesn't overwrite component information.
    pub(crate) fn register(&mut self, mut desc: EntityReg) -> Result<EntityIndex, EcsError> {
        if desc.is_empty() {
            let reason = debug_format!(
                "failed to register an entity: `{:?}` has no components",
                desc.get_name()
            );
            return Err(EcsError::InvalidEntity(reason, ()));
        }

        let gkey = desc
            .get_key_info_pairs()
            .iter()
            .map(|(ckey, _)| *ckey)
            .collect::<Vec<_>>();
        let index = self.map.next_index(&*gkey);
        let ei = EntityIndex::new(With::new(index, self.generation));
        desc.set_index(ei);

        let ename = desc.get_name().cloned();
        match self.map.add_group(desc) {
            Ok(i) => {
                debug_assert_eq!(i, index);
                self.generation += 1;

                // Adds mapping.
                if let Some(name) = ename {
                    self.name_to_index.insert(name, ei);
                }

                // Writes current generation on the slot.
                while self.ent_gens.len() <= index {
                    self.ent_gens.push(0);
                }
                self.ent_gens[index] = ei.generation();

                Ok(ei)
            }
            Err(desc) => {
                let cont = self.map.get_group2(&desc.group_key).unwrap();
                let reason = debug_format!(
                    "failed to register an entity: two entities `{:?}` and `{:?}` are the same",
                    ename,
                    cont.get_tag().get_name()
                );
                Err(EcsError::InvalidEntity(reason, ()))
            }
        }
    }

    /// Unregister entity and tries to unregister corresponding components as well. But components
    /// that are linked to another entity won't be unregistered.
    pub(crate) fn unregister<'r, K>(
        &mut self,
        key: K,
    ) -> Option<(Arc<[ComponentKey]>, EntityContainer)>
    where
        K: Into<EntityKeyRef<'r>>,
    {
        return inner(self, key.into());

        // === Internal helper functions ===

        fn inner<S>(
            this: &mut EntityStorage<S>,
            key: EntityKeyRef<'_>,
        ) -> Option<(Arc<[ComponentKey]>, EntityContainer)>
        where
            S: BuildHasher + Default,
        {
            let index = this.entity_index(key)?;
            let ckeys_cont = this.map.remove_group(index);
            debug_assert!(ckeys_cont.is_some());

            // Removes mapping.
            if let Some((_ckeys, cont)) = ckeys_cont.as_ref() {
                if let Some(name) = cont.get_tag().get_name() {
                    this.name_to_index.remove(name);
                }
            }

            // In contrast to `register_entity()`, we don't need to reset generation in the slot
            // because we know that it doesn't exist.

            ckeys_cont
        }
    }

    pub(crate) fn entity_index<'r, K>(&self, key: K) -> Option<usize>
    where
        K: Into<EntityKeyRef<'r>>,
    {
        return inner(self, key.into());

        // === Internal helper functions ===

        fn inner<S>(this: &EntityStorage<S>, key: EntityKeyRef<'_>) -> Option<usize>
        where
            S: BuildHasher + Default,
        {
            match key {
                EntityKeyRef::Ckeys(ckeys) => this.map.get_group_index(ckeys),
                EntityKeyRef::Index(ei) => this.is_valid_index(ei).then_some(ei.index()),
                EntityKeyRef::Name(name) => {
                    let ei = this.name_to_index.get(name)?;
                    this.is_valid_index(ei).then_some(ei.index())
                }
            }
        }
    }

    /// # Safety
    ///
    /// Undefined behavior if exclusive borrow happened before.
    //
    // Allows dead_code for test.
    #[allow(dead_code)]
    pub(crate) unsafe fn get_ptr(&self, ei: &EntityIndex) -> Option<NonNull<dyn ContainEntity>> {
        let ekey = EntityKeyRef::from(ei);
        let cont = self.get_entity_container(ekey)?;
        let ptr = *cont.cont_ptr.borrow().unwrap();
        Some(ptr)
    }

    fn is_valid_index(&self, ei: &EntityIndex) -> bool {
        self.map.contains_group(ei.index()) && ei.generation() == self.ent_gens[ei.index()]
    }
}

impl<S> Default for EntityStorage<S>
where
    S: Default,
{
    fn default() -> Self {
        Self::with_hasher(|| S::default())
    }
}

/// A descriptor for registration of an entity storage.
pub struct EntityReg {
    name: Option<EntityName>,
    index: Option<EntityIndex>,
    cont: Box<dyn ContainEntity>,
    key_info_pairs: Vec<(ComponentKey, TypeInfo)>,
}

impl fmt::Debug for EntityReg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EntityReg")
            .field("name", &self.get_name())
            .field("index", &self.get_index())
            .field("key_info_pairs", &self.get_key_info_pairs())
            .finish_non_exhaustive()
    }
}

impl EntityReg {
    pub fn new(name: Option<EntityName>, mut cont: Box<dyn ContainEntity>) -> Self {
        // Removes remaining columns.
        for ci in (0..cont.num_columns()).rev() {
            cont.remove_column(ci);
        }

        Self {
            name,
            index: None,
            cont,
            key_info_pairs: Vec::new(),
        }
    }

    pub(crate) const fn get_name(&self) -> Option<&EntityName> {
        self.name.as_ref()
    }

    pub(crate) const fn get_index(&self) -> Option<&EntityIndex> {
        self.index.as_ref()
    }

    pub(crate) const fn get_key_info_pairs(&self) -> &Vec<(ComponentKey, TypeInfo)> {
        &self.key_info_pairs
    }

    pub fn add_component_of<C: Component>(&mut self) {
        self.add_component(C::type_info());
    }

    pub fn add_component(&mut self, tinfo: TypeInfo) {
        let pair = (ComponentKey::from(&tinfo), tinfo);
        self.key_info_pairs.push(pair);
    }

    fn is_empty(&self) -> bool {
        self.key_info_pairs.is_empty()
    }

    fn set_index(&mut self, index: EntityIndex) {
        self.index = Some(index);
    }

    fn finish(self) -> GroupDesc<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> {
        let Self {
            name,
            index,
            mut cont,
            mut key_info_pairs,
        } = self;
        let index = index.unwrap();

        // Sorts by component key.
        let old_len = key_info_pairs.len();
        key_info_pairs.sort_unstable_by_key(|(key, _)| *key);
        key_info_pairs.dedup_by_key(|(key, _)| *key);
        assert_eq!(
            key_info_pairs.len(),
            old_len,
            "entity cannot have duplicated components"
        );

        // Puts sorted component columns in the container.
        let mut ckeys = Vec::new();
        let mut cnames = Vec::new();
        for (ckey, tinfo) in key_info_pairs.iter() {
            cnames.push(tinfo.name);
            ckeys.push(*ckey);
            cont.add_column(*tinfo);
        }
        let ckeys: Arc<[ComponentKey]> = ckeys.into();
        let cnames: Box<[&'static str]> = cnames.into();

        // Creates `EntityContainer` which is more info-rich.
        let tag = EntityTag::new(index, name, Arc::clone(&ckeys), cnames);
        let tag = Arc::new(tag);
        let cont = EntityContainer::new(tag, cont);

        GroupDesc {
            group_key: ckeys,
            group_value: cont,
            items: key_info_pairs,
        }
    }
}

impl DescribeGroup<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> for EntityReg {
    fn into_group_and_items(
        self,
    ) -> GroupDesc<Arc<[ComponentKey]>, EntityContainer, ComponentKey, TypeInfo> {
        self.finish()
    }
}

/// A wrapper of an entity container.
///
/// The entity container is held as a trait object, and this wrapper provides access to the entity
/// container with some information such as entity name.
pub(crate) struct EntityContainer {
    tag: Arc<EntityTag>,

    /// Container that including components for the entity.
    cont: Box<dyn ContainEntity>,

    /// Pointer to the `dyn ContainEntity`.
    cont_ptr: SimpleHolder<NonNull<dyn ContainEntity>>,
}

impl fmt::Debug for EntityContainer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EntityContainer")
            .field("tag", &self.get_tag())
            .field("cont_ptr", &self.cont_ptr)
            .finish_non_exhaustive()
    }
}

impl EntityContainer {
    pub(crate) fn new(tag: Arc<EntityTag>, mut cont: Box<dyn ContainEntity>) -> Self {
        // Safety: Infallible
        let cont_ptr = unsafe { NonNull::new_unchecked(&mut *cont as *mut _) };
        let cont_ptr = SimpleHolder::new(cont_ptr);

        Self {
            tag,
            cont,
            cont_ptr,
        }
    }

    pub(crate) fn into_inner(self) -> Box<dyn ContainEntity> {
        self.cont
    }

    pub(crate) const fn get_tag(&self) -> &Arc<EntityTag> {
        &self.tag
    }

    pub(crate) fn borrow(&self) -> BorrowResult<NonNull<dyn ContainEntity>> {
        self.cont_ptr.borrow()
    }
}

impl Deref for EntityContainer {
    type Target = dyn ContainEntity;

    fn deref(&self) -> &Self::Target {
        &*self.cont
    }
}

impl DerefMut for EntityContainer {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.cont
    }
}

pub struct EntityContainerRef<'buf, T> {
    etag: &'buf EntityTag,
    cont: &'buf mut dyn ContainEntity,
    _marker: PhantomData<&'buf mut T>,
}

impl<T> fmt::Debug for EntityContainerRef<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EntityContainerRef")
            .field("etag", &self.get_entity_tag())
            .finish_non_exhaustive()
    }
}

impl<'buf, T> EntityContainerRef<'buf, T> {
    pub(crate) fn new(etag: &'buf EntityTag, cont: &'buf mut dyn ContainEntity) -> Self {
        Self {
            etag,
            cont,
            _marker: PhantomData,
        }
    }

    pub const fn get_entity_tag(&self) -> &EntityTag {
        self.etag
    }

    /// Returns number of items.
    pub fn len(&self) -> usize {
        self.cont.len()
    }

    /// Returns `true` if the entity container is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns capacity if the entity container supports gauging capacity. Otherwise, returns
    /// number of items, which is equal to
    /// [`EntityContainerRef::len`].
    pub fn capacity(&self) -> usize {
        self.cont.capacity()
    }

    /// Returns number of component columns.
    pub fn num_columns(&self) -> usize {
        self.cont.num_columns()
    }

    /// Returns `true` if all components in the entity container are [`Default`].
    pub fn is_default(&self) -> bool {
        (0..self.num_columns()).all(|ci| {
            // Safety: Infallible.
            let tinfo = unsafe { self.cont.get_column_info(ci).unwrap_unchecked() };
            tinfo.is_default
        })
    }

    /// Returns `true` if all components in the entity container are [`Clone`].
    pub fn is_clone(&self) -> bool {
        (0..self.num_columns()).all(|ci| {
            // Safety: Infallible.
            let tinfo = unsafe { self.cont.get_column_info(ci).unwrap_unchecked() };
            tinfo.is_clone
        })
    }

    /// Reserves extra `additional` capacity if the entity container supports to do so. Otherwise,
    /// nothing takes place.
    pub fn reserve(&mut self, additional: usize) {
        self.cont.reserve(additional);
    }

    pub fn shrink_to_fit(&mut self) {
        self.cont.shrink_to_fit();
    }

    /// Retrieves borrowed getter for a certain component column.
    ///
    /// If the component type doesn't belong to the entity or the column was borrowed mutably in the
    /// past and not returned yet, returns None.
    pub fn get_column_of<C: Component>(&self) -> Option<Borrowed<Getter<'_, C>>> {
        let ci = self.cont.get_column_index(&TypeId::of::<C>())?;
        self.cont.borrow_column(ci).ok().map(|col| {
            // Safety: We got the column index from the type, so the type is correct.
            col.map(|raw_getter| unsafe { Getter::from_raw(raw_getter) })
        })
    }

    /// Retrieves borrowed mutable getter for a certain component column.
    ///
    /// If the component type doesn't belong to the entity or the column was borrowed in the past
    /// and not returned yet, returns None.
    pub fn get_column_mut_of<C: Component>(&mut self) -> Option<Borrowed<GetterMut<'_, C>>> {
        let ci = self.cont.get_column_index(&TypeId::of::<C>())?;
        self.cont.borrow_column_mut(ci).ok().map(|col| {
            // Safety: We got the column index from the type, so the type is correct.
            col.map(|raw_getter| unsafe { GetterMut::from_raw(raw_getter) })
        })
    }

    /// Removes an entity for the given entity id from the entity container.
    pub fn remove(&mut self, eid: &EntityId) {
        if eid.container_index() == self.get_entity_tag().index() {
            if let Some(vi) = self.cont.to_value_index(eid.row_index()) {
                self.remove_by_value_index(vi);
            }
        }
    }

    /// Removes an entity for the given value index from the entity container.
    ///
    /// # Panics
    ///
    /// Panics if the given value index is out of bounds.
    pub fn remove_by_value_index(&mut self, vi: usize) {
        self.cont.remove_row_by_value_index(vi);
    }
}

impl<T> EntityContainerRef<'_, T>
where
    T: Entity,
{
    /// Returns a struct holding shared references to components that belong to an entity for the
    /// given entity id.
    ///
    /// If it failed to find an entity using the given entity id, returns `None`. See
    /// [`EntityContainerRef::get_by_value_index`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// # use my_ecs::prelude::*;
    ///
    /// #[derive(Entity)]
    /// struct Ea {
    ///     ca: Ca,
    /// }
    /// #[derive(Component, Debug)]
    /// struct Ca(i32);
    ///
    /// fn system(ew: EntWrite<Ea>) {
    ///     let mut ew = ew.take_recur();
    ///     let eid = ew.add(Ea { ca: Ca(0) });
    ///     println!("{:?}", ew.get(&eid).unwrap());
    /// }
    /// ```
    pub fn get(&self, eid: &EntityId) -> Option<T::Ref<'_>> {
        if eid.container_index() == self.get_entity_tag().index() {
            let vi = self.cont.to_value_index(eid.row_index())?;
            Some(self.get_by_value_index(vi))
        } else {
            None
        }
    }

    /// Returns a struct holding mutable references to components that belong to an entity for the
    /// given entity id.
    ///
    /// If it failed to find an entity using the given entity id, returns `None`. See
    /// [`EntityContainerRef::get_mut_by_value_index`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// # use my_ecs::prelude::*;
    ///
    /// #[derive(Entity)]
    /// struct Ea {
    ///     ca: Ca,
    /// }
    /// #[derive(Component, Debug)]
    /// struct Ca(i32);
    ///
    /// fn system(ew: EntWrite<Ea>) {
    ///     let mut ew = ew.take_recur();
    ///     let eid = ew.add(Ea { ca: Ca(0) });
    ///     let ea = ew.get_mut(&eid).unwrap();
    ///     *ea.ca = Ca(42);
    ///     println!("{:?}", ew.get(&eid).unwrap());
    /// }
    /// ```
    pub fn get_mut(&mut self, eid: &EntityId) -> Option<T::Mut<'_>> {
        if eid.container_index() == self.get_entity_tag().index() {
            let vi = self.cont.to_value_index(eid.row_index())?;
            Some(self.get_mut_by_value_index(vi))
        } else {
            None
        }
    }

    /// Returns a struct holding shared references to components that belong to an entity for the
    /// given value index.
    ///
    /// Note, however, that entity is not stored as it is. It is split up into its components then
    /// stored in each component container. That means collecting those references like this
    /// function is inefficient because it requires random access to memory.
    ///
    /// # Panics
    ///
    /// Panics if the given value index is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use my_ecs::prelude::*;
    ///
    /// #[derive(Entity)]
    /// struct Ea {
    ///     ca: Ca,
    /// }
    /// #[derive(Component, Debug)]
    /// struct Ca(i32);
    ///
    /// fn system(ew: EntWrite<Ea>) {
    ///     let ew = ew.take_recur();
    ///     for vi in 0..ew.len() {
    ///         println!("{:?}", ew.get_by_value_index(vi));
    ///     }
    /// }
    /// ```
    pub fn get_by_value_index(&self, vi: usize) -> T::Ref<'_> {
        // Panics here.
        T::get_ref_from(self.cont, vi)
    }

    /// Returns a struct holding mutable references to components that belong to an entity for the
    /// given value index.
    ///
    /// Note, however, that entity is not stored as it is. It is split up into its components then
    /// stored in each component container. That means collecting those references like this
    /// function is inefficient because it requires random access to memory.
    ///
    /// # Panics
    ///
    /// Panics if the given value index is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use my_ecs::prelude::*;
    ///
    /// #[derive(Entity)]
    /// struct Ea {
    ///     ca: Ca,
    /// }
    /// #[derive(Component, Debug)]
    /// struct Ca(i32);
    ///
    /// fn system(ew: EntWrite<Ea>) {
    ///     let mut ew = ew.take_recur();
    ///     for vi in 0..ew.len() {
    ///         let ea = ew.get_mut_by_value_index(vi);
    ///         *ea.ca = Ca(42);
    ///         println!("{:?}", ea);
    ///     }
    /// }
    /// ```
    pub fn get_mut_by_value_index(&mut self, vi: usize) -> T::Mut<'_> {
        // Panics here.
        T::get_mut_from(self.cont, vi)
    }

    /// Inserts an entity to the entity container then returns entity id of the inserted entity.
    ///
    /// It's encouraged to use combination of [`EntityContainerRef::resize`] and
    /// [`EntityContainerRef::get_column_mut_of`] when you need to add lots of entities at once.
    /// It's more cache-friendly so it would be faster than calling to this method many times.
    pub fn add(&mut self, value: T) -> EntityId {
        let ei = self.get_entity_tag().index();
        let ri = value.move_to(self.cont);
        EntityId::new(ei, ri)
    }

    /// Removes an entity for the given entity id from the entity container then returns the entity.
    pub fn take(&mut self, eid: &EntityId) -> Option<T> {
        if eid.container_index() == self.get_entity_tag().index() {
            let vi = self.cont.to_value_index(eid.row_index())?;
            Some(self.take_by_value_index(vi))
        } else {
            None
        }
    }

    /// Removes an entity for the given value index from the entity container then returns the
    /// entity.
    ///
    /// # Panics
    ///
    /// Panics if the given value index is out of bounds.
    pub fn take_by_value_index(&mut self, vi: usize) -> T {
        T::take_from(self.cont, vi)
    }

    /// Resizes the entity container to the new length by cloning the given value.
    ///
    /// Resizing occurs column by column.
    ///
    /// # Panics
    ///
    /// Panics if any components of the entity are not [`Clone`].
    pub fn resize(&mut self, new_len: usize, value: T) {
        for ci in 0..T::num_components() {
            // Safety: All columns are resized.
            unsafe {
                self.cont
                    .resize_column(ci, new_len, value.component_ptr(ci));
            }
        }
    }
}