2023-12-28 03:53:58 +00:00
|
|
|
use std::{marker::PhantomData, ptr::NonNull, cell::{Ref, RefMut}};
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2024-03-03 02:34:20 +00:00
|
|
|
use crate::{World, ComponentColumn, DynTypeId, Tick, Component};
|
2023-11-30 04:21:27 +00:00
|
|
|
|
2023-12-04 04:14:27 +00:00
|
|
|
use super::{Fetch, Query, AsQuery};
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2023-11-27 02:05:35 +00:00
|
|
|
/// Fetcher for borrowing components from archetypes.
|
2023-11-25 23:43:11 +00:00
|
|
|
pub struct FetchBorrow<'a, T> {
|
2023-12-01 21:50:41 +00:00
|
|
|
col: &'a ComponentColumn,
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: PhantomData<&'a T>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Fetch<'a> for FetchBorrow<'a, T>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
{
|
2023-12-01 21:50:41 +00:00
|
|
|
type Item = Ref<'a, T>;
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2023-11-26 05:56:30 +00:00
|
|
|
fn dangling() -> Self {
|
2023-12-01 21:50:41 +00:00
|
|
|
unreachable!()
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_item(&mut self, entity: crate::world::ArchetypeEntityId) -> Self::Item {
|
2024-03-03 02:34:20 +00:00
|
|
|
self.col.get(entity.0 as _)
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 02:05:35 +00:00
|
|
|
/// A Query for borrowing components from archetypes.
|
|
|
|
///
|
|
|
|
/// Since [`AsQuery`] is implemented for `&T`, you can use this query like this:
|
2023-12-01 04:05:06 +00:00
|
|
|
/// ```nobuild
|
2023-11-27 02:05:35 +00:00
|
|
|
/// for ts in world.view::<&T>() {
|
2023-12-01 04:05:06 +00:00
|
|
|
/// println!("Got a &T!");
|
2023-11-27 02:05:35 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2023-11-25 23:43:11 +00:00
|
|
|
pub struct QueryBorrow<T> {
|
2023-12-09 16:40:41 +00:00
|
|
|
type_id: DynTypeId,
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: PhantomData<T>
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
impl<T: 'static> Default for QueryBorrow<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
type_id: DynTypeId::of::<T>(),
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 04:14:27 +00:00
|
|
|
impl<T> Copy for QueryBorrow<T> {}
|
|
|
|
|
|
|
|
impl<T> Clone for QueryBorrow<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 00:58:14 +00:00
|
|
|
impl<T: 'static> QueryBorrow<T> {
|
|
|
|
pub fn new() -> Self {
|
2023-12-28 03:53:58 +00:00
|
|
|
Self::default()
|
2023-11-26 00:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:43:11 +00:00
|
|
|
impl<T> Query for QueryBorrow<T>
|
|
|
|
where
|
|
|
|
T: 'static
|
|
|
|
{
|
2023-12-01 21:50:41 +00:00
|
|
|
type Item<'a> = Ref<'a, T>;
|
2023-11-25 23:43:11 +00:00
|
|
|
|
|
|
|
type Fetch<'a> = FetchBorrow<'a, T>;
|
|
|
|
|
2023-12-04 04:14:27 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
QueryBorrow::<T>::new()
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:43:11 +00:00
|
|
|
fn can_visit_archetype(&self, archetype: &crate::archetype::Archetype) -> bool {
|
2024-03-03 02:34:20 +00:00
|
|
|
archetype.has_column(self.type_id)
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 19:12:53 +00:00
|
|
|
unsafe fn fetch<'a>(&self, _world: &'a World, archetype: &'a crate::archetype::Archetype, tick: crate::Tick) -> Self::Fetch<'a> {
|
2023-12-28 03:53:58 +00:00
|
|
|
let _ = tick;
|
2024-03-03 02:34:20 +00:00
|
|
|
let col = archetype.get_column(self.type_id)
|
2023-11-25 23:43:11 +00:00
|
|
|
.expect("You ignored 'can_visit_archetype'!");
|
|
|
|
|
|
|
|
FetchBorrow {
|
2023-12-01 21:50:41 +00:00
|
|
|
col,
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 01:49:27 +00:00
|
|
|
impl<T: Component> AsQuery for QueryBorrow<T> {
|
2023-11-26 00:58:14 +00:00
|
|
|
type Query = Self;
|
|
|
|
}
|
|
|
|
|
2024-01-05 01:49:27 +00:00
|
|
|
impl<T: Component> AsQuery for &T {
|
2023-11-26 00:58:14 +00:00
|
|
|
type Query = QueryBorrow<T>;
|
|
|
|
}
|
|
|
|
|
2023-11-27 02:05:35 +00:00
|
|
|
/// A fetcher for mutably borrowing components from archetypes.
|
2023-11-25 23:43:11 +00:00
|
|
|
pub struct FetchBorrowMut<'a, T> {
|
2023-12-26 19:12:53 +00:00
|
|
|
col: NonNull<ComponentColumn>,
|
|
|
|
tick: Tick,
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: PhantomData<&'a T>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Fetch<'a> for FetchBorrowMut<'a, T>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
{
|
2023-12-01 21:50:41 +00:00
|
|
|
type Item = RefMut<'a, T>;
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2023-11-26 05:56:30 +00:00
|
|
|
fn dangling() -> Self {
|
2023-12-01 21:50:41 +00:00
|
|
|
unreachable!()
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_item(&mut self, entity: crate::world::ArchetypeEntityId) -> Self::Item {
|
2023-12-26 19:12:53 +00:00
|
|
|
let col = unsafe { self.col.as_mut() };
|
2024-03-03 02:34:20 +00:00
|
|
|
col.get_mut(entity.0 as _, &self.tick)
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 02:05:35 +00:00
|
|
|
/// A Query for mutably borrowing components from archetypes.
|
|
|
|
///
|
|
|
|
/// Since [`AsQuery`] is implemented for `&mut T`, you can use this query like this:
|
2023-12-01 04:05:06 +00:00
|
|
|
/// ```nobuild
|
2023-11-27 02:05:35 +00:00
|
|
|
/// for ts in world.view::<&mut T>() {
|
2023-12-01 04:05:06 +00:00
|
|
|
/// println!("Got an &mut T!");
|
2023-11-27 02:05:35 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2023-11-25 23:43:11 +00:00
|
|
|
pub struct QueryBorrowMut<T> {
|
2023-12-09 16:40:41 +00:00
|
|
|
type_id: DynTypeId,
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: PhantomData<T>
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
impl<T: 'static> Default for QueryBorrowMut<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
type_id: DynTypeId::of::<T>(),
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 04:14:27 +00:00
|
|
|
impl<T> Copy for QueryBorrowMut<T> {}
|
|
|
|
|
|
|
|
impl<T> Clone for QueryBorrowMut<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 00:58:14 +00:00
|
|
|
impl<T: 'static> QueryBorrowMut<T> {
|
|
|
|
pub fn new() -> Self {
|
2023-12-28 03:53:58 +00:00
|
|
|
Self::default()
|
2023-11-26 00:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:43:11 +00:00
|
|
|
impl<T> Query for QueryBorrowMut<T>
|
|
|
|
where
|
|
|
|
T: 'static
|
|
|
|
{
|
2023-12-01 21:50:41 +00:00
|
|
|
type Item<'a> = RefMut<'a, T>;
|
2023-11-25 23:43:11 +00:00
|
|
|
|
|
|
|
type Fetch<'a> = FetchBorrowMut<'a, T>;
|
|
|
|
|
2023-12-26 19:12:53 +00:00
|
|
|
const MUTATES: bool = true;
|
|
|
|
|
2023-12-04 04:14:27 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
QueryBorrowMut::<T>::new()
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:43:11 +00:00
|
|
|
fn can_visit_archetype(&self, archetype: &crate::archetype::Archetype) -> bool {
|
2024-03-03 02:34:20 +00:00
|
|
|
archetype.has_column(self.type_id)
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 19:12:53 +00:00
|
|
|
unsafe fn fetch<'a>(&self, _world: &'a World, archetype: &'a crate::archetype::Archetype, tick: crate::Tick) -> Self::Fetch<'a> {
|
2024-03-03 02:34:20 +00:00
|
|
|
let col = archetype.get_column(self.type_id)
|
2023-11-25 23:43:11 +00:00
|
|
|
.expect("You ignored 'can_visit_archetype'!");
|
2024-03-08 05:19:23 +00:00
|
|
|
|
2023-12-26 19:12:53 +00:00
|
|
|
// TODO: find a way to get the component column mutable with a borrowed archetype so its tick can be updated.
|
|
|
|
// the fetcher needs to tick the entities tick in the archetype
|
2024-03-08 05:19:23 +00:00
|
|
|
let col = NonNull::from(col);
|
2023-11-25 23:43:11 +00:00
|
|
|
|
|
|
|
FetchBorrowMut {
|
2023-12-28 03:53:58 +00:00
|
|
|
col,
|
2023-12-26 19:12:53 +00:00
|
|
|
tick,
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 01:49:27 +00:00
|
|
|
impl<T: Component> AsQuery for QueryBorrowMut<T> {
|
2023-11-26 00:58:14 +00:00
|
|
|
type Query = Self;
|
|
|
|
}
|
|
|
|
|
2024-01-05 01:49:27 +00:00
|
|
|
impl<T: Component> AsQuery for &mut T {
|
2023-11-26 00:58:14 +00:00
|
|
|
type Query = QueryBorrowMut<T>;
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:43:11 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-03-03 02:34:20 +00:00
|
|
|
use std::{marker::PhantomData, ptr::NonNull};
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2024-03-03 02:34:20 +00:00
|
|
|
use crate::{archetype::{Archetype, ArchetypeId}, bundle::Bundle, query::{Fetch, ViewState}, tests::Vec2, World, DynTypeId, Entity, EntityId, Tick};
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2023-11-28 03:24:29 +00:00
|
|
|
use super::{QueryBorrow, QueryBorrowMut, FetchBorrowMut};
|
2023-11-25 23:43:11 +00:00
|
|
|
|
|
|
|
/// Creates a world with two entities, one at Vec(10, 50) and
|
|
|
|
/// the other at Vec2(25, 30).
|
|
|
|
fn prepare_world() -> World {
|
|
|
|
let mut world = World::new();
|
|
|
|
|
|
|
|
world.spawn((Vec2::new(10.0, 50.0),));
|
|
|
|
world.spawn((Vec2::new(25.0, 30.0),));
|
|
|
|
world
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn borrow_query() {
|
|
|
|
let world = prepare_world();
|
|
|
|
|
|
|
|
let borrow = QueryBorrow::<Vec2> {
|
2023-12-09 16:40:41 +00:00
|
|
|
type_id: DynTypeId::of::<Vec2>(),
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: std::marker::PhantomData,
|
|
|
|
};
|
|
|
|
let archetypes: Vec<&Archetype> = world.archetypes.values().collect();
|
2024-03-03 01:20:38 +00:00
|
|
|
let v = ViewState::<QueryBorrow<Vec2>, ()>::new(&world, borrow, (), archetypes);
|
2023-11-25 23:43:11 +00:00
|
|
|
|
|
|
|
for e in v.into_iter() {
|
|
|
|
println!("Found entity at {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn borrow_mut_query() {
|
|
|
|
let world = prepare_world();
|
|
|
|
|
|
|
|
let borrow = QueryBorrowMut::<Vec2> {
|
2023-12-09 16:40:41 +00:00
|
|
|
type_id: DynTypeId::of::<Vec2>(),
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: std::marker::PhantomData,
|
|
|
|
};
|
|
|
|
let archetypes: Vec<&Archetype> = world.archetypes.values().collect();
|
2024-03-03 01:20:38 +00:00
|
|
|
let v = ViewState::<QueryBorrowMut<Vec2>, ()>::new(&world, borrow, (), archetypes);
|
2023-11-25 23:43:11 +00:00
|
|
|
|
|
|
|
let mut orig = vec![];
|
|
|
|
|
2023-12-01 21:50:41 +00:00
|
|
|
for mut v in v.into_iter() {
|
2023-11-25 23:43:11 +00:00
|
|
|
orig.push(v.clone());
|
|
|
|
v.x += 10.0;
|
|
|
|
v.y += 10.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure the changes were actually made
|
|
|
|
|
|
|
|
let borrow = QueryBorrow::<Vec2> {
|
2023-12-09 16:40:41 +00:00
|
|
|
type_id: DynTypeId::of::<Vec2>(),
|
2023-11-25 23:43:11 +00:00
|
|
|
_phantom: std::marker::PhantomData,
|
|
|
|
};
|
|
|
|
let archetypes: Vec<&Archetype> = world.archetypes.values().collect();
|
2024-03-03 01:20:38 +00:00
|
|
|
let v = ViewState::<QueryBorrow<Vec2>, ()>::new(&world, borrow, (), archetypes);
|
2023-11-25 23:43:11 +00:00
|
|
|
|
|
|
|
for (new, orig) in v.into_iter().zip(orig.iter()) {
|
|
|
|
assert!(new.x - orig.x == 10.0);
|
|
|
|
assert!(new.y - orig.y == 10.0);
|
|
|
|
}
|
|
|
|
println!("They were modified!");
|
|
|
|
}
|
2023-11-28 03:24:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn only_one_mutable_borrow() {
|
|
|
|
let info = (Vec2::new(0.0, 0.0),).info();
|
|
|
|
let mut a = Archetype::from_bundle_info(ArchetypeId(0), info);
|
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
a.add_entity(Entity {
|
|
|
|
id: EntityId(i),
|
|
|
|
generation: 0,
|
2023-12-26 19:12:53 +00:00
|
|
|
}, (Vec2::rand(),), &Tick::default());
|
2023-11-28 03:24:29 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 02:34:20 +00:00
|
|
|
let col = a.get_column(DynTypeId::of::<Vec2>()).unwrap();
|
2023-11-28 03:24:29 +00:00
|
|
|
|
2023-12-01 21:50:41 +00:00
|
|
|
let mut bmut = FetchBorrowMut::<Vec2> {
|
2023-12-26 19:12:53 +00:00
|
|
|
col: NonNull::from(col),
|
|
|
|
tick: Tick::default(),
|
2023-11-28 03:24:29 +00:00
|
|
|
_phantom: PhantomData,
|
|
|
|
};
|
2023-12-01 21:50:41 +00:00
|
|
|
let item = unsafe { bmut.get_item(crate::ArchetypeEntityId(0)) };
|
|
|
|
|
|
|
|
// ensure only one mutable borrow can be done at a time
|
2023-11-28 03:24:29 +00:00
|
|
|
assert!(col.try_borrow_mut_ptr().is_err());
|
2023-12-01 21:50:41 +00:00
|
|
|
drop(item);
|
2023-11-28 03:24:29 +00:00
|
|
|
assert!(col.try_borrow_mut_ptr().is_ok());
|
|
|
|
}
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|