use core::marker::PhantomData;
use core::ptr::NonNull;
use crate::archetype::Archetype;
use crate::entities::EntityMeta;
use crate::{Component, Entity};
pub trait Query {
#[doc(hidden)]
type Fetch: for<'a> Fetch<'a>;
}
pub trait Fetch<'a>: Sized {
type Item;
fn access(archetype: &Archetype) -> Option<Access>;
fn borrow(archetype: &Archetype);
unsafe fn get(archetype: &'a Archetype, offset: usize) -> Option<Self>;
fn release(archetype: &Archetype);
unsafe fn next(&mut self) -> Self::Item;
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Access {
Iterate,
Read,
Write,
}
impl<'a, T: Component> Query for &'a T {
type Fetch = FetchRead<T>;
}
#[doc(hidden)]
pub struct FetchRead<T>(NonNull<T>);
impl<'a, T: Component> Fetch<'a> for FetchRead<T> {
type Item = &'a T;
fn access(archetype: &Archetype) -> Option<Access> {
if archetype.has::<T>() {
Some(Access::Read)
} else {
None
}
}
fn borrow(archetype: &Archetype) {
archetype.borrow::<T>();
}
unsafe fn get(archetype: &'a Archetype, offset: usize) -> Option<Self> {
archetype
.get::<T>()
.map(|x| Self(NonNull::new_unchecked(x.as_ptr().add(offset))))
}
fn release(archetype: &Archetype) {
archetype.release::<T>();
}
unsafe fn next(&mut self) -> &'a T {
let x = self.0.as_ptr();
self.0 = NonNull::new_unchecked(x.add(1));
&*x
}
}
impl<'a, T: Component> Query for &'a mut T {
type Fetch = FetchWrite<T>;
}
#[doc(hidden)]
pub struct FetchWrite<T>(NonNull<T>);
impl<'a, T: Component> Fetch<'a> for FetchWrite<T> {
type Item = &'a mut T;
fn access(archetype: &Archetype) -> Option<Access> {
if archetype.has::<T>() {
Some(Access::Write)
} else {
None
}
}
fn borrow(archetype: &Archetype) {
archetype.borrow_mut::<T>();
}
unsafe fn get(archetype: &'a Archetype, offset: usize) -> Option<Self> {
archetype
.get::<T>()
.map(|x| Self(NonNull::new_unchecked(x.as_ptr().add(offset))))
}
fn release(archetype: &Archetype) {
archetype.release_mut::<T>();
}
unsafe fn next(&mut self) -> &'a mut T {
let x = self.0.as_ptr();
self.0 = NonNull::new_unchecked(x.add(1));
&mut *x
}
}
impl<T: Query> Query for Option<T> {
type Fetch = TryFetch<T::Fetch>;
}
#[doc(hidden)]
pub struct TryFetch<T>(Option<T>);
impl<'a, T: Fetch<'a>> Fetch<'a> for TryFetch<T> {
type Item = Option<T::Item>;
fn access(archetype: &Archetype) -> Option<Access> {
Some(T::access(archetype).unwrap_or(Access::Iterate))
}
fn borrow(archetype: &Archetype) {
T::borrow(archetype)
}
unsafe fn get(archetype: &'a Archetype, offset: usize) -> Option<Self> {
Some(Self(T::get(archetype, offset)))
}
fn release(archetype: &Archetype) {
T::release(archetype)
}
unsafe fn next(&mut self) -> Option<T::Item> {
Some(self.0.as_mut()?.next())
}
}
pub struct Without<T, Q>(PhantomData<(Q, fn(T))>);
impl<T: Component, Q: Query> Query for Without<T, Q> {
type Fetch = FetchWithout<T, Q::Fetch>;
}
#[doc(hidden)]
pub struct FetchWithout<T, F>(F, PhantomData<fn(T)>);
impl<'a, T: Component, F: Fetch<'a>> Fetch<'a> for FetchWithout<T, F> {
type Item = F::Item;
fn access(archetype: &Archetype) -> Option<Access> {
if archetype.has::<T>() {
None
} else {
F::access(archetype)
}
}
fn borrow(archetype: &Archetype) {
F::borrow(archetype)
}
unsafe fn get(archetype: &'a Archetype, offset: usize) -> Option<Self> {
if archetype.has::<T>() {
return None;
}
Some(Self(F::get(archetype, offset)?, PhantomData))
}
fn release(archetype: &Archetype) {
F::release(archetype)
}
unsafe fn next(&mut self) -> F::Item {
self.0.next()
}
}
pub struct With<T, Q>(PhantomData<(Q, fn(T))>);
impl<T: Component, Q: Query> Query for With<T, Q> {
type Fetch = FetchWith<T, Q::Fetch>;
}
#[doc(hidden)]
pub struct FetchWith<T, F>(F, PhantomData<fn(T)>);
impl<'a, T: Component, F: Fetch<'a>> Fetch<'a> for FetchWith<T, F> {
type Item = F::Item;
fn access(archetype: &Archetype) -> Option<Access> {
if archetype.has::<T>() {
F::access(archetype)
} else {
None
}
}
fn borrow(archetype: &Archetype) {
F::borrow(archetype)
}
unsafe fn get(archetype: &'a Archetype, offset: usize) -> Option<Self> {
if !archetype.has::<T>() {
return None;
}
Some(Self(F::get(archetype, offset)?, PhantomData))
}
fn release(archetype: &Archetype) {
F::release(archetype)
}
unsafe fn next(&mut self) -> F::Item {
self.0.next()
}
}
pub struct QueryBorrow<'w, Q: Query> {
meta: &'w [EntityMeta],
archetypes: &'w [Archetype],
borrowed: bool,
_marker: PhantomData<Q>,
}
impl<'w, Q: Query> QueryBorrow<'w, Q> {
pub(crate) fn new(meta: &'w [EntityMeta], archetypes: &'w [Archetype]) -> Self {
Self {
meta,
archetypes,
borrowed: false,
_marker: PhantomData,
}
}
pub fn iter<'q>(&'q mut self) -> QueryIter<'q, 'w, Q> {
self.borrow();
QueryIter {
borrow: self,
archetype_index: 0,
iter: None,
}
}
pub fn iter_batched<'q>(&'q mut self, batch_size: u32) -> BatchedIter<'q, 'w, Q> {
self.borrow();
BatchedIter {
borrow: self,
archetype_index: 0,
batch_size,
batch: 0,
}
}
fn borrow(&mut self) {
if self.borrowed {
panic!(
"called QueryBorrow::iter twice on the same borrow; construct a new query instead"
);
}
for x in self.archetypes {
if Q::Fetch::access(x) >= Some(Access::Read) {
Q::Fetch::borrow(x);
}
}
self.borrowed = true;
}
pub fn with<T: Component>(self) -> QueryBorrow<'w, With<T, Q>> {
self.transform()
}
pub fn without<T: Component>(self) -> QueryBorrow<'w, Without<T, Q>> {
self.transform()
}
fn transform<R: Query>(mut self) -> QueryBorrow<'w, R> {
let x = QueryBorrow {
meta: self.meta,
archetypes: self.archetypes,
borrowed: self.borrowed,
_marker: PhantomData,
};
self.borrowed = false;
x
}
}
unsafe impl<'w, Q: Query> Send for QueryBorrow<'w, Q> {}
unsafe impl<'w, Q: Query> Sync for QueryBorrow<'w, Q> {}
impl<'w, Q: Query> Drop for QueryBorrow<'w, Q> {
fn drop(&mut self) {
if self.borrowed {
for x in self.archetypes {
if Q::Fetch::access(x) >= Some(Access::Read) {
Q::Fetch::release(x);
}
}
}
}
}
impl<'q, 'w, Q: Query> IntoIterator for &'q mut QueryBorrow<'w, Q> {
type Item = (Entity, <Q::Fetch as Fetch<'q>>::Item);
type IntoIter = QueryIter<'q, 'w, Q>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub struct QueryIter<'q, 'w, Q: Query> {
borrow: &'q mut QueryBorrow<'w, Q>,
archetype_index: u32,
iter: Option<ChunkIter<Q>>,
}
unsafe impl<'q, 'w, Q: Query> Send for QueryIter<'q, 'w, Q> {}
unsafe impl<'q, 'w, Q: Query> Sync for QueryIter<'q, 'w, Q> {}
impl<'q, 'w, Q: Query> Iterator for QueryIter<'q, 'w, Q> {
type Item = (Entity, <Q::Fetch as Fetch<'q>>::Item);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter {
None => {
let archetype = self.borrow.archetypes.get(self.archetype_index as usize)?;
self.archetype_index += 1;
unsafe {
self.iter = Q::Fetch::get(archetype, 0).map(|fetch| ChunkIter {
entities: archetype.entities(),
fetch,
len: archetype.len(),
});
}
}
Some(ref mut iter) => match unsafe { iter.next() } {
None => {
self.iter = None;
continue;
}
Some((id, components)) => {
return Some((
Entity {
id,
generation: self.borrow.meta[id as usize].generation,
},
components,
));
}
},
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let n = self.len();
(n, Some(n))
}
}
impl<'q, 'w, Q: Query> ExactSizeIterator for QueryIter<'q, 'w, Q> {
fn len(&self) -> usize {
self.borrow
.archetypes
.iter()
.filter(|&x| Q::Fetch::access(x).is_some())
.map(|x| x.len() as usize)
.sum()
}
}
struct ChunkIter<Q: Query> {
entities: NonNull<u32>,
fetch: Q::Fetch,
len: u32,
}
impl<Q: Query> ChunkIter<Q> {
#[inline]
unsafe fn next<'a>(&mut self) -> Option<(u32, <Q::Fetch as Fetch<'a>>::Item)> {
if self.len == 0 {
return None;
}
self.len -= 1;
let entity = self.entities.as_ptr();
self.entities = NonNull::new_unchecked(entity.add(1));
Some((*entity, self.fetch.next()))
}
}
pub struct BatchedIter<'q, 'w, Q: Query> {
borrow: &'q mut QueryBorrow<'w, Q>,
archetype_index: u32,
batch_size: u32,
batch: u32,
}
unsafe impl<'q, 'w, Q: Query> Send for BatchedIter<'q, 'w, Q> {}
unsafe impl<'q, 'w, Q: Query> Sync for BatchedIter<'q, 'w, Q> {}
impl<'q, 'w, Q: Query> Iterator for BatchedIter<'q, 'w, Q> {
type Item = Batch<'q, 'w, Q>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let archetype = self.borrow.archetypes.get(self.archetype_index as usize)?;
let offset = self.batch_size * self.batch;
if offset >= archetype.len() {
self.archetype_index += 1;
self.batch = 0;
continue;
}
if let Some(fetch) = unsafe { Q::Fetch::get(archetype, offset as usize) } {
self.batch += 1;
return Some(Batch {
_marker: PhantomData,
meta: self.borrow.meta,
state: ChunkIter {
entities: unsafe {
NonNull::new_unchecked(
archetype.entities().as_ptr().add(offset as usize),
)
},
fetch,
len: self.batch_size.min(archetype.len() - offset),
},
});
} else {
self.archetype_index += 1;
debug_assert_eq!(
self.batch, 0,
"query fetch should always reject at the first batch or not at all"
);
continue;
}
}
}
}
pub struct Batch<'q, 'w, Q: Query> {
_marker: PhantomData<&'q ()>,
meta: &'w [EntityMeta],
state: ChunkIter<Q>,
}
impl<'q, 'w, Q: Query> Iterator for Batch<'q, 'w, Q> {
type Item = (Entity, <Q::Fetch as Fetch<'q>>::Item);
fn next(&mut self) -> Option<Self::Item> {
let (id, components) = unsafe { self.state.next()? };
Some((
Entity {
id,
generation: self.meta[id as usize].generation,
},
components,
))
}
}
unsafe impl<'q, 'w, Q: Query> Send for Batch<'q, 'w, Q> {}
unsafe impl<'q, 'w, Q: Query> Sync for Batch<'q, 'w, Q> {}
macro_rules! tuple_impl {
($($name: ident),*) => {
impl<'a, $($name: Fetch<'a>),*> Fetch<'a> for ($($name,)*) {
type Item = ($($name::Item,)*);
#[allow(unused_variables, unused_mut)]
fn access(archetype: &Archetype) -> Option<Access> {
let mut access = Access::Iterate;
$(
access = access.max($name::access(archetype)?);
)*
Some(access)
}
#[allow(unused_variables)]
fn borrow(archetype: &Archetype) {
$($name::borrow(archetype);)*
}
#[allow(unused_variables)]
unsafe fn get(archetype: &'a Archetype, offset: usize) -> Option<Self> {
Some(($($name::get(archetype, offset)?,)*))
}
#[allow(unused_variables)]
fn release(archetype: &Archetype) {
$($name::release(archetype);)*
}
unsafe fn next(&mut self) -> Self::Item {
#[allow(non_snake_case)]
let ($($name,)*) = self;
($($name.next(),)*)
}
}
impl<$($name: Query),*> Query for ($($name,)*) {
type Fetch = ($($name::Fetch,)*);
}
};
}
smaller_tuples_too!(tuple_impl, O, N, M, L, K, J, I, H, G, F, E, D, C, B, A);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn access_order() {
assert!(Access::Write > Access::Read);
assert!(Access::Read > Access::Iterate);
assert!(Some(Access::Iterate) > None);
}
}