atomic-traits 0.4.0

The traits for generic atomic operations
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
//! The traits for generic atomic operations
//!
//! # Compatibility
//!
//! The crate is tested for rustc 1.34 and greater.
//!
//! # Example
//!
//! ```
//! # extern crate num_traits;
//! # extern crate atomic_traits;
//! use std::sync::atomic::{AtomicUsize, Ordering};
//!
//! use num_traits::One;
//! use atomic_traits::{Atomic, NumOps, fetch};
//! # use atomic_traits::fetch::{Add, Sub};
//!
//! #[derive(Debug, Default)]
//! pub struct RefCnt<T>(T);
//!
//! impl<T> RefCnt<T>
//! where
//!     T: Atomic + NumOps + Default,
//!     <T as Atomic>::Type: One
//! {
//!     pub fn inc(&self) -> <T as Atomic>::Type {
//!         self.0.fetch_add(<T as Atomic>::Type::one(), Ordering::Acquire)
//!     }
//!
//!     pub fn dec(&self) -> <T as Atomic>::Type {
//!         self.0.fetch_sub(<T as Atomic>::Type::one(), Ordering::Release)
//!     }
//!
//!     pub fn val(&self) -> <T as Atomic>::Type {
//!         self.0.load(Ordering::SeqCst)
//!     }
//! }
//!
//! # fn main() {
//! let refcnt = RefCnt::<AtomicUsize>::default();
//!
//! assert_eq!(refcnt.inc(), 0);
//! assert_eq!(refcnt.dec(), 1);
//! assert_eq!(refcnt.val(), 0);
//! # }
//! ```
#![no_std]
#![deny(missing_docs)]
#![cfg_attr(feature = "atomic_bool_fetch_not", feature(atomic_bool_fetch_not))]
#![cfg_attr(feature = "atomic_from_mut", feature(atomic_from_mut))]

#[macro_use]
extern crate cfg_if;

cfg_if! {
    if #[cfg(doctest)] {
        #[macro_use]
        extern crate doc_comment;

        #[cfg(doctest)]
        doc_comment!(include_str!("../README.md"));
    }
}

use core::sync::atomic::*;

pub mod fetch;

/// Generic atomic types
pub trait Atomic {
    /// The underlying type
    type Type;

    /// Creates a new atomic type.
    fn new(v: Self::Type) -> Self;

    /// Returns a mutable reference to the underlying type.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let mut some_bool = AtomicBool::new(true);
    /// assert_eq!(*Atomic::get_mut(&mut some_bool), true);
    /// *Atomic::get_mut(&mut some_bool) = false;
    /// assert_eq!(Atomic::load(&some_bool, Ordering::SeqCst), false);
    /// ```
    #[cfg(all(
        any(feature = "atomic_access", feature = "since_1_15_0"),
        not(feature = "loom_atomics")
    ))]
    fn get_mut(&mut self) -> &mut Self::Type;

    /// Consumes the atomic and returns the contained value.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::AtomicBool;
    /// use atomic_traits::Atomic;
    ///
    /// let some_bool = AtomicBool::new(true);
    /// assert_eq!(Atomic::into_inner(some_bool), true);
    /// ```
    #[cfg(all(
        any(feature = "atomic_access", feature = "since_1_15_0"),
        not(feature = "loom_atomics")
    ))]
    fn into_inner(self) -> Self::Type;

    /// Loads a value from the atomic type.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let some_bool = AtomicBool::new(true);
    ///
    /// assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), true);
    /// ```
    fn load(&self, order: Ordering) -> Self::Type;

    /// Stores a value into the atomic type.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let some_bool = AtomicBool::new(true);
    ///
    /// Atomic::store(&some_bool, false, Ordering::Relaxed);
    /// assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
    /// ```
    fn store(&self, val: Self::Type, order: Ordering);

    /// Stores a value into the atomic type, returning the previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let some_bool = AtomicBool::new(true);
    ///
    /// assert_eq!(Atomic::swap(&some_bool, false, Ordering::Relaxed), true);
    /// assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
    /// ```
    fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type;

    /// Stores a value into the atomic type if the current value is the same as the `current` value.
    ///
    /// The return value is always the previous value. If it is equal to `current`, then the value was updated.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let some_bool = AtomicBool::new(true);
    ///
    /// assert_eq!(Atomic::compare_and_swap(&some_bool, true, false, Ordering::Relaxed), true);
    /// assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
    ///
    /// assert_eq!(Atomic::compare_and_swap(&some_bool, true, true, Ordering::Relaxed), false);
    /// assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
    /// ```
    #[cfg_attr(
        feature = "since_1_50_0",
        deprecated = "Use `compare_exchange` or `compare_exchange_weak` instead"
    )]
    fn compare_and_swap(&self, current: Self::Type, new: Self::Type, order: Ordering)
        -> Self::Type;

    /// Stores a value into the atomic type if the current value is the same as the `current` value.
    ///
    /// The return value is a result indicating whether the new value was written and containing the previous value.
    /// On success this value is guaranteed to be equal to `current`.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let some_bool = AtomicBool::new(true);
    ///
    /// assert_eq!(Atomic::compare_exchange(&some_bool,
    ///                                      true,
    ///                                      false,
    ///                                      Ordering::Acquire,
    ///                                      Ordering::Relaxed),
    ///            Ok(true));
    /// assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
    ///
    /// assert_eq!(Atomic::compare_exchange(&some_bool,
    ///                                     true,
    ///                                     true,
    ///                                     Ordering::SeqCst,
    ///                                     Ordering::Acquire),
    ///            Err(false));
    /// assert_eq!(Atomic::load(&some_bool, Ordering::Relaxed), false);
    /// ```
    #[cfg(any(feature = "extended_compare_and_swap", feature = "since_1_10_0"))]
    fn compare_exchange(
        &self,
        current: Self::Type,
        new: Self::Type,
        success: Ordering,
        failure: Ordering,
    ) -> Result<Self::Type, Self::Type>;

    /// Stores a value into the atomic type if the current value is the same as the current value.
    ///
    /// Unlike `compare_exchange`, this function is allowed to spuriously fail even when the comparison succeeds,
    /// which can result in more efficient code on some platforms.
    /// The return value is a result indicating whether the new value was written and containing the previous value.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let val = AtomicBool::new(false);
    ///
    /// let new = true;
    /// let mut old = Atomic::load(&val, Ordering::Relaxed);
    /// loop {
    ///     match Atomic::compare_exchange_weak(&val, old, new, Ordering::SeqCst, Ordering::Relaxed) {
    ///         Ok(_) => break,
    ///         Err(x) => old = x,
    ///     }
    /// }
    /// ```
    #[cfg(any(feature = "extended_compare_and_swap", feature = "since_1_10_0"))]
    fn compare_exchange_weak(
        &self,
        current: Self::Type,
        new: Self::Type,
        success: Ordering,
        failure: Ordering,
    ) -> Result<Self::Type, Self::Type>;
}

cfg_if! {
    if #[cfg(all(any(feature = "atomic_nand", feature = "since_1_27_0"), not(feature = "loom_atomics")))] {
        /// The trait for types implementing atomic bitwise operations
        pub trait Bitwise: Atomic
            + fetch::And<Type = <Self as Atomic>::Type>
            + fetch::Nand<Type = <Self as Atomic>::Type>
            + fetch::Or<Type = <Self as Atomic>::Type>
            + fetch::Xor<Type = <Self as Atomic>::Type>
        {
        }
    } else {
        /// The trait for types implementing atomic bitwise operations
        pub trait Bitwise: Atomic
            + fetch::And<Type = <Self as Atomic>::Type>
            + fetch::Or<Type = <Self as Atomic>::Type>
            + fetch::Xor<Type = <Self as Atomic>::Type>
        {
        }
    }
}

cfg_if! {
    if #[cfg(feature = "loom_atomics")] {
        /// The trait for types implementing atomic numeric operations
        pub trait NumOps:
            Atomic
            + fetch::Add<Type = <Self as Atomic>::Type>
            + fetch::Sub<Type = <Self as Atomic>::Type>
            + fetch::Update<Type = <Self as Atomic>::Type>
        {
        }
    } else if #[cfg(feature = "since_1_45_0")] {
        /// The trait for types implementing atomic numeric operations
        pub trait NumOps:
            Atomic
            + fetch::Add<Type = <Self as Atomic>::Type>
            + fetch::Sub<Type = <Self as Atomic>::Type>
            + fetch::Update<Type = <Self as Atomic>::Type>
            + fetch::Max<Type = <Self as Atomic>::Type>
            + fetch::Min<Type = <Self as Atomic>::Type>
        {
        }
    } else {
        /// The trait for types implementing atomic numeric operations
        pub trait NumOps:
            Atomic
            + fetch::Add<Type = <Self as Atomic>::Type>
            + fetch::Sub<Type = <Self as Atomic>::Type>
        {
        }
    }
}

/// Returns a mutable pointer to the underlying type.
#[cfg(any(feature = "atomic_as_ptr", feature = "since_1_70_0"))]
pub trait AsPtr: Atomic {
    /// Returns a mutable pointer to the underlying type.
    ///
    /// # Examples
    ///
    /// ```ignore (extern-declaration)
    /// # fn main() {
    /// use std::sync::atomic::AtomicBool;
    /// use atomic_traits::Atomic;
    ///
    /// extern "C" {
    ///     fn my_atomic_op(arg: *mut bool);
    /// }
    ///
    /// let mut atomic = AtomicBool::new(true);
    /// unsafe {
    ///     my_atomic_op(Atomic::as_ptr(&atomic));
    /// }
    /// # }    
    fn as_ptr(&self) -> *mut Self::Type;
}

/// Creates a new atomic type from a pointer.
#[cfg(all(
    any(feature = "atomic_from_ptr", feature = "since_1_75_0"),
    not(feature = "loom_atomics")
))]
pub trait FromPtr: Atomic {
    /// Creates a new atomic type from a pointer.
    ///     
    /// # Safety
    ///
    /// * `ptr` must be aligned to `align_of::<Self>()` (note that on some platforms this can
    ///   be bigger than `align_of::<Self::Type>()`).
    /// * `ptr` must be [valid] for both reads and writes for the whole lifetime `'a`.
    /// * You must adhere to the [Memory model for atomic accesses]. In particular, it is not
    ///   allowed to mix atomic and non-atomic accesses, or atomic accesses of different sizes,
    ///   without synchronization.
    unsafe fn from_ptr<'a>(ptr: *mut Self::Type) -> &'a Self;
}

#[cfg(feature = "atomic_from_mut")]
/// Get atomic access to mutable atomic type or slice.
pub trait FromMut: Atomic
where
    Self: Sized,
{
    /// Get atomic access to an atomic type.
    ///
    /// # Examples
    ///
    /// ```
    /// #![feature(atomic_from_mut)]
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let mut some_bool = true;
    /// let a = <AtomicBool as Atomic>::from_mut(&mut some_bool);
    /// Atomic::store(&a, false, Ordering::Relaxed);
    /// assert_eq!(some_bool, false);
    /// ```    
    fn from_mut(v: &mut Self::Type) -> &mut Self;

    /// Get atomic access to a `&mut [Self::Type]` slice.
    ///
    /// # Examples
    ///
    /// ```
    /// #![feature(atomic_from_mut)]
    /// use std::sync::atomic::{AtomicBool, Ordering};
    ///
    /// let mut some_bools = [false; 10];
    /// let a = &*AtomicBool::from_mut_slice(&mut some_bools);
    /// std::thread::scope(|s| {
    ///     for i in 0..a.len() {
    ///         s.spawn(move || a[i].store(true, Ordering::Relaxed));
    ///     }
    /// });
    /// assert_eq!(some_bools, [true; 10]);
    /// ```    
    fn from_mut_slice(v: &mut [Self::Type]) -> &mut [Self];

    /// Get non-atomic access to a `&mut [Self]` slice.
    ///
    /// This is safe because the mutable reference guarantees that no other threads are
    /// concurrently accessing the atomic data.
    ///
    /// # Examples
    ///
    /// ```
    /// #![feature(atomic_from_mut, inline_const)]
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use atomic_traits::Atomic;
    ///
    /// let mut some_bools = [const { AtomicBool::new(false) }; 10];
    ///
    /// let view: &mut [bool] = <AtomicBool as Atomic>::get_mut_slice(&mut some_bools);
    /// assert_eq!(view, [false; 10]);
    /// view[..5].copy_from_slice(&[true; 5]);
    ///
    /// std::thread::scope(|s| {
    ///     for t in &some_bools[..5] {
    ///         s.spawn(move || assert_eq!(t.load(Ordering::Relaxed), true));
    ///     }
    ///
    ///     for f in &some_bools[5..] {
    ///         s.spawn(move || assert_eq!(f.load(Ordering::Relaxed), false));
    ///     }
    /// });
    /// ```    
    fn get_mut_slice(this: &mut [Self]) -> &mut [Self::Type];
}

macro_rules! impl_atomic {
    ($atomic:path : $primitive:ty ; $( $rest:tt ),*) => {
        impl_atomic!(__impl atomic $atomic : $primitive);

        $(
            impl_atomic!(__impl $rest $atomic : $primitive);
        )*

    };
    ($atomic:ident < $param:ident > ; $( $rest:tt ),*) => {
        impl<$param> Atomic for $atomic <$param> {
            type Type = *mut $param;

            impl_atomic!(__impl atomic_methods $atomic);
        }

        $(
            impl_atomic!(__impl $rest $atomic <$param>);
        )*
    };
    (__loom $atomic:ident < $param:ident >) => {
        impl<$param> Atomic for loom::sync::atomic::$atomic <$param> {
            type Type = *mut $param;

            impl_atomic!(__impl atomic_methods loom::sync::atomic::$atomic);
        }
    };
    (__impl atomic $atomic:path : $primitive:ty) => {
        impl Atomic for $atomic {
            type Type = $primitive;

            impl_atomic!(__impl atomic_methods $atomic);
        }
    };

    (__impl atomic_methods $atomic:path) => {
        #[inline(always)]
        fn new(v: Self::Type) -> Self {
            Self::new(v)
        }

        #[cfg(all(
            any(feature = "atomic_access", feature = "since_1_15_0"),
            not(feature = "loom_atomics")
        ))]
        #[inline(always)]
        fn get_mut(&mut self) -> &mut Self::Type {
            Self::get_mut(self)
        }

        #[cfg(all(
            any(feature = "atomic_access", feature = "since_1_15_0"),
            not(feature = "loom_atomics")
        ))]
        #[inline(always)]
        fn into_inner(self) -> Self::Type {
            Self::into_inner(self)
        }

        #[inline(always)]
        fn load(&self, order: Ordering) -> Self::Type {
            Self::load(self, order)
        }

        #[inline(always)]
        fn store(&self, val: Self::Type, order: Ordering) {
            Self::store(self, val, order)
        }

        #[inline(always)]
        fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type {
            Self::swap(self, val, order)
        }

        #[inline(always)]
        fn compare_and_swap(
            &self,
            current: Self::Type,
            new: Self::Type,
            order: Ordering,
        ) -> Self::Type {
            #[cfg_attr(feature = "since_1_50_0", allow(deprecated))]
            Self::compare_and_swap(self, current, new, order)
        }

        #[cfg(any(feature = "extended_compare_and_swap", feature = "since_1_10_0"))]
        #[inline(always)]
        fn compare_exchange(
            &self,
            current: Self::Type,
            new: Self::Type,
            success: Ordering,
            failure: Ordering,
        ) -> Result<Self::Type, Self::Type> {
            Self::compare_exchange(self, current, new, success, failure)
        }

        #[cfg(any(feature = "extended_compare_and_swap", feature = "since_1_10_0"))]
        #[inline(always)]
        fn compare_exchange_weak(
            &self,
            current: Self::Type,
            new: Self::Type,
            success: Ordering,
            failure: Ordering,
        ) -> Result<Self::Type, Self::Type> {
            Self::compare_exchange_weak(self, current, new, success, failure)
        }
    };

    (__impl bitwise $atomic:path : $primitive:ty) => {
        impl Bitwise for $atomic {}

        impl $crate::fetch::And for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_and(&self, val: Self::Type, order: Ordering) -> Self::Type {
                Self::fetch_and(self, val, order)
            }
        }

        #[cfg(any(feature = "atomic_nand", feature = "since_1_27_0"))]
        impl $crate::fetch::Nand for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_nand(&self, val: Self::Type, order: Ordering) -> Self::Type {
                Self::fetch_nand(self, val, order)
            }
        }

        impl $crate::fetch::Or for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_or(&self, val: Self::Type, order: Ordering) -> Self::Type {
                Self::fetch_or(self, val, order)
            }
        }

        impl $crate::fetch::Xor for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_xor(&self, val: Self::Type, order: Ordering) -> Self::Type {
                Self::fetch_xor(self, val, order)
            }
        }
    };

    (__impl numops $atomic:path : $primitive:ty) => {
        impl NumOps for $atomic {}

        impl $crate::fetch::Add for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_add(&self, val: Self::Type, order: Ordering) -> Self::Type {
                Self::fetch_add(self, val, order)
            }
        }

        impl $crate::fetch::Sub for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_sub(&self, val: Self::Type, order: Ordering) -> Self::Type {
                Self::fetch_sub(self, val, order)
            }
        }

        #[cfg(any(feature = "since_1_45_0", feature = "loom_atomics"))]
        impl_atomic!(__impl fetch_update $atomic : $primitive);

        impl_atomic!(__impl min_max $atomic : $primitive);
    };

    (__impl fetch_update $atomic:ident < $param:ident >) => {
        impl < $param > $crate::fetch::Update for $atomic < $param > {
            type Type = *mut $param;

            #[inline(always)]
            fn fetch_update<F>(
                &self,
                fetch_order: Ordering,
                set_order: Ordering,
                f: F,
            ) -> Result<Self::Type, Self::Type>
            where
                F: FnMut(Self::Type) -> Option<Self::Type> {
                Self::fetch_update(self, fetch_order, set_order, f)
            }
        }
    };

    (__impl fetch_update $atomic:path : $primitive:ty) => {
        impl $crate::fetch::Update for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_update<F>(
                &self,
                fetch_order: Ordering,
                set_order: Ordering,
                f: F,
            ) -> Result<Self::Type, Self::Type>
            where
                F: FnMut(Self::Type) -> Option<Self::Type> {
                Self::fetch_update(self, fetch_order, set_order, f)
            }
        }
    };

    (__impl fetch_not $atomic:path : $primitive:ty) => {
        #[cfg(feature = "atomic_bool_fetch_not")]
        impl $crate::fetch::Not for $atomic {
            type Type = $primitive;

            #[inline(always)]
            fn fetch_not(&self, order: Ordering) -> Self::Type {
                Self::fetch_not(self, order)
            }
        }
    };

    (__impl min_max $atomic:path : $primitive:ty) => {
        cfg_if! {
            if #[cfg(any(feature = "atomic_min_max", feature = "since_1_45_0"))] {
                impl $crate::fetch::Max for $atomic {
                    type Type = $primitive;

                    #[inline(always)]
                    fn fetch_max(&self, val: Self::Type, order: Ordering) -> Self::Type {
                        Self::fetch_max(self, val, order)
                    }
                }

                impl $crate::fetch::Min for $atomic {
                    type Type = $primitive;

                    #[inline(always)]
                    fn fetch_min(&self, val: Self::Type, order: Ordering) -> Self::Type {
                        Self::fetch_min(self, val, order)
                    }
                }
            }
        }
    };

    (__impl from_ptr $atomic:path : $primitive:ty) => {
        #[cfg(all(
            any(feature = "atomic_from_ptr", feature = "since_1_75_0"),
            not(feature = "loom_atomics")
        ))]
        impl FromPtr for $atomic {
            unsafe fn from_ptr<'a>(ptr: *mut Self::Type) -> &'a Self {
                Self::from_ptr(ptr)
            }
        }
    };

    (__impl from_ptr $atomic:ident < $param:ident >) => {
        #[cfg(all(
            any(feature = "atomic_from_ptr", feature = "since_1_75_0"),
            not(feature = "loom_atomics")
        ))]
        impl < $param > FromPtr for $atomic < $param > {
            unsafe fn from_ptr<'a>(ptr: *mut Self::Type) -> &'a Self {
                Self::from_ptr(ptr)
            }
        }
    };

    (__impl as_ptr $atomic:path : $primitive:ty) => {
        #[cfg(any(feature = "atomic_as_ptr", feature = "since_1_70_0"))]
        impl AsPtr for $atomic {
            #[inline(always)]
            fn as_ptr(&self) -> *mut Self::Type {
                Self::as_ptr(&self)
            }
        }
    };

    (__impl as_ptr $atomic:ident < $param:ident >) => {
        #[cfg(any(feature = "atomic_as_ptr", feature = "since_1_70_0"))]
        impl < $param > AsPtr for $atomic < $param > {
            #[inline(always)]
            fn as_ptr(&self) -> *mut Self::Type {
                Self::as_ptr(&self)
            }
        }
    };

    (__impl from_mut $atomic:path : $primitive:ty) => {
        #[cfg(feature = "atomic_from_mut")]
        impl FromMut for $atomic
        where
            Self: Sized,
        {
            #[inline(always)]
            fn from_mut(v: &mut Self::Type) -> &mut Self {
                Self::from_mut(v)
            }

            #[inline(always)]
            fn from_mut_slice(v: &mut [Self::Type]) -> &mut [Self] {
                Self::from_mut_slice(v)
            }

            #[inline(always)]
            fn get_mut_slice(this: &mut [Self]) -> &mut [Self::Type] {
                Self::get_mut_slice(this)
            }
        }
    };

    (__impl from_mut $atomic:ident < $param:ident >) => {
        #[cfg(feature = "atomic_from_mut")]
        impl < $param > FromMut for $atomic < $param >
        where
            Self: Sized,
        {
            #[inline(always)]
            fn from_mut(v: &mut Self::Type) -> &mut Self {
                Self::from_mut(v)
            }

            #[inline(always)]
            fn from_mut_slice(v: &mut [Self::Type]) -> &mut [Self] {
                Self::from_mut_slice(v)
            }

            #[inline(always)]
            fn get_mut_slice(this: &mut [Self]) -> &mut [Self::Type] {
                Self::get_mut_slice(this)
            }
        }
    };
}

cfg_if! {
    if #[cfg(any(
        all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "8"),
        all(
            not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
            any(
                target_pointer_width = "16",
                target_pointer_width = "32",
                target_pointer_width = "64"
            )
        )
    ))] {
        impl_atomic!(AtomicBool: bool; bitwise, fetch_not, as_ptr, from_mut);

        #[cfg(any(feature = "since_1_53_0", feature = "loom_atomics"))]
        impl_atomic!(__impl fetch_update AtomicBool : bool);
    }
}

cfg_if! {
    if #[cfg(any(not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")), target_has_atomic = "ptr"))] {
        impl_atomic!(AtomicIsize: isize; bitwise, numops, as_ptr, from_ptr, from_mut);
        impl_atomic!(AtomicUsize: usize; bitwise, numops, as_ptr, from_ptr, from_mut);
        impl_atomic!(AtomicPtr<T>; as_ptr, from_ptr, from_mut);

        #[cfg(any(feature = "since_1_53_0", feature = "loom_atomics"))]
        impl_atomic!(__impl fetch_update AtomicPtr<T>);
    }
}

#[cfg(any(feature = "integer_atomics", feature = "since_1_34_0"))]
mod integer_atomics {
    use super::*;

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "8"),
            all(
                not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
                any(
                    target_pointer_width = "16",
                    target_pointer_width = "32",
                    target_pointer_width = "64"
                )
            )
        ))] {
            impl_atomic!(AtomicI8: i8; bitwise, numops, as_ptr, from_ptr, from_mut);
            impl_atomic!(AtomicU8: u8; bitwise, numops, as_ptr, from_ptr, from_mut);
        }
    }

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "16"),
            all(
                not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
                any(
                    target_pointer_width = "16",
                    target_pointer_width = "32",
                    target_pointer_width = "64"
                )
            )
        ))] {
            impl_atomic!(AtomicI16: i16; bitwise, numops, as_ptr, from_ptr, from_mut);
            impl_atomic!(AtomicU16: u16; bitwise, numops, as_ptr, from_ptr, from_mut);
        }
    }

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "32"),
            all(
                not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
                any(target_pointer_width = "32", target_pointer_width = "64")
            )
        ))] {
            impl_atomic!(AtomicI32: i32; bitwise, numops, as_ptr, from_ptr, from_mut);
            impl_atomic!(AtomicU32: u32; bitwise, numops, as_ptr, from_ptr, from_mut);
        }
    }

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "64"),
            all(not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")), target_pointer_width = "64")
        ))] {
            impl_atomic!(AtomicI64: i64; bitwise, numops, as_ptr, from_ptr, from_mut);
            impl_atomic!(AtomicU64: u64; bitwise, numops, as_ptr, from_ptr, from_mut);
        }
    }
}

#[cfg(feature = "loom_atomics")]
mod loom_atomics {
    extern crate loom;

    use super::*;

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "8"),
            all(
                not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
                any(
                    target_pointer_width = "16",
                    target_pointer_width = "32",
                    target_pointer_width = "64"
                )
            )
        ))] {
            impl_atomic!(loom::sync::atomic::AtomicBool: bool; bitwise);
        }
    }

    cfg_if! {
        if #[cfg(any(not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")), target_has_atomic = "ptr"))] {
            impl_atomic!(loom::sync::atomic::AtomicIsize: isize; bitwise, numops);
            impl_atomic!(loom::sync::atomic::AtomicUsize: usize; bitwise, numops);

            impl_atomic!(__loom AtomicPtr<T>);
        }
    }

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "8"),
            all(
                not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
                any(
                    target_pointer_width = "16",
                    target_pointer_width = "32",
                    target_pointer_width = "64"
                )
            )
        ))] {
            impl_atomic!(loom::sync::atomic::AtomicI8: i8; bitwise, numops);
            impl_atomic!(loom::sync::atomic::AtomicU8: u8; bitwise, numops);
        }
    }

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "16"),
            all(
                not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
                any(
                    target_pointer_width = "16",
                    target_pointer_width = "32",
                    target_pointer_width = "64"
                )
            )
        ))] {
            impl_atomic!(loom::sync::atomic::AtomicI16: i16; bitwise, numops);
            impl_atomic!(loom::sync::atomic::AtomicU16: u16; bitwise, numops);
        }
    }

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "32"),
            all(
                not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")),
                any(target_pointer_width = "32", target_pointer_width = "64")
            )
        ))] {
            impl_atomic!(loom::sync::atomic::AtomicI32: i32; bitwise, numops);
            impl_atomic!(loom::sync::atomic::AtomicU32: u32; bitwise, numops);
        }
    }

    cfg_if! {
        if #[cfg(any(
            all(any(feature = "use_target_has_atomic", feature = "since_1_60_0"), target_has_atomic = "64"),
            all(not(any(feature = "use_target_has_atomic", feature = "since_1_60_0")), target_pointer_width = "64")
        ))] {
            impl_atomic!(loom::sync::atomic::AtomicI64: i64; bitwise, numops);
            impl_atomic!(loom::sync::atomic::AtomicU64: u64; bitwise, numops);
        }
    }
}