Skip to main content

qubit_atomic/atomic/
arc_atomic_signed_count.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9
10//! # Shared Atomic Signed Count Wrapper
11//!
12//! Provides [`ArcAtomicSignedCount`], a convenience wrapper around
13//! `Arc<AtomicSignedCount>`.
14//!
15//! # Author
16//!
17//! Haixing Hu
18
19use std::fmt;
20use std::ops::Deref;
21use std::sync::Arc;
22
23use super::atomic_signed_count::AtomicSignedCount;
24
25/// Shared-owner wrapper around [`AtomicSignedCount`].
26///
27/// This type is a convenience newtype for `Arc<AtomicSignedCount>`. Cloning an
28/// [`ArcAtomicSignedCount`] clones the shared owner handle, so all clones
29/// operate on the same underlying signed counter.
30///
31/// # Example
32///
33/// ```rust
34/// use qubit_atomic::ArcAtomicSignedCount;
35///
36/// let counter = ArcAtomicSignedCount::zero();
37/// let shared = counter.clone();
38///
39/// shared.sub(3);
40/// assert_eq!(counter.get(), -3);
41/// assert_eq!(counter.strong_count(), 2);
42/// ```
43pub struct ArcAtomicSignedCount {
44    /// Shared owner of the underlying signed atomic counter.
45    inner: Arc<AtomicSignedCount>,
46}
47
48impl ArcAtomicSignedCount {
49    /// Creates a new shared signed atomic counter.
50    ///
51    /// # Parameters
52    ///
53    /// * `value` - The initial counter value.
54    ///
55    /// # Returns
56    ///
57    /// A shared signed counter wrapper initialized to `value`.
58    #[inline]
59    pub fn new(value: isize) -> Self {
60        Self::from_count(AtomicSignedCount::new(value))
61    }
62
63    /// Creates a new shared signed counter initialized to zero.
64    ///
65    /// # Returns
66    ///
67    /// A shared signed counter wrapper whose current value is zero.
68    #[inline]
69    pub fn zero() -> Self {
70        Self::new(0)
71    }
72
73    /// Wraps an existing [`AtomicSignedCount`] in an [`Arc`].
74    ///
75    /// # Parameters
76    ///
77    /// * `counter` - The signed counter container to share.
78    ///
79    /// # Returns
80    ///
81    /// A shared signed counter wrapper owning `counter`.
82    #[inline]
83    pub fn from_count(counter: AtomicSignedCount) -> Self {
84        Self {
85            inner: Arc::new(counter),
86        }
87    }
88
89    /// Wraps an existing shared signed atomic counter.
90    ///
91    /// # Parameters
92    ///
93    /// * `inner` - The shared signed atomic counter to wrap.
94    ///
95    /// # Returns
96    ///
97    /// A wrapper around `inner`.
98    #[inline]
99    pub fn from_arc(inner: Arc<AtomicSignedCount>) -> Self {
100        Self { inner }
101    }
102
103    /// Returns the underlying [`Arc`] without cloning it.
104    ///
105    /// # Returns
106    ///
107    /// A shared reference to the underlying `Arc<AtomicSignedCount>`.
108    #[inline]
109    pub fn as_arc(&self) -> &Arc<AtomicSignedCount> {
110        &self.inner
111    }
112
113    /// Consumes this wrapper and returns the underlying [`Arc`].
114    ///
115    /// # Returns
116    ///
117    /// The underlying `Arc<AtomicSignedCount>`.
118    #[inline]
119    pub fn into_arc(self) -> Arc<AtomicSignedCount> {
120        self.inner
121    }
122
123    /// Returns the number of strong [`Arc`] owners.
124    ///
125    /// # Returns
126    ///
127    /// The current strong reference count of the shared signed counter.
128    #[inline]
129    pub fn strong_count(&self) -> usize {
130        Arc::strong_count(&self.inner)
131    }
132}
133
134impl Clone for ArcAtomicSignedCount {
135    /// Clones the shared owner handle.
136    ///
137    /// # Returns
138    ///
139    /// A new wrapper pointing to the same underlying signed atomic counter.
140    #[inline]
141    fn clone(&self) -> Self {
142        Self {
143            inner: Arc::clone(&self.inner),
144        }
145    }
146}
147
148impl Default for ArcAtomicSignedCount {
149    /// Creates a zero-valued shared signed atomic counter.
150    ///
151    /// # Returns
152    ///
153    /// A shared signed counter wrapper whose current value is zero.
154    #[inline]
155    fn default() -> Self {
156        Self::zero()
157    }
158}
159
160impl Deref for ArcAtomicSignedCount {
161    type Target = AtomicSignedCount;
162
163    /// Dereferences to the underlying [`AtomicSignedCount`].
164    ///
165    /// # Returns
166    ///
167    /// A shared reference to the signed atomic counter.
168    #[inline]
169    fn deref(&self) -> &Self::Target {
170        self.inner.as_ref()
171    }
172}
173
174impl From<isize> for ArcAtomicSignedCount {
175    /// Converts an initial counter value into a shared signed atomic counter.
176    ///
177    /// # Parameters
178    ///
179    /// * `value` - The initial counter value.
180    ///
181    /// # Returns
182    ///
183    /// A shared signed counter wrapper initialized to `value`.
184    #[inline]
185    fn from(value: isize) -> Self {
186        Self::new(value)
187    }
188}
189
190impl From<AtomicSignedCount> for ArcAtomicSignedCount {
191    /// Converts a signed atomic counter into a shared signed counter wrapper.
192    ///
193    /// # Parameters
194    ///
195    /// * `counter` - The signed counter container to share.
196    ///
197    /// # Returns
198    ///
199    /// A shared signed counter wrapper owning `counter`.
200    #[inline]
201    fn from(counter: AtomicSignedCount) -> Self {
202        Self::from_count(counter)
203    }
204}
205
206impl From<Arc<AtomicSignedCount>> for ArcAtomicSignedCount {
207    /// Converts an existing shared signed atomic counter into its wrapper.
208    ///
209    /// # Parameters
210    ///
211    /// * `inner` - The shared signed atomic counter to wrap.
212    ///
213    /// # Returns
214    ///
215    /// A wrapper around `inner`.
216    #[inline]
217    fn from(inner: Arc<AtomicSignedCount>) -> Self {
218        Self::from_arc(inner)
219    }
220}
221
222impl fmt::Debug for ArcAtomicSignedCount {
223    /// Formats the current counter value and sharing state for debugging.
224    ///
225    /// # Parameters
226    ///
227    /// * `f` - The formatter receiving the debug representation.
228    ///
229    /// # Returns
230    ///
231    /// A formatting result from the formatter.
232    #[inline]
233    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234        f.debug_struct("ArcAtomicSignedCount")
235            .field("value", &self.get())
236            .field("strong_count", &self.strong_count())
237            .finish()
238    }
239}
240
241impl fmt::Display for ArcAtomicSignedCount {
242    /// Formats the current counter value with decimal display formatting.
243    ///
244    /// # Parameters
245    ///
246    /// * `f` - The formatter receiving the displayed value.
247    ///
248    /// # Returns
249    ///
250    /// A formatting result from the formatter.
251    #[inline]
252    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253        write!(f, "{}", self.get())
254    }
255}