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
use crate::entity_id::EntityId;
use crate::error;
use crate::r#mut::Mut;
use crate::sparse_set::SparseSet;
use crate::view::{View, ViewMut};
use core::any::type_name;

/// Retrives components based on their type and entity id.
pub trait Get {
    #[allow(missing_docs)]
    type Out;
    #[allow(missing_docs)]
    type FastOut;
    /// Retrieve components of `entity`.
    ///
    /// Multiple components can be queried at the same time using a tuple.
    ///
    /// ### Example:
    /// ```
    /// use shipyard::{Get, View, World};
    ///
    /// let mut world = World::new();
    ///
    /// let entity = world.add_entity((0usize, 1u32));
    ///
    /// let (usizes, u32s) = world.borrow::<(View<usize>, View<u32>)>().unwrap();
    /// assert_eq!((&usizes, &u32s).get(entity), Ok((&0, &1)));
    /// ```
    fn get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent>;
    /// Retrieve components of `entity` without fine modification tracking.
    ///
    /// Multiple components can be queried at the same time using a tuple.
    ///
    /// ### Example:
    /// ```
    /// use shipyard::{Get, View, World};
    ///
    /// let mut world = World::new();
    ///
    /// let entity = world.add_entity((0usize, 1u32));
    ///
    /// let (usizes, u32s) = world.borrow::<(View<usize>, View<u32>)>().unwrap();
    /// assert_eq!((&usizes, &u32s).fast_get(entity), Ok((&0, &1)));
    /// ```
    fn fast_get(self, entity: EntityId) -> Result<Self::FastOut, error::MissingComponent>;
}

impl<'a: 'b, 'b, T: 'static> Get for &'b View<'a, T> {
    type Out = &'b T;
    type FastOut = &'b T;

    #[inline]
    fn get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        (**self)
            .private_get(entity)
            .ok_or_else(|| error::MissingComponent {
                id: entity,
                name: type_name::<T>(),
            })
    }
    #[inline]
    fn fast_get(self, entity: EntityId) -> Result<Self::FastOut, error::MissingComponent> {
        (**self)
            .private_get(entity)
            .ok_or_else(|| error::MissingComponent {
                id: entity,
                name: type_name::<T>(),
            })
    }
}

impl<'a: 'b, 'b, T: 'static> Get for &'b ViewMut<'a, T> {
    type Out = &'b T;
    type FastOut = &'b T;

    #[inline]
    fn get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        (**self)
            .private_get(entity)
            .ok_or_else(|| error::MissingComponent {
                id: entity,
                name: type_name::<T>(),
            })
    }
    #[inline]
    fn fast_get(self, entity: EntityId) -> Result<Self::FastOut, error::MissingComponent> {
        (**self)
            .private_get(entity)
            .ok_or_else(|| error::MissingComponent {
                id: entity,
                name: type_name::<T>(),
            })
    }
}

impl<'a: 'b, 'b, T: 'static> Get for &'b mut ViewMut<'a, T> {
    type Out = Mut<'b, T>;
    type FastOut = &'b mut T;

    #[inline]
    fn get(self, entity: EntityId) -> Result<Self::Out, error::MissingComponent> {
        let index = self
            .index_of(entity)
            .ok_or_else(|| error::MissingComponent {
                id: entity,
                name: type_name::<T>(),
            })?;

        if self.is_tracking_modification() {
            let SparseSet {
                sparse: _,
                dense,
                data,
                metadata: _,
            } = &mut **self;

            let entity = unsafe { dense.get_unchecked_mut(index) };

            Ok(Mut {
                flag: if !entity.is_inserted() {
                    Some(entity)
                } else {
                    None
                },
                data: unsafe { data.get_unchecked_mut(index) },
            })
        } else {
            Ok(Mut {
                flag: None,
                data: unsafe { self.data.get_unchecked_mut(index) },
            })
        }
    }
    #[inline]
    fn fast_get(self, entity: EntityId) -> Result<Self::FastOut, error::MissingComponent> {
        self.private_get_mut(entity)
            .ok_or_else(|| error::MissingComponent {
                id: entity,
                name: type_name::<T>(),
            })
    }
}

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

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)];