Implement query borrow and borrow mut for all &T and &mut T
This commit is contained in:
parent
92e284e625
commit
2e53156b0d
|
@ -8,6 +8,9 @@ mod component;
|
|||
mod query;
|
||||
mod component_info;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Position2d(i32, i32);
|
||||
|
||||
|
@ -28,10 +31,6 @@ fn main() {
|
|||
|
||||
println!("\nstart of querying!\n");
|
||||
|
||||
/* for pos in world.query::<Position2d>() {
|
||||
println!("Queried Position2d: {:?}", pos);
|
||||
} */
|
||||
|
||||
let mut q = world.query_better();
|
||||
q.with_component::<Position2d>();
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use std::{marker::PhantomData, any::TypeId, ptr::NonNull};
|
||||
|
||||
use super::{Fetch, Query};
|
||||
use crate::component::Component;
|
||||
|
||||
use super::{Fetch, Query, AsQuery, IntoQuery, DefaultQuery};
|
||||
|
||||
pub struct FetchBorrow<'a, T> {
|
||||
ptr: NonNull<u8>,
|
||||
|
@ -35,6 +37,15 @@ pub struct QueryBorrow<T> {
|
|||
_phantom: PhantomData<T>
|
||||
}
|
||||
|
||||
impl<T: 'static> QueryBorrow<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
type_id: TypeId::of::<T>(),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Query for QueryBorrow<T>
|
||||
where
|
||||
T: 'static
|
||||
|
@ -60,6 +71,26 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> AsQuery for QueryBorrow<T> {
|
||||
type Query = Self;
|
||||
}
|
||||
|
||||
impl<T: 'static> DefaultQuery for QueryBorrow<T> {
|
||||
fn default_query() -> Self {
|
||||
QueryBorrow::<T>::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> AsQuery for &T {
|
||||
type Query = QueryBorrow<T>;
|
||||
}
|
||||
|
||||
impl<T: 'static> DefaultQuery for &T {
|
||||
fn default_query() -> QueryBorrow<T> {
|
||||
QueryBorrow::<T>::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FetchBorrowMut<'a, T> {
|
||||
ptr: NonNull<u8>,
|
||||
size: usize,
|
||||
|
@ -93,6 +124,15 @@ pub struct QueryBorrowMut<T> {
|
|||
_phantom: PhantomData<T>
|
||||
}
|
||||
|
||||
impl<T: 'static> QueryBorrowMut<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
type_id: TypeId::of::<T>(),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Query for QueryBorrowMut<T>
|
||||
where
|
||||
T: 'static
|
||||
|
@ -118,29 +158,34 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> AsQuery for QueryBorrowMut<T> {
|
||||
type Query = Self;
|
||||
}
|
||||
|
||||
impl<T: 'static> DefaultQuery for QueryBorrowMut<T> {
|
||||
fn default_query() -> Self {
|
||||
QueryBorrowMut::<T>::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> AsQuery for &mut T {
|
||||
type Query = QueryBorrowMut<T>;
|
||||
}
|
||||
|
||||
impl<T: 'static> DefaultQuery for &mut T {
|
||||
fn default_query() -> QueryBorrowMut<T> {
|
||||
QueryBorrowMut::<T>::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::any::TypeId;
|
||||
|
||||
use crate::{world::World, archetype::Archetype, query::View};
|
||||
use crate::{world::World, archetype::Archetype, query::View, tests::Vec2};
|
||||
|
||||
use super::{QueryBorrow, QueryBorrowMut};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
struct Vec2 {
|
||||
x: f32,
|
||||
y: f32,
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
pub fn new(x: f32, y: f32) -> Self {
|
||||
Self {
|
||||
x,
|
||||
y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a world with two entities, one at Vec(10, 50) and
|
||||
/// the other at Vec2(25, 30).
|
||||
fn prepare_world() -> World {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{world::Entity, archetype::{Archetype, ArchetypeId}};
|
||||
|
||||
use super::{Fetch, Query};
|
||||
use super::{Fetch, Query, DefaultQuery, AsQuery};
|
||||
|
||||
pub struct EntitiesFetch<'a> {
|
||||
entities: &'a [Entity],
|
||||
|
@ -38,3 +38,13 @@ impl Query for Entities {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* impl AsQuery for Entities {
|
||||
type Query = Entities;
|
||||
}
|
||||
|
||||
impl DefaultQuery for Entities {
|
||||
fn default_query() -> Self {
|
||||
Entities
|
||||
}
|
||||
} */
|
|
@ -111,27 +111,26 @@ pub trait Query {
|
|||
unsafe fn fetch<'a>(&self, arch_id: ArchetypeId, archetype: &'a Archetype) -> Self::Fetch<'a>;
|
||||
}
|
||||
|
||||
/// A trait for getting the query of a type.
|
||||
pub trait AsQuery {
|
||||
/// The query for this type
|
||||
type Query: Query;
|
||||
}
|
||||
|
||||
pub trait IntoQuery {
|
||||
fn into_query(self) -> Self;
|
||||
}
|
||||
|
||||
pub trait DefaultQuery: AsQuery {
|
||||
fn default_query() -> Self::Query;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{world::World, archetype::Archetype};
|
||||
use crate::{world::World, archetype::Archetype, tests::Vec2};
|
||||
|
||||
use super::{View, Entities};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
struct Vec2 {
|
||||
x: f32,
|
||||
y: f32,
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
pub fn new(x: f32, y: f32) -> Self {
|
||||
Self {
|
||||
x,
|
||||
y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_view() {
|
||||
let mut world = World::new();
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
/// This source file includes some common things that tests are using.
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
|
||||
pub struct Vec2 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
pub fn new(x: f32, y: f32) -> Self {
|
||||
Self {
|
||||
x,
|
||||
y,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use std::{collections::{HashMap, VecDeque}, any::{Any, TypeId}};
|
||||
use std::slice::Iter;
|
||||
|
||||
use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component, query::QuerySimple};
|
||||
use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component, query::{QuerySimple, ViewIter, IntoQuery, View, AsQuery, DefaultQuery}};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct EntityId(pub u64);
|
||||
|
@ -109,11 +109,10 @@ impl World {
|
|||
archetype.get_entity_component(record.index)
|
||||
}
|
||||
|
||||
pub fn query<T: Component>(&self) -> impl Iterator<Item = &T> {
|
||||
self.archetypes
|
||||
.iter()
|
||||
.filter_map(|(_, a)| a.get_component_column::<T>())
|
||||
.flatten()
|
||||
pub fn view<'a, T: 'static + Component + DefaultQuery>(&'a self) -> ViewIter<'a, T::Query> {
|
||||
let archetypes = self.archetypes.values().collect();
|
||||
let v = View::new(T::default_query(), archetypes);
|
||||
v.into_iter()
|
||||
}
|
||||
|
||||
pub fn query_better(&self) -> QuerySimple {
|
||||
|
@ -131,12 +130,9 @@ impl World {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::World;
|
||||
use crate::tests::Vec2;
|
||||
|
||||
struct Vec2 {
|
||||
x: f32,
|
||||
y: f32,
|
||||
}
|
||||
use super::World;
|
||||
|
||||
#[test]
|
||||
fn spawning_entity() {
|
||||
|
@ -146,4 +142,28 @@ mod tests {
|
|||
y: 15.0,
|
||||
}, ));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_entities() {
|
||||
let mut world = World::new();
|
||||
world.spawn((Vec2 {
|
||||
x: 10.0,
|
||||
y: 15.0,
|
||||
}, ));
|
||||
world.spawn((Vec2 {
|
||||
x: 152.0,
|
||||
y: 3585.0,
|
||||
}, ));
|
||||
world.spawn((Vec2 {
|
||||
x: 235.0,
|
||||
y: 734.0,
|
||||
}, ));
|
||||
|
||||
let mut count = 0;
|
||||
for pos in world.view::<&Vec2>() {
|
||||
println!("Found entity at {:?}", pos);
|
||||
count += 1;
|
||||
}
|
||||
assert!(count == 3);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue