pub unsafe fn delete_lifetime<'a, 'b, T: ?Sized>(t: &'a T) -> &'b T{
let t = t as *const T;
&*t
}
pub unsafe fn delete_lifetime_mut<'a, 'b, T: ?Sized>(t: &'a mut T) -> &'b mut T{
let t = t as *mut T;
&mut *t
}
#[cfg(all(feature="stats_events", not(feature = "stdweb")))]
pub mod time{
pub fn now() -> std::time::Instant {
std::time::Instant::now()
}
pub fn elapsed(start: std::time::Instant, end: std::time::Instant) -> std::time::Duration{
end - start
}
}
#[cfg(all(feature="stats_events", feature = "stdweb"))]
pub mod time{
pub fn now() -> f64 {
stdweb::web::Date::now()
}
pub fn elapsed(start: f64, end: f64) -> std::time::Duration{
let diff = end - start;
let secs = (diff / 1000.) as u64;
let nanos = ((diff - secs as f64) * 1_000_000.) as u32;
std::time::Duration::new(secs, nanos)
}
}
#[macro_export]
macro_rules! cast {
($resource: ident as $trait: ty) => {
($resource, |r| r as &$trait, |r| r as &mut $trait)
};
}
#[cfg(all(feature="stats_events", feature = "stdweb"))]
type Instant = f64;
use core::slice;
use std::{iter::{Copied, FusedIterator}, marker::PhantomData, ops::{Deref, Index}};
#[cfg(all(feature="stats_events", not(feature = "stdweb")))]
use std::time::Instant;
#[cfg(feature="stats_events")]
use std::time::Duration;
use hashbrown::HashSet;
#[cfg(feature="parallel_iter")]
use rayon::iter::plumbing::UnindexedConsumer;
#[cfg(feature="parallel_iter")]
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use crate::{
Entity, StorageRef,
operators::{EntitiesComponentIter, EntitiesComponentIterMut, EntitiesComponentOptIter, EntitiesComponentOptIterMut},
storage::ReadOnlyStorage
};
#[cfg(feature="parallel_iter")]
use crate::operators::{ParEntitiesComponentIter, ParEntitiesComponentOptIter};
#[cfg(all(feature="stats_events", not(target_arch = "wasm32")))]
pub fn thread_id() -> u64 {
thread_id::get() as u64
}
#[cfg(all(feature="stats_events", target_arch = "wasm32"))]
pub fn thread_id() -> u64 {
0
}
#[cfg(feature="stats_events")]
#[derive(Clone, Debug)]
pub struct Stat{
start: Instant,
end: Instant,
duration: Duration,
tid: u64,
}
#[cfg(feature="stats_events")]
impl Default for Stat{
fn default() -> Stat{
let start = time::now();
let end = start;
let duration = Duration::default();
Stat{
start,
end,
duration,
tid: thread_id(),
}
}
}
#[cfg(feature="stats_events")]
impl Stat{
pub fn new(start: Instant, end: Instant, tid: u64) -> Stat{
Stat{
duration: time::elapsed(start, end),
start,
end,
tid,
}
}
pub fn start(&self) -> Instant {
self.start
}
pub fn end(&self) -> Instant {
self.end
}
pub fn duration(&self) -> Duration {
self.duration
}
pub fn thread_id(&self) -> u64{
self.tid
}
}
#[cfg(feature="storage_macros_16")]
pub const STORAGE_MACROS_NUM: usize = 16;
#[cfg(all(feature="storage_macros_8", not(feature="storage_macros_16")))]
pub const STORAGE_MACROS_NUM: usize = 8;
#[cfg(all(feature="storage_macros_4", not(feature="storage_macros_8"), not(feature="storage_macros_16")))]
pub const STORAGE_MACROS_NUM: usize = 4;
#[cfg(all(not(feature="storage_macros_4"), not(feature="storage_macros_8"), not(feature="storage_macros_16")))]
pub const STORAGE_MACROS_NUM: usize = 2;
#[macro_export]
macro_rules! unwrap_or_return {
( $e:expr ) => {
match $e {
Some(x) => x,
None => return,
}
}
}
pub struct UniqueEntity(Entity);
impl Deref for UniqueEntity {
type Target = Entity;
fn deref(&self) -> &Entity {
&self.0
}
}
impl UniqueEntity {
pub fn entity(&self) -> &Entity {
&self.0
}
}
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UniqueEntities(Vec<Entity>);
impl UniqueEntities {
pub fn try_new(entities: Vec<Entity>) -> Option<Self> {
let mut uniq = HashSet::new();
if entities.iter().all(move |x| uniq.insert(x)) {
Some(Self(entities))
}else{
None
}
}
pub unsafe fn new_from_unchecked(entities: Vec<Entity>) -> Self {
Self(entities)
}
pub fn with_capacity(capacity: usize) -> Self {
Self(Vec::with_capacity(capacity))
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn unique_iter(&self) -> UniqueEntitiesIter {
self.into_iter()
}
pub fn iter(&self) -> slice::Iter<Entity> {
self.0.iter()
}
pub fn first(&self) -> Option<Entity> {
self.0.first().copied()
}
pub fn last(&self) -> Option<Entity> {
self.0.last().copied()
}
#[cfg(feature="parallel_iter")]
pub fn unique_par_iter(&self) -> UniqueEntitiesParIter {
UniqueEntitiesParIter{
iter: self.0.par_iter().copied()
}
}
#[cfg(feature="parallel_iter")]
pub fn par_iter(&self) -> rayon::slice::Iter<Entity> {
self.0.par_iter()
}
pub fn into_vec(self) -> Vec<Entity> {
self.0
}
}
impl IntoIterator for UniqueEntities {
type Item = Entity;
type IntoIter = UniqueEntitiesIntoIter;
fn into_iter(self) -> Self::IntoIter {
UniqueEntitiesIntoIter{ iter: self.0.into_iter() }
}
}
impl<'a> IntoIterator for &'a UniqueEntities {
type Item = &'a UniqueEntity;
type IntoIter = UniqueEntitiesIter<'a>;
fn into_iter(self) -> Self::IntoIter {
UniqueEntitiesIter{ iter: self.0.iter() }
}
}
impl<'a> IntoIterator for &'a mut UniqueEntities {
type Item = &'a UniqueEntity;
type IntoIter = UniqueEntitiesIter<'a>;
fn into_iter(self) -> Self::IntoIter {
UniqueEntitiesIter{ iter: self.0.iter() }
}
}
impl Index<usize> for UniqueEntities {
type Output = Entity;
fn index(&self, index: usize) -> &Self::Output {
self.0.index(index)
}
}
pub struct UniqueEntitiesIter<'a> {
iter: std::slice::Iter<'a, Entity>
}
impl<'a> Iterator for UniqueEntitiesIter<'a> {
type Item = &'a UniqueEntity;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|e| unsafe{ std::mem::transmute(e) })
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a> DoubleEndedIterator for UniqueEntitiesIter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|e| unsafe{ std::mem::transmute(e) })
}
}
impl<'a> ExactSizeIterator for UniqueEntitiesIter<'a> {
}
impl<'a> FusedIterator for UniqueEntitiesIter<'a> {
}
pub struct UniqueEntitiesIntoIter {
iter: std::vec::IntoIter<Entity>
}
impl Iterator for UniqueEntitiesIntoIter {
type Item = Entity;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl DoubleEndedIterator for UniqueEntitiesIntoIter {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl ExactSizeIterator for UniqueEntitiesIntoIter {
}
impl FusedIterator for UniqueEntitiesIntoIter {
}
pub struct UniqueEntitiesIterWrapper<I>{
iter: I
}
pub trait IntoUniqueIterator {
unsafe fn into_unique(self) -> UniqueEntitiesIterWrapper<Self> where Self: Sized {
UniqueEntitiesIterWrapper {
iter: self
}
}
}
impl<I> IntoUniqueIterator for I
where I: Iterator
{}
#[cfg(feature="parallel_iter")]
pub trait IntoUniqueParallelIterator {
unsafe fn into_unique(self) -> UniqueEntitiesIterWrapper<Self> where Self: Sized {
UniqueEntitiesIterWrapper {
iter: self
}
}
}
#[cfg(feature="parallel_iter")]
impl<I> IntoUniqueParallelIterator for I
where I: ParallelIterator
{}
#[cfg(feature="parallel_iter")]
pub struct UniqueEntitiesParIter<'a> {
iter: rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>
}
#[cfg(feature="parallel_iter")]
impl<'a> ParallelIterator for UniqueEntitiesParIter<'a> {
type Item = Entity;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>
{
self.iter.drive_unindexed(consumer)
}
}
pub trait IntoEntitiesIterator<'a, S>{
type IntoEntitiesIter;
type IntoEntitiesIterMut;
type IntoEntitiesOptIter;
type IntoEntitiesOptIterMut;
fn storage_entities_iter(self, storage: Option<S>) -> Self::IntoEntitiesIter
where S: ReadOnlyStorage;
fn storage_entities_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesIterMut;
fn storage_entities_opt_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptIter
where S: ReadOnlyStorage;
fn storage_entities_opt_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptIterMut;
}
impl<'a, S> IntoEntitiesIterator<'a, S> for &'a UniqueEntities
where
S: StorageRef<'a> + 'a,
{
type IntoEntitiesIter = EntitiesComponentIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
type IntoEntitiesIterMut = EntitiesComponentIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptIter = EntitiesComponentOptIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptIterMut = EntitiesComponentOptIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
fn storage_entities_iter(self, storage: Option<S>) -> Self::IntoEntitiesIter
where S: ReadOnlyStorage
{
self.unique_iter().storage_entities_iter(storage)
}
fn storage_entities_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesIterMut {
self.unique_iter().storage_entities_iter_mut(storage)
}
fn storage_entities_opt_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptIter
where S: ReadOnlyStorage
{
self.unique_iter().storage_entities_opt_iter(storage)
}
fn storage_entities_opt_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptIterMut {
self.unique_iter().storage_entities_opt_iter_mut(storage)
}
}
impl<'a, S> IntoEntitiesIterator<'a, S> for UniqueEntitiesIter<'a>
where
S: StorageRef<'a> + 'a,
{
type IntoEntitiesIter = EntitiesComponentIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
type IntoEntitiesIterMut = EntitiesComponentIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptIter = EntitiesComponentOptIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptIterMut = EntitiesComponentOptIter<'a, Copied<slice::Iter<'a, Entity>>, S>;
fn storage_entities_iter(self, storage: Option<S>) -> Self::IntoEntitiesIter
where S: ReadOnlyStorage
{
EntitiesComponentIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesIterMut {
EntitiesComponentIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_opt_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptIter
where S: ReadOnlyStorage
{
EntitiesComponentOptIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_opt_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptIterMut {
EntitiesComponentOptIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
}
impl<'a, S, I> IntoEntitiesIterator<'a, S> for UniqueEntitiesIterWrapper<I>
where
S: StorageRef<'a> + 'a,
I: Iterator<Item = &'a Entity>,
{
type IntoEntitiesIter = EntitiesComponentIter<'a, Copied<I>, S>;
type IntoEntitiesIterMut = EntitiesComponentIter<'a, Copied<I>, S>;
type IntoEntitiesOptIter = EntitiesComponentOptIter<'a, Copied<I>, S>;
type IntoEntitiesOptIterMut = EntitiesComponentOptIter<'a, Copied<I>, S>;
fn storage_entities_iter(self, storage: Option<S>) -> Self::IntoEntitiesIter
where S: ReadOnlyStorage
{
EntitiesComponentIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesIterMut {
EntitiesComponentIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_opt_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptIter
where S: ReadOnlyStorage
{
EntitiesComponentOptIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_opt_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptIterMut {
EntitiesComponentOptIter{
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
}
impl<'a, S> IntoEntitiesIterator<'a, S> for UniqueEntitiesIntoIter
where
S: StorageRef<'a> + 'a,
{
type IntoEntitiesIter = EntitiesComponentIter<'a, Self, S>;
type IntoEntitiesIterMut = EntitiesComponentIter<'a, Self, S>;
type IntoEntitiesOptIter = EntitiesComponentOptIter<'a, Self, S>;
type IntoEntitiesOptIterMut = EntitiesComponentOptIter<'a, Self, S>;
fn storage_entities_iter(self, storage: Option<S>) -> Self::IntoEntitiesIter
where S: ReadOnlyStorage
{
EntitiesComponentIter{
entities: self,
storage,
marker: PhantomData
}
}
fn storage_entities_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesIterMut {
EntitiesComponentIter{
entities: self,
storage,
marker: PhantomData
}
}
fn storage_entities_opt_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptIter
where S: ReadOnlyStorage
{
EntitiesComponentOptIter{
entities: self,
storage,
marker: PhantomData
}
}
fn storage_entities_opt_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptIterMut {
EntitiesComponentOptIter{
entities: self,
storage,
marker: PhantomData
}
}
}
impl<'a, 'e, S, I> IntoEntitiesIterator<'a, S> for I
where
S: StorageRef<'a> + 'a,
I: IntoIterator<Item = &'e Entity>,
{
type IntoEntitiesIter = EntitiesComponentIter<'a, Copied<I::IntoIter>, S>;
type IntoEntitiesIterMut = EntitiesComponentIterMut<'a, Copied<I::IntoIter>, S>;
type IntoEntitiesOptIter = EntitiesComponentOptIter<'a, Copied<I::IntoIter>, S>;
type IntoEntitiesOptIterMut = EntitiesComponentOptIterMut<'a, Copied<I::IntoIter>, S>;
fn storage_entities_iter(self, storage: Option<S>) -> Self::IntoEntitiesIter
where S: ReadOnlyStorage
{
EntitiesComponentIter{
entities: self.into_iter().copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesIterMut {
EntitiesComponentIterMut{
entities: self.into_iter().copied(),
storage,
current: None,
marker: PhantomData
}
}
fn storage_entities_opt_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptIter
where S: ReadOnlyStorage
{
EntitiesComponentOptIter{
entities: self.into_iter().copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_opt_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptIterMut {
EntitiesComponentOptIterMut{
entities: self.into_iter().copied(),
storage,
current: None,
marker: PhantomData
}
}
}
#[cfg(feature="parallel_iter")]
pub trait IntoEntitiesParIterator<'a, S>{
type IntoEntitiesParIter;
type IntoEntitiesParIterMut;
type IntoEntitiesOptParIter;
type IntoEntitiesOptParIterMut;
fn storage_entities_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesParIter
where S: ReadOnlyStorage;
fn storage_entities_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesParIterMut;
fn storage_entities_opt_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptParIter
where S: ReadOnlyStorage;
fn storage_entities_opt_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptParIterMut;
}
#[cfg(feature="parallel_iter")]
impl<'a, S> IntoEntitiesParIterator<'a, S> for UniqueEntitiesParIter<'a>
where
S: StorageRef<'a> + 'a,
{
type IntoEntitiesParIter = ParEntitiesComponentIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
type IntoEntitiesParIterMut = ParEntitiesComponentIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptParIter = ParEntitiesComponentOptIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptParIterMut = ParEntitiesComponentOptIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
fn storage_entities_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesParIter
where S: ReadOnlyStorage
{
ParEntitiesComponentIter {
entities: self.iter,
storage,
marker: PhantomData
}
}
fn storage_entities_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesParIterMut {
ParEntitiesComponentIter {
entities: self.iter,
storage,
marker: PhantomData
}
}
fn storage_entities_opt_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptParIter
where S: ReadOnlyStorage
{
ParEntitiesComponentOptIter {
entities: self.iter,
storage,
marker: PhantomData
}
}
fn storage_entities_opt_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptParIterMut {
ParEntitiesComponentOptIter {
entities: self.iter,
storage,
marker: PhantomData
}
}
}
#[cfg(feature="parallel_iter")]
impl<'a, S, I> IntoEntitiesParIterator<'a, S> for UniqueEntitiesIterWrapper<I>
where
S: StorageRef<'a> + 'a,
I: ParallelIterator<Item = &'a Entity>,
{
type IntoEntitiesParIter = ParEntitiesComponentIter<'a, rayon::iter::Copied<I>, S>;
type IntoEntitiesParIterMut = ParEntitiesComponentIter<'a, rayon::iter::Copied<I>, S>;
type IntoEntitiesOptParIter = ParEntitiesComponentOptIter<'a, rayon::iter::Copied<I>, S>;
type IntoEntitiesOptParIterMut = ParEntitiesComponentOptIter<'a, rayon::iter::Copied<I>, S>;
fn storage_entities_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesParIter
where S: ReadOnlyStorage
{
ParEntitiesComponentIter {
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesParIterMut {
ParEntitiesComponentIter {
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_opt_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptParIter
where S: ReadOnlyStorage
{
ParEntitiesComponentOptIter {
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
fn storage_entities_opt_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptParIterMut {
ParEntitiesComponentOptIter {
entities: self.iter.copied(),
storage,
marker: PhantomData
}
}
}
#[cfg(feature="parallel_iter")]
impl<'a, S> IntoEntitiesParIterator<'a, S> for &'a UniqueEntities
where
S: StorageRef<'a> + 'a,
{
type IntoEntitiesParIter = ParEntitiesComponentIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
type IntoEntitiesParIterMut = ParEntitiesComponentIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptParIter = ParEntitiesComponentOptIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
type IntoEntitiesOptParIterMut = ParEntitiesComponentOptIter<'a, rayon::iter::Copied<rayon::slice::Iter<'a, Entity>>, S>;
fn storage_entities_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesParIter
where S: ReadOnlyStorage
{
self.unique_par_iter().storage_entities_par_iter(storage)
}
fn storage_entities_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesParIterMut {
self.unique_par_iter().storage_entities_par_iter_mut(storage)
}
fn storage_entities_opt_par_iter(self, storage: Option<S>) -> Self::IntoEntitiesOptParIter
where S: ReadOnlyStorage
{
self.unique_par_iter().storage_entities_opt_par_iter(storage)
}
fn storage_entities_opt_par_iter_mut(self, storage: Option<S>) -> Self::IntoEntitiesOptParIterMut {
self.unique_par_iter().storage_entities_opt_par_iter_mut(storage)
}
}