Skip to content

Commit

Permalink
Make some constructors const function at 1.31+
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 22, 2019
1 parent 30f5d8b commit 308a87c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crossbeam-epoch/Cargo.toml
Expand Up @@ -39,5 +39,8 @@ optional = true
version = "1"
default-features = false

[build-dependencies]
autocfg = "0.1.6"

[dev-dependencies]
rand = "0.6"
8 changes: 8 additions & 0 deletions crossbeam-epoch/build.rs
@@ -0,0 +1,8 @@
extern crate autocfg;

fn main() {
let cfg = autocfg::new();
if cfg.probe_rustc_version(1, 31) {
println!("cargo:rustc-cfg=has_min_const_fn");
}
}
4 changes: 2 additions & 2 deletions crossbeam-epoch/src/atomic.rs
Expand Up @@ -150,7 +150,7 @@ impl<T> Atomic<T> {
///
/// let a = Atomic::<i32>::null();
/// ```
#[cfg(not(feature = "nightly"))]
#[cfg(not(has_min_const_fn))]
pub fn null() -> Atomic<T> {
Self {
data: AtomicUsize::new(0),
Expand All @@ -167,7 +167,7 @@ impl<T> Atomic<T> {
///
/// let a = Atomic::<i32>::null();
/// ```
#[cfg(feature = "nightly")]
#[cfg(has_min_const_fn)]
pub const fn null() -> Atomic<T> {
Self {
data: AtomicUsize::new(0),
Expand Down
1 change: 0 additions & 1 deletion crossbeam-epoch/src/lib.rs
Expand Up @@ -57,7 +57,6 @@
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "nightly", feature(const_fn))]
#![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))]

#[macro_use]
Expand Down
3 changes: 3 additions & 0 deletions crossbeam-utils/Cargo.toml
Expand Up @@ -25,5 +25,8 @@ alloc = []
cfg-if = "0.1.2"
lazy_static = { version = "1.1.0", optional = true }

[build-dependencies]
autocfg = "0.1.6"

[dev-dependencies]
rand = "0.6"
8 changes: 8 additions & 0 deletions crossbeam-utils/build.rs
@@ -0,0 +1,8 @@
extern crate autocfg;

fn main() {
let cfg = autocfg::new();
if cfg.probe_rustc_version(1, 31) {
println!("cargo:rustc-cfg=has_min_const_fn");
}
}
17 changes: 17 additions & 0 deletions crossbeam-utils/src/atomic/atomic_cell.rs
Expand Up @@ -52,12 +52,29 @@ impl<T> AtomicCell<T> {
///
/// let a = AtomicCell::new(7);
/// ```
#[cfg(not(has_min_const_fn))]
pub fn new(val: T) -> AtomicCell<T> {
AtomicCell {
value: UnsafeCell::new(val),
}
}

/// Creates a new atomic cell initialized with `val`.
///
/// # Examples
///
/// ```
/// use crossbeam_utils::atomic::AtomicCell;
///
/// let a = AtomicCell::new(7);
/// ```
#[cfg(has_min_const_fn)]
pub const fn new(val: T) -> AtomicCell<T> {
AtomicCell {
value: UnsafeCell::new(val),
}
}

/// Unwraps the atomic cell and returns its inner value.
///
/// # Examples
Expand Down
9 changes: 9 additions & 0 deletions crossbeam-utils/tests/atomic_cell.rs
Expand Up @@ -223,3 +223,12 @@ fn garbage_padding() {
assert!(cell.compare_exchange(prev, next).is_ok());
println!();
}

#[cfg(has_min_const_fn)]
#[test]
fn const_atomic_cell_new() {
static CELL: AtomicCell<usize> = AtomicCell::new(0);

CELL.store(1);
assert_eq!(CELL.load(), 1);
}

0 comments on commit 308a87c

Please sign in to comment.