Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some constructors const function at 1.31+ #437

Merged
merged 1 commit into from Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}