sceller 0.3.0

An ECS crate written in Rust based on the tutorial series by Brooks Builds on Youtube.
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
//! # Entities
//! 
//! The [Entities] module serves to declare the Entities struct, which stores entities and components
//! in the entity component system. 

mod query;
mod query_entity;
pub mod auto_query;
mod fn_query;

use std::{any::{Any, TypeId}, rc::Rc, cell::{RefCell}, collections::HashMap};
use eyre::*;

pub use self::query::Query;
pub use self::query_entity::QueryEntity;
pub use self::auto_query::*;
pub use self::fn_query::*;

pub type ComponentType = Rc<RefCell<dyn Any>>;


#[derive(Debug, Default)]
/**

  Struct to store Entites and Components in an Entity component System.
  
  Uses bitmaps to execute queries, and as such has a hard limit on the number of individual components that
  are able to be registered at a time. This particular instance uses a u128, allowing for 128 unique components.
  
  The struct also contains an entity counter to help with automatic registering of components, as well as
  a hashmap of the different bit masks of each component as well as a vector containing the entity id's 
  in the form of their bit masks. 'insert_index' serves as a kind of cursor for where the next 'insert' function call
  will place the new component.
  
  The struct stores a 'map' which is a vector of bitmasks that function essentially as entity id's.
  When an entity is created, a bitmask is appended to this vector with the components the new entity has.
  So the vector will look something like this (assuming arbitrary entites and components exist in the system.
  
   map [...0010_1101, ...0111_1111, ...0101_000, ...] where each 1 corresponds to a component the entity has.
  
  In contrast, the 'bit_masks' hashmap will ressemble this:
  
   bit_masks { Component1: ...0000_0001, Component2: ...0000_0010, Component3: ...0000_0100, ... } where a 1 corresponds to the component's 'id.
  
  Note: in the place of 'Component1' the code actually uses TypeIds, so it would be TypeId::of::<Component1>().
 */
pub struct Entities {
    components: HashMap<TypeId, Vec<Option<ComponentType>>>,
    entity_count: usize,

    bit_masks: HashMap<TypeId, u128>,
    map: Vec<u128>,

    insert_cursor: usize,
}

impl Entities {
    /**

      Adds new index into the hashmap of components and adds the bitmask of the new type into bitmask vec.
     */
    pub fn register_component<T: Any + 'static>(&mut self) {
        let typeid = TypeId::of::<T>();
        let bitmask = 2_u128.pow(self.components.len() as u32);
        self.components.insert(typeid, Vec::new());
        self.bit_masks.insert(typeid, bitmask);
    }

    // #[allow(dead_code)]
    /**

      Convenience function used when auto registering new components but returns an optional value if it fails.
      
      Basically it makes sure to fill the blank spots in the hashmap with 'None' values
      to make sure the indexing doesn't get messes up doing queries.
      
      |-----------------------------------------------|
      |           | ent1 | ent2 | ent3 | newent | ... |
      |-----------------------------------------------|
      |Component1 |  56  | 45   |  56  |  None  | ... |
      |Component2 | None | None |  12  |  None  | ... |
      |NewComp    |      |      |      |        | ... | <- New component added 
      |-----------------------------------------------|    automatically with [insert()](struct.Entities.html#method.insert) method.
      
      |-----------------------------------------------|
      |           | ent1 | ent2 | ent3 | newent | ... |
      |-----------------------------------------------|
      |Component1 |  56  | 45   |  56  |  None  | ... |
      |Component2 | None | None |  12  |  None  | ... |
      |NewComp    | None | None | None |  None  | ... | <- The new vec is filled with None values by this function. 
      |-----------------------------------------------|
     */
    fn fill_new_component_checked<T: Any>(&mut self) -> Result<()> {
        let comps = self.components.get_mut(&TypeId::of::<T>()).ok_or(ComponentError::AutomaticRegistrationError)?;
        for _ in 0..self.entity_count { comps.push(None); }
        Ok(())
    }

    /**

      Adds a new entry in each component vector and fills it with a 'None' option value.
      In effect, adds a new entity into the struct. The entity will be pushed to the end
      and as such any subsequent calls to [insert()](struct.Entities.html#method.insert) will
      effect the latest entity added with this function.
      
      ```
      use sceller::prelude::*;
      use std::any::TypeId;
      
      struct Health(u8);
      struct Speed(i8);
      
      let mut ents = Entities::default();
      
      ents.create_entity()
          .insert_checked(Health(10_u8)).unwrap()
          .insert_checked(Speed(-16)).unwrap();
      ```
     */
    pub fn create_entity(&mut self) -> &mut Self {
        if let Some((index, _)) = self.map.iter().enumerate().find(|(_index, map_val)| **map_val == 0) {
            self.insert_cursor = index;
        } else {
            self.components.iter_mut().for_each(|(_key, value)| {
                value.push(None);
            });
    
            self.map.push(0);
    
            self.entity_count += 1;

            self.insert_cursor = self.entity_count - 1;
        }
        self
    }

    /**

      Inserts a component into whatever is the newest newly created entity. Returns Err if the component 
      
      Note: automatically calls [register_component()](struct.Entities.html#method.register_component) and 
      [fill_new_component()](struct.Entities.html#method.fill_new_component) to streamline the creation of new
      entities.
      
      ```
      use sceller::prelude::*;
      use std::any::TypeId;
      
      struct Health(u8);
      struct Speed(i8);
      
      let mut ents = Entities::default();
      
      ents.create_entity()
          .insert_checked(Health(10_u8)).unwrap()
          .insert_checked(Speed(-16)).unwrap();
      ```
     */
    pub fn insert<T: Any>(&mut self, data: T) -> &mut Self {
        self.insert_checked(data).unwrap()
    }

    /**

      Inserts a component into whatever is the newest newly created entity. Returns Err if the component isn't registered.
      
      Note: automatically calls [register_component()](struct.Entities.html#method.register_component) and 
      [fill_new_component()](struct.Entities.html#method.fill_new_component) to streamline the creation of new
      entities.
      
      ```
      use sceller::prelude::*;
      use std::any::TypeId;
      
      struct Health(u8);
      struct Speed(i8);
      
      let mut ents = Entities::default();
      
      ents.create_entity()
          .insert_checked(Health(10_u8)).unwrap()
          .insert_checked(Speed(-16)).unwrap();
      ```
     */
    pub fn insert_checked<T: Any>(&mut self, data: T) -> eyre::Result<&mut Self> {
        // auto register new component types
        if !self.bit_masks.contains_key(&TypeId::of::<T>()) {
            // register and initialize with default value of none
            self.register_component::<T>();
            self.fill_new_component_checked::<T>()?;
        }

        let map_index = self.insert_cursor;

        if let Some(components) = self.components.get_mut(&data.type_id()) {
            let component = components.get_mut(map_index).ok_or(ComponentError::NonexistentEntity)?;
            let typeid = data.type_id();
            *component = Some(Rc::new(RefCell::new(data)));

            let bitmask = self.bit_masks.get(&typeid).unwrap();
            self.map[map_index] |= *bitmask;
        } else {
            bail!("Attempted to add a component that was not registered to an entity.");
        }
        Ok(self)
    }

    /**

      Deletes a component from an entity using the entity's index in the ECS. 
      
      ```
      use sceller::prelude::*;
      use std::any::TypeId;
      
      struct Health(u8);
      struct Speed(i8);
      
      let mut ents = Entities::default();
      
      ents.create_entity()
          .insert_checked(Health(10_u8)).unwrap()
          .insert_checked(Speed(-16)).unwrap();
      
      ents.delete_component_by_entity_id::<Health>(0);
      
      let query = Query::new(&ents)
          .with_component_checked::<Health>().unwrap().run();
      
      assert_eq!(query[0].len(), 0);
      ```
      
      Returns an error if the component that is trying to be deleted isn't registered.

      This operation is fast, because there are no big read or writes to memory. All this function does 
      is do an xOr operation on the bitmask of the entity's index given, making this a cheap operation. 
     */
    pub fn delete_component_by_entity_id_checked<T: Any>(&mut self, index: usize) -> Result<()> {
        let typeid = TypeId::of::<T>();
        let mask = self.bit_masks.get(&typeid).ok_or(ComponentError::UnregisteredComponentError)?;

        // 3 ^= 1 = 2
        // 2 ^= 1 = 3
        //
        // 0011 ^= 0001 = 0010
        // 0010 ^= 0001 = 0011 

        // ^ operator is: 1 ^ 0 = 1, 0 ^ 0 = 0 
        //
        // desired behaviour:
        //
        // 0011 ? 0001 = 0010
        // 0010 ? 0001 = 0010
        //
        // 0011 & 0001 = 0001 / 0011 | 0001 = 0011 
        // 0010 | 0001 = 0011 / 0010 & 0001 = 0000

        // this executes if the entity does contain this component
        if self.map[index] & *mask != 0 {
            self.map[index] ^= *mask;
        }

        Ok(())
    }

    /**

      Deletes a component from an entity using the entity's index in the ECS. 
      
      ```
      use sceller::prelude::*;
      use std::any::TypeId;
      
      struct Health(u8);
      struct Speed(i8);
      
      let mut ents = Entities::default();
      
      ents.create_entity()
          .insert_checked(Health(10_u8)).unwrap()
          .insert_checked(Speed(-16)).unwrap();
      
      ents.delete_component_by_entity_id::<Health>(0);
      
      let query = Query::new(&ents)
          .with_component_checked::<Health>().unwrap().run();
      
      assert_eq!(query[0].len(), 0);
      ```
      
      Panics if the component that is trying to be deleted isn't registered.

      This operation is fast, because there are no big read or writes to memory. All this function does 
      is do an xOr operation on the bitmask of the entity's index given, making this a cheap operation. 
     */
    pub fn delete_component_by_entity_id<T: Any>(&mut self, index: usize) {
        self.delete_component_by_entity_id_checked::<T>(index).unwrap()
    }

    /**

      Inserts a new instance of a component into an entity using it's id. (index)
      
      ```
      use sceller::prelude::*;
      
      struct Foo(f32);
      struct Bar(u16);
      
      let mut ents = Entities::default();
      
      ents.register_component::<Bar>(); // this step is neccessary so that the first Query doesn't fail.
      
      ents.create_entity()
          .insert_checked(Foo(9.0_f32)).unwrap();
      
      let query1 = Query::new(&ents).with_component_checked::<Bar>().unwrap().run();
      
      // There are none of the component 'Bar' in the query, so none in the system
      assert_eq!(query1[0].len(), 0);
      
      ents.insert_component_into_entity_by_id(Bar(29), 0); // insert 'Bar' into entity at position 0
      
      let query1 = Query::new(&ents).with_component_checked::<Bar>().unwrap().run();
      
      // There is 1 Bar component in the system, we have successfully added a component.
      assert_eq!(query1[0].len(), 1);
      ```

      Panics when applying this function without first creating a new entity with [creat_entity()](struct.Entities.html#method.create_entity).
     */
    pub fn insert_component_into_entity_by_id<T: Any>(&mut self, data: T, map_index: usize) {
        self.insert_component_into_entity_by_id_checked(data, map_index).unwrap()
    }

    /**

      Inserts a new instance of a component into an entity using it's id. (index)
      
      ```
      use sceller::prelude::*;
      
      struct Foo(f32);
      struct Bar(u16);
      
      let mut ents = Entities::default();
      
      ents.register_component::<Bar>(); // this step is neccessary so that the first Query doesn't fail.
      
      ents.create_entity()
          .insert_checked(Foo(9.0_f32)).unwrap();
      
      let query1 = Query::new(&ents).with_component_checked::<Bar>().unwrap().run();
      
      // There are none of the component 'Bar' in the query, so none in the system
      assert_eq!(query1[0].len(), 0);
      
      ents.insert_component_into_entity_by_id(Bar(29), 0); // insert 'Bar' into entity at position 0
      
      let query1 = Query::new(&ents).with_component_checked::<Bar>().unwrap().run();
      
      // There is 1 Bar component in the system, we have successfully added a component.
      assert_eq!(query1[0].len(), 1);
      ```

      Returns an error if the component inserted is unregistered (which should never happen, as this function auto-registers components like [insert()](struct.Entities.html#method.insert))
      or if the user tries to insert a component without creating a new entity.
     */
    pub fn insert_component_into_entity_by_id_checked<T: Any>(&mut self, data: T, map_index: usize) -> eyre::Result<()> {
        // auto register new component types
        if !self.bit_masks.contains_key(&TypeId::of::<T>()) {
            // register and initialize with default value of none
            self.register_component::<T>();
            self.fill_new_component_checked::<T>()?;
        }

        if let Some(components) = self.components.get_mut(&data.type_id()) {
            let replaced_component = components.get_mut(map_index).ok_or(ComponentError::NonexistentEntity)?;
            let typeid = data.type_id();
            *replaced_component = Some(Rc::new(RefCell::new(data)));

            let bitmask = self.bit_masks.get(&typeid).ok_or(ComponentError::UnregisteredComponentError)?;
            self.map[map_index] |= *bitmask;
        } else {
            bail!("Attempted to add a component that was not registered to an entity.");
        }
        Ok(())
    }

    /**

    Deletes all occurences of a component from the Entity Component System and unregisters it.

    ```
    use sceller::prelude::*;

    struct Foo(char); struct Bar(u16);

    let mut ents = Entities::default();

    // create two dummy entities
    ents.create_entity().insert_checked(Foo('b')).unwrap().insert_checked(Bar(6)).unwrap();
    ents.create_entity().insert_checked(Foo('h')).unwrap().insert_checked(Bar(101)).unwrap();

    let query1 = Query::new(&ents).with_component_checked::<Bar>().unwrap().run();

    // The system contains two instances of the struct 'Bar', and is able to recognize them.
    assert_eq!(query1[0].len(), 2);

    ents.delete_component::<Bar>(); // unregister the 'Bar' component from the system.

    let mut query2 = Query::new(&ents);
    let result = query2.with_component_checked::<Bar>();

    // the 'Bar' component no longer exists, and as such will throw an error
    // if we try and Query for it.
    assert!(result.is_err()); 
    ```

    This function will panic if the component entered doesn't exist.

    This operation is fast, because there are no heavy read/writes to memory. This function
    simply xOrs the bitmask of every entity to remove this component from it.
     */
    pub fn delete_component<T: Any>(&mut self) {
        self.delete_component_checked::<T>().unwrap()
    }

    /**

    Deletes all occurences of a component from the Entity Component System and unregisters it.

    ```
    use sceller::prelude::*;

    struct Foo(char); struct Bar(u16);

    let mut ents = Entities::default();

    // create two dummy entities
    ents.create_entity().insert_checked(Foo('b')).unwrap().insert_checked(Bar(6)).unwrap();
    ents.create_entity().insert_checked(Foo('h')).unwrap().insert_checked(Bar(101)).unwrap();

    let query1 = Query::new(&ents).with_component_checked::<Bar>().unwrap().run();

    // The system contains two instances of the struct 'Bar', and is able to recognize them.
    assert_eq!(query1[0].len(), 2);

    ents.delete_component::<Bar>(); // unregister the 'Bar' component from the system.

    let mut query2 = Query::new(&ents);
    let result = query2.with_component_checked::<Bar>();

    // the 'Bar' component no longer exists, and as such will throw an error
    // if we try and Query for it.
    assert!(result.is_err()); 
    ```

    This function will return an error if the component entered doesn't exist.

    This operation is fast, because there are no heavy read/writes to memory. This function
    simply xOrs the bitmask of every entity to remove this component from it.
     */
    pub fn delete_component_checked<T: Any>(&mut self) -> eyre::Result<()> {
        let (_, bitmask) = self.bit_masks.remove_entry(&TypeId::of::<T>()).ok_or(ComponentError::UnregisteredComponentError)?;
        for component_bitmask in &mut self.map {
            *component_bitmask ^= bitmask;
        }
        Ok(())
    }

    pub fn delete_entity_by_id(&mut self, index: usize) -> eyre::Result<()> {
        let len = self.map.len();
        *self.map.get_mut(index).ok_or(ComponentError::IndexOutOfBoundsError { expected: len, found: index })? = 0;

        Ok(())
    }

    /**

    Convenience function to get the bitmask of a given TypeId. 
    
    Returns None if the component requested isn't registered.
     */
    pub fn get_bitmask(&self, typeid: &TypeId) -> Option<u128> {
        self.bit_masks.get(typeid).copied()
    }
}

// Trait implementations
impl std::fmt::Display for Entities {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:#?}")
    }
}

#[derive(thiserror::Error, Debug)]
enum ComponentError {
    #[error("Attempt to add component to nothing.")]
    NonexistentEntity,
    #[error("This error should never happen. (Failed to fill fields of newly generated component on the fly)")]
    AutomaticRegistrationError,
    #[error("Attempt to make use of unregistered component.")]
    UnregisteredComponentError,
    #[error("Index out of bounds when accessing entity id. (Expected range from 0..{expected}, got {found}).")]
    IndexOutOfBoundsError { expected: usize, found: usize },
    #[error("Attempted to get component data that does not exist. Error in bitmask probably?")]
    NonexistentComponentDataError,
}

#[cfg(test)]
mod tests {
    use std::{any::TypeId};

    use super::*;

    #[test]
    fn new_entities_fill_deleted_spots() -> eyre::Result<()> {
        let mut ents = Entities::default();

        ents.create_entity()
            .insert_checked(Health(100))?
            .insert_checked(Id(String::from("hi")))?;

        ents.create_entity()
            .insert_checked(Health(50))?
            .insert_checked(Id(String::from("hey")))?;

        ents.delete_entity_by_id(0)?;

        ents.create_entity()
            .insert_checked(Health(20))?;

        assert_eq!(ents.map[0], 1);

        let hp = ents.components.get(&TypeId::of::<Health>()).unwrap()[0]
            .as_ref()
            .unwrap()
            .borrow();
        let hp = hp.downcast_ref::<Health>()
            .unwrap();

        assert_eq!(hp.0, 20);

        Ok(())
    }

    #[test]
    fn delete_entities_by_id() -> eyre::Result<()> {
        let mut ents = Entities::default();

        ents.create_entity()
            .insert_checked(Health(100))?
            .insert_checked(Id(String::from("hi")))?;

        ents.create_entity()
            .insert_checked(Unique)?
            .insert_checked(Health(50))?
            .insert_checked(Id(String::from("hey")))?;

        ents.delete_entity_by_id(0)?;

        assert_eq!(ents.map[0], 0);

        Ok(())
    }

    #[test]
    fn register_entities() {
        let mut ents = Entities::default();
        ents.register_component::<Health>();
        ents.register_component::<Id>();

        let hp_component = ents.components.get(&TypeId::of::<Health>()).unwrap();

        assert_eq!(hp_component.len(), 0);
        dbg!(ents);
    }

    #[test]
    fn bitmask_update_on_register_entities() {
        let mut ents = Entities::default();
        ents.register_component::<Health>();
        ents.register_component::<Id>();

        let hp_component = ents.bit_masks.get(&TypeId::of::<Health>()).unwrap();

        assert_eq!(*hp_component, 1);
        dbg!(ents);
    }

    #[test]
    fn create_entity() {
        let mut ents = Entities::default();
        ents.register_component::<Health>();
        ents.register_component::<Id>();

        ents.create_entity();
        let hp = ents.components.get(&TypeId::of::<Health>()).unwrap();
        let speed = ents.components.get(&TypeId::of::<Id>()).unwrap();

        assert!(hp.len() == speed.len() && hp.len() == 1);
        assert!(speed[0].is_none());
        assert!(hp[0].is_none());

        dbg!(ents.components);
    }

    #[test]
    fn with_component() -> Result<()> {
        let mut ents = Entities::default();

        ents.create_entity()
            .insert(Health(100))
            .insert(Id(String::from("hi")));

        ents.create_entity()
            .insert(Unique)
            .insert(Health(50))
            .insert(Id(String::from("hey")));

        let health1 = &ents.components.get(&TypeId::of::<Health>()).unwrap()[0];
        let wrapped_health = health1.as_ref().unwrap();
        let borrowed_health = wrapped_health.borrow();
        let hp = borrowed_health.downcast_ref::<Health>().unwrap();

        assert_eq!(hp.0, 100);
        dbg!(hp);

        let hp = ents.components.get(&TypeId::of::<Health>()).unwrap();
        let speed = ents.components.get(&TypeId::of::<Unique>()).unwrap();

        assert!(hp.len() == speed.len() && hp.len() == ents.entity_count);
        // assert!(speed[0].is_none());
        // assert!(hp[0].is_none());

        Ok(())
    }

    #[test]
    fn map_is_updated() -> Result<()> {
        let mut ents = Entities::default();
        // ents.register_component::<Health>();
        // ents.register_component::<Id>();

        ents.create_entity()
            .insert(Health(100))
            .insert(Id(String::from("hi")));

        let entity_map = ents.map[0];
        
        assert_eq!(entity_map, 3);

        ents.create_entity()
            .insert(Id(String::from("hi")));

        let entity_map = ents.map[1];
        
        assert_eq!(entity_map, 2);

        Ok(())
    }

    #[test]
    fn delete_component_by_ent_id() -> Result<()> {
        let mut ents = Entities::default();

        ents.create_entity()
            .insert(Health(100))
            .insert(Id(String::from("hi")));

        ents.create_entity()
            .insert(Health(50))
            .insert(Id(String::from("hey")));

        ents.delete_component_by_entity_id_checked::<Health>(0)?;

        assert_eq!(ents.map[0], 2);

        Ok(())
    }

    #[test] 
    fn add_component_by_ent_id() -> eyre::Result<()> {
        let mut ents = Entities::default();

        ents.create_entity()
            .insert(Health(100))
            .insert(Id(String::from("hi")));

        ents.create_entity()
            .insert(Health(50))
            .insert(Id(String::from("hey")));

        // first entity will be: ...0000_0011
        // after this operation: ...0000_0111
        ents.insert_component_into_entity_by_id(Unique, 0);

        assert_eq!(ents.map[0], 7);

        Ok(())
    }

    #[test]
    fn remove_component() -> eyre::Result<()> {
        let mut ents = Entities::default();

        ents.create_entity()
            .insert_checked(Health(100))?
            .insert_checked(Id(String::from("hi")))?;

        ents.create_entity()
            .insert_checked(Health(50))?
            .insert_checked(Id(String::from("hey")))?;

        assert_eq!(ents.map[0], 3_u128);

        ents.delete_component_checked::<Health>()?;

        // asserts that when querying we will no longer find this component, effectively removing it.
        assert_eq!(ents.map[0], 2_u128);

        Ok(())
    }

    #[test] 
    fn double_delete_fix() -> eyre::Result<()> {
        let mut ents = Entities::default();

        ents.create_entity()
            .insert_checked(Health(100))?
            .insert_checked(Id(String::from("hi")))?;

        // ents.create_entity()
        //     .insert_checked(Health(50))?
        //     .insert_checked(Id(String::from("hey")))?;

        ents.delete_component_by_entity_id_checked::<Health>(0)?;

        // assert only 'Id' component is left 
        assert_eq!(ents.map[0], 2);

        ents.delete_component_by_entity_id_checked::<Health>(0)?;

        assert_eq!(ents.map[0], 2);

        Ok(())
    }

    #[derive(Debug)]
    struct Health(u16);
    struct Id(String);

    struct Unique;
}