zrx_stream/stream/barrier/set/drain.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//! Drain iterator implementation for [`Barriers`].
27
28use zrx_scheduler::{Id, Key};
29use zrx_store::stash::{items, Items};
30use zrx_store::Stash;
31
32use crate::stream::barrier::Barrier;
33
34use super::{Advance, Barriers};
35
36// ----------------------------------------------------------------------------
37// Structs
38// ----------------------------------------------------------------------------
39
40/// Drain iterator for [`Barriers`].
41pub struct Drain<'a, I> {
42 /// Inner set of barriers.
43 inner: &'a Stash<Key<I>, Barrier<I>>,
44 /// All known scopes.
45 scopes: &'a Stash<Key<I>, Items>,
46 /// Drain iterator over fulfilled barriers.
47 items: items::Drain<'a>,
48}
49
50// ----------------------------------------------------------------------------
51// Implementations
52// ----------------------------------------------------------------------------
53
54impl<I> Barriers<I>
55where
56 I: Id,
57{
58 /// Creates a drain iterator over the barrier set.
59 ///
60 /// Returns all pending advances, consuming only those that are still
61 /// fulfilled at the time of collection. Barriers that were fulfilled but
62 /// subsequently invalidated by a new scope insertion are excluded and
63 /// removed from the pending set.
64 #[must_use]
65 pub fn drain(&mut self) -> Drain<'_, I> {
66 Drain {
67 inner: &self.inner,
68 scopes: &self.scopes,
69 items: self.fulfilled.drain(),
70 }
71 }
72}
73
74// ----------------------------------------------------------------------------
75// Trait implementations
76// ----------------------------------------------------------------------------
77
78impl<'a, I> Iterator for Drain<'a, I>
79where
80 I: Id,
81{
82 type Item = Advance<'a, I>;
83
84 /// Returns the next barrier advancement.
85 fn next(&mut self) -> Option<Self::Item> {
86 let index = self.items.next()?;
87 let barrier = &self.inner[index];
88 Some(Advance::new(
89 self.inner.key(index).expect("invariant"),
90 barrier.items(),
91 self.scopes,
92 ))
93 }
94}