From 308a87c2712713a5f7ef910d52fd78ac732ba23c Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 22 Oct 2019 14:32:43 +0900 Subject: [PATCH] Make some constructors const function at 1.31+ --- crossbeam-epoch/Cargo.toml | 3 +++ crossbeam-epoch/build.rs | 8 ++++++++ crossbeam-epoch/src/atomic.rs | 4 ++-- crossbeam-epoch/src/lib.rs | 1 - crossbeam-utils/Cargo.toml | 3 +++ crossbeam-utils/build.rs | 8 ++++++++ crossbeam-utils/src/atomic/atomic_cell.rs | 17 +++++++++++++++++ crossbeam-utils/tests/atomic_cell.rs | 9 +++++++++ 8 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 crossbeam-epoch/build.rs create mode 100644 crossbeam-utils/build.rs diff --git a/crossbeam-epoch/Cargo.toml b/crossbeam-epoch/Cargo.toml index 79e0f5ec9..2e882bfd7 100644 --- a/crossbeam-epoch/Cargo.toml +++ b/crossbeam-epoch/Cargo.toml @@ -39,5 +39,8 @@ optional = true version = "1" default-features = false +[build-dependencies] +autocfg = "0.1.6" + [dev-dependencies] rand = "0.6" diff --git a/crossbeam-epoch/build.rs b/crossbeam-epoch/build.rs new file mode 100644 index 000000000..d451c24b2 --- /dev/null +++ b/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"); + } +} diff --git a/crossbeam-epoch/src/atomic.rs b/crossbeam-epoch/src/atomic.rs index 0444c118b..a2044740b 100644 --- a/crossbeam-epoch/src/atomic.rs +++ b/crossbeam-epoch/src/atomic.rs @@ -150,7 +150,7 @@ impl Atomic { /// /// let a = Atomic::::null(); /// ``` - #[cfg(not(feature = "nightly"))] + #[cfg(not(has_min_const_fn))] pub fn null() -> Atomic { Self { data: AtomicUsize::new(0), @@ -167,7 +167,7 @@ impl Atomic { /// /// let a = Atomic::::null(); /// ``` - #[cfg(feature = "nightly")] + #[cfg(has_min_const_fn)] pub const fn null() -> Atomic { Self { data: AtomicUsize::new(0), diff --git a/crossbeam-epoch/src/lib.rs b/crossbeam-epoch/src/lib.rs index 112180b25..c5b504e50 100644 --- a/crossbeam-epoch/src/lib.rs +++ b/crossbeam-epoch/src/lib.rs @@ -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] diff --git a/crossbeam-utils/Cargo.toml b/crossbeam-utils/Cargo.toml index 23f049ec1..00f333198 100644 --- a/crossbeam-utils/Cargo.toml +++ b/crossbeam-utils/Cargo.toml @@ -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" diff --git a/crossbeam-utils/build.rs b/crossbeam-utils/build.rs new file mode 100644 index 000000000..d451c24b2 --- /dev/null +++ b/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"); + } +} diff --git a/crossbeam-utils/src/atomic/atomic_cell.rs b/crossbeam-utils/src/atomic/atomic_cell.rs index 5166a74f2..4fa54c85b 100644 --- a/crossbeam-utils/src/atomic/atomic_cell.rs +++ b/crossbeam-utils/src/atomic/atomic_cell.rs @@ -52,12 +52,29 @@ impl AtomicCell { /// /// let a = AtomicCell::new(7); /// ``` + #[cfg(not(has_min_const_fn))] pub fn new(val: T) -> AtomicCell { 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 { + AtomicCell { + value: UnsafeCell::new(val), + } + } + /// Unwraps the atomic cell and returns its inner value. /// /// # Examples diff --git a/crossbeam-utils/tests/atomic_cell.rs b/crossbeam-utils/tests/atomic_cell.rs index 940619233..57206aeda 100644 --- a/crossbeam-utils/tests/atomic_cell.rs +++ b/crossbeam-utils/tests/atomic_cell.rs @@ -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 = AtomicCell::new(0); + + CELL.store(1); + assert_eq!(CELL.load(), 1); +}