Create QueryResourceMut
This commit is contained in:
parent
068eeecd4c
commit
c3c1e81913
|
@ -1,4 +1,4 @@
|
||||||
use std::{marker::PhantomData, any::TypeId, ptr::NonNull, cell::Ref};
|
use std::{marker::PhantomData, any::TypeId, ptr::NonNull, cell::{Ref, RefMut}};
|
||||||
|
|
||||||
use crate::{world::World, resource::ResourceObject};
|
use crate::{world::World, resource::ResourceObject};
|
||||||
|
|
||||||
|
@ -30,13 +30,11 @@ impl<'a, T: 'a + 'static> Fetch<'a> for FetchResource<'a, T> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A query type for getting Resources
|
/// A query type for borrowing Resources.
|
||||||
///
|
|
||||||
/// Resources are stored in an archetype with the id of [`world::RESOURCE_ARCHETYPE_ID`]. There is only one instance of a type of resource.
|
|
||||||
/// The resources are stored in that archetype in the first entity in the column.
|
|
||||||
pub struct QueryResource<T: ResourceObject> {
|
pub struct QueryResource<T: ResourceObject> {
|
||||||
_phantom: PhantomData<T>
|
_phantom: PhantomData<T>
|
||||||
}
|
}
|
||||||
|
pub type Resource<T> = QueryResource<T>;
|
||||||
|
|
||||||
impl<T: ResourceObject> Copy for QueryResource<T> {}
|
impl<T: ResourceObject> Copy for QueryResource<T> {}
|
||||||
|
|
||||||
|
@ -46,8 +44,6 @@ impl<T: ResourceObject> Clone for QueryResource<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Resource<T> = QueryResource<T>;
|
|
||||||
|
|
||||||
impl<T: ResourceObject + 'static> Query for QueryResource<T> {
|
impl<T: ResourceObject + 'static> Query for QueryResource<T> {
|
||||||
type Item<'a> = Ref<'a, T>;
|
type Item<'a> = Ref<'a, T>;
|
||||||
|
|
||||||
|
@ -80,9 +76,83 @@ impl<T: ResourceObject + 'static> Query for QueryResource<T> {
|
||||||
impl<R: ResourceObject> AsQuery for QueryResource<R> {
|
impl<R: ResourceObject> AsQuery for QueryResource<R> {
|
||||||
type Query = QueryResource<R>;
|
type Query = QueryResource<R>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct FetchResourceMut<'a, T> {
|
||||||
|
world: Option<&'a World>,
|
||||||
|
_phantom: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: 'a + 'static> Fetch<'a> for FetchResourceMut<'a, T> {
|
||||||
|
type Item = RefMut<'a, T>;
|
||||||
|
|
||||||
|
fn dangling() -> Self {
|
||||||
|
Self {
|
||||||
|
world: None,
|
||||||
|
_phantom: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn can_visit_item(&mut self, _entity: crate::world::ArchetypeEntityId) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn get_item(&mut self, _entity: crate::world::ArchetypeEntityId) -> Self::Item {
|
||||||
|
let w = self.world.unwrap();
|
||||||
|
w.get_resource_mut::<T>()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A query type for borrowing Resources mutably.
|
||||||
|
pub struct QueryResourceMut<T: ResourceObject> {
|
||||||
|
_phantom: PhantomData<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type ResourceMut<T> = QueryResourceMut<T>;
|
||||||
|
|
||||||
|
impl<T: ResourceObject> Copy for QueryResourceMut<T> {}
|
||||||
|
|
||||||
|
impl<T: ResourceObject> Clone for QueryResourceMut<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ResourceObject + 'static> Query for QueryResourceMut<T> {
|
||||||
|
type Item<'a> = RefMut<'a, T>;
|
||||||
|
|
||||||
|
type Fetch<'a> = FetchResourceMut<'a, T>;
|
||||||
|
|
||||||
|
const ALWAYS_FETCHES: bool = true;
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
QueryResourceMut::<T> {
|
||||||
|
_phantom: PhantomData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn can_visit_archetype(&self, _archetype: &crate::archetype::Archetype) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn fetch<'a>(&self, world: &'a World, _arch_id: crate::archetype::ArchetypeId, _archetype: &'a crate::archetype::Archetype) -> Self::Fetch<'a> {
|
||||||
|
self.fetch_world(world).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn fetch_world<'a>(&self, world: &'a World) -> Option<Self::Fetch<'a>> {
|
||||||
|
Some(FetchResourceMut {
|
||||||
|
world: Some(world),
|
||||||
|
_phantom: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: ResourceObject> AsQuery for QueryResourceMut<R> {
|
||||||
|
type Query = QueryResourceMut<R>;
|
||||||
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{world::World, tests::{Vec2, Vec3}};
|
use crate::{world::World, tests::{Vec2, Vec3}, QueryResourceMut};
|
||||||
|
|
||||||
use super::QueryResource;
|
use super::QueryResource;
|
||||||
|
|
||||||
|
@ -129,4 +199,25 @@ mod tests {
|
||||||
let i = world.view::<QueryResource<SomeCounter>>();
|
let i = world.view::<QueryResource<SomeCounter>>();
|
||||||
assert_eq!(i.count(), 1);
|
assert_eq!(i.count(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mutate_query() {
|
||||||
|
let mut world = World::new();
|
||||||
|
{
|
||||||
|
let counter = SomeCounter(0);
|
||||||
|
world.add_resource(counter);
|
||||||
|
println!("Added resource");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut resmut_iter = world.view::<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 res = res_iter.next().unwrap();
|
||||||
|
assert_eq!(res.0, 20);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue