Skip to content

Commit

Permalink
Add Atomic::const_null
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed May 31, 2021
1 parent 1f3104e commit 62ab077
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions crossbeam-epoch/src/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,47 @@ impl<T> Atomic<T> {
pub fn new(init: T) -> Atomic<T> {
Self::init(init)
}

/// Returns a new null atomic pointer.
///
/// This allows creating a null atomic pointer in a constant context on stable Rust.
///
/// # Examples
///
/// ```
/// use crossbeam_epoch::Atomic;
///
/// static A: Atomic<i32> = Atomic::const_null();
/// ```
#[cfg(not(crossbeam_loom))]
pub const fn const_null() -> Self {
Self {
data: AtomicUsize::new(0),
_marker: PhantomData,
}
}
}

impl<T> Atomic<[MaybeUninit<T>]> {
/// Returns a new null atomic pointer.
///
/// This allows creating a null atomic pointer in a constant context on stable Rust.
///
/// # Examples
///
/// ```
/// use crossbeam_epoch::Atomic;
/// use std::mem::MaybeUninit;
///
/// static A: Atomic<[MaybeUninit<i32>]> = Atomic::const_null();
/// ```
#[cfg(not(crossbeam_loom))]
pub const fn const_null() -> Self {
Self {
data: AtomicUsize::new(0),
_marker: PhantomData,
}
}
}

impl<T: ?Sized + Pointable> Atomic<T> {
Expand Down Expand Up @@ -342,7 +383,6 @@ impl<T: ?Sized + Pointable> Atomic<T> {
///
/// let a = Atomic::<i32>::null();
/// ```
///
#[cfg_attr(all(feature = "nightly", not(crossbeam_loom)), const_fn::const_fn)]
pub fn null() -> Atomic<T> {
Self {
Expand Down Expand Up @@ -1530,7 +1570,7 @@ impl<T: ?Sized + Pointable> Default for Shared<'_, T> {

#[cfg(all(test, not(crossbeam_loom)))]
mod tests {
use super::{Owned, Shared};
use super::{Atomic, Owned, Shared};
use std::mem::MaybeUninit;

#[test]
Expand All @@ -1543,11 +1583,10 @@ mod tests {
Shared::<i64>::null().with_tag(7);
}

#[cfg(feature = "nightly")]
#[test]
fn const_atomic_null() {
use super::Atomic;
static _U: Atomic<u8> = Atomic::<u8>::null();
static _U1: Atomic<u8> = Atomic::<u8>::const_null();
static _U2: Atomic<[MaybeUninit<u8>]> = Atomic::<[MaybeUninit<u8>]>::const_null();
}

#[test]
Expand Down

0 comments on commit 62ab077

Please sign in to comment.