qubit_atomic/atomic/atomic_signed_count.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10
11//! # Atomic Signed Count
12//!
13//! Provides an atomic counter for values that may legitimately become
14//! negative.
15//!
16
17use std::fmt;
18use std::sync::atomic::{
19 AtomicIsize as StdAtomicIsize,
20 Ordering,
21};
22
23/// A signed atomic counter with synchronization-oriented operations.
24///
25/// Use this type when the counter models a delta, balance, backlog, offset, or
26/// other quantity that may legitimately cross zero. Examples include producer
27/// minus consumer deltas, permit debt, retry backlog changes, or accumulated
28/// scheduling offsets.
29///
30/// For counters that must never be negative, prefer
31/// [`AtomicCount`](crate::AtomicCount). For pure metrics or statistics,
32/// prefer the regular atomic integer types such as
33/// [`Atomic<isize>`](crate::Atomic).
34///
35/// This counter never wraps. Operations that would overflow the signed range
36/// panic. Use [`try_add`](Self::try_add) or [`try_sub`](Self::try_sub) when
37/// overflow is a normal business outcome.
38///
39/// # Example
40///
41/// ```rust
42/// use qubit_atomic::AtomicSignedCount;
43///
44/// let backlog_delta = AtomicSignedCount::zero();
45///
46/// assert_eq!(backlog_delta.add(5), 5);
47/// assert_eq!(backlog_delta.sub(8), -3);
48/// assert!(backlog_delta.is_negative());
49/// ```
50///
51#[repr(transparent)]
52pub struct AtomicSignedCount {
53 /// Standard-library atomic storage for the signed counter value.
54 inner: StdAtomicIsize,
55}
56
57impl AtomicSignedCount {
58 /// Creates a new signed atomic counter.
59 ///
60 /// # Parameters
61 ///
62 /// * `value` - The initial counter value.
63 ///
64 /// # Returns
65 ///
66 /// A signed counter initialized to `value`.
67 ///
68 /// # Example
69 ///
70 /// ```rust
71 /// use qubit_atomic::AtomicSignedCount;
72 ///
73 /// let counter = AtomicSignedCount::new(-3);
74 /// assert_eq!(counter.get(), -3);
75 /// ```
76 #[inline]
77 pub const fn new(value: isize) -> Self {
78 Self {
79 inner: StdAtomicIsize::new(value),
80 }
81 }
82
83 /// Creates a new signed counter initialized to zero.
84 ///
85 /// # Returns
86 ///
87 /// A signed counter whose current value is zero.
88 ///
89 /// # Example
90 ///
91 /// ```rust
92 /// use qubit_atomic::AtomicSignedCount;
93 ///
94 /// let counter = AtomicSignedCount::zero();
95 /// assert!(counter.is_zero());
96 /// ```
97 #[inline]
98 pub const fn zero() -> Self {
99 Self::new(0)
100 }
101
102 /// Gets the current counter value.
103 ///
104 /// # Returns
105 ///
106 /// The current counter value.
107 ///
108 /// # Example
109 ///
110 /// ```rust
111 /// use qubit_atomic::AtomicSignedCount;
112 ///
113 /// let counter = AtomicSignedCount::new(-7);
114 /// assert_eq!(counter.get(), -7);
115 /// ```
116 #[inline]
117 pub fn get(&self) -> isize {
118 self.inner.load(Ordering::Acquire)
119 }
120
121 /// Returns whether the current counter value is zero.
122 ///
123 /// # Returns
124 ///
125 /// `true` if the current value is zero, otherwise `false`.
126 ///
127 /// # Example
128 ///
129 /// ```rust
130 /// use qubit_atomic::AtomicSignedCount;
131 ///
132 /// let counter = AtomicSignedCount::zero();
133 /// assert!(counter.is_zero());
134 /// ```
135 #[inline]
136 pub fn is_zero(&self) -> bool {
137 self.get() == 0
138 }
139
140 /// Returns whether the current counter value is greater than zero.
141 ///
142 /// # Returns
143 ///
144 /// `true` if the current value is greater than zero, otherwise `false`.
145 ///
146 /// # Example
147 ///
148 /// ```rust
149 /// use qubit_atomic::AtomicSignedCount;
150 ///
151 /// let counter = AtomicSignedCount::new(1);
152 /// assert!(counter.is_positive());
153 /// ```
154 #[inline]
155 pub fn is_positive(&self) -> bool {
156 self.get() > 0
157 }
158
159 /// Returns whether the current counter value is less than zero.
160 ///
161 /// # Returns
162 ///
163 /// `true` if the current value is less than zero, otherwise `false`.
164 ///
165 /// # Example
166 ///
167 /// ```rust
168 /// use qubit_atomic::AtomicSignedCount;
169 ///
170 /// let counter = AtomicSignedCount::new(-1);
171 /// assert!(counter.is_negative());
172 /// ```
173 #[inline]
174 pub fn is_negative(&self) -> bool {
175 self.get() < 0
176 }
177
178 /// Increments the counter by one and returns the new value.
179 ///
180 /// # Returns
181 ///
182 /// The counter value after the increment.
183 ///
184 /// # Panics
185 ///
186 /// Panics if the increment would overflow [`isize::MAX`].
187 ///
188 /// # Example
189 ///
190 /// ```rust
191 /// use qubit_atomic::AtomicSignedCount;
192 ///
193 /// let counter = AtomicSignedCount::zero();
194 /// assert_eq!(counter.inc(), 1);
195 /// ```
196 #[inline]
197 pub fn inc(&self) -> isize {
198 self.add(1)
199 }
200
201 /// Decrements the counter by one and returns the new value.
202 ///
203 /// # Returns
204 ///
205 /// The counter value after the decrement.
206 ///
207 /// # Panics
208 ///
209 /// Panics if the decrement would underflow [`isize::MIN`].
210 ///
211 /// # Example
212 ///
213 /// ```rust
214 /// use qubit_atomic::AtomicSignedCount;
215 ///
216 /// let counter = AtomicSignedCount::zero();
217 /// assert_eq!(counter.dec(), -1);
218 /// ```
219 #[inline]
220 pub fn dec(&self) -> isize {
221 self.sub(1)
222 }
223
224 /// Adds `delta` to the counter and returns the new value.
225 ///
226 /// # Parameters
227 ///
228 /// * `delta` - The amount to add. It may be negative.
229 ///
230 /// # Returns
231 ///
232 /// The counter value after the addition.
233 ///
234 /// # Panics
235 ///
236 /// Panics if the addition would overflow or underflow the signed range.
237 ///
238 /// # Example
239 ///
240 /// ```rust
241 /// use qubit_atomic::AtomicSignedCount;
242 ///
243 /// let counter = AtomicSignedCount::new(2);
244 /// assert_eq!(counter.add(-5), -3);
245 /// ```
246 #[inline]
247 pub fn add(&self, delta: isize) -> isize {
248 self.try_add(delta)
249 .expect("atomic signed counter out of range")
250 }
251
252 /// Tries to add `delta` to the counter.
253 ///
254 /// # Parameters
255 ///
256 /// * `delta` - The amount to add. It may be negative.
257 ///
258 /// # Returns
259 ///
260 /// `Some(new_value)` if the addition succeeds, or `None` if it would
261 /// overflow or underflow the signed range. On `None`, the counter is left
262 /// unchanged.
263 ///
264 /// # Example
265 ///
266 /// ```rust
267 /// use qubit_atomic::AtomicSignedCount;
268 ///
269 /// let counter = AtomicSignedCount::new(-2);
270 /// assert_eq!(counter.try_add(5), Some(3));
271 /// ```
272 #[inline]
273 pub fn try_add(&self, delta: isize) -> Option<isize> {
274 self.try_update(|current| current.checked_add(delta))
275 }
276
277 /// Subtracts `delta` from the counter and returns the new value.
278 ///
279 /// # Parameters
280 ///
281 /// * `delta` - The amount to subtract. It may be negative.
282 ///
283 /// # Returns
284 ///
285 /// The counter value after the subtraction.
286 ///
287 /// # Panics
288 ///
289 /// Panics if the subtraction would overflow or underflow the signed range.
290 ///
291 /// # Example
292 ///
293 /// ```rust
294 /// use qubit_atomic::AtomicSignedCount;
295 ///
296 /// let counter = AtomicSignedCount::new(2);
297 /// assert_eq!(counter.sub(5), -3);
298 /// ```
299 #[inline]
300 pub fn sub(&self, delta: isize) -> isize {
301 self.try_sub(delta)
302 .expect("atomic signed counter out of range")
303 }
304
305 /// Tries to subtract `delta` from the counter.
306 ///
307 /// # Parameters
308 ///
309 /// * `delta` - The amount to subtract. It may be negative.
310 ///
311 /// # Returns
312 ///
313 /// `Some(new_value)` if the subtraction succeeds, or `None` if it would
314 /// overflow or underflow the signed range. On `None`, the counter is left
315 /// unchanged.
316 ///
317 /// # Example
318 ///
319 /// ```rust
320 /// use qubit_atomic::AtomicSignedCount;
321 ///
322 /// let counter = AtomicSignedCount::new(2);
323 /// assert_eq!(counter.try_sub(5), Some(-3));
324 /// ```
325 #[inline]
326 pub fn try_sub(&self, delta: isize) -> Option<isize> {
327 self.try_update(|current| current.checked_sub(delta))
328 }
329
330 /// Applies a checked update with synchronization semantics.
331 ///
332 /// # Parameters
333 ///
334 /// * `update` - A function that maps the current value to the next value,
335 /// or returns `None` to reject the update.
336 ///
337 /// # Returns
338 ///
339 /// `Some(new_value)` if the update succeeds, or `None` if `update`
340 /// rejects the current value. A rejected update leaves the counter
341 /// unchanged.
342 #[inline]
343 fn try_update<F>(&self, update: F) -> Option<isize>
344 where
345 F: Fn(isize) -> Option<isize>,
346 {
347 let mut current = self.get();
348 loop {
349 let next = update(current)?;
350 match self.inner.compare_exchange_weak(
351 current,
352 next,
353 Ordering::AcqRel,
354 Ordering::Acquire,
355 ) {
356 Ok(_) => return Some(next),
357 Err(actual) => current = actual,
358 }
359 }
360 }
361}
362
363impl Default for AtomicSignedCount {
364 /// Creates a zero-valued signed atomic counter.
365 ///
366 /// # Returns
367 ///
368 /// A signed counter whose current value is zero.
369 #[inline]
370 fn default() -> Self {
371 Self::zero()
372 }
373}
374
375impl From<isize> for AtomicSignedCount {
376 /// Converts an initial counter value into an [`AtomicSignedCount`].
377 ///
378 /// # Parameters
379 ///
380 /// * `value` - The initial counter value.
381 ///
382 /// # Returns
383 ///
384 /// A signed counter initialized to `value`.
385 #[inline]
386 fn from(value: isize) -> Self {
387 Self::new(value)
388 }
389}
390
391impl fmt::Debug for AtomicSignedCount {
392 /// Formats the current counter value for debugging.
393 ///
394 /// # Parameters
395 ///
396 /// * `f` - The formatter receiving the debug representation.
397 ///
398 /// # Returns
399 ///
400 /// A formatting result from the formatter.
401 #[inline]
402 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
403 f.debug_struct("AtomicSignedCount")
404 .field("value", &self.get())
405 .finish()
406 }
407}
408
409impl fmt::Display for AtomicSignedCount {
410 /// Formats the current counter value with decimal display formatting.
411 ///
412 /// # Parameters
413 ///
414 /// * `f` - The formatter receiving the displayed value.
415 ///
416 /// # Returns
417 ///
418 /// A formatting result from the formatter.
419 #[inline]
420 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
421 write!(f, "{}", self.get())
422 }
423}