radium/lib.rs
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
//! `radium` provides a series of helpers for a uniform API over 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`].
//!
//! The `types` module provides type names that are atomic where the target
//! supports it, and fall back to `Cell` when the target does not.
//!
//! The `if_atomic!` macro provides a means of conditional compilation based on
//! the presence of atomic instructions. It is a substitute for the
//! `cfg(target_has_atomic)` or `cfg(accessible)` attribute tests, which are not
//! yet stabilized.
//!
//! ---
//!
//! **@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)]
#[macro_use]
mod macros;
pub mod types;
use core::cell::Cell;
use core::sync::atomic::Ordering;
if_atomic! {
if atomic(8) {
use core::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
}
if atomic(16) {
use core::sync::atomic::{AtomicI16, AtomicU16};
}
if atomic(32) {
use core::sync::atomic::{AtomicI32, AtomicU32};
}
if atomic(64) {
use core::sync::atomic::{AtomicI64, AtomicU64};
}
if atomic(ptr) {
use core::sync::atomic::{AtomicIsize, AtomicPtr, AtomicUsize};
}
}
/// 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 {
type Item;
/// Creates a new value of this type.
fn new(value: Self::Item) -> 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 Self::Item;
/// Consumes the wrapper and returns the contained value.
///
/// This is safe as passing by value ensures no other references exist.
fn into_inner(self) -> Self::Item;
/// 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) -> Self::Item;
/// 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: Self::Item, 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: Self::Item, order: Ordering) -> Self::Item;
/// 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
#[deprecated = "Use `compare_exchange` or `compare_exchange_weak` instead"]
fn compare_and_swap(&self, current: Self::Item, new: Self::Item, order: Ordering)
-> Self::Item;
/// 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: Self::Item,
new: Self::Item,
success: Ordering,
failure: Ordering,
) -> Result<Self::Item, Self::Item>;
/// 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: Self::Item,
new: Self::Item,
success: Ordering,
failure: Ordering,
) -> Result<Self::Item, Self::Item>;
/// 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: Self::Item, order: Ordering) -> Self::Item
where
Self::Item: 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: Self::Item, order: Ordering) -> Self::Item
where
Self::Item: 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: Self::Item, order: Ordering) -> Self::Item
where
Self::Item: 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: Self::Item, order: Ordering) -> Self::Item
where
Self::Item: 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: Self::Item, order: Ordering) -> Self::Item
where
Self::Item: 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: Self::Item, order: Ordering) -> Self::Item
where
Self::Item: marker::NumericOps;
/// Fetches the value, and applies a function to it that returns an
/// optional new value.
///
/// Note: This may call the function multiple times if the value has been
/// changed from other threads in the meantime, as long as the function
/// returns `Some(_)`, but the function will have been applied only once to
/// the stored value.
///
/// Returns a `Result` of `Ok(previous_value)` if the function returned
/// `Some(_)`, else `Err(previous_value)`.
///
/// Ordering arguments are ignored by non-atomic types.
///
/// See also: [`AtomicUsize::fetch_update`].
///
/// [`AtomicUsize::fetch_update`]: core::sync::atomic::AtomicUsize::fetch_update
fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<Self::Item, Self::Item>
where
F: FnMut(Self::Item) -> Option<Self::Item>;
}
/// 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) {
core::sync::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]
#[allow(deprecated)]
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)
}
#[inline]
fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<$base, $base>
where
F: FnMut($base) -> Option<$base>,
{
self.fetch_update(set_order, fetch_order, f)
}
};
// 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)
}
#[inline]
fn fetch_update<F>(&self, _: Ordering, _: Ordering, mut f: F) -> Result<$base, $base>
where
F: FnMut($base) -> Option<$base>,
{
match f(self.get()) {
Some(x) => Ok(self.replace(x)),
None => Err(self.get()),
}
}
};
// 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))
}
};
}
macro_rules! radium_int {
( $( $width:tt: $base:ty , $atom:ty ; )* ) => { $(
impl marker::BitOps for $base {}
impl marker::NumericOps for $base {}
if_atomic!(if atomic($width) {
impl Radium for $atom {
type Item = $base;
radium!(atom $base);
radium!(atom_bit $base);
radium!(atom_int $base);
}
});
impl Radium for Cell<$base> {
type Item = $base;
radium!(cell $base);
radium!(cell_bit $base);
radium!(cell_int $base);
}
)* };
}
radium_int! {
8: i8, AtomicI8;
8: u8, AtomicU8;
16: i16, AtomicI16;
16: u16, AtomicU16;
32: i32, AtomicI32;
32: u32, AtomicU32;
64: i64, AtomicI64;
64: u64, AtomicU64;
size: isize, AtomicIsize;
size: usize, AtomicUsize;
}
impl marker::BitOps for bool {}
if_atomic!(if atomic(bool) {
impl Radium for AtomicBool {
type Item = bool;
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 for Cell<bool> {
type Item = 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")
}
}
if_atomic!(if atomic(ptr) {
impl<T> Radium for AtomicPtr<T> {
type Item = *mut 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 for Cell<*mut T> {
type Item = *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::*;
use core::cell::Cell;
#[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);
}
#[test]
fn always_cell() {
static_assertions::assert_impl_all!(Cell<bool>: Radium<Item = bool>);
static_assertions::assert_impl_all!(Cell<i8>: Radium<Item = i8>);
static_assertions::assert_impl_all!(Cell<u8>: Radium<Item = u8>);
static_assertions::assert_impl_all!(Cell<i16>: Radium<Item = i16>);
static_assertions::assert_impl_all!(Cell<u16>: Radium<Item = u16>);
static_assertions::assert_impl_all!(Cell<i32>: Radium<Item = i32>);
static_assertions::assert_impl_all!(Cell<u32>: Radium<Item = u32>);
static_assertions::assert_impl_all!(Cell<i64>: Radium<Item = i64>);
static_assertions::assert_impl_all!(Cell<u64>: Radium<Item = u64>);
static_assertions::assert_impl_all!(Cell<isize>: Radium<Item = isize>);
static_assertions::assert_impl_all!(Cell<usize>: Radium<Item = usize>);
static_assertions::assert_impl_all!(Cell<*mut ()>: Radium<Item = *mut ()>);
}
#[test]
fn always_alias() {
static_assertions::assert_impl_all!(types::RadiumBool: Radium<Item = bool>);
static_assertions::assert_impl_all!(types::RadiumI8: Radium<Item = i8>);
static_assertions::assert_impl_all!(types::RadiumU8: Radium<Item = u8>);
static_assertions::assert_impl_all!(types::RadiumI16: Radium<Item = i16>);
static_assertions::assert_impl_all!(types::RadiumU16: Radium<Item = u16>);
static_assertions::assert_impl_all!(types::RadiumI32: Radium<Item = i32>);
static_assertions::assert_impl_all!(types::RadiumU32: Radium<Item = u32>);
static_assertions::assert_impl_all!(types::RadiumI64: Radium<Item = i64>);
static_assertions::assert_impl_all!(types::RadiumU64: Radium<Item = u64>);
static_assertions::assert_impl_all!(types::RadiumIsize: Radium<Item = isize>);
static_assertions::assert_impl_all!(types::RadiumUsize: Radium<Item = usize>);
static_assertions::assert_impl_all!(types::RadiumPtr<()>: Radium<Item = *mut ()>);
}
#[test]
fn maybe_atom() {
if_atomic! {
if atomic(bool) {
use core::sync::atomic::*;
static_assertions::assert_impl_all!(AtomicBool: Radium<Item = bool>);
}
if atomic(8) {
static_assertions::assert_impl_all!(AtomicI8: Radium<Item = i8>);
static_assertions::assert_impl_all!(AtomicU8: Radium<Item = u8>);
}
if atomic(16) {
static_assertions::assert_impl_all!(AtomicI16: Radium<Item = i16>);
static_assertions::assert_impl_all!(AtomicU16: Radium<Item = u16>);
}
if atomic(32) {
static_assertions::assert_impl_all!(AtomicI32: Radium<Item = i32>);
static_assertions::assert_impl_all!(AtomicU32: Radium<Item = u32>);
}
if atomic(64) {
static_assertions::assert_impl_all!(AtomicI64: Radium<Item = i64>);
static_assertions::assert_impl_all!(AtomicU64: Radium<Item = u64>);
}
if atomic(size) {
static_assertions::assert_impl_all!(AtomicIsize: Radium<Item = isize>);
static_assertions::assert_impl_all!(AtomicUsize: Radium<Item = usize>);
}
if atomic(ptr) {
static_assertions::assert_impl_all!(AtomicPtr<()>: Radium<Item = *mut ()>);
}
}
}
}