use crate::entity;
mod vec;
pub use vec::VecStorage as Vec;
mod tree;
pub use tree::Tree;
pub(crate) mod simple;
pub(crate) use simple::Simple;
mod isotope;
pub(crate) use isotope::{AnyMap as AnyIsotopeMap, Map as IsotopeMap, MapInner as IsotopeMapInner};
#[cfg(test)]
mod tests;
pub trait Storage: Access + Default + Send + Sync + 'static {
fn get(&self, id: Self::RawEntity) -> Option<&Self::Comp>;
fn set(&mut self, id: Self::RawEntity, value: Option<Self::Comp>) -> Option<Self::Comp>;
fn cardinality(&self) -> usize;
type Iter<'t>: Iterator<Item = (Self::RawEntity, &'t Self::Comp)> + 't;
fn iter(&self) -> Self::Iter<'_>;
type IterChunks<'t>: Iterator<Item = ChunkRef<'t, Self>> + 't;
fn iter_chunks(&self) -> Self::IterChunks<'_>;
type IterChunksMut<'t>: Iterator<Item = ChunkMut<'t, Self>> + 't;
fn iter_chunks_mut(&mut self) -> Self::IterChunksMut<'_>;
type Partition<'u>: Partition<'u, RawEntity = Self::RawEntity, Comp = Self::Comp>
where
Self: 'u;
fn as_partition(&mut self) -> Self::Partition<'_>;
}
pub trait Partition<'t>: Access + Send + Sync + Sized + 't {
type ByRef<'u>: Partition<'u, RawEntity = Self::RawEntity, Comp = Self::Comp>
where
Self: 'u;
fn by_ref(&mut self) -> Self::ByRef<'_>;
fn split_at(mut self, entity: Self::RawEntity) -> (Self, Self) {
let right = self.split_out(entity);
(self, right)
}
fn split_out(&mut self, entity: Self::RawEntity) -> Self;
type IntoIterMut: Iterator<Item = (Self::RawEntity, &'t mut Self::Comp)>;
fn into_iter_mut(self) -> Self::IntoIterMut;
fn into_mut(self, entity: Self::RawEntity) -> Option<&'t mut Self::Comp>;
fn into_many_mut<const N: usize>(
self,
entities: [Self::RawEntity; N],
) -> Option<[&'t mut Self::Comp; N]>;
}
pub trait Access {
type RawEntity: entity::Raw;
type Comp: Send + Sync + 'static;
fn get_mut(&mut self, entity: Self::RawEntity) -> Option<&mut Self::Comp>;
fn get_many_mut<const N: usize>(
&mut self,
entities: [Self::RawEntity; N],
) -> Option<[&mut Self::Comp; N]>;
type IterMut<'u>: Iterator<Item = (Self::RawEntity, &'u mut Self::Comp)> + 'u
where
Self: 'u;
fn iter_mut(&mut self) -> Self::IterMut<'_>;
}
pub trait Chunked: Storage + AccessChunked {
fn get_chunk(&self, start: Self::RawEntity, end: Self::RawEntity) -> Option<&[Self::Comp]>;
type PartitionChunked<'u>: PartitionChunked<'u, RawEntity = Self::RawEntity, Comp = Self::Comp>;
fn as_partition_chunk(&mut self) -> Self::PartitionChunked<'_>;
}
pub trait AccessChunked: Access {
fn get_chunk_mut(
&mut self,
start: Self::RawEntity,
end: Self::RawEntity,
) -> Option<&mut [Self::Comp]>;
}
pub trait PartitionChunked<'t>: Partition<'t> + AccessChunked {
fn into_chunk_mut(
self,
start: Self::RawEntity,
end: Self::RawEntity,
) -> Option<&'t mut [Self::Comp]>;
type IntoIterChunksMut: Iterator<Item = (Self::RawEntity, &'t mut [Self::Comp])>;
fn into_iter_chunks_mut(self) -> Self::IntoIterChunksMut;
}
pub struct ChunkRef<'t, S: Storage> {
pub slice: &'t [S::Comp],
pub start: S::RawEntity,
}
pub struct ChunkMut<'t, S: Storage> {
pub slice: &'t mut [S::Comp],
pub start: S::RawEntity,
}