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
use crate::error;
use crate::sparse_set::{Window, WindowMut};
use crate::storage::EntityId;
use crate::view::{View, ViewMut};
use core::any::type_name;

/// Retrives components based on their type and entity id.
pub trait Get {
    type Out;
    /// Retrieve components of `entity`.
    ///
    /// Multiple components can be queried at the same time using a tuple.
    ///
    /// ### Example:
    /// ```
    /// use shipyard::{EntitiesViewMut, Get, ViewMut, World};
    ///
    /// let world = World::new();
    ///
    /// world.run(
    ///     |mut entities: EntitiesViewMut, mut usizes: ViewMut<usize>, mut u32s: ViewMut<u32>| {
    ///         let entity = entities.add_entity((&mut usizes, &mut u32s), (0usize, 1u32));
    ///         assert_eq!((&usizes, &u32s).try_get(entity), Ok((&0, &1)));
    ///     },
    /// );
    /// ```
    fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent>;
    /// Retrieve components of `entity`.  
    /// Unwraps errors.
    ///
    /// Multiple components can be queried at the same time using a tuple.
    ///
    /// ### Example:
    /// ```
    /// use shipyard::{EntitiesViewMut, Get, ViewMut, World};
    ///
    /// let world = World::new();
    ///
    /// world.run(
    ///     |mut entities: EntitiesViewMut, mut usizes: ViewMut<usize>, mut u32s: ViewMut<u32>| {
    ///         let entity = entities.add_entity((&mut usizes, &mut u32s), (0usize, 1u32));
    ///         assert_eq!((&usizes, &u32s).get(entity), (&0, &1));
    ///     },
    /// );
    /// ```
    fn get(self, entity: EntityId) -> Self::Out;
}

impl<'a: 'b, 'b, T: 'static> Get for &'b Window<'a, T> {
    type Out = &'b T;
    fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        self.get(entity).ok_or_else(|| error::MissingComponent {
            id: entity,
            name: type_name::<T>(),
        })
    }
    fn get(self, entity: EntityId) -> Self::Out {
        self.try_get(entity).unwrap()
    }
}

impl<'a: 'b, 'b, T: 'static> Get for &'b WindowMut<'a, T> {
    type Out = &'b T;
    fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        self.get(entity).ok_or_else(|| error::MissingComponent {
            id: entity,
            name: type_name::<T>(),
        })
    }
    fn get(self, entity: EntityId) -> Self::Out {
        self.try_get(entity).unwrap()
    }
}

impl<'a: 'b, 'b, T: 'static> Get for &'b mut WindowMut<'a, T> {
    type Out = &'b mut T;
    fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        self.get_mut(entity).ok_or_else(|| error::MissingComponent {
            id: entity,
            name: type_name::<T>(),
        })
    }
    fn get(self, entity: EntityId) -> Self::Out {
        self.try_get(entity).unwrap()
    }
}

impl<'a: 'b, 'b, T: 'static> Get for &'b View<'a, T> {
    type Out = &'b T;
    fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        (**self).get(entity).ok_or_else(|| error::MissingComponent {
            id: entity,
            name: type_name::<T>(),
        })
    }
    fn get(self, entity: EntityId) -> Self::Out {
        self.try_get(entity).unwrap()
    }
}

impl<'a: 'b, 'b, T: 'static> Get for &'b ViewMut<'a, T> {
    type Out = &'b T;
    fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        (**self).get(entity).ok_or_else(|| error::MissingComponent {
            id: entity,
            name: type_name::<T>(),
        })
    }
    fn get(self, entity: EntityId) -> Self::Out {
        self.try_get(entity).unwrap()
    }
}

impl<'a: 'b, 'b, T: 'static> Get for &'b mut ViewMut<'a, T> {
    type Out = &'b mut T;
    fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        self.get_mut(entity).ok_or_else(|| error::MissingComponent {
            id: entity,
            name: type_name::<T>(),
        })
    }
    fn get(self, entity: EntityId) -> Self::Out {
        self.try_get(entity).unwrap()
    }
}

macro_rules! impl_get_component {
    ($(($type: ident, $index: tt))+) => {
        impl<$($type: Get),+> Get for ($($type,)+) {
            type Out = ($($type::Out,)+);
            fn try_get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
                Ok(($(self.$index.try_get(entity)?,)+))
            }
            fn get(self, entity: EntityId) -> Self::Out {
                self.try_get(entity).unwrap()
            }
        }
    }
}

macro_rules! get_component {
    ($(($type: ident, $index: tt))+; ($type1: ident, $index1: tt) $(($queue_type: ident, $queue_index: tt))*) => {
        impl_get_component![$(($type, $index))*];
        get_component![$(($type, $index))* ($type1, $index1); $(($queue_type, $queue_index))*];
    };
    ($(($type: ident, $index: tt))+;) => {
        impl_get_component![$(($type, $index))*];
    }
}

get_component![(A, 0); (B, 1) (C, 2) (D, 3) (E, 4) (F, 5) (G, 6) (H, 7) (I, 8) (J, 9)];