1
1
//! Dynamic query infrastructure.
2
2
3
- use std:: { any:: TypeId , cell:: Cell , ops:: Deref } ;
3
+ use std:: { any:: TypeId , borrow :: Cow , cell:: Cell , ops:: Deref } ;
4
4
5
5
use crate :: { storage:: sparse_set, Component , Components , Ecs , SparseSetRef , SparseSetStorage } ;
6
6
7
7
/// Drives a query by yielding the entities
8
8
/// whose components satisfy the query parameters.
9
9
pub struct QueryDriver < ' w , ' q > {
10
10
/// A sparse set for each component in the query.
11
- sparse_sets : & ' q [ SparseSetRef < ' w > ] ,
11
+ sparse_sets : Cow < ' q , [ SparseSetRef < ' w > ] > ,
12
12
13
13
/// The "lead" sparse set, chosen as the set with
14
14
/// the smallest number of components.
15
- lead : SparseSetRef < ' q > ,
15
+ lead : SparseSetRef < ' w > ,
16
16
17
17
/// Used as the yielded value for the iterator.
18
18
/// (We can't own this because of the lack of GATs.)
19
- dense_indices : & ' q [ Cell < u32 > ] ,
19
+ dense_indices : Cow < ' q , [ Cell < u32 > ] > ,
20
20
}
21
21
22
22
impl < ' w , ' q > QueryDriver < ' w , ' q > {
@@ -27,15 +27,19 @@ impl<'w, 'q> QueryDriver<'w, 'q> {
27
27
///
28
28
/// # Panics
29
29
/// Panics if `sparse_sets.len() != dense_indices.len()`.
30
- pub fn new ( sparse_sets : & ' q [ SparseSetRef < ' w > ] , dense_indices : & ' q [ Cell < u32 > ] ) -> Self {
30
+ pub fn new (
31
+ sparse_sets : Cow < ' q , [ SparseSetRef < ' w > ] > ,
32
+ dense_indices : Cow < ' q , [ Cell < u32 > ] > ,
33
+ ) -> Self {
31
34
let lead = sparse_sets
32
35
. iter ( )
33
36
. min_by_key ( |set| set. len ( ) )
34
- . unwrap_or ( SparseSetRef :: empty ( ) ) ;
37
+ . copied ( )
38
+ . unwrap_or ( * SparseSetRef :: empty ( ) ) ;
35
39
36
40
Self {
37
41
sparse_sets,
38
- lead : * lead ,
42
+ lead,
39
43
dense_indices,
40
44
}
41
45
}
@@ -60,10 +64,8 @@ pub struct QueryDriverIter<'w, 'q> {
60
64
lead_iter : sparse_set:: Iter < ' q > ,
61
65
}
62
66
63
- impl < ' w , ' q > Iterator for QueryDriverIter < ' w , ' q > {
64
- type Item = QueryItem < ' q > ;
65
-
66
- fn next ( & mut self ) -> Option < Self :: Item > {
67
+ impl < ' w , ' q > QueryDriverIter < ' w , ' q > {
68
+ pub fn next ( & mut self ) -> Option < QueryItem > {
67
69
loop {
68
70
let ( sparse_index, lead_dense_index) = self . lead_iter . next ( ) ?;
69
71
@@ -98,36 +100,35 @@ pub struct QueryItem<'q> {
98
100
99
101
// -- Static queries
100
102
101
- /*
102
103
/// A typed query element.
103
- pub trait QueryParameter {
104
- type Output;
104
+ pub trait QueryParameter < ' a > {
105
+ type Output : ' a ;
105
106
type Component : Component ;
106
107
107
108
unsafe fn get_unchecked_by_dense_index (
108
- storage: &SparseSetStorage,
109
+ storage : & ' a SparseSetStorage ,
109
110
dense_index : u32 ,
110
111
) -> Self :: Output ;
111
112
}
112
113
113
- impl<'a, T> QueryParameter for &'a T
114
+ impl < ' a , T > QueryParameter < ' a > for & ' a T
114
115
where
115
116
T : Component ,
116
117
{
117
- type Output<'b> = &'b T;
118
+ type Output = & ' a T ;
118
119
type Component = T ;
119
120
120
121
unsafe fn get_unchecked_by_dense_index (
121
- storage: &SparseSetStorage,
122
+ storage : & ' a SparseSetStorage ,
122
123
dense_index : u32 ,
123
- ) -> Self::Output<'_> {
124
+ ) -> Self :: Output {
124
125
storage. get_unchecked_by_dense_index ( dense_index)
125
126
}
126
127
}
127
128
128
129
/// A tuple of query parameters.
129
- pub trait QueryTuple {
130
- type Output<'s> ;
130
+ pub trait QueryTuple < ' a > {
131
+ type Output : ' a ;
131
132
132
133
// avoiding allocations here is blocked on const generics and/or GATs
133
134
fn sparse_sets ( components : & Components ) -> Option < Vec < & SparseSetStorage > > ;
@@ -141,16 +142,16 @@ pub trait QueryTuple {
141
142
/// `dense_indices` and `sparse_sets` must have a length equal
142
143
/// to the length of the vectors returned by the corresponding methods
143
144
/// of this trait.
144
- unsafe fn make_output<'s> (
145
- sparse_sets: &'s [&'s SparseSetStorage],
145
+ unsafe fn make_output (
146
+ sparse_sets : & [ & ' a SparseSetStorage ] ,
146
147
dense_indices : & [ Cell < u32 > ] ,
147
- ) -> Self::Output<'s> ;
148
+ ) -> Self :: Output ;
148
149
}
149
150
150
151
macro_rules! query_tuple_impl {
151
152
( $count: literal, $( ( $ty: ident, $index: literal) ) ,* $( , ) ?) => {
152
- impl <$($ty: QueryParameter),*> QueryTuple for ($($ty),*) {
153
- type Output<'s> = ($($ty::Output<'s> ),*);
153
+ impl <' a , $( $ty: QueryParameter < ' a> ) ,* > QueryTuple < ' a> for ( $( $ty) ,* ) {
154
+ type Output = ( $( $ty:: Output ) ,* ) ;
154
155
155
156
fn sparse_sets( components: & Components ) -> Option <Vec <& SparseSetStorage >> {
156
157
Some ( vec![
@@ -164,10 +165,10 @@ macro_rules! query_tuple_impl {
164
165
vec![ Cell :: new( 0 ) ; $count]
165
166
}
166
167
167
- unsafe fn make_output<'s> (
168
- sparse_sets: &'s [&'s SparseSetStorage],
168
+ unsafe fn make_output(
169
+ sparse_sets: & [ & ' a SparseSetStorage ] ,
169
170
dense_indices: & [ Cell <u32 >] ,
170
- ) -> Self::Output<'s> {
171
+ ) -> Self :: Output {
171
172
(
172
173
$(
173
174
$ty:: get_unchecked_by_dense_index( sparse_sets. get_unchecked( $index) , dense_indices. get_unchecked( $index) . get( ) )
@@ -179,4 +180,4 @@ macro_rules! query_tuple_impl {
179
180
}
180
181
181
182
query_tuple_impl ! ( 1 , ( T1 , 0 ) ) ;
182
- */
183
+ query_tuple_impl ! ( 2 , ( T1 , 0 ) , ( T2 , 1 ) ) ;
0 commit comments