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
use super::*;
impl World {
pub(crate) fn archetype_epoch(&self) -> usize {
self.archetype_epoch
}
pub(crate) fn storage_epoch(&self) -> u64 {
self.storage_epoch
}
pub(crate) fn cache_token(&self) -> &Arc<()> {
&self.cache_token
}
pub(crate) fn query_snapshot<Q, Flt>(&self) -> Arc<Vec<CachedArchetype>>
where
Q: QuerySpec + 'static,
Flt: QueryFilter + 'static,
{
self.query_cache.snapshot::<Q, Flt>(self)
}
pub(crate) fn query_parallel_snapshot<Q, Flt>(
&self,
archetypes: &[CachedArchetype],
) -> ParallelJobSnapshot
where
Q: QuerySpec + 'static,
Flt: QueryFilter + 'static,
{
self.query_cache
.parallel_snapshot::<Q, Flt>(self, archetypes)
}
#[cfg(test)]
pub(crate) fn query_cache_len(&self) -> usize {
self.query_cache.len()
}
#[cfg(test)]
pub(crate) fn query_parallel_rebuild_count<Q, Flt>(&self) -> usize
where
Q: QuerySpec + 'static,
Flt: QueryFilter + 'static,
{
self.query_cache.parallel_rebuild_count::<Q, Flt>()
}
/// Creates a read-only query bound to this world.
///
/// The query parameter `Q` is usually a shared reference (`&T`), an
/// optional shared reference (`Option<&T>`), or a tuple of those.
/// Type-level filters can be attached with [`Query::filter`].
///
/// # Examples
///
/// ```
/// # use sky_ecs::World;
/// # #[derive(Clone, Copy)] struct Pos { x: f32, y: f32 }
/// # #[derive(Clone, Copy)] struct Vel { x: f32, y: f32 }
/// # let mut world = World::new();
/// # world.spawn((Pos { x: 0.0, y: 0.0 }, Vel { x: 1.0, y: 0.0 }));
/// let query = world.query::<(&Pos, &Vel)>();
/// query.for_each(|(pos, vel)| {
/// let _ = (pos.x, vel.x);
/// });
/// ```
///
/// Mutable parameters use [`World::query_mut`] instead:
///
/// ```compile_fail
/// # use sky_ecs::World;
/// # #[derive(Clone, Copy)] struct Pos { x: f32, y: f32 }
/// # let world = World::new();
/// let _query = world.query::<&mut Pos>();
/// ```
pub fn query<Q>(&self) -> Query<'_, Q>
where
Q: ReadOnlyQuerySpec + 'static,
{
Query::new(self)
}
/// Creates a query with exclusive access to this world.
///
/// Mutable query parameters require this entry point. Read-only
/// parameters may be mixed into the same query.
///
/// ```
/// # use sky_ecs::World;
/// # #[derive(Clone, Copy)] struct Pos { x: f32, y: f32 }
/// # #[derive(Clone, Copy)] struct Vel { x: f32, y: f32 }
/// # let mut world = World::new();
/// # world.spawn((Pos { x: 0.0, y: 0.0 }, Vel { x: 1.0, y: 0.0 }));
/// let mut query = world.query_mut::<(&mut Pos, &Vel)>();
/// query.for_each(|(pos, vel)| {
/// pos.x += vel.x;
/// });
/// ```
pub fn query_mut<Q>(&mut self) -> QueryMut<'_, Q>
where
Q: QuerySpec + 'static,
{
QueryMut::new(self)
}
}