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
#![deny(missing_docs)]

//! # Nano-ECS
//! A bare-bones macro-based Entity-Component-System
//!
//! - Maximum 64 components per entity
//! - Stores components sequentially in same array
//! - Masks for enabled/disabled components
//!
//! ```rust
//! use nano_ecs::*;
//!
//! #[derive(Clone)]
//! pub struct Position(pub f32);
//! #[derive(Clone)]
//! pub struct Velocity(pub f32);
//!
//! ecs!{4: Position, Velocity}
//!
//! fn main() {
//!     let mut world = World::new();
//!     world.push(Position(0.0));
//!     world.push((Position(0.0), Velocity(0.0)));
//!     let dt = 1.0;
//!     system!(world, |pos: &mut Position, vel: &Velocity| {
//!         pos.0 = pos.0 + vel.0 * dt;
//!     });
//! }
//! ```
//!
//! ### Design
//!
//! The `ecs!` macro generates a `World` and `Component` object.
//!
//! Can be used with any Rust data structure that implements `Clone`.
//!
//!
//! The order of declared components is used to assign every component an index.
//! This index is used in the mask per entity and to handle slice memory correctly.
//!
//! - All components are stored in one array inside `World`.
//! - All entities have a slice refering to components
//! - All entities have a mask that enable/disable components

/// Stores masks efficiently and allows fast iteration.
pub struct MaskStorage {
    /// Stores `(active, initial)` masks.
    pub masks: Vec<(u64, u64)>,
    /// Stores the offsets of the mask.
    pub offsets: Vec<usize>,
}

impl MaskStorage {
    /// Creates a new mask storage.
    pub fn new() -> MaskStorage {
        MaskStorage {masks: vec![], offsets: vec![]}
    }

    /// Sorts optimally and returns a sort to update
    /// entity slices and components.
    pub fn optimize(&mut self, n: usize) -> Vec<usize> {
        if n == 0 {return vec![]};

        let mut ids: Vec<usize> = (0..n).collect();
        let masks: Vec<(u64, u64)> = ids.iter().map(|&id| self.both_masks_of(id)).collect();
        ids.sort_by(|a, b| {
            let (am, ai) = masks[*a];
            let (bm, bi) = masks[*b];
            ai.cmp(&bi).then(bm.cmp(&am))
        });

        self.masks.clear();
        self.offsets.clear();
        let mut prev = masks[ids[0]];
        self.masks.push(prev);
        self.offsets.push(0);
        for (i, &id) in ids.iter().enumerate().skip(1) {
            if masks[id] != prev {
                self.masks.push(masks[id]);
                self.offsets.push(i);
            }
            prev = masks[id];
        }

        ids
    }

    /// Gets the next range of entities with active mask pattern.
    pub fn next(&self, mask_pat: u64, i: &mut usize, n: usize) -> Option<(usize, usize)> {
        loop {
            if *i >= self.masks.len() {return None};
            if self.masks[*i].0 & mask_pat == mask_pat {
                if let Some(&next) = self.offsets.get(*i + 1) {
                    return Some((self.offsets[*i], next));
                } else {
                    return Some((self.offsets[*i], n));
                }
            }
            *i += 1;
        }
    }

    /// Returns the active mask of component id.
    pub fn mask_of(&self, cid: usize) -> u64 {
        match self.offsets.binary_search(&cid) {
            Ok(ind) => self.masks[ind].0,
            Err(ind) if ind > 0 => self.masks[ind - 1].0,
            Err(_) => panic!("Mask storage offset not including `0`")
        }
    }

    /// Returns the initial mask of entity id.
    pub fn init_mask_of(&self, id: usize) -> u64 {
        match self.offsets.binary_search(&id) {
            Ok(ind) => self.masks[ind].1,
            Err(ind) if ind > 0 => self.masks[ind - 1].1,
            Err(_) => panic!("Mask storage offset not including `0`")
        }
    }

    /// Returns both active and initial mask of entity id.
    pub fn both_masks_of(&self, id: usize) -> (u64, u64) {
        match self.offsets.binary_search(&id) {
            Ok(ind) => self.masks[ind],
            Err(ind) if ind > 0 => self.masks[ind - 1],
            Err(_) => panic!("Mask storage offset not including `0`")
        }
    }

    /// Pushes a new mask.
    pub fn push(&mut self, mask: u64, id: usize) {
        if let Some(&(active, last)) = self.masks.last() {
            if mask == last && active == last {return};
        }
        self.masks.push((mask, mask));
        self.offsets.push(id);
    }

    /// Updates a mask for an entity, returning `Err(swap_id)` to swap components.
    pub fn update(&mut self, mask: u64, id: usize, n: usize) -> Result<(), usize> {
        let ind = match self.offsets.binary_search(&id) {
            Ok(ind) => ind,
            Err(ind) if ind > 0 => {ind - 1},
            Err(_) => panic!("Mask storage offset not including `0`")
        };

        if self.masks[ind].0 == mask {return Ok(())};

        let offset = self.offsets[ind];
        let init_mask = self.masks[ind].1;
        let next_offset = self.offsets.get(ind + 1);
        let prev_offset = if ind == 0 {None} else {self.offsets.get(ind - 1)};
        let next_range_same_masks = next_offset.is_some() &&
            self.masks[ind + 1] == (mask, init_mask);
        let prev_range_same_masks = prev_offset.is_some() &&
            self.masks[ind - 1] == (mask, init_mask);

        let next_offset = next_offset.map(|x| *x).unwrap_or(n);
        let at_beginning_in_old_range = offset == id;
        let at_end_in_old_range = next_offset == id + 1;

        let only_in_old_range = at_beginning_in_old_range && at_end_in_old_range;
        let mut remove_old_range = false;
        let mut remove_next_range = false;
        let mut res = Ok(());
        match (prev_range_same_masks, next_range_same_masks, only_in_old_range) {
            (true, false, _) => {
                // Join previous range.
                remove_old_range = only_in_old_range;
                if !at_beginning_in_old_range {res = Err(offset)};
            }
            (false, true, _) | (_, true, false) => {
                // Join next range.
                remove_old_range = only_in_old_range;
                if !at_end_in_old_range {res = Err(next_offset - 1)}
            }
            (true, true, true) => {
                // Join previous and next range.
                remove_old_range = true;
                remove_next_range = true;
            }
            (false, false, true) => {
                // Change mask on old range.
                self.masks[ind].0 = mask;
            }
            (false, false, false) => {
                // Insert range.
                self.offsets.insert(ind + 1, id + 1);
                let old_masks = self.masks[ind];
                self.masks.insert(ind + 1, old_masks);
                self.offsets.insert(ind + 1, id);
                self.masks.insert(ind + 1, (mask, init_mask));
            }
        }
        if remove_next_range {
            self.offsets.remove(ind + 1);
            self.masks.remove(ind + 1);
        }
        if remove_old_range {
            self.offsets.remove(ind);
            self.masks.remove(ind);
        }
        res
    }
}

/// Creates an Entity-Component-System.
///
/// The first number is how many components are allowed per entity.
/// A lower number reduces compile time.
/// This can be `4, 8, 16, 32, 64`.
///
/// Example: `ecs!{4; Position, Velocity}`
#[macro_export]
macro_rules! ecs{
    ($max_components:tt : $($x:ident),* $(,)?) => {
        /// Stores a single component.
        #[allow(missing_docs)]
        pub enum Component {
            $($x($x)),*
        }

        /// World storing components and entities.
        pub struct World {
            /// A list of all components.
            pub components: Vec<Component>,
            /// Entities with indices into components.
            pub entities: Vec<(usize, u8)>,
            /// Masks for ranges of components.
            pub masks: MaskStorage,
        }

        impl World {
            /// Creates a new empty world.
            pub fn new() -> World {
                World {
                    components: vec![],
                    entities: vec![],
                    masks: MaskStorage::new(),
                }
            }

            /// Creates a new empty world with pre-allocated capacity.
            pub fn with_capacity(entities: usize, components: usize) -> World {
                World {
                    components: Vec::with_capacity(components),
                    entities: Vec::with_capacity(entities),
                    masks: MaskStorage::new(),
                }
            }

            /// Optimizes storage of components for cache friendliness.
            ///
            /// This does not preserve the entity ids.
            ///
            /// Returns a list of indices for new entities.
            pub fn optimize(&mut self) -> Vec<usize> {
                let n = self.entities.len();
                let mut ids = self.masks.optimize(n);

                let n = self.components.len();
                let mut gen: Vec<usize> = vec![0; n];
                let mut k = 0;
                for (i, &id) in ids.iter().enumerate() {
                    let off = self.entities[id].0;
                    let m = self.entities[id].1 as usize;
                    for j in 0..m {
                        gen[off + j] = k;
                        k += 1;
                    }
                }

                for i in 0..n {
                    while gen[i] != i {
                        let j = gen[i];
                        self.components.swap(i, j);
                        gen.swap(i, j);
                    }
                }

                let n = self.entities.len();
                let mut k = 0;
                let old = self.entities.clone();
                for i in 0..n {
                    let m = old[ids[i]].1;
                    self.entities[i] = (k, m);
                    k += m as usize;
                }

                ids
            }

            /// An iterator for all entities.
            #[inline(always)]
            pub fn all(&self) -> impl Iterator<Item = usize> {0..self.entities.len()}

            /// Gets entity slice of components from id.
            #[inline(always)]
            pub fn entity_slice(&mut self, id: usize) -> &mut [Component] {
                let (at, len) = self.entities[id];
                &mut self.components[at..at + len as usize]
            }

            /// Returns `true` if entity has a component by index.
            #[inline(always)]
            pub fn has_component_index(&self, id: usize, ind: u8) -> bool {
                (self.masks.mask_of(id) >> ind) & 1 == 1
            }

            /// Returns `true` if entity has a component.
            #[inline(always)]
            pub fn has_component<T>(&self, id: usize) -> bool
                where Component: Ind<T>
            {
                self.has_component_index(id, self.component_index::<T>())
            }

            /// Returns `true` if entity has a specified mask (a set of components).
            #[inline(always)]
            pub fn has_mask(&self, id: usize, mask: u64) -> bool {
                self.masks.mask_of(id) & mask == mask
            }

            /// Returns the component index of a component.
            #[inline(always)]
            pub fn component_index<T>(&self) -> u8
                where Component: Ind<T>
            {
                <Component as Ind<T>>::ind()
            }

            /// Returns the mask of an entity.
            #[inline(always)]
            pub fn mask_of(&self, id: usize) -> u64 {self.masks.mask_of(id)}

            /// Returns the initial mask of an entity.
            #[inline(always)]
            pub fn init_mask_of(&self, id: usize) -> u64 {self.masks.init_mask_of(id)}

            /// Swaps components of two entities.
            ///
            /// This is unsafe because the number of components
            /// and their type are not checked.
            pub unsafe fn unchecked_swap_components(&mut self, id: usize, j: usize) {
                let id_slice = self.entity_slice(id);
                let mut n = id_slice.len();
                let mut id_ptr = id_slice.as_mut_ptr();
                drop(id_slice);
                let mut j_ptr = self.entity_slice(j).as_mut_ptr();
                while n > 0 {
                    std::mem::swap(&mut *id_ptr, &mut *j_ptr);
                    id_ptr = id_ptr.add(1);
                    j_ptr = j_ptr.add(1);
                    n -= 1;
                }
                self.entities.swap(id, j);
            }

            /// Enables component for entity.
            ///
            /// The entity must be pushed with the component active to enable it again.
            /// Returns `true` if successful.
            pub fn enable_component<T>(&mut self, id: usize) -> bool
                where Component: Ind<T>
            {
                let (mut mask, init_mask) = self.masks.both_masks_of(id);
                if init_mask >> <Component as Ind<T>>::ind() & 1 == 1 {
                    mask |= 1 << <Component as Ind<T>>::ind();
                    if let Err(j) = self.masks.update(mask, id, self.entities.len()) {
                        if j != id {unsafe {self.unchecked_swap_components(id, j)}}
                    }
                    true
                } else {
                    false
                }
            }

            /// Disables component for entity.
            #[inline(always)]
            pub fn disable_component<T>(&mut self, id: usize)
                where Component: Ind<T>
            {
                let mut mask = self.masks.mask_of(id);
                mask &= !(1 << <Component as Ind<T>>::ind());
                if let Err(j) = self.masks.update(mask, id, self.entities.len()) {
                    if j != id {unsafe {self.unchecked_swap_components(id, j)}}
                }
            }

            /// Disables all components for entity.
            #[inline(always)]
            pub fn disable(&mut self, id: usize) {
                if let Err(j) = self.masks.update(0, id, self.entities.len()) {
                    if j != id {unsafe {self.unchecked_swap_components(id, j)}}
                }
            }
        }

        /// The index of a component type `T` from `Component`.
        ///
        /// This is used to store the components in the declared order.
        pub trait Ind<T> {
            /// Returns the component index.
            fn ind() -> u8;
        }
        /// Gets a component type `T` from a raw pointer of `Component`.
        ///
        /// Implemented for `&mut T` and `&T`.
        pub trait Get<T> {
            /// Gets component type.
            ///
            /// This is an unsafe method because the lifetime of the return value is only valid for the scope.
            unsafe fn get(self) -> Option<T>;
        }
        /// Creates a new entity from a set of components.
        pub trait Push<T> {
            /// Pushes/spawns a new entity.
            fn push(&mut self, val: T) -> usize;
        }

        push_impl!{$max_components}

        ind!{Component, 0, $($x),*}

        $(
            impl<'a> Get<&'a mut $x> for *mut Component {
                unsafe fn get(self) -> Option<&'a mut $x> {
                    if let Component::$x(x) = (&mut *self) {Some(x)} else {None}
                }
            }

            impl<'a> Get<&'a $x> for *mut Component {
                unsafe fn get(self) -> Option<&'a $x> {
                    if let Component::$x(x) = (&*self) {Some(x)} else {None}
                }
            }

            impl From<$x> for Component {
                fn from(x: $x) -> Component {Component::$x(x)}
            }
        )*
    }
}

/// Helper macro for counting size of a tuple.
///
/// This is used to check that every component in a system is uniquely accessed.
#[macro_export]
macro_rules! tup_count(
    () => {0};
    ($x0:ident $(, $y:ident)* $(,)?) => {1 + tup_count!($($y),*)};
);

/// Generates mask pattern based on a set of components.
#[macro_export]
macro_rules! mask_pat(
    ($($x:ident),* $(,)?) => {($(1 << <Component as Ind<$x>>::ind())|*)}
);

/// Used internally by other macros.
///
/// Checks that same component is not used twice.
#[macro_export]
macro_rules! mask_pre(
    ($mask:ident, |$($n:ident: $x:ty),*|) => {
        let $mask: u64 = ($(1 << <Component as Ind<$x>>::ind())|*);
        let __component_len = $mask.count_ones() as isize;
        assert_eq!(__component_len, tup_count!($($n),*), "Component used twice");
    }
);

/// Declares and executes a system.
///
/// Example: `system!(world, |pos: &mut Position| {...});`
///
/// One or more filters can be added using the `world` object:
///
/// `system!(world, filter: |n| world.has_component::<Velocity>(); |pos: &mut Position| {...})`
///
/// *Warning! This is unsafe to call nested when accessing same entities more than one.*
#[macro_export]
macro_rules! system(
    ($world:ident, $(filter: |$filter_id:ident| $filter:expr ;)*
    |$($n:ident: $x:ty),* $(,)?| $e:expr) => {
        mask_pre!(__mask, |$($n: $x),*|);

        let __n = $world.entities.len();
        let mut __i = 0;
        while let Some((__start, __end)) = $world.masks.next(__mask, &mut __i, __n) {
            let __init_mask = $world.masks.masks[__i].1;
            let __components = __init_mask.count_ones() as usize;
            let mut __ptr = $world.entity_slice(__start).as_mut_ptr();
            for __i in __start..__end {
                entity_unchecked_access!($world, __i, __init_mask, __ptr,
                    $(filter: |$filter_id| $filter ;)* |$($n : $x,)*| $e);
                __ptr = unsafe {__ptr.add(__components)};
            }
            __i += 1;
        }
    };
);

/// Same as `system!`, but with entity ids.
///
/// Example: `system_ids!(world, filter: |n| ...; id, |&Position| {...});`
#[macro_export]
macro_rules! system_ids(
    ($world:ident,
     $(filter: |$filter_id:ident| $filter:expr ;)*
     $id:ident,
     |$($n:ident: $x:ty),* $(,)?| $e:expr) => {
        mask_pre!(__mask, |$($n: $x),*|);

        let __n = $world.entities.len();
        let mut __i = 0;
        while let Some((__start, __end)) = $world.masks.next(__mask, &mut __i, __n) {
            let __init_mask = $world.masks.masks[__i].1;
            let __components = __init_mask.count_ones() as usize;
            let mut __ptr = $world.entity_slice(__start).as_mut_ptr();
            for __i in __start..__end {
                let $id = __i;
                entity_unchecked_access!($world, $id, __init_mask, __ptr,
                    $(filter: |$filter_id| $filter ;)* |$($n : $x,)*| $e);
                __ptr = unsafe {__ptr.add(__components)};
            }
            __i += 1;
        }
    };
);

/// Enumerates indices of entities only.
#[macro_export]
macro_rules! entity_ids(
    ($world:ident, $id:ident, |$($x:ty),* $(,)?| $e:expr) => {
        mask_pre!(__mask, |$($n: $x),*|);

        let __n = $world.entities.len();
        let mut __i = 0;
        while let Some((__start, __end)) = $world.masks.next(__mask, &mut __i, __n) {
            for __i in __start..__end {
                let $id = __i;
                $e
            }
            __i += 1;
        }
    };
);

/// Accesses a single entity.
///
/// *Warning! This is unsafe to call nested when accessing same entities more than one.*
#[macro_export]
macro_rules! entity(
    ($world:ident, $ind:expr, |$($n:ident: $x:ty),* $(,)?| $e:expr) => {
        mask_pre!(__mask, |$($n: $x),*|);

        let __i = $ind;
        entity_access!($world, __i, __mask, |$($n : $x,)*| $e);
    }
);

/// Accesses an entity.
///
/// This macro is used internally.
#[macro_export]
macro_rules! entity_access(
    ($world:ident, $i:ident, $__mask:ident,
     $(filter: |$filter_id:ident| $filter:expr ;)*
    |$($n:ident : $x:ty,)*| $e:expr) => {
        let __init_mask = $world.init_mask_of($i);
        let __entity_mask = $world.mask_of($i);
        if __init_mask & __entity_mask & $__mask == $__mask {
            $(
                let $filter_id = $i;
                if !$filter {continue};
            )*
            let __ptr = $world.entity_slice($i).as_mut_ptr();
            $(
            let $n: $x = unsafe {__ptr.offset(
                (((1_u64 << <Component as Ind<$x>>::ind()) - 1) & __init_mask).count_ones() as isize
            ).get()}.unwrap();
            )*
            $e
        }
    }
);

/// Accesses an entity, but without checking active mask.
///
/// This macro is used internally.
#[macro_export]
macro_rules! entity_unchecked_access(
    ($world:ident, $i:ident, $__init_mask:ident, $__ptr:ident,
     $(filter: |$filter_id:ident| $filter:expr ;)*
    |$($n:ident : $x:ty,)*| $e:expr) => {
        $(
            let $filter_id = $i;
            if !$filter {continue};
        )*
        $(
        let $n: $x = unsafe {$__ptr.offset(
            (((1_u64 << <Component as Ind<$x>>::ind()) - 1) & $__init_mask).count_ones() as isize
        ).get()}.unwrap();
        )*
        $e
    }
);

/// Calls `push` macro with smaller arguments.
#[macro_export]
macro_rules! push_impl {
    (4) => {
        push_impl!{
            x0: T0, x1: T1, x2: T2, x3: T3
        }
    };
    (8) => {
        push_impl!{
            x0: T0, x1: T1, x2: T2, x3: T3, x4: T4, x5: T5,x6: T6, x7: T7
        }
    };
    (16) => {
        push_impl!{
            x0: T0, x1: T1, x2: T2, x3: T3, x4: T4, x5: T5,x6: T6, x7: T7,
            x8: T8, x9: T9, x10: T10, x11: T11, x12: T12, x13: T13, x14: T14, x15: T15
        }
    };
    (32) => {
        push_impl!{
            x0: T0, x1: T1, x2: T2, x3: T3, x4: T4, x5: T5,x6: T6, x7: T7,
            x8: T8, x9: T9, x10: T10, x11: T11, x12: T12, x13: T13, x14: T14, x15: T15,
            x16: T16, x17: T17, x18: T18, x19: T19, x20: T20, x21: T21, x22: T22, x23: T23,
            x24: T24, x25: T25, x26: T26, x27: T27, x28: T28, x29: T29, x30: T30, x31: T31
        }
    };
    (64) => {
        push_impl!{
            x0: T0, x1: T1, x2: T2, x3: T3, x4: T4, x5: T5,x6: T6, x7: T7,
            x8: T8, x9: T9, x10: T10, x11: T11, x12: T12, x13: T13, x14: T14, x15: T15,
            x16: T16, x17: T17, x18: T18, x19: T19, x20: T20, x21: T21, x22: T22, x23: T23,
            x24: T24, x25: T25, x26: T26, x27: T27, x28: T28, x29: T29, x30: T30, x31: T31,
            x32: T32, x33: T33, x34: T34, x35: T35, x36: T36, x37: T37, x38: T38, x39: T39,
            x40: T40, x41: T41, x42: T42, x43: T43, x44: T44, x45: T45, x46: T46, x47: T47,
            x48: T48, x49: T49, x50: T50, x51: T51, x52: T52, x53: T53, x54: T54, x55: T55,
            x56: T56, x57: T57, x58: T58, x59: T59, x60: T60, x61: T61, x62: T62, x63: T63
        }
    };
    ($n:ident : $x:ident) => {
        push!{$n : $x}
    };
    ($n:ident : $x:ident, $($n2:ident : $x2:ident),*) => {
        push!{$n : $x, $($n2 : $x2),*}
        push_impl!{$($n2 : $x2),*}
    };
}

/// Generates `Push` impl for `World`.
#[macro_export]
macro_rules! push{
    ($($n:ident : $x:ident),+) => {
        #[allow(unused_parens)]
        impl<$($x),*> Push<($($x),*)> for World
            where $(Component: From<$x> + Ind<$x>,)*
                  $($x: Clone),*
        {
            fn push(&mut self, ($($n),*): ($($x),*)) -> usize {
                let id = self.entities.len();
                let comp = self.components.len();
                let mask: u64 = $(1 << <Component as Ind<$x>>::ind())|+;
                self.masks.push(mask, id);
                let count = tup_count!($($n),*);
                assert_eq!(mask.count_ones(), count, "Component declared twice");
                let mut i = 0;
                let mut bit = 0;
                while i < count {
                    let mut set = false;
                    $(
                        if <Component as Ind<$x>>::ind() == bit {
                            self.components.push($n.clone().into());
                            set = true;
                        }
                    )*
                    if set {i += 1}
                    bit += 1;
                }
                self.entities.push((comp, count as u8));
                id
            }
        }
    }
}

/// Generates `Ind` impl for `Component`.
#[macro_export]
macro_rules! ind{
    ($c:ident, $id:expr, $x:ident) => {
        impl Ind<&mut $x> for $c {#[inline(always)] fn ind() -> u8 {$id}}
        impl Ind<&$x> for $c {#[inline(always)] fn ind() -> u8 {$id}}
        impl Ind<$x> for $c {#[inline(always)] fn ind() -> u8 {$id}}
    };
    ($c:ident, $id:expr, $x:ident, $($y:ident),+) => {
        impl Ind<&mut $x> for $c {#[inline(always)] fn ind() -> u8 {$id}}
        impl Ind<&$x> for $c {#[inline(always)] fn ind() -> u8 {$id}}
        impl Ind<$x> for $c {#[inline(always)] fn ind() -> u8 {$id}}
        ind!{$c, $id + 1, $($y),+}
    };
}