zrx_stream/stream/barrier.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the 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//! Barrier.
27
28use std::fmt::{self, Debug};
29use std::sync::Arc;
30
31use zrx_scheduler::{Id, Key, Value};
32use zrx_store::stash::Items;
33
34pub mod advance;
35mod lifecycle;
36pub mod set;
37
38pub use advance::Advance;
39use lifecycle::Lifecycle;
40pub use set::Barriers;
41
42// ----------------------------------------------------------------------------
43// Traits
44// ----------------------------------------------------------------------------
45
46/// Barrier function.
47trait BarrierFn<I>: Send + Sync {
48 /// Returns whether the barrier contains the given scope.
49 fn contains(&self, scope: &Key<I>) -> bool;
50}
51
52// ----------------------------------------------------------------------------
53// Structs
54// ----------------------------------------------------------------------------
55
56/// Barrier.
57#[derive(Clone)]
58pub struct Barrier<I> {
59 /// Barrier function.
60 function: Arc<dyn BarrierFn<I>>,
61 /// Contained scopes.
62 items: Items,
63}
64
65// ----------------------------------------------------------------------------
66// Implementations
67// ----------------------------------------------------------------------------
68
69impl<I> Barrier<I>
70where
71 I: Id,
72{
73 /// Creates a barrier with the given function.
74 #[must_use]
75 pub fn new<F>(f: F) -> Self
76 where
77 F: Fn(&Key<I>) -> bool + Send + Sync + 'static,
78 {
79 Self {
80 function: Arc::new(f),
81 items: Items::new(),
82 }
83 }
84
85 /// Returns whether the barrier contains the given scope.
86 #[inline]
87 #[must_use]
88 pub fn contains(&self, scope: &Key<I>) -> bool {
89 self.function.contains(scope)
90 }
91
92 /// Inserts the given index into the barrier.
93 #[inline]
94 fn insert(&mut self, index: usize) -> bool {
95 self.items.insert(index)
96 }
97
98 /// Removes the given index from the barrier.
99 #[inline]
100 fn remove(&mut self, index: usize) -> bool {
101 self.items.remove(index)
102 }
103
104 /// Returns `true` if the barrier is fulfilled.
105 #[inline]
106 fn is_complete(&self, lifecycle: &Lifecycle) -> bool {
107 lifecycle.is_complete(&self.items)
108 }
109}
110
111#[allow(clippy::must_use_candidate)]
112impl<I> Barrier<I> {
113 /// Returns the items of the barrier.
114 #[inline]
115 pub fn items(&self) -> &Items {
116 &self.items
117 }
118}
119
120// ----------------------------------------------------------------------------
121// Trait implementations
122// ----------------------------------------------------------------------------
123
124impl<I> Value for Barrier<I> where I: Id {}
125
126// ----------------------------------------------------------------------------
127
128impl<I> PartialEq for Barrier<I> {
129 /// Compares two barriers for equality.
130 #[inline]
131 fn eq(&self, other: &Self) -> bool {
132 Arc::ptr_eq(&self.function, &other.function)
133 }
134}
135
136impl<I> Eq for Barrier<I> {}
137
138// ----------------------------------------------------------------------------
139
140impl<I> Debug for Barrier<I> {
141 /// Formats the barrier for debugging.
142 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
143 let function = "Box<dyn BarrierFn>";
144 f.debug_struct("Barrier")
145 .field("function", &function)
146 .field("items", &self.items)
147 .finish()
148 }
149}
150
151// ----------------------------------------------------------------------------
152// Blanket implementations
153// ----------------------------------------------------------------------------
154
155impl<F, I> BarrierFn<I> for F
156where
157 F: Fn(&Key<I>) -> bool + Send + Sync,
158{
159 #[inline]
160 fn contains(&self, scope: &Key<I>) -> bool {
161 self(scope)
162 }
163}