@@ -10,6 +10,41 @@ use std::fmt;
10
10
pub trait Query < X : XConn > {
11
11
/// Run this query for a given window ID.
12
12
fn run ( & self , id : Xid , x : & X ) -> Result < bool > ;
13
+
14
+ /// Combine this query with another query using a logical AND.
15
+ fn and < Other : Query < X > > ( self , other : Other ) -> AndQuery < X , Self , Other >
16
+ where
17
+ Self : Sized ,
18
+ {
19
+ AndQuery {
20
+ first : self ,
21
+ second : other,
22
+ _phantom : std:: marker:: PhantomData ,
23
+ }
24
+ }
25
+
26
+ /// Combine this query with another query using a logical OR.
27
+ fn or < Other : Query < X > > ( self , other : Other ) -> OrQuery < X , Self , Other >
28
+ where
29
+ Self : Sized ,
30
+ {
31
+ OrQuery {
32
+ first : self ,
33
+ second : other,
34
+ _phantom : std:: marker:: PhantomData ,
35
+ }
36
+ }
37
+
38
+ /// Apply a logical NOT to this query.
39
+ fn not ( self ) -> NotQuery < X , Self >
40
+ where
41
+ Self : Sized ,
42
+ {
43
+ NotQuery {
44
+ inner : self ,
45
+ _phantom : std:: marker:: PhantomData ,
46
+ }
47
+ }
13
48
}
14
49
15
50
impl < X : XConn > fmt:: Debug for Box < dyn Query < X > > {
@@ -101,97 +136,41 @@ where
101
136
102
137
/// A meta [Query] for combining two queries with a logical AND.
103
138
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
104
- pub struct AndQuery < Q1 , Q2 > ( pub Q1 , pub Q2 ) ;
139
+ pub struct AndQuery < X : XConn , Q1 , Q2 > {
140
+ first : Q1 ,
141
+ second : Q2 ,
142
+ _phantom : std:: marker:: PhantomData < X > ,
143
+ }
105
144
106
- impl < X : XConn , Q1 : Query < X > , Q2 : Query < X > > Query < X > for AndQuery < Q1 , Q2 > {
145
+ impl < X : XConn , Q1 : Query < X > , Q2 : Query < X > > Query < X > for AndQuery < X , Q1 , Q2 > {
107
146
fn run ( & self , id : Xid , x : & X ) -> Result < bool > {
108
- Ok ( self . 0 . run ( id, x) ? && self . 1 . run ( id, x) ?)
147
+ Ok ( self . first . run ( id, x) ? && self . second . run ( id, x) ?)
109
148
}
110
149
}
111
150
112
151
/// A meta [Query] for combining two queries with a logical OR.
113
152
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
114
- pub struct OrQuery < Q1 , Q2 > ( pub Q1 , pub Q2 ) ;
153
+ pub struct OrQuery < X : XConn , Q1 , Q2 > {
154
+ first : Q1 ,
155
+ second : Q2 ,
156
+ _phantom : std:: marker:: PhantomData < X > ,
157
+ }
115
158
116
- impl < X : XConn , Q1 : Query < X > , Q2 : Query < X > > Query < X > for OrQuery < Q1 , Q2 > {
159
+ impl < X : XConn , Q1 : Query < X > , Q2 : Query < X > > Query < X > for OrQuery < X , Q1 , Q2 > {
117
160
fn run ( & self , id : Xid , x : & X ) -> Result < bool > {
118
- Ok ( self . 0 . run ( id, x) ? || self . 1 . run ( id, x) ?)
161
+ Ok ( self . first . run ( id, x) ? || self . second . run ( id, x) ?)
119
162
}
120
163
}
121
164
122
165
/// A meta [Query] for applying a logical NOT to a query.
123
166
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
124
- pub struct NotQuery < Q > ( pub Q ) ;
125
-
126
- impl < X : XConn , Q : Query < X > > Query < X > for NotQuery < Q > {
127
- fn run ( & self , id : Xid , x : & X ) -> Result < bool > {
128
- Ok ( !self . 0 . run ( id, x) ?)
129
- }
167
+ pub struct NotQuery < X : XConn , Q > {
168
+ inner : Q ,
169
+ _phantom : std:: marker:: PhantomData < X > ,
130
170
}
131
171
132
- /// A meta [Query] for combining multiple queries with a logical OR.
133
- pub struct AnyQuery < X > ( pub Vec < Box < dyn Query < X > > > ) ;
134
-
135
- impl < X : XConn > fmt:: Debug for AnyQuery < X > {
136
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
137
- f. debug_struct ( "AnyQuery" ) . finish ( )
138
- }
139
- }
140
-
141
- impl < X : XConn > Query < X > for AnyQuery < X > {
172
+ impl < X : XConn , Q : Query < X > > Query < X > for NotQuery < X , Q > {
142
173
fn run ( & self , id : Xid , x : & X ) -> Result < bool > {
143
- self . 0
144
- . iter ( )
145
- . try_fold ( false , |acc, query| Ok ( acc || query. run ( id, x) ?) )
174
+ Ok ( !self . inner . run ( id, x) ?)
146
175
}
147
176
}
148
-
149
- /// A meta [Query] for combining multiple queries with a logical AND.
150
- pub struct AllQuery < X > ( pub Vec < Box < dyn Query < X > > > ) ;
151
-
152
- impl < X : XConn > fmt:: Debug for AllQuery < X > {
153
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
154
- f. debug_struct ( "AllQuery" ) . finish ( )
155
- }
156
- }
157
-
158
- impl < X : XConn > Query < X > for AllQuery < X > {
159
- fn run ( & self , id : Xid , x : & X ) -> Result < bool > {
160
- self . 0
161
- . iter ( )
162
- . try_fold ( true , |acc, query| Ok ( acc && query. run ( id, x) ?) )
163
- }
164
- }
165
-
166
- trait QueryExt < X > : Query < X >
167
- where
168
- X : XConn ,
169
- {
170
- fn and ( self , other : impl Query < X > ) -> AndQuery < Self , impl Query < X > >
171
- where
172
- Self : Sized ,
173
- {
174
- AndQuery ( self , other)
175
- }
176
-
177
- fn or ( self , other : impl Query < X > ) -> OrQuery < Self , impl Query < X > >
178
- where
179
- Self : Sized ,
180
- {
181
- OrQuery ( self , other)
182
- }
183
-
184
- fn not ( self ) -> NotQuery < impl Query < X > >
185
- where
186
- Self : Sized ,
187
- {
188
- NotQuery ( self )
189
- }
190
- }
191
-
192
- impl < X , Q > QueryExt < X > for Q
193
- where
194
- X : XConn ,
195
- Q : Query < X > ,
196
- {
197
- }
0 commit comments