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
171
172
173
174
//! Preallocated bounded ring buffer for process-site stream boundaries.
use sim_kernel::{Error, Result};
use sim_lib_stream_core::StreamStats;
/// Push result for the bounded process-site ring.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ProcessRingPush<T> {
/// The item was stored in the ring.
Accepted,
/// The ring was full; the rejected item is returned.
DroppedNewest(T),
/// The ring was closed; the rejected item is returned.
Closed(T),
}
/// Capacity snapshot for steady-state process-site checks.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProcessRingSnapshot {
capacity: usize,
len: usize,
allocated_slots: usize,
}
/// Preallocated bounded ring used at process-site stream boundaries.
#[derive(Clone, Debug)]
pub struct ProcessSharedRing<T> {
slots: Vec<Option<T>>,
head: usize,
len: usize,
closed: bool,
stats: StreamStats,
}
impl<T> ProcessSharedRing<T> {
/// Creates a ring preallocated to hold `capacity` items.
///
/// Returns an evaluation error when `capacity` is zero.
///
/// # Examples
///
/// ```
/// use sim_lib_stream_host::{ProcessRingPush, ProcessSharedRing};
///
/// let mut ring = ProcessSharedRing::with_capacity(2).unwrap();
/// assert_eq!(ring.try_push(1), ProcessRingPush::Accepted);
/// assert_eq!(ring.try_push(2), ProcessRingPush::Accepted);
/// assert_eq!(ring.try_push(3), ProcessRingPush::DroppedNewest(3));
/// assert_eq!(ring.try_pop(), Some(1));
/// ```
pub fn with_capacity(capacity: usize) -> Result<Self> {
if capacity == 0 {
return Err(Error::Eval(
"process ring capacity must be greater than zero".to_owned(),
));
}
let slots = std::iter::repeat_with(|| None).take(capacity).collect();
Ok(Self {
slots,
head: 0,
len: 0,
closed: false,
stats: StreamStats::default(),
})
}
/// Pushes an item, returning whether it was accepted, dropped (full), or
/// rejected (closed).
pub fn try_push(&mut self, item: T) -> ProcessRingPush<T> {
self.stats.pushed = self.stats.pushed.saturating_add(1);
if self.closed {
self.stats.closed = true;
return ProcessRingPush::Closed(item);
}
if self.len == self.capacity() {
self.stats.dropped_newest = self.stats.dropped_newest.saturating_add(1);
return ProcessRingPush::DroppedNewest(item);
}
let tail = (self.head + self.len) % self.capacity();
self.slots[tail] = Some(item);
self.len += 1;
self.stats.accepted = self.stats.accepted.saturating_add(1);
ProcessRingPush::Accepted
}
/// Pops the oldest item, or returns `None` when the ring is empty.
pub fn try_pop(&mut self) -> Option<T> {
if self.len == 0 {
return None;
}
let item = self.slots[self.head].take();
self.head = (self.head + 1) % self.capacity();
self.len -= 1;
if item.is_some() {
self.stats.yielded = self.stats.yielded.saturating_add(1);
}
item
}
/// Closes the ring; further pushes are rejected.
pub fn close(&mut self) {
self.closed = true;
self.stats.closed = true;
}
/// Drains all buffered items and closes the ring, recording cancellation.
pub fn cancel(&mut self) {
while self.try_pop().is_some() {}
self.closed = true;
self.stats.closed = true;
self.stats.cancelled = true;
}
/// Returns the number of buffered items.
pub fn len(&self) -> usize {
self.len
}
/// Returns whether the ring is empty.
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Returns the ring capacity in items.
pub fn capacity(&self) -> usize {
self.slots.len()
}
/// Returns the number of slots backing storage has allocated.
pub fn allocated_slots(&self) -> usize {
self.slots.capacity()
}
/// Returns whether the ring has been closed.
pub fn is_closed(&self) -> bool {
self.closed
}
/// Captures a capacity snapshot for steady-state checks.
pub fn snapshot(&self) -> ProcessRingSnapshot {
ProcessRingSnapshot {
capacity: self.capacity(),
len: self.len,
allocated_slots: self.allocated_slots(),
}
}
/// Returns a clone of the ring's running statistics.
pub fn stats(&self) -> StreamStats {
self.stats.clone()
}
}
impl ProcessRingSnapshot {
/// Returns the captured ring capacity.
pub fn capacity(self) -> usize {
self.capacity
}
/// Returns the captured buffered length.
pub fn len(self) -> usize {
self.len
}
/// Returns whether the ring was empty when captured.
pub fn is_empty(self) -> bool {
self.len == 0
}
/// Returns the captured number of allocated slots.
pub fn allocated_slots(self) -> usize {
self.allocated_slots
}
}