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
//! `radium` provides a series of helper traits providing a uniform API for
//! interacting with both atomic types like
//! [`AtomicUsize`], and non-atomic types like [`Cell<T>`].
//!
//! This crate is `#![no_std]`-compatible, and uses no non-core types.
//!
//! For details, see the documentation for [`Radium`].
//!
//! ---
//!
//! **@kneecaw** - <https://twitter.com/kneecaw/status/1132695060812849154>
//! > Feelin' lazy: Has someone already written a helper trait abstracting
//! > operations over `AtomicUsize` and `Cell<usize>` for generic code which may
//! > not care about atomicity?
//!
//! **@ManishEarth** - <https://twitter.com/ManishEarth/status/1132706585300496384>
//! > no but call the crate radium
//! >
//! > (since people didn't care that it was radioactive and used it in everything)
//!
//! [`AtomicUsize`]: core::sync::atomic::AtomicUsize
//! [`Cell<T>`]: core::cell::Cell

#![no_std]
#![deny(unconditional_recursion)]

use core::cell::Cell;
use core::sync::atomic::{
    self, AtomicBool, AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicPtr, AtomicU16,
    AtomicU32, AtomicU64, AtomicU8, AtomicUsize, Ordering,
};

/// A maybe-atomic shared mutable fundamental type `T`.
///
/// This trait is implemented by both the [atomic wrapper] type for `T`, and by
/// [`Cell<T>`], providing a consistent interface for interacting with the two
/// types.
///
/// This trait provides methods predicated on marker traits for the underlying
/// fundamental. Only types which can be viewed as sequences of bits may use the
/// functions for bit-wise arithmetic, and only types which can be used as
/// integers may use the functions for numeric arithmetic. Use of these methods
/// on insufficient underlying types (for example, `Radium::fetch_and` on an
/// atomic or cell-wrapped pointer) will cause a compiler error.
///
/// [atomic wrapper]: core::sync::atomic
/// [`Cell<T>`]: core::cell::Cell
pub trait Radium<T> {
    /// Creates a new value of this type.
    fn new(value: T) -> Self;

    /// If the underlying value is atomic, calls [`fence`] with the given
    /// [`Ordering`]. Otherwise, does nothing.
    ///
    /// [`Ordering`]: core::sync::atomic::Ordering
    /// [`fence`]: core::sync::atomic::fence
    fn fence(order: Ordering);

    /// Returns a mutable reference to the underlying value.
    ///
    /// This is safe because the mutable reference to `self` guarantees that no
    /// other references exist to this value.
    fn get_mut(&mut self) -> &mut T;

    /// Consumes the wrapper and returns the contained value.
    ///
    /// This is safe as passing by value ensures no other references exist.
    fn into_inner(self) -> T;

    /// Load a value from this object.
    ///
    /// Ordering values are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::load`].
    ///
    /// [`AtomicUsize::load`]: core::sync::atomic::AtomicUsize::load
    fn load(&self, order: Ordering) -> T;

    /// Store a value in this object.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::store`].
    ///
    /// [`AtomicUsize::store`]: core::sync::atomic::AtomicUsize::store
    fn store(&self, value: T, order: Ordering);

    /// Swap with the value stored in this object.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::swap`].
    ///
    /// [`AtomicUsize::swap`]: core::sync::atomic::AtomicUsize::swap
    fn swap(&self, value: T, order: Ordering) -> T;

    /// Stores a value into this object if the currently-stored value is the
    /// same as the `current` value.
    ///
    /// The return value is always the previously-stored value. If it is equal to
    /// `current`, then the value was updated with `new`.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::compare_and_swap`].
    ///
    /// [`AtomicUsize::compare_and_swap`]: core::sync::atomic::AtomicUsize::compare_and_swap
    fn compare_and_swap(&self, current: T, new: T, order: Ordering) -> T;

    /// Stores a value into this object if the currently-stored value is the
    /// same as the `current` value.
    ///
    /// The return value is a `Result` indicating whether the new value was
    /// written, and containing the previously-stored value. On success, this
    /// value is guaranteed to be equal to `current`.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::compare_exchange`].
    ///
    /// [`AtomicUsize::compare_exchange`]: core::sync::atomic::AtomicUsize::compare_exchange
    fn compare_exchange(
        &self,
        current: T,
        new: T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<T, T>;

    /// Stores a value into this object if the currently-stored 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 previously-stored
    /// value.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::compare_exchange_weak`].
    ///
    /// [`AtomicUsize::compare_exchange_weak`]: core::sync::atomic::AtomicUsize::compare_exchange_weak
    fn compare_exchange_weak(
        &self,
        current: T,
        new: T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<T, T>;

    /// Performs a bitwise "and" on the currently-stored value and the argument
    /// `value`, and stores the result in `self`.
    ///
    /// Returns the previously-stored value.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::fetch_and`].
    ///
    /// [`AtomicUsize::fetch_and`]: core::sync::atomic::AtomicUsize::fetch_and
    fn fetch_and(&self, value: T, order: Ordering) -> T
    where
        T: marker::BitOps;

    /// Performs a bitwise "nand" on the currently-stored value and the argument
    /// `value`, and stores the result in `self`.
    ///
    /// Returns the previously-stored value.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::fetch_nand`].
    ///
    /// [`AtomicUsize::fetch_nand`]: core::sync::atomic::AtomicUsize::fetch_nand
    fn fetch_nand(&self, value: T, order: Ordering) -> T
    where
        T: marker::BitOps;

    /// Performs a bitwise "or" on the currently-stored value and the argument
    /// `value`, and stores the result in `self`.
    ///
    /// Returns the previously-stored value.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::fetch_or`].
    ///
    /// [`AtomicUsize::fetch_or`]: core::sync::atomic::AtomicUsize::fetch_or
    fn fetch_or(&self, value: T, order: Ordering) -> T
    where
        T: marker::BitOps;

    /// Performs a bitwise "xor" on the currently-stored value and the argument
    /// `value`, and stores the result in `self`.
    ///
    /// Returns the previously-stored value.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::fetch_xor`].
    ///
    /// [`AtomicUsize::fetch_xor`]: core::sync::atomic::AtomicUsize::fetch_xor
    fn fetch_xor(&self, value: T, order: Ordering) -> T
    where
        T: marker::BitOps;

    /// Adds `value` to the currently-stored value, wrapping on overflow, and
    /// stores the result in `self`.
    ///
    /// Returns the previously-stored value.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::fetch_add`].
    ///
    /// [`AtomicUsize::fetch_add`]: core::sync::atomic::AtomicUsize::fetch_add
    fn fetch_add(&self, value: T, order: Ordering) -> T
    where
        T: marker::NumericOps;

    /// Subtracts `value` from the currently-stored value, wrapping on
    /// underflow, and stores the result in `self`.
    ///
    /// Returns the previously-stored value.
    ///
    /// Ordering arguments are ignored by non-atomic types.
    ///
    /// See also: [`AtomicUsize::fetch_sub`].
    ///
    /// [`AtomicUsize::fetch_sub`]: core::sync::atomic::AtomicUsize::fetch_sub
    fn fetch_sub(&self, value: T, order: Ordering) -> T
    where
        T: marker::NumericOps;
}

/// Marker traits used by [`Radium`].
pub mod marker {
    /// Types supporting maybe-atomic bitwise operations.
    ///
    /// Types implementing this trait support the [`fetch_and`], [`fetch_nand`],
    /// [`fetch_or`], and [`fetch_xor`] maybe-atomic operations.
    ///
    /// [`fetch_and`]: crate::Radium::fetch_and
    /// [`fetch_nand`]: crate::Radium::fetch_nand
    /// [`fetch_or`]: crate::Radium::fetch_or
    /// [`fetch_xor`]: crate::Radium::fetch_xor
    ///
    /// `bool` and all integer fundamental types implement this.
    ///
    /// ```rust
    /// # use core::sync::atomic::*;
    /// # use radium::Radium;
    /// let num: AtomicUsize = AtomicUsize::new(0);
    /// Radium::fetch_or(&num, 2, Ordering::Relaxed);
    /// ```
    ///
    /// Pointers do not. This will cause a compiler error.
    ///
    /// ```rust,compile_fail
    /// # use core::sync::atomic::*;
    /// # use radium::Radium;
    /// # use core::ptr;
    /// let ptr: AtomicPtr<usize> = Default::default();
    /// Radium::fetch_or(&ptr, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    pub trait BitOps {}

    /// Types supporting maybe-atomic arithmetic operations.
    ///
    /// Types implementing this trait support the [`fetch_add`] and
    /// [`fetch_sub`] maybe-atomic operations.
    ///
    /// [`fetch_add`]: crate::Radium::fetch_add
    /// [`fetch_sub`]: crate::Radium::fetch_sub
    ///
    /// The integer types, such as `usize` and `i32`, implement this trait.
    ///
    /// ```rust
    /// # use core::sync::atomic::*;
    /// # use radium::Radium;
    /// let num: AtomicUsize = AtomicUsize::new(2);
    /// Radium::fetch_add(&num, 2, Ordering::Relaxed);
    /// ```
    ///
    /// `bool` and pointers do not. This will cause a compiler error.
    ///
    /// ```rust,compile_fail
    /// # use core::sync::atomic::*;
    /// # use radium::Radium;
    /// let bit: AtomicBool = AtomicBool::new(false);
    /// Radium::fetch_add(&bit, true, Ordering::Relaxed);
    /// ```
    pub trait NumericOps: BitOps {}
}

macro_rules! radium {
    // Emit the universal `Radium` trait function bodies for atomic types.
    ( atom $base:ty ) => {
        #[inline]
        fn new(value: $base) -> Self {
            Self::new(value)
        }

        #[inline]
        fn fence(order: Ordering) {
            atomic::fence(order);
        }

        #[inline]
        fn get_mut(&mut self) -> &mut $base {
            self.get_mut()
        }

        #[inline]
        fn into_inner(self) -> $base {
            self.into_inner()
        }

        #[inline]
        fn load(&self, order: Ordering) -> $base {
            self.load(order)
        }

        #[inline]
        fn store(&self, value: $base, order: Ordering) {
            self.store(value, order);
        }

        #[inline]
        fn swap(&self, value: $base, order: Ordering) -> $base {
            self.swap(value, order)
        }

        #[inline]
        fn compare_and_swap(
            &self,
            current: $base,
            new: $base,
            order: Ordering,
        ) -> $base {
            self.compare_and_swap(current, new, order)
        }

        #[inline]
        fn compare_exchange(
            &self,
            current: $base,
            new: $base,
            success: Ordering,
            failure: Ordering,
        ) -> Result<$base, $base> {
            self.compare_exchange(current, new, success, failure)
        }

        #[inline]
        fn compare_exchange_weak(
            &self,
            current: $base,
            new: $base,
            success: Ordering,
            failure: Ordering,
        ) -> Result<$base, $base> {
            self.compare_exchange_weak(current, new, success, failure)
        }
    };

    // Emit the `Radium` trait function bodies for bit-wise types.
    ( atom_bit $base:ty ) => {
        #[inline]
        fn fetch_and(&self, value: $base, order: Ordering) -> $base {
            self.fetch_and(value, order)
        }

        #[inline]
        fn fetch_nand(&self, value: $base, order: Ordering) -> $base {
            self.fetch_nand(value, order)
        }

        #[inline]
        fn fetch_or(&self, value: $base, order: Ordering) -> $base {
            self.fetch_or(value, order)
        }

        #[inline]
        fn fetch_xor(&self, value: $base, order: Ordering) -> $base {
            self.fetch_xor(value, order)
        }
    };

    // Emit the `Radium` trait function bodies for integral types.
    ( atom_int $base:ty ) => {
        #[inline]
        fn fetch_add(&self, value: $base, order: Ordering) -> $base {
            self.fetch_add(value, order)
        }

        #[inline]
        fn fetch_sub(&self, value: $base, order: Ordering) -> $base {
            self.fetch_sub(value, order)
        }
    };

    // Emit the universal `Radium` trait function bodies for `Cell<_>`.
    ( cell $base:ty ) => {
        #[inline]
        fn new(value: $base) -> Self {
            Cell::new(value)
        }

        #[inline]
        fn fence(_: Ordering) {}

        #[inline]
        fn get_mut(&mut self) -> &mut $base {
            self.get_mut()
        }

        #[inline]
        fn into_inner(self) -> $base {
            self.into_inner()
        }

        #[inline]
        fn load(&self, _: Ordering) -> $base {
            self.get()
        }

        #[inline]
        fn store(&self, value: $base, _: Ordering) {
            self.set(value);
        }

        #[inline]
        fn swap(&self, value: $base, _: Ordering) -> $base {
            self.replace(value)
        }

        #[inline]
        fn compare_and_swap(
            &self,
            current: $base,
            new: $base,
            _: Ordering,
        ) -> $base {
            if self.get() == current {
                self.replace(new)
            } else {
                self.get()
            }
        }

        #[inline]
        fn compare_exchange(
            &self,
            current: $base,
            new: $base,
            _: Ordering,
            _: Ordering,
        ) -> Result<$base, $base> {
            if self.get() == current {
                Ok(self.replace(new))
            } else {
                Err(self.get())
            }
        }

        #[inline]
        fn compare_exchange_weak(
            &self,
            current: $base,
            new: $base,
            success: Ordering,
            failure: Ordering,
        ) -> Result<$base, $base> {
            Radium::compare_exchange(self, current, new, success, failure)
        }
    };

    // Emit the `Radium` trait function bodies for bit-wise types.
    ( cell_bit $base:ty ) => {
        #[inline]
        fn fetch_and(&self, value: $base, _: Ordering) -> $base {
            self.replace(self.get() & value)
        }

        #[inline]
        fn fetch_nand(&self, value: $base, _: Ordering) -> $base {
            self.replace(!(self.get() & value))
        }

        #[inline]
        fn fetch_or(&self, value: $base, _: Ordering) -> $base {
            self.replace(self.get() | value)
        }

        #[inline]
        fn fetch_xor(&self, value: $base, _: Ordering) -> $base {
            self.replace(self.get() ^ value)
        }
    };

    // Emit the `Radium` trait function bodies for integral types.
    ( cell_int $base:ty ) => {
        #[inline]
        fn fetch_add(&self, value: $base, _: Ordering) -> $base {
            self.replace(self.get().wrapping_add(value))
        }

        #[inline]
        fn fetch_sub(&self, value: $base, _: Ordering) -> $base {
            self.replace(self.get().wrapping_sub(value))
        }
    };

    // Implement `Radium` for integral fundamentals.
    ( int $( $base:ty , $atom:ty ; )* ) => { $(
        impl marker::BitOps for $base {}
        impl marker::NumericOps for $base {}

        impl Radium<$base> for $atom {
            radium!(atom $base);
            radium!(atom_bit $base);
            radium!(atom_int $base);
        }

        impl Radium<$base> for Cell<$base> {
            radium!(cell $base);
            radium!(cell_bit $base);
            radium!(cell_int $base);
        }
    )* };
}

radium![
    int
    i8, AtomicI8;
    i16, AtomicI16;
    i32, AtomicI32;
    i64, AtomicI64;
    isize, AtomicIsize;
    u8, AtomicU8;
    u16, AtomicU16;
    u32, AtomicU32;
    u64, AtomicU64;
    usize, AtomicUsize;
];

impl marker::BitOps for bool {}

impl Radium<bool> for AtomicBool {
    radium!(atom bool);
    radium!(atom_bit bool);

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicBool::new(false);
    /// Radium::fetch_add(&atom, true, Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_add(&self, _value: bool, _order: Ordering) -> bool {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicBool::new(false);
    /// Radium::fetch_sub(&atom, true, Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_sub(&self, _value: bool, _order: Ordering) -> bool {
        unreachable!("This method statically cannot be called")
    }
}

impl Radium<bool> for Cell<bool> {
    radium!(cell bool);
    radium!(cell_bit bool);

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<bool>::new(false);
    /// Radium::fetch_add(&cell, true, Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_add(&self, _value: bool, _order: Ordering) -> bool {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<bool>::new(false);
    /// Radium::fetch_sub(&cell, true, Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_sub(&self, _value: bool, _order: Ordering) -> bool {
        unreachable!("This method statically cannot be called")
    }
}

impl<T> Radium<*mut T> for AtomicPtr<T> {
    radium!(atom *mut T);

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicPtr::<u8>::new(ptr::null_mut());
    /// Radium::fetch_and(&atom, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_and(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicPtr::<u8>::new(ptr::null_mut());
    /// Radium::fetch_nand(&atom, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_nand(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicPtr::<u8>::new(ptr::null_mut());
    /// Radium::fetch_or(&atom, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_or(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicPtr::<u8>::new(ptr::null_mut());
    /// Radium::fetch_xor(&atom, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_xor(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicPtr::<u8>::new(ptr::null_mut());
    /// Radium::fetch_add(&atom, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_add(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let atom = AtomicPtr::<u8>::new(ptr::null_mut());
    /// Radium::fetch_sub(&atom, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_sub(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }
}

impl<T> Radium<*mut T> for Cell<*mut T> {
    radium!(cell *mut T);

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<*mut u8>::new(ptr::null_mut());
    /// Radium::fetch_and(&cell, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_and(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<*mut u8>::new(ptr::null_mut());
    /// Radium::fetch_nand(&cell, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_nand(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<*mut u8>::new(ptr::null_mut());
    /// Radium::fetch_or(&cell, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_or(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<*mut u8>::new(ptr::null_mut());
    /// Radium::fetch_xor(&cell, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_xor(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<*mut u8>::new(ptr::null_mut());
    /// Radium::fetch_add(&cell, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_add(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }

    /// ```compile_fail
    /// # use std::{ptr, sync::atomic::*, cell::*};
    /// # use radium::*;
    /// let cell = Cell::<*mut u8>::new(ptr::null_mut());
    /// Radium::fetch_sub(&cell, ptr::null_mut(), Ordering::Relaxed);
    /// ```
    #[doc(hidden)]
    fn fetch_sub(&self, _value: *mut T, _order: Ordering) -> *mut T {
        unreachable!("This method statically cannot be called")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn absent_traits() {
        static_assertions::assert_not_impl_any!(bool: marker::NumericOps);
        static_assertions::assert_not_impl_any!(*mut u8: marker::BitOps, marker::NumericOps);
    }

    #[test]
    fn present_traits() {
        static_assertions::assert_impl_all!(bool: marker::BitOps);
        static_assertions::assert_impl_all!(usize: marker::BitOps, marker::NumericOps);
    }
}