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