qubit-atomic 0.10.0

User-friendly atomic operations wrapper providing JDK-like atomic API
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/

//! # Generic Atomic Wrapper
//!
//! Provides the public [`Atomic<T>`] wrapper for supported primitive values.
//!
//! # Author
//!
//! Haixing Hu

use std::fmt;

use super::atomic_integer_value::AtomicIntegerValue;
use super::atomic_number_ops::AtomicNumberOps;
use super::atomic_ops::AtomicOps;
use super::atomic_value::AtomicValue;

/// A high-level atomic wrapper for supported primitive value types.
///
/// This type is the main entry point for primitive atomic values. It hides
/// explicit memory-ordering parameters behind crate-defined defaults while
/// still exposing the raw backend through [`inner`](Self::inner) for advanced
/// use cases.
///
/// Supported value types are:
///
/// - `bool`
/// - `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`, `u128`, `i128`
/// - `usize`, `isize`
/// - `f32`, `f64`
///
/// The `i128` and `u128` specializations use `portable-atomic` internally
/// because the corresponding standard-library atomic types are not yet stable.
///
/// # Specialization API
///
/// Rustdoc documents [`Atomic<T>`] as one generic type rather than generating a
/// separate page for each concrete `T`. The following table summarizes the
/// methods available for each specialization:
///
/// | Specialization | Additional methods |
/// | --- | --- |
/// | `Atomic<bool>` | `fetch_set`, `fetch_clear`, `fetch_not`, `fetch_and`, `fetch_or`, `fetch_xor`, `set_if_false`, `set_if_true` |
/// | `Atomic<i8>`, `Atomic<i16>`, `Atomic<i32>`, `Atomic<i64>`, `Atomic<i128>`, `Atomic<isize>` | `fetch_add`, `fetch_sub`, `fetch_mul`, `fetch_div`, `fetch_inc`, `fetch_dec`, `fetch_and`, `fetch_or`, `fetch_xor`, `fetch_not`, `fetch_accumulate`, `fetch_max`, `fetch_min` |
/// | `Atomic<u8>`, `Atomic<u16>`, `Atomic<u32>`, `Atomic<u64>`, `Atomic<u128>`, `Atomic<usize>` | `fetch_add`, `fetch_sub`, `fetch_mul`, `fetch_div`, `fetch_inc`, `fetch_dec`, `fetch_and`, `fetch_or`, `fetch_xor`, `fetch_not`, `fetch_accumulate`, `fetch_max`, `fetch_min` |
/// | `Atomic<f32>`, `Atomic<f64>` | `fetch_add`, `fetch_sub`, `fetch_mul`, `fetch_div` |
///
/// All supported specializations also provide [`new`](Self::new),
/// [`load`](Self::load), [`store`](Self::store), [`swap`](Self::swap),
/// [`compare_set`](Self::compare_set),
/// [`compare_set_weak`](Self::compare_set_weak),
/// [`compare_and_exchange`](Self::compare_and_exchange),
/// [`compare_and_exchange_weak`](Self::compare_and_exchange_weak),
/// [`fetch_update`](Self::fetch_update), and [`inner`](Self::inner).
///
/// # Example
///
/// ```rust
/// use qubit_atomic::Atomic;
///
/// let counter = Atomic::new(0);
/// counter.fetch_inc();
/// assert_eq!(counter.load(), 1);
///
/// let flag = Atomic::new(false);
/// assert_eq!(flag.fetch_set(), false);
/// assert!(flag.load());
/// ```
///
/// When the value type is ambiguous (for example integer literals), specify
/// `T` explicitly with a [turbofish] on the constructor, or by annotating the
/// binding:
///
/// ```rust
/// use qubit_atomic::Atomic;
///
/// let wide: Atomic<u64> = Atomic::new(0);
/// assert_eq!(wide.load(), 0u64);
///
/// let narrow = Atomic::<i16>::new(0);
/// assert_eq!(narrow.load(), 0i16);
/// ```
///
/// [turbofish]: https://doc.rust-lang.org/book/appendix-02-operators.html#the-turbofish
///
/// # Author
///
/// Haixing Hu
#[doc(alias = "AtomicBool")]
#[doc(alias = "AtomicI8")]
#[doc(alias = "AtomicU8")]
#[doc(alias = "AtomicI16")]
#[doc(alias = "AtomicU16")]
#[doc(alias = "AtomicI32")]
#[doc(alias = "AtomicU32")]
#[doc(alias = "AtomicI64")]
#[doc(alias = "AtomicU64")]
#[doc(alias = "AtomicI128")]
#[doc(alias = "AtomicU128")]
#[doc(alias = "AtomicIsize")]
#[doc(alias = "AtomicUsize")]
#[doc(alias = "AtomicF32")]
#[doc(alias = "AtomicF64")]
#[repr(transparent)]
pub struct Atomic<T>
where
    T: AtomicValue,
{
    /// Primitive backend that performs the concrete atomic operations for `T`.
    primitive: T::Primitive,
}

impl<T> Atomic<T>
where
    T: AtomicValue,
{
    /// Creates a new atomic value.
    ///
    /// # Parameters
    ///
    /// * `value` - The initial value.
    ///
    /// # Returns
    ///
    /// An atomic wrapper initialized to `value`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(42);
    /// assert_eq!(atomic.load(), 42);
    /// ```
    ///
    /// To pick a concrete `T` when inference would be ambiguous (for example
    /// `0` as `u64` vs `i32`), write `Atomic::<T>::new(...)` (turbofish) or add
    /// a type annotation on the binding (for example `let x: Atomic<u64> = ...`):
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let a = Atomic::<u64>::new(0);
    /// assert_eq!(a.load(), 0u64);
    ///
    /// let b: Atomic<isize> = Atomic::new(0);
    /// assert_eq!(b.load(), 0isize);
    /// ```
    #[inline]
    pub fn new(value: T) -> Self {
        Self {
            primitive: T::new_primitive(value),
        }
    }

    /// Loads the current value.
    ///
    /// Uses `Acquire` ordering by default.
    ///
    /// # Returns
    ///
    /// The current value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(7);
    /// assert_eq!(atomic.load(), 7);
    /// ```
    #[inline]
    pub fn load(&self) -> T {
        AtomicOps::load(&self.primitive)
    }

    /// Stores a new value.
    ///
    /// Uses `Release` ordering by default.
    ///
    /// # Parameters
    ///
    /// * `value` - The new value to store.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(1);
    /// atomic.store(2);
    /// assert_eq!(atomic.load(), 2);
    /// ```
    #[inline]
    pub fn store(&self, value: T) {
        AtomicOps::store(&self.primitive, value);
    }

    /// Swaps the current value with `value`.
    ///
    /// Unlike [`store`](Self::store), this returns the value that was in the
    /// atomic immediately before the swap, in the same atomic step. Use
    /// [`store`](Self::store) when you do not need the previous value.
    ///
    /// Uses `AcqRel` ordering by default.
    ///
    /// # Parameters
    ///
    /// * `value` - The new value to store.
    ///
    /// # Returns
    ///
    /// The previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let a = Atomic::new(100);
    /// a.store(200);
    /// // store has no return value; you only see the new value via load().
    /// assert_eq!(a.load(), 200);
    ///
    /// let b = Atomic::new(100);
    /// // swap returns the old value and installs the new one atomically.
    /// assert_eq!(b.swap(200), 100);
    /// assert_eq!(b.load(), 200);
    /// ```
    #[inline]
    pub fn swap(&self, value: T) -> T {
        AtomicOps::swap(&self.primitive, value)
    }

    /// Compares the current value with `current` and stores `new` on match.
    ///
    /// Uses `AcqRel` ordering on success and `Acquire` ordering on failure.
    ///
    /// # Parameters
    ///
    /// * `current` - The expected current value.
    /// * `new` - The replacement value to store when the comparison matches.
    ///
    /// # Returns
    ///
    /// `Ok(())` when the value was replaced.
    ///
    /// # Errors
    ///
    /// Returns `Err(actual)` with the observed value when the comparison
    /// fails. In that case, `new` is not stored.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(1);
    /// assert!(atomic.compare_set(1, 2).is_ok());
    /// assert_eq!(atomic.load(), 2);
    /// assert_eq!(atomic.compare_set(1, 3), Err(2));
    /// ```
    #[inline]
    pub fn compare_set(&self, current: T, new: T) -> Result<(), T> {
        AtomicOps::compare_set(&self.primitive, current, new)
    }

    /// Weak version of [`compare_set`](Self::compare_set).
    ///
    /// This operation may fail spuriously and is intended for retry loops.
    ///
    /// # Parameters
    ///
    /// * `current` - The expected current value.
    /// * `new` - The replacement value to store when the comparison matches.
    ///
    /// # Returns
    ///
    /// `Ok(())` when the value was replaced.
    ///
    /// # Errors
    ///
    /// Returns `Err(actual)` with the observed value when the comparison
    /// fails, including possible spurious failures. In that case, `new` is not
    /// stored.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(1);
    /// loop {
    ///     match atomic.compare_set_weak(1, 2) {
    ///         Ok(()) => break,
    ///         Err(actual) => assert_eq!(actual, 1),
    ///     }
    /// }
    /// assert_eq!(atomic.load(), 2);
    /// ```
    #[inline]
    pub fn compare_set_weak(&self, current: T, new: T) -> Result<(), T> {
        AtomicOps::compare_set_weak(&self.primitive, current, new)
    }

    /// Compares and exchanges the value, returning the value seen before the
    /// operation.
    ///
    /// If the return value equals `current`, the exchange succeeded.
    ///
    /// # Parameters
    ///
    /// * `current` - The expected current value.
    /// * `new` - The replacement value to store when the comparison matches.
    ///
    /// # Returns
    ///
    /// The value observed before the operation completed. If the returned
    /// value equals `current`, the exchange succeeded; otherwise it is the
    /// actual value that prevented the exchange.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(5);
    /// assert_eq!(atomic.compare_and_exchange(5, 10), 5);
    /// assert_eq!(atomic.load(), 10);
    /// assert_eq!(atomic.compare_and_exchange(5, 0), 10);
    /// ```
    #[inline]
    pub fn compare_and_exchange(&self, current: T, new: T) -> T {
        AtomicOps::compare_exchange(&self.primitive, current, new)
    }

    /// Weak version of [`compare_and_exchange`](Self::compare_and_exchange).
    ///
    /// This operation may fail spuriously and is intended for retry loops.
    ///
    /// # Parameters
    ///
    /// * `current` - The expected current value.
    /// * `new` - The replacement value to store when the comparison matches.
    ///
    /// # Returns
    ///
    /// The value observed before the operation completed. Because this
    /// operation may fail spuriously, a returned value equal to `current` does
    /// not by itself prove that `new` was stored; use
    /// [`compare_set_weak`](Self::compare_set_weak) when the caller needs an
    /// explicit success indicator.
    ///
    /// # Example
    ///
    /// Weak CAS may fail spuriously; retry until [`load`](Self::load) shows the
    /// expected outcome (or use [`compare_set_weak`](Self::compare_set_weak)
    /// which reports success explicitly).
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(5);
    /// loop {
    ///     let _ = atomic.compare_and_exchange_weak(5, 10);
    ///     if atomic.load() == 10 {
    ///         break;
    ///     }
    /// }
    /// ```
    #[inline]
    pub fn compare_and_exchange_weak(&self, current: T, new: T) -> T {
        AtomicOps::compare_exchange_weak(&self.primitive, current, new)
    }

    /// Updates the value with a function and returns the previous value.
    ///
    /// The update uses a CAS loop until it succeeds. The closure may be called
    /// more than once under contention.
    ///
    /// # Parameters
    ///
    /// * `f` - A function that maps the current value to the next value.
    ///
    /// # Returns
    ///
    /// The value before the successful update.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(3);
    /// assert_eq!(atomic.fetch_update(|x| x * 2), 3);
    /// assert_eq!(atomic.load(), 6);
    /// ```
    #[inline]
    pub fn fetch_update<F>(&self, f: F) -> T
    where
        F: Fn(T) -> T,
    {
        AtomicOps::fetch_update(&self.primitive, f)
    }

    /// Returns the raw backend atomic value.
    ///
    /// Use this method only when the default orderings are not appropriate
    /// and the caller needs direct access to the standard-library backend.
    ///
    /// # Returns
    ///
    /// A shared reference to the raw backend atomic value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    /// use std::sync::atomic::Ordering;
    ///
    /// let atomic = Atomic::<i32>::new(0);
    /// assert_eq!(atomic.inner().load(Ordering::Relaxed), 0);
    /// ```
    #[inline]
    pub fn inner(&self) -> &T::Inner {
        T::inner(&self.primitive)
    }
}

impl<T> Atomic<T>
where
    T: AtomicValue,
    T::Primitive: AtomicNumberOps<Value = T>,
{
    /// Adds `delta` to the value and returns the previous value.
    ///
    /// Integer atomics use relaxed ordering for this operation. Floating-point
    /// atomics use a CAS loop.
    ///
    /// # Parameters
    ///
    /// * `delta` - The value to add.
    ///
    /// # Returns
    ///
    /// The value before the addition.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(10);
    /// assert_eq!(atomic.fetch_add(3), 10);
    /// assert_eq!(atomic.load(), 13);
    /// ```
    #[inline]
    pub fn fetch_add(&self, delta: T) -> T {
        AtomicNumberOps::fetch_add(&self.primitive, delta)
    }

    /// Subtracts `delta` from the value and returns the previous value.
    ///
    /// Integer atomics use relaxed ordering for this operation. Floating-point
    /// atomics use a CAS loop.
    ///
    /// # Parameters
    ///
    /// * `delta` - The value to subtract.
    ///
    /// # Returns
    ///
    /// The value before the subtraction.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(10);
    /// assert_eq!(atomic.fetch_sub(3), 10);
    /// assert_eq!(atomic.load(), 7);
    /// ```
    #[inline]
    pub fn fetch_sub(&self, delta: T) -> T {
        AtomicNumberOps::fetch_sub(&self.primitive, delta)
    }

    /// Multiplies the value by `factor` and returns the previous value.
    ///
    /// This operation uses a CAS loop.
    ///
    /// # Parameters
    ///
    /// * `factor` - The value to multiply by.
    ///
    /// # Returns
    ///
    /// The value before the multiplication.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(3);
    /// assert_eq!(atomic.fetch_mul(4), 3);
    /// assert_eq!(atomic.load(), 12);
    /// ```
    #[inline]
    pub fn fetch_mul(&self, factor: T) -> T {
        AtomicNumberOps::fetch_mul(&self.primitive, factor)
    }

    /// Divides the value by `divisor` and returns the previous value.
    ///
    /// This operation uses a CAS loop.
    ///
    /// # Parameters
    ///
    /// * `divisor` - The value to divide by.
    ///
    /// # Returns
    ///
    /// The value before the division.
    ///
    /// # Panics
    ///
    /// For integer specializations, panics if `divisor` is zero. Floating-point
    /// specializations follow IEEE-754 division semantics and do not panic
    /// solely because `divisor` is zero.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(20);
    /// assert_eq!(atomic.fetch_div(4), 20);
    /// assert_eq!(atomic.load(), 5);
    /// ```
    #[inline]
    pub fn fetch_div(&self, divisor: T) -> T {
        AtomicNumberOps::fetch_div(&self.primitive, divisor)
    }
}

impl<T> Atomic<T>
where
    T: AtomicIntegerValue,
{
    /// Increments the value by one and returns the previous value.
    ///
    /// # Returns
    ///
    /// The value before the increment.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(0);
    /// assert_eq!(atomic.fetch_inc(), 0);
    /// assert_eq!(atomic.load(), 1);
    /// ```
    #[inline]
    pub fn fetch_inc(&self) -> T {
        T::fetch_inc(&self.primitive)
    }

    /// Decrements the value by one and returns the previous value.
    ///
    /// # Returns
    ///
    /// The value before the decrement.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(1);
    /// assert_eq!(atomic.fetch_dec(), 1);
    /// assert_eq!(atomic.load(), 0);
    /// ```
    #[inline]
    pub fn fetch_dec(&self) -> T {
        T::fetch_dec(&self.primitive)
    }

    /// Applies bitwise AND and returns the previous value.
    ///
    /// # Parameters
    ///
    /// * `value` - The mask to apply.
    ///
    /// # Returns
    ///
    /// The value before the operation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::<u8>::new(0b1111);
    /// assert_eq!(atomic.fetch_and(0b1010), 0b1111);
    /// assert_eq!(atomic.load(), 0b1010);
    /// ```
    #[inline]
    pub fn fetch_and(&self, value: T) -> T {
        T::fetch_and(&self.primitive, value)
    }

    /// Applies bitwise OR and returns the previous value.
    ///
    /// # Parameters
    ///
    /// * `value` - The mask to apply.
    ///
    /// # Returns
    ///
    /// The value before the operation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::<u8>::new(0b1000);
    /// assert_eq!(atomic.fetch_or(0b0011), 0b1000);
    /// assert_eq!(atomic.load(), 0b1011);
    /// ```
    #[inline]
    pub fn fetch_or(&self, value: T) -> T {
        T::fetch_or(&self.primitive, value)
    }

    /// Applies bitwise XOR and returns the previous value.
    ///
    /// # Parameters
    ///
    /// * `value` - The mask to apply.
    ///
    /// # Returns
    ///
    /// The value before the operation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::<u8>::new(0b1111);
    /// assert_eq!(atomic.fetch_xor(0b1010), 0b1111);
    /// assert_eq!(atomic.load(), 0b0101);
    /// ```
    #[inline]
    pub fn fetch_xor(&self, value: T) -> T {
        T::fetch_xor(&self.primitive, value)
    }

    /// Flips all bits and returns the previous value.
    ///
    /// # Returns
    ///
    /// The value before the operation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::<i32>::new(0);
    /// assert_eq!(atomic.fetch_not(), 0);
    /// assert_eq!(atomic.load(), !0);
    /// ```
    #[inline]
    pub fn fetch_not(&self) -> T {
        T::fetch_not(&self.primitive)
    }

    /// Updates the value by accumulating it with `value`.
    ///
    /// # Parameters
    ///
    /// * `value` - The right-hand input 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.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(10);
    /// assert_eq!(atomic.fetch_accumulate(5, |a, b| a + b), 10);
    /// assert_eq!(atomic.load(), 15);
    /// ```
    #[inline]
    pub fn fetch_accumulate<F>(&self, value: T, f: F) -> T
    where
        F: Fn(T, T) -> T,
    {
        T::fetch_accumulate(&self.primitive, value, f)
    }

    /// Replaces the value with the maximum of the current value and `value`.
    ///
    /// # Parameters
    ///
    /// * `value` - The value to compare with the current value.
    ///
    /// # Returns
    ///
    /// The value before the operation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(3);
    /// assert_eq!(atomic.fetch_max(10), 3);
    /// assert_eq!(atomic.load(), 10);
    /// ```
    #[inline]
    pub fn fetch_max(&self, value: T) -> T {
        T::fetch_max(&self.primitive, value)
    }

    /// Replaces the value with the minimum of the current value and `value`.
    ///
    /// # Parameters
    ///
    /// * `value` - The value to compare with the current value.
    ///
    /// # Returns
    ///
    /// The value before the operation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(10);
    /// assert_eq!(atomic.fetch_min(3), 10);
    /// assert_eq!(atomic.load(), 3);
    /// ```
    #[inline]
    pub fn fetch_min(&self, value: T) -> T {
        T::fetch_min(&self.primitive, value)
    }
}

impl Atomic<bool> {
    /// Stores `true` and returns the previous value.
    ///
    /// # Returns
    ///
    /// The previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(false);
    /// assert_eq!(flag.fetch_set(), false);
    /// assert!(flag.load());
    /// ```
    #[inline]
    pub fn fetch_set(&self) -> bool {
        self.primitive.fetch_set()
    }

    /// Stores `false` and returns the previous value.
    ///
    /// # Returns
    ///
    /// The previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(true);
    /// assert_eq!(flag.fetch_clear(), true);
    /// assert!(!flag.load());
    /// ```
    #[inline]
    pub fn fetch_clear(&self) -> bool {
        self.primitive.fetch_clear()
    }

    /// Negates the value and returns the previous value.
    ///
    /// # Returns
    ///
    /// The previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(false);
    /// assert_eq!(flag.fetch_not(), false);
    /// assert!(flag.load());
    /// ```
    #[inline]
    pub fn fetch_not(&self) -> bool {
        self.primitive.fetch_not()
    }

    /// Applies logical AND and returns the previous value.
    ///
    /// # Parameters
    ///
    /// * `value` - The value to combine with the current value.
    ///
    /// # Returns
    ///
    /// The previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(true);
    /// assert_eq!(flag.fetch_and(false), true);
    /// assert!(!flag.load());
    /// ```
    #[inline]
    pub fn fetch_and(&self, value: bool) -> bool {
        self.primitive.fetch_and(value)
    }

    /// Applies logical OR and returns the previous value.
    ///
    /// # Parameters
    ///
    /// * `value` - The value to combine with the current value.
    ///
    /// # Returns
    ///
    /// The previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(false);
    /// assert_eq!(flag.fetch_or(true), false);
    /// assert!(flag.load());
    /// ```
    #[inline]
    pub fn fetch_or(&self, value: bool) -> bool {
        self.primitive.fetch_or(value)
    }

    /// Applies logical XOR and returns the previous value.
    ///
    /// # Parameters
    ///
    /// * `value` - The value to combine with the current value.
    ///
    /// # Returns
    ///
    /// The previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(true);
    /// assert_eq!(flag.fetch_xor(true), true);
    /// assert!(!flag.load());
    /// ```
    #[inline]
    pub fn fetch_xor(&self, value: bool) -> bool {
        self.primitive.fetch_xor(value)
    }

    /// Stores `new` only when the current value is `false`.
    ///
    /// # Parameters
    ///
    /// * `new` - The replacement value.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the value was replaced.
    ///
    /// # Errors
    ///
    /// Returns `Err(true)` if the observed current value was already `true`.
    /// In that case, `new` is not stored.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(false);
    /// assert!(flag.set_if_false(true).is_ok());
    /// assert!(flag.load());
    /// assert!(flag.set_if_false(false).is_err());
    /// ```
    #[inline]
    pub fn set_if_false(&self, new: bool) -> Result<(), bool> {
        self.primitive.set_if_false(new)
    }

    /// Stores `new` only when the current value is `true`.
    ///
    /// # Parameters
    ///
    /// * `new` - The replacement value.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the value was replaced.
    ///
    /// # Errors
    ///
    /// Returns `Err(false)` if the observed current value was already `false`.
    /// In that case, `new` is not stored.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let flag = Atomic::new(true);
    /// assert!(flag.set_if_true(false).is_ok());
    /// assert!(!flag.load());
    /// assert!(flag.set_if_true(true).is_err());
    /// ```
    #[inline]
    pub fn set_if_true(&self, new: bool) -> Result<(), bool> {
        self.primitive.set_if_true(new)
    }
}

impl<T> Default for Atomic<T>
where
    T: AtomicValue + Default,
{
    /// Creates an atomic with [`Default::default`] as the initial value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::<i32>::default();
    /// assert_eq!(atomic.load(), 0);
    /// ```
    #[inline]
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T> From<T> for Atomic<T>
where
    T: AtomicValue,
{
    /// Converts `value` into an [`Atomic`] via [`Atomic::new`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::from(42i32);
    /// assert_eq!(atomic.load(), 42);
    /// ```
    #[inline]
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

impl<T> fmt::Debug for Atomic<T>
where
    T: AtomicValue + fmt::Debug,
{
    /// Formats the loaded value as `Atomic { value: ... }`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(7);
    /// assert!(format!("{:?}", atomic).contains("7"));
    /// ```
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Atomic")
            .field("value", &self.load())
            .finish()
    }
}

impl<T> fmt::Display for Atomic<T>
where
    T: AtomicValue + fmt::Display,
{
    /// Formats the loaded value using its [`Display`](fmt::Display) implementation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_atomic::Atomic;
    ///
    /// let atomic = Atomic::new(42);
    /// assert_eq!(format!("{}", atomic), "42");
    /// ```
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.load())
    }
}