zrx_stream/stream/barrier/
condition.rs

1// Copyright (c) 2025 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// Third-party contributions licensed under DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Condition.
27
28use std::fmt;
29use std::sync::Arc;
30use zrx_scheduler::{Id, Value};
31
32mod id;
33
34// ----------------------------------------------------------------------------
35// Traits
36// ----------------------------------------------------------------------------
37
38/// Condition function.
39pub trait ConditionFn<I>: Send + Sync {
40    /// Returns whether the identifier satisfies the condition.
41    fn satisfies(&self, id: &I) -> bool;
42}
43
44// ----------------------------------------------------------------------------
45// Structs
46// ----------------------------------------------------------------------------
47
48/// Condition.
49///
50/// Conditions are used to determine whether a [`Barrier`][] contains a specific
51/// identifier. They implement [`Value`], so they can be created and returned by
52/// any [`Operator`][]. The resulting [`Stream`][] of conditions can be used in
53/// any operator that expects conditions, such as [`Stream::select`][].
54///
55/// [`Barrier`]: crate::stream::barrier::Barrier
56/// [`Operator`]: crate::stream::operator::Operator
57/// [`Stream`]: crate::stream::Stream
58/// [`Stream::select`]: crate::stream::Stream::select
59///
60/// # Examples
61///
62/// ```
63/// use zrx_stream::barrier::Condition;
64///
65/// // Create condition and test identifier
66/// let condition = Condition::new(|&id: &i32| id < 100);
67/// assert!(condition.satisfies(&42));
68/// ```
69#[derive(Clone)]
70pub struct Condition<I> {
71    /// Condition function.
72    function: Arc<dyn ConditionFn<I>>,
73}
74
75// ----------------------------------------------------------------------------
76// Implementations
77// ----------------------------------------------------------------------------
78
79impl<I> Condition<I> {
80    /// Creates a condition.
81    ///
82    /// # Examples
83    ///
84    /// ```
85    /// use zrx_stream::barrier::Condition;
86    ///
87    /// // Create condition
88    /// let condition = Condition::new(|&id: &i32| id < 100);
89    /// ```
90    pub fn new<F>(f: F) -> Self
91    where
92        F: ConditionFn<I> + 'static,
93    {
94        Self { function: Arc::new(f) }
95    }
96
97    /// Returns whether the given identifier satisfies the condition.
98    ///
99    /// # Examples
100    ///
101    /// ```
102    /// use zrx_stream::barrier::Condition;
103    ///
104    /// // Create condition and test identifier
105    /// let condition = Condition::new(|&id: &i32| id < 100);
106    /// assert!(condition.satisfies(&42));
107    /// ```
108    #[inline]
109    pub fn satisfies(&self, id: &I) -> bool {
110        self.function.satisfies(id)
111    }
112}
113
114// ----------------------------------------------------------------------------
115// Trait implementations
116// ----------------------------------------------------------------------------
117
118impl<I> Value for Condition<I> where I: Id {}
119
120// ----------------------------------------------------------------------------
121
122impl<I> fmt::Debug for Condition<I> {
123    /// Formats the condition for debugging.
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        let function = "Box<dyn ConditionFn>";
126        f.debug_struct("Condition")
127            .field("function", &function)
128            .finish()
129    }
130}
131
132// ----------------------------------------------------------------------------
133// Blanket implementations
134// ----------------------------------------------------------------------------
135
136impl<F, I> ConditionFn<I> for F
137where
138    F: Fn(&I) -> bool + Send + Sync,
139{
140    #[inline]
141    fn satisfies(&self, id: &I) -> bool {
142        self(id)
143    }
144}