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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*******************************************************************************
*
* Copyright (c) 2025 - 2026 Haixing Hu.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0.
*
******************************************************************************/
//! # Atomic Integer Value Marker
//!
//! Defines the hidden marker trait for integer values supported by
//! integer-only [`crate::Atomic<T>`] operations.
//!
use std::sync::atomic::Ordering;
use super::{
atomic_i8,
atomic_i16,
atomic_i32,
atomic_i64,
atomic_i128,
atomic_isize,
atomic_u8,
atomic_u16,
atomic_u32,
atomic_u64,
atomic_u128,
atomic_usize,
atomic_value::AtomicValue,
sealed,
};
/// Marker trait for integer values supported by integer-only operations.
///
/// This trait is sealed and hidden from the public documentation. It provides
/// the integer-only operations used by [`crate::Atomic<T>`] when `T` is one of
/// the supported signed or unsigned integer types.
#[doc(hidden)]
pub trait AtomicIntegerValue: AtomicValue {
/// Increments the atomic integer and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
///
/// # Returns
///
/// The value before the increment.
fn fetch_inc(primitive: &Self::Primitive) -> Self;
/// Increments the atomic integer with an explicit memory ordering and
/// returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `ordering` - The memory ordering used by the atomic read-modify-write
/// operation.
///
/// # Returns
///
/// The value before the increment.
fn fetch_inc_with_ordering(primitive: &Self::Primitive, ordering: Ordering) -> Self;
/// Decrements the atomic integer and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
///
/// # Returns
///
/// The value before the decrement.
fn fetch_dec(primitive: &Self::Primitive) -> Self;
/// Decrements the atomic integer with an explicit memory ordering and
/// returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `ordering` - The memory ordering used by the atomic read-modify-write
/// operation.
///
/// # Returns
///
/// The value before the decrement.
fn fetch_dec_with_ordering(primitive: &Self::Primitive, ordering: Ordering) -> Self;
/// Adds a delta with an explicit memory ordering and returns the previous
/// value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The delta to add.
/// * `ordering` - The memory ordering used by the atomic read-modify-write
/// operation.
///
/// # Returns
///
/// The value before the addition.
fn fetch_add_with_ordering(
primitive: &Self::Primitive,
value: Self,
ordering: Ordering,
) -> Self;
/// Subtracts a delta with an explicit memory ordering and returns the
/// previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The delta to subtract.
/// * `ordering` - The memory ordering used by the atomic read-modify-write
/// operation.
///
/// # Returns
///
/// The value before the subtraction.
fn fetch_sub_with_ordering(
primitive: &Self::Primitive,
value: Self,
ordering: Ordering,
) -> Self;
/// Applies bitwise AND and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The mask to AND with the current value.
///
/// # Returns
///
/// The value before the operation.
fn fetch_and(primitive: &Self::Primitive, value: Self) -> Self;
/// Applies bitwise OR and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The mask to OR with the current value.
///
/// # Returns
///
/// The value before the operation.
fn fetch_or(primitive: &Self::Primitive, value: Self) -> Self;
/// Applies bitwise XOR and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The mask to XOR with the current value.
///
/// # Returns
///
/// The value before the operation.
fn fetch_xor(primitive: &Self::Primitive, value: Self) -> Self;
/// Flips all bits and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
///
/// # Returns
///
/// The value before the operation.
fn fetch_not(primitive: &Self::Primitive) -> Self;
/// Accumulates a value and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The right-hand input passed to the accumulator.
/// * `f` - A function that combines the current value and `value`.
///
/// # Returns
///
/// The value before the successful update.
///
/// The closure may be called more than once when concurrent updates cause
/// CAS retries.
fn fetch_accumulate<F>(primitive: &Self::Primitive, value: Self, f: F) -> Self
where
F: Fn(Self, Self) -> Self;
/// Replaces with the maximum value and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The value to compare with the current value.
///
/// # Returns
///
/// The value before the operation.
fn fetch_max(primitive: &Self::Primitive, value: Self) -> Self;
/// Replaces with the minimum value and returns the previous value.
///
/// # Parameters
///
/// * `primitive` - The primitive wrapper to update.
/// * `value` - The value to compare with the current value.
///
/// # Returns
///
/// The value before the operation.
fn fetch_min(primitive: &Self::Primitive, value: Self) -> Self;
}
impl_atomic_integer_value!(u8, atomic_u8::AtomicU8, std::sync::atomic::AtomicU8);
impl_atomic_integer_value!(i8, atomic_i8::AtomicI8, std::sync::atomic::AtomicI8);
impl_atomic_integer_value!(u16, atomic_u16::AtomicU16, std::sync::atomic::AtomicU16);
impl_atomic_integer_value!(i16, atomic_i16::AtomicI16, std::sync::atomic::AtomicI16);
impl_atomic_integer_value!(u32, atomic_u32::AtomicU32, std::sync::atomic::AtomicU32);
impl_atomic_integer_value!(i32, atomic_i32::AtomicI32, std::sync::atomic::AtomicI32);
impl_atomic_integer_value!(u64, atomic_u64::AtomicU64, std::sync::atomic::AtomicU64);
impl_atomic_integer_value!(i64, atomic_i64::AtomicI64, std::sync::atomic::AtomicI64);
impl_atomic_integer_value!(u128, atomic_u128::AtomicU128, portable_atomic::AtomicU128);
impl_atomic_integer_value!(i128, atomic_i128::AtomicI128, portable_atomic::AtomicI128);
impl_atomic_integer_value!(
usize,
atomic_usize::AtomicUsize,
std::sync::atomic::AtomicUsize
);
impl_atomic_integer_value!(
isize,
atomic_isize::AtomicIsize,
std::sync::atomic::AtomicIsize
);