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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use futures::{future::BoxFuture, Future};
use std::sync::Arc;

type BoxedPredicate<C> =
    Box<dyn Fn(Arc<C>) -> BoxFuture<'static, bool> + Send + Sync + 'static>;
type BoxedStatefulPredicate<C, S> = Box<
    dyn Fn(Arc<C>, Arc<S>) -> BoxFuture<'static, bool> + Send + Sync + 'static,
>;

/// Boolean operations on predicates.
pub trait PredicateBooleanOperations<C, F>: Fn(Arc<C>) -> F
where
    F: Future<Output = bool> + Send,
    C: Send + Sync + 'static,
    Self: Sized + Send + Sync + 'static,
{
    /// `self(..).await && other(..).await`
    fn and<P, PF>(self, other: P) -> BoxedPredicate<C>
    where
        P: PredicateBooleanOperations<C, PF> + 'static,
        PF: Future<Output = bool> + Send,
    {
        let other = Arc::new(other);
        let this = Arc::new(self);

        Box::new(move |ctx| {
            let other = Arc::clone(&other);
            let this = Arc::clone(&this);
            Box::pin(async move { this(ctx.clone()).await && other(ctx).await })
        })
    }

    /// `self(..).await || other(..).await`
    fn or<P, PF>(self, other: P) -> BoxedPredicate<C>
    where
        P: PredicateBooleanOperations<C, PF> + 'static,
        PF: Future<Output = bool> + Send,
    {
        let other = Arc::new(other);
        let this = Arc::new(self);

        Box::new(move |ctx| {
            let other = Arc::clone(&other);
            let this = Arc::clone(&this);
            Box::pin(async move { this(ctx.clone()).await || other(ctx).await })
        })
    }

    /// `self(..).await != other(..).await`
    #[allow(clippy::mixed_read_write_in_expression)] // everything is clear here
    fn xor<P, PF>(self, other: P) -> BoxedPredicate<C>
    where
        P: PredicateBooleanOperations<C, PF> + 'static,
        PF: Future<Output = bool> + Send,
    {
        let other = Arc::new(other);
        let this = Arc::new(self);

        Box::new(move |ctx| {
            let other = Arc::clone(&other);
            let this = Arc::clone(&this);
            Box::pin(async move { this(ctx.clone()).await != other(ctx).await })
        })
    }

    /// `!self(..).await`
    fn not(self) -> BoxedPredicate<C> {
        let this = Arc::new(self);

        Box::new(move |ctx| {
            let this = Arc::clone(&this);
            Box::pin(async move { !this(ctx).await })
        })
    }
}

/// Boolean operations on stateful predicates.
pub trait StatefulPredicateBooleanOperations<C, S, F>:
    Fn(Arc<C>, Arc<S>) -> F
where
    F: Future<Output = bool> + Send,
    C: Send + Sync + 'static,
    S: Send + Sync + 'static,
    Self: Sized + Send + Sync + 'static,
{
    /// `self(..).await && other(..).await`
    fn and<P, PF>(self, other: P) -> BoxedStatefulPredicate<C, S>
    where
        P: StatefulPredicateBooleanOperations<C, S, PF> + 'static,
        PF: Future<Output = bool> + Send,
    {
        let other = Arc::new(other);
        let this = Arc::new(self);

        Box::new(move |ctx, state| {
            let other = Arc::clone(&other);
            let this = Arc::clone(&this);
            Box::pin(async move {
                this(ctx.clone(), state.clone()).await
                    && other(ctx, state).await
            })
        })
    }

    /// `self(..).await || other(..).await`
    fn or<P, PF>(self, other: P) -> BoxedStatefulPredicate<C, S>
    where
        P: StatefulPredicateBooleanOperations<C, S, PF> + 'static,
        PF: Future<Output = bool> + Send,
    {
        let other = Arc::new(other);
        let this = Arc::new(self);

        Box::new(move |ctx, state| {
            let other = Arc::clone(&other);
            let this = Arc::clone(&this);
            Box::pin(async move {
                this(ctx.clone(), state.clone()).await
                    || other(ctx, state).await
            })
        })
    }

    /// `self(..).await != other(..).await`
    #[allow(clippy::mixed_read_write_in_expression)] // everything is clear here
    fn xor<P, PF>(self, other: P) -> BoxedStatefulPredicate<C, S>
    where
        P: StatefulPredicateBooleanOperations<C, S, PF> + 'static,
        PF: Future<Output = bool> + Send,
    {
        let other = Arc::new(other);
        let this = Arc::new(self);

        Box::new(move |ctx, state| {
            let other = Arc::clone(&other);
            let this = Arc::clone(&this);
            Box::pin(async move {
                this(ctx.clone(), state.clone()).await
                    != other(ctx, state).await
            })
        })
    }

    /// `!self(..).await`
    fn not(self) -> BoxedStatefulPredicate<C, S> {
        let this = Arc::new(self);

        Box::new(move |ctx, state| {
            let this = Arc::clone(&this);
            Box::pin(async move { !this(ctx, state).await })
        })
    }
}

impl<C, F, T> PredicateBooleanOperations<C, F> for T
where
    T: (Fn(Arc<C>) -> F) + Sized + Send + Sync + 'static,
    F: Future<Output = bool> + Send,
    C: Send + Sync + 'static,
{
}

impl<C, S, F, T> StatefulPredicateBooleanOperations<C, S, F> for T
where
    T: (Fn(Arc<C>, Arc<S>) -> F) + Sized + Send + Sync + 'static,
    F: Future<Output = bool> + Send,
    C: Send + Sync + 'static,
    S: Send + Sync + 'static,
{
}