Rename World::view to World::view_iter, some code cleanup

This commit is contained in:
SeanOMik 2023-12-09 18:55:57 -05:00
parent a68b0a7fb4
commit da206b4824
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
5 changed files with 26 additions and 40 deletions

View File

@ -5,18 +5,16 @@ use crate::{Fetch, world::World, Query, ComponentColumn, ComponentInfo};
pub mod view;
pub use view::*;
#[derive(Clone, Copy, Hash)]
enum TypeId {
Rust(std::any::TypeId),
Unknown(u128),
}
/// Data that rust does not know the type of
pub struct DynamicType {
pub info: ComponentInfo,
pub ptr: NonNull<u8>,
}
/// A struct that fetches some dynamic type.
///
/// Currently it can only fetch from archetypes, later it will be able to fetch dynamic
/// resources as well. Its meant to be a single Fetcher for all dynamic types.
pub struct FetchDynamicType<'a> {
col: &'a ComponentColumn,
info: ComponentInfo,
@ -41,10 +39,6 @@ impl<'a> Fetch<'a> for FetchDynamicType<'a> {
}
}
pub trait DynamicQuery: Query {
}
/// A query for receiving a dynamic type.
///
/// There are no Ref or RefMut variants since this fetches the pointer to the component.
@ -59,24 +53,12 @@ impl QueryDynamicType {
info,
}
}
}
impl Query for QueryDynamicType {
type Item<'a> = DynamicType;
type Fetch<'a> = FetchDynamicType<'a>;
const ALWAYS_FETCHES: bool = false;
fn new() -> Self {
panic!("QueryDynamicType does not implement QueryDefault, this should not have been called")
}
fn can_visit_archetype(&self, archetype: &crate::archetype::Archetype) -> bool {
archetype.has_column(self.info.type_id)
}
unsafe fn fetch<'a>(&self, _world: &'a World, _arch_id: crate::archetype::ArchetypeId, archetype: &'a crate::archetype::Archetype) -> Self::Fetch<'a> {
unsafe fn fetch<'a>(&self, _world: &'a World, _arch_id: crate::archetype::ArchetypeId, archetype: &'a crate::archetype::Archetype) -> FetchDynamicType<'a> {
let col = archetype.columns.iter().find(|c| c.info.type_id == self.info.type_id)
.expect("You ignored 'can_visit_archetype'!");

View File

@ -1,8 +1,8 @@
use std::ops::Range;
use crate::{world::World, Archetype, Query, ArchetypeEntityId, Fetch, ArchetypeId};
use crate::{world::World, Archetype, ArchetypeEntityId, Fetch, ArchetypeId};
use super::{DynamicQuery, QueryDynamicType, FetchDynamicType};
use super::{QueryDynamicType, FetchDynamicType, DynamicType};
pub struct DynamicView<'a> {
world: &'a World,
@ -23,7 +23,7 @@ impl<'a> DynamicView<'a> {
}
impl<'a> IntoIterator for DynamicView<'a> {
type Item = Vec<<QueryDynamicType as Query>::Item<'a>>;
type Item = Vec<DynamicType>;
type IntoIter = DynamicViewIter<'a>;
@ -34,7 +34,7 @@ impl<'a> IntoIterator for DynamicView<'a> {
world: self.world,
queries: self.queries,
fetchers: Vec::new(),
archetypes: archetypes,
archetypes,
next_archetype: 0,
component_indices: 0..0,
}
@ -51,7 +51,7 @@ pub struct DynamicViewIter<'a> {
}
impl<'a> Iterator for DynamicViewIter<'a> {
type Item = Vec<<QueryDynamicType as Query>::Item<'a>>;
type Item = Vec<DynamicType>;
fn next(&mut self) -> Option<Self::Item> {
loop {
@ -104,7 +104,7 @@ impl<'a> Iterator for DynamicViewIter<'a> {
mod tests {
use std::{alloc::Layout, ptr::NonNull};
use crate::{world::World, MemoryLayout, ComponentInfo, DynTypeId, DynamicBundle, dynamic::{DynamicQuery, QueryDynamicType}};
use crate::{world::World, MemoryLayout, ComponentInfo, DynTypeId, DynamicBundle, dynamic::QueryDynamicType};
use super::DynamicView;

View File

@ -167,7 +167,7 @@ mod tests {
println!("Added resource");
}
let mut res_iter = world.view::<QueryResource<SomeCounter>>();
let mut res_iter = world.view_iter::<QueryResource<SomeCounter>>();
let res = res_iter.next().unwrap();
assert_eq!(res.0, 0);
}
@ -187,16 +187,16 @@ mod tests {
println!("Added resource");
}
let i = world.view::<(QueryResource<SomeCounter>, &Vec2)>();
let i = world.view_iter::<(QueryResource<SomeCounter>, &Vec2)>();
assert_eq!(i.count(), 3);
let i = world.view::<(&Vec2, QueryResource<SomeCounter>)>();
let i = world.view_iter::<(&Vec2, QueryResource<SomeCounter>)>();
assert_eq!(i.count(), 3);
for (res, e) in world.view::<(QueryResource<SomeCounter>, &Vec2)>() {
for (res, e) in world.view_iter::<(QueryResource<SomeCounter>, &Vec2)>() {
println!("Got res {}! and entity at {:?}", res.0, e);
}
let i = world.view::<QueryResource<SomeCounter>>();
let i = world.view_iter::<QueryResource<SomeCounter>>();
assert_eq!(i.count(), 1);
}
@ -210,13 +210,13 @@ mod tests {
}
{
let mut resmut_iter = world.view::<QueryResourceMut<SomeCounter>>();
let mut resmut_iter = world.view_iter::<QueryResourceMut<SomeCounter>>();
let mut resmut = resmut_iter.next().unwrap();
assert_eq!(resmut.0, 0);
resmut.0 += 20;
}
let mut res_iter = world.view::<QueryResource<SomeCounter>>();
let mut res_iter = world.view_iter::<QueryResource<SomeCounter>>();
let res = res_iter.next().unwrap();
assert_eq!(res.0, 20);
}

View File

@ -165,7 +165,7 @@ mod tests {
world.spawn(comps[0]);
world.spawn(comps[1]);
for (pos2, pos3) in world.view::<(&Vec2, &Vec3)>() {
for (pos2, pos3) in world.view_iter::<(&Vec2, &Vec3)>() {
println!("Entity at {:?} and {:?}", pos2, pos3);
assert!(comps.contains(&( pos2.clone(), pos3.clone() )));
}

View File

@ -1,6 +1,6 @@
use std::{collections::{HashMap, VecDeque}, any::TypeId, cell::{Ref, RefMut}};
use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component, query::{ViewIter, View}, resource::ResourceData, Query, AsQuery};
use crate::{archetype::{ArchetypeId, Archetype}, bundle::Bundle, component::Component, query::{ViewIter, View}, resource::ResourceData, Query, AsQuery, dynamic::{DynamicViewIter, QueryDynamicType, DynamicView}};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct EntityId(pub u64);
@ -115,12 +115,16 @@ impl World {
}
}
pub fn view<'a, T: 'static + Component + AsQuery>(&'a self) -> ViewIter<'a, T::Query> {
pub fn view_iter<'a, T: 'static + Component + AsQuery>(&'a self) -> ViewIter<'a, T::Query> {
let archetypes = self.archetypes.values().collect();
let v = View::new(self, T::Query::new(), archetypes);
v.into_iter()
}
pub fn dynamic_view(&self) -> DynamicView {
DynamicView::new(self)
}
pub fn add_resource<T: 'static>(&mut self, data: T) {
self.resources.insert(TypeId::of::<T>(), ResourceData::new(data));
}
@ -196,7 +200,7 @@ mod tests {
}, ));
let mut count = 0;
for pos in world.view::<&Vec2>() {
for pos in world.view_iter::<&Vec2>() {
println!("Found entity at {:?}", pos);
count += 1;
}