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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
use std::any::type_name;
use std::marker::PhantomData;
use crate::World;
use crate::component::{
Component,
Handle as ComponentHandle,
HandleMut as ComponentHandleMut,
};
use crate::entity::Handle as EntityHandle;
use crate::query::{
Term,
TermsBuilder,
TermsBuilderInterface,
};
use crate::tuple::Tuple;
use crate::uid::With as WithUid;
pub struct With<WithUidT>
where
WithUidT: WithUid,
{
_pd: PhantomData<WithUidT>,
}
impl<'world, WithUidT> Term<'world> for With<WithUidT>
where
WithUidT: WithUid,
{
type AddField<Fields: Tuple> = Fields;
fn apply_to_terms_builder<const MAX_TERM_CNT: usize>(
terms_builder: &mut TermsBuilder<MAX_TERM_CNT>,
)
{
terms_builder.present([WithUidT::uid()]);
}
fn add_field<Fields: Tuple>(
_entity_handle: &EntityHandle<'world>,
_world: &'world World,
fields: Fields,
) -> Self::AddField<Fields>
{
fields
}
}
pub struct Without<WithUidT>
where
WithUidT: WithUid,
{
_pd: PhantomData<WithUidT>,
}
impl<'world, WithUidT> Term<'world> for Without<WithUidT>
where
WithUidT: WithUid,
{
type AddField<Fields: Tuple> = Fields;
fn apply_to_terms_builder<const MAX_TERM_CNT: usize>(
terms_builder: &mut TermsBuilder<MAX_TERM_CNT>,
)
{
terms_builder.absent([WithUidT::uid()]);
}
fn add_field<Fields: Tuple>(
_entity_handle: &EntityHandle<'world>,
_world: &'world World,
fields: Fields,
) -> Self::AddField<Fields>
{
fields
}
}
impl<'world, ComponentT: Component> Term<'world> for Option<&ComponentT>
{
type AddField<Fields: Tuple> =
Fields::WithElementAtEnd<Option<ComponentHandle<'world, ComponentT>>>;
fn apply_to_terms_builder<const MAX_TERM_CNT: usize>(
_terms_builder: &mut TermsBuilder<MAX_TERM_CNT>,
)
{
}
fn add_field<Fields: Tuple>(
entity_handle: &EntityHandle<'world>,
_world: &'world World,
fields: Fields,
) -> Self::AddField<Fields>
{
let component = (|| {
let comp_ref = &entity_handle
.get_matching_components(ComponentT::id())
.next()?;
match ComponentHandle::<ComponentT>::from_entity_component_ref(comp_ref) {
Ok(component) => Some(component),
Err(err) => {
panic!(
"Creating handle to component {} failed: {err}",
type_name::<ComponentT>()
);
}
}
})();
fields.with_elem(component)
}
}
impl<'world, ComponentT: Component> Term<'world> for Option<&mut ComponentT>
{
type AddField<Fields: Tuple> =
Fields::WithElementAtEnd<Option<ComponentHandleMut<'world, ComponentT>>>;
fn apply_to_terms_builder<const MAX_TERM_CNT: usize>(
_terms_builder: &mut TermsBuilder<MAX_TERM_CNT>,
)
{
}
fn add_field<Fields: Tuple>(
entity_handle: &EntityHandle<'world>,
world: &'world World,
fields: Fields,
) -> Self::AddField<Fields>
{
let component = (|| {
let comp_ref = &entity_handle
.get_matching_components(ComponentT::id())
.next()?;
match ComponentHandleMut::<ComponentT>::from_entity_component_ref(comp_ref, world) {
Ok(component) => Some(component),
Err(err) => {
panic!(
"Creating mut handle to component {} failed: {err}",
type_name::<ComponentT>()
);
}
}
})();
fields.with_elem(component)
}
}
|