Skip to content

Commit f54db88

Browse files
bors[bot]taiki-e
andauthored
Merge #437
437: Make some constructors const function at 1.31+ r=stjepang a=taiki-e Co-authored-by: Taiki Endo <[email protected]>
2 parents caa52c4 + 308a87c commit f54db88

File tree

8 files changed

+50
-3
lines changed

8 files changed

+50
-3
lines changed

crossbeam-epoch/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@ optional = true
3939
version = "1"
4040
default-features = false
4141

42+
[build-dependencies]
43+
autocfg = "0.1.6"
44+
4245
[dev-dependencies]
4346
rand = "0.6"

crossbeam-epoch/build.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern crate autocfg;
2+
3+
fn main() {
4+
let cfg = autocfg::new();
5+
if cfg.probe_rustc_version(1, 31) {
6+
println!("cargo:rustc-cfg=has_min_const_fn");
7+
}
8+
}

crossbeam-epoch/src/atomic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<T> Atomic<T> {
150150
///
151151
/// let a = Atomic::<i32>::null();
152152
/// ```
153-
#[cfg(not(feature = "nightly"))]
153+
#[cfg(not(has_min_const_fn))]
154154
pub fn null() -> Atomic<T> {
155155
Self {
156156
data: AtomicUsize::new(0),
@@ -167,7 +167,7 @@ impl<T> Atomic<T> {
167167
///
168168
/// let a = Atomic::<i32>::null();
169169
/// ```
170-
#[cfg(feature = "nightly")]
170+
#[cfg(has_min_const_fn)]
171171
pub const fn null() -> Atomic<T> {
172172
Self {
173173
data: AtomicUsize::new(0),

crossbeam-epoch/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#![warn(missing_docs)]
5858
#![warn(missing_debug_implementations)]
5959
#![cfg_attr(not(feature = "std"), no_std)]
60-
#![cfg_attr(feature = "nightly", feature(const_fn))]
6160
#![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))]
6261

6362
#[macro_use]

crossbeam-utils/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ alloc = []
2525
cfg-if = "0.1.2"
2626
lazy_static = { version = "1.1.0", optional = true }
2727

28+
[build-dependencies]
29+
autocfg = "0.1.6"
30+
2831
[dev-dependencies]
2932
rand = "0.6"

crossbeam-utils/build.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern crate autocfg;
2+
3+
fn main() {
4+
let cfg = autocfg::new();
5+
if cfg.probe_rustc_version(1, 31) {
6+
println!("cargo:rustc-cfg=has_min_const_fn");
7+
}
8+
}

crossbeam-utils/src/atomic/atomic_cell.rs

+17
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,29 @@ impl<T> AtomicCell<T> {
5151
///
5252
/// let a = AtomicCell::new(7);
5353
/// ```
54+
#[cfg(not(has_min_const_fn))]
5455
pub fn new(val: T) -> AtomicCell<T> {
5556
AtomicCell {
5657
value: UnsafeCell::new(val),
5758
}
5859
}
5960

61+
/// Creates a new atomic cell initialized with `val`.
62+
///
63+
/// # Examples
64+
///
65+
/// ```
66+
/// use crossbeam_utils::atomic::AtomicCell;
67+
///
68+
/// let a = AtomicCell::new(7);
69+
/// ```
70+
#[cfg(has_min_const_fn)]
71+
pub const fn new(val: T) -> AtomicCell<T> {
72+
AtomicCell {
73+
value: UnsafeCell::new(val),
74+
}
75+
}
76+
6077
/// Unwraps the atomic cell and returns its inner value.
6178
///
6279
/// # Examples

crossbeam-utils/tests/atomic_cell.rs

+9
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,12 @@ fn garbage_padding() {
223223
assert!(cell.compare_exchange(prev, next).is_ok());
224224
println!();
225225
}
226+
227+
#[cfg(has_min_const_fn)]
228+
#[test]
229+
fn const_atomic_cell_new() {
230+
static CELL: AtomicCell<usize> = AtomicCell::new(0);
231+
232+
CELL.store(1);
233+
assert_eq!(CELL.load(), 1);
234+
}

0 commit comments

Comments
 (0)