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
use std::marker::PhantomData;
use std::{any, fmt};
use super::accessor;
use crate::{comp, entity, Archetype};
/// Generalizes [`ReadSimple`] and [`ReadIsotope`] for a specific discriminant
/// (through [`ReadIsotope::split`]).
pub trait Read<A: Archetype, C: 'static> {
/// Returns an immutable reference to the component for the specified entity,
/// or `None` if the component is not present in the entity.
fn try_get<E: entity::Ref<Archetype = A>>(&self, entity: E) -> Option<&C>;
/// Returns an immutable reference to the component for the specified entity.
///
/// # Panics
/// This method panics if the entity is not fully initialized yet.
/// This happens when an entity is newly created and the cycle hasn't joined yet.
fn get<E: entity::Ref<Archetype = A>>(&self, entity: E) -> &C
where
C: comp::Must<A>,
{
match self.try_get(entity) {
Some(comp) => comp,
None => panic!(
"Component {}/{} implements comp::Must but is not present",
any::type_name::<A>(),
any::type_name::<C>()
),
}
}
/// Return value of [`iter`](Self::iter).
type Iter<'t>: Iterator<Item = (entity::TempRef<'t, A>, &'t C)>
where
Self: 't;
/// Iterates over all initialized components in this storage.
fn iter(&self) -> Self::Iter<'_>;
/// Returns an [`Accessor`](accessor::Accessor) implementor that yields `&C` for each entity.
fn access(&self) -> accessor::MustRead<A, C, &Self>
where
C: comp::Must<A>,
{
accessor::MustRead(self, PhantomData)
}
/// Returns an [`Accessor`](accessor::Accessor) implementor that yields `Option<&C>` for each entity.
fn try_access(&self) -> accessor::TryRead<A, C, &Self> { accessor::TryRead(self, PhantomData) }
/// Return value of [`duplicate_immut`](Self::duplicate_immut).
type DuplicateImmut<'t>: Read<A, C> + 't
where
Self: 't;
/// Duplicates the current reader,
/// producing two new values that can only access the storage immutably.
fn duplicate_immut(&self) -> (Self::DuplicateImmut<'_>, Self::DuplicateImmut<'_>);
}
/// Extends [`Read`] with chunk reading ability
/// for storages that support chunked access.
pub trait ReadChunk<A: Archetype, C: 'static> {
/// Returns the chunk of components as a slice.
///
/// # Panics
/// This method panics if any component in the chunk is missing.
/// In general, users should not get an [`entity::TempRefChunk`]
/// that includes an uninitialized entity,
/// so panic is basically impossible if [`comp::Must`] was implemented correctly.
fn get_chunk(&self, chunk: entity::TempRefChunk<'_, A>) -> &'_ [C]
where
C: comp::Must<A>;
}
/// Generalizes [`WriteSimple`], [`WriteIsotope`] and their split storages.
///
/// Only supports mutable access to an existing component,
/// but does not support adding or removing components
/// since only the storage values but not the storage structure can be borrowed mutably.
pub trait Mut<A: Archetype, C: 'static> {
/// Returns a mutable reference to the component for the specified entity,
/// or `None` if the component is not present in the entity.
///
/// Note that this method returns `Option<&mut C>`, not `&mut Option<C>`.
/// This means setting the Option itself to `Some`/`None` will not modify any stored value.
/// Use [`Write::set`] to add/remove a component.
fn try_get_mut<E: entity::Ref<Archetype = A>>(&mut self, entity: E) -> Option<&mut C>;
/// Return value of [`iter_mut`](Self::iter_mut).
type IterMut<'t>: Iterator<Item = (entity::TempRef<'t, A>, &'t mut C)>
where
Self: 't;
/// Iterates over mutable references to all initialized components in this storage.
fn iter_mut(&mut self) -> Self::IterMut<'_>;
/// Return value of [`split_entities_at`](Self::split_entities_at).
type SplitEntitiesAt<'u>: Mut<A, C> + 'u
where
Self: 'u;
/// Partitions the accessor into two disjoint halves of entities.
///
/// This method is not required for [`Read`]
/// because shared references can be reused directly.
fn split_entities_at<E: entity::Ref<Archetype = A>>(
&mut self,
entity: E,
) -> (Self::SplitEntitiesAt<'_>, Self::SplitEntitiesAt<'_>);
}
/// Generalizes [`WriteSimple`] and [`WriteIsotope`] for a specific discriminant
/// (through [`WriteIsotope::split_isotopes`]).
pub trait Write<A: Archetype, C: 'static>: Read<A, C> + Mut<A, C> {
/// Returns a mutable reference to the component for the specified entity.
///
/// This method is infallible, assuming [`comp::Must`] is only implemented
/// for components with [`Required`](comp::SimplePresence::Required) presence.
fn get_mut<E: entity::Ref<Archetype = A>>(&mut self, entity: E) -> &mut C
where
C: comp::Must<A>,
{
match self.try_get_mut(entity) {
Some(comp) => comp,
None => panic!(
"Component {}/{} implements comp::Must but is not present",
any::type_name::<A>(),
any::type_name::<C>(),
),
}
}
/// Overwrites the component for the specified entity.
///
/// Passing `None` to this method removes the component from the entity.
/// This leads to a panic for components with [`comp::SimplePresence::Required`] presence.
fn set<E: entity::Ref<Archetype = A>>(&mut self, entity: E, value: Option<C>) -> Option<C>;
/// Returns an [`Accessor`](accessor::Accessor) implementor that yields `&C` for each entity.
fn access_mut(&mut self) -> accessor::MustWrite<A, C, &mut Self>
where
C: comp::Must<A>,
{
accessor::MustWrite(self, PhantomData)
}
/// Returns an [`Accessor`](accessor::Accessor) implementor that yields `Option<&C>` for each entity.
fn try_access_mut(&mut self) -> accessor::TryWrite<A, C, &mut Self> {
accessor::TryWrite(self, PhantomData)
}
}
/// Extends [`Write`] with chunk writing ability
/// for storages that support chunked access.
pub trait WriteChunk<A: Archetype, C: 'static> {
/// Returns the chunk of components as a mutable slice.
/// Typically called from an accessor.
///
/// # Panics
/// This method panics if any component in the chunk is missing.
/// In general, users should not get an [`entity::TempRefChunk`]
/// that includes an uninitialized entity,
/// so panic is basically impossible if [`comp::Must`] was implemented correctly.
fn get_chunk_mut(&mut self, chunk: entity::TempRefChunk<'_, A>) -> &'_ mut [C]
where
C: comp::Must<A>;
}
/// Provides access to a simple component in a specific archetype.
pub trait ReadSimple<A: Archetype, C: comp::Simple<A>>: Read<A, C> {
/// Returns a [`Chunked`](accessor::Chunked) accessor that can be used in
/// [`EntityIterator`](super::EntityIterator)
/// to provide chunked iteration to an entity.
fn access_chunk(&self) -> accessor::MustReadChunkSimple<'_, A, C>;
}
/// Provides access to a simple component in a specific archetype.
pub trait WriteSimple<A: Archetype, C: comp::Simple<A>>: ReadSimple<A, C> + Write<A, C> {
/// Returns a [`Chunked`](accessor::Chunked) accessor that can be used in
/// [`EntityIterator`](super::EntityIterator)
/// to provide chunked iteration to an entity.
fn access_chunk_mut(&mut self) -> accessor::MustWriteChunkSimple<'_, A, C>;
}
/// Provides access to an isotope component in a specific archetype.
///
/// `K` is the type used to index the discriminant.
/// For partial isotope access, `K` is usually `usize`.
/// For full isotope access, `K` is the discriminant type.
pub trait ReadIsotope<A: Archetype, C: comp::Isotope<A>, K = <C as comp::Isotope<A>>::Discrim>
where
K: fmt::Debug + Copy + 'static,
{
/// Retrieves the component for the given entity and discriminant.
///
/// This method is infallible for correctly implemented `comp::Must`,
/// which returns the auto-initialized value for missing components.
fn get<E: entity::Ref<Archetype = A>>(&self, entity: E, discrim: K) -> &C
where
C: comp::Must<A>,
{
match self.try_get(entity, discrim) {
Some(value) => value,
None => panic!(
"{}: comp::Must<{}> but has no default initializer",
any::type_name::<C>(),
any::type_name::<A>()
),
}
}
/// Returns an immutable reference to the component for the specified entity and discriminant,
/// or the default value for isotopes with a default initializer or `None`
/// if the component is not present in the entity.
fn try_get<E: entity::Ref<Archetype = A>>(&self, entity: E, discrim: K) -> Option<&C>;
/// Return value of [`get_all`](Self::get_all).
type GetAll<'t>: Iterator<Item = (<C as comp::Isotope<A>>::Discrim, &'t C)> + 't
where
Self: 't;
/// Iterates over all isotopes of the component type for the given entity.
///
/// The yielded discriminants are not in any guaranteed order.
fn get_all<E: entity::Ref<Archetype = A>>(&self, entity: E) -> Self::GetAll<'_>;
/// Return value of [`iter`](Self::iter).
type Iter<'t>: Iterator<Item = (entity::TempRef<'t, A>, &'t C)>
where
Self: 't;
/// Iterates over all components of a specific discriminant.
///
/// Note that the initializer is not called for lazy-initialized isotope components.
/// To avoid confusing behavior, do not use this function if [`C: comp::Must<A>`](comp::Must).
fn iter(&self, discrim: K) -> Self::Iter<'_>;
/// Return value of [`split`](Self::split).
type Split<'t>: Read<A, C> + 't
where
Self: 't;
/// Splits the accessor into multiple [`Read`] implementors
/// so that they can be used independently.
fn split<const N: usize>(&self, keys: [K; N]) -> [Self::Split<'_>; N];
}
/// Provides access to an isotope component in a specific archetype.
pub trait WriteIsotope<A: Archetype, C: comp::Isotope<A>, K = <C as comp::Isotope<A>>::Discrim>:
ReadIsotope<A, C, K>
where
K: fmt::Debug + Copy + 'static,
{
/// Returns a mutable reference to the component for the specified entity and discriminant,
/// automatically initialized with the default initializer if present,
/// or `None` if the component is unset and has no default initializer.
///
/// Note that this method returns `Option<&mut C>`, not `&mut Option<C>`.
/// This means setting the Option itself to `Some`/`None` will not modify any stored value.
/// Use [`WriteIsotope::set`] to add/remove a component.
fn try_get_mut<E: entity::Ref<Archetype = A>>(
&mut self,
entity: E,
discrim: K,
) -> Option<&mut C>;
/// Overwrites the component for the specified entity and discriminant.
///
/// Passing `None` to this method removes the component from the entity.
fn set<E: entity::Ref<Archetype = A>>(
&mut self,
entity: E,
discrim: K,
value: Option<C>,
) -> Option<C>;
/// Return value of [`iter_mut`](Self::iter_mut).
type IterMut<'t>: Iterator<Item = (entity::TempRef<'t, A>, &'t mut C)>
where
Self: 't;
/// Iterates over mutable references to all components of a specific discriminant.
fn iter_mut(&mut self, discrim: K) -> Self::IterMut<'_>;
/// Return value of [`split_isotopes`](Self::split_isotopes).
type SplitDiscrim<'t>: Write<A, C> + 't
where
Self: 't;
/// Splits the accessor into multiple [`Write`] implementors
/// so that they can be used in entity iteration independently.
fn split_isotopes<const N: usize>(&mut self, keys: [K; N]) -> [Self::SplitDiscrim<'_>; N];
}