Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 15bb7ba

Browse files
committedJun 17, 2024··
Make the changes requested by @sminez
1 parent ad882a4 commit 15bb7ba

File tree

1 file changed

+54
-75
lines changed

1 file changed

+54
-75
lines changed
 

‎src/x/query.rs

+54-75
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@ use std::fmt;
1010
pub trait Query<X: XConn> {
1111
/// Run this query for a given window ID.
1212
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+
}
1348
}
1449

1550
impl<X: XConn> fmt::Debug for Box<dyn Query<X>> {
@@ -101,97 +136,41 @@ where
101136

102137
/// A meta [Query] for combining two queries with a logical AND.
103138
#[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+
}
105144

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> {
107146
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)?)
109148
}
110149
}
111150

112151
/// A meta [Query] for combining two queries with a logical OR.
113152
#[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+
}
115158

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> {
117160
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)?)
119162
}
120163
}
121164

122165
/// A meta [Query] for applying a logical NOT to a query.
123166
#[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>,
130170
}
131171

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> {
142173
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)?)
146175
}
147176
}
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

Comments
 (0)
Please sign in to comment.