1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use atomic_refcell::AtomicRef;
use std::{any::type_name, marker::PhantomData, ops::Deref};
use crate::{access::*, borrow::ComponentBorrow, Error, Result};
use crate::{GenericWorld, QueryOne};
use hecs::{Component, Entity, Query, QueryBorrow, World};
pub type SubWorld<'a, T> = SubWorldRaw<AtomicRef<'a, World>, T>;
pub type SubWorldRefCell<'a, T> = SubWorldRaw<std::cell::Ref<'a, World>, T>;
pub type SubWorldRef<'a, T> = SubWorldRaw<&'a World, T>;
pub type EmptyWorld<'a> = SubWorldRef<'a, ()>;
pub struct SubWorldRaw<A, T> {
pub(crate) world: A,
marker: PhantomData<T>,
}
impl<A, T> SubWorldRaw<A, T> {
pub fn new(world: A) -> Self {
Self {
world,
marker: PhantomData,
}
}
}
impl<A, T: ComponentBorrow> SubWorldRaw<A, T> {
pub fn has<U: IntoAccess>(&self) -> bool {
T::has::<U>()
}
pub fn has_all<U: Subset>(&self) -> bool {
U::is_subset::<T>()
}
}
impl<'w, A: 'w + Deref<Target = World>, T: ComponentBorrow> SubWorldRaw<A, T> {
pub fn query<Q: Query + Subset>(&self) -> QueryBorrow<'_, Q> {
self.try_query()
.expect("Failed to execute query on subworld")
}
pub fn query_one<Q: Query + Subset>(&'w self, entity: Entity) -> Result<QueryOne<'w, Q>> {
if !self.has_all::<Q>() {
return Err(Error::IncompatibleSubworld {
subworld: type_name::<T>(),
query: type_name::<Q>(),
});
}
let query = self
.world
.query_one(entity)
.map_err(|_| Error::NoSuchEntity(entity))?;
Ok(QueryOne::new(entity, query))
}
pub fn get<C: Component>(&self, entity: Entity) -> Result<hecs::Ref<C>> {
if !self.has::<&C>() {
return Err(Error::IncompatibleSubworld {
subworld: type_name::<T>(),
query: type_name::<&C>(),
});
}
match self.world.get(entity) {
Ok(val) => Ok(val),
Err(hecs::ComponentError::NoSuchEntity) => Err(Error::NoSuchEntity(entity)),
Err(hecs::ComponentError::MissingComponent(name)) => {
Err(Error::MissingComponent(entity, name))
}
}
}
pub fn get_mut<C: Component>(&self, entity: Entity) -> Result<hecs::RefMut<C>> {
if !self.has::<&C>() {
return Err(Error::IncompatibleSubworld {
subworld: type_name::<T>(),
query: type_name::<&C>(),
});
}
match self.world.get_mut(entity) {
Ok(val) => Ok(val),
Err(hecs::ComponentError::NoSuchEntity) => Err(Error::NoSuchEntity(entity)),
Err(hecs::ComponentError::MissingComponent(name)) => {
Err(Error::MissingComponent(entity, name))
}
}
}
pub fn reserve_entities(&self, count: u32) -> impl Iterator<Item = Entity> + '_ {
self.world.reserve_entities(count)
}
pub fn query_par<Q: Query + Subset>(&self) -> QueryBorrow<'_, Q> {
self.try_query()
.expect("Failed to execute query on subworld")
}
}