Skip to content

Commit 0c4106e

Browse files
committed
Make AtomicRef::new const fn
Requires the following unstable features: - `const_fn` for constructing `PhantomData<&mut _>` <rust-lang/rust#57563> - `const_if_match` for using `match` in a user-defined `const fn` <rust-lang/rust#49146>
1 parent fd4c68e commit 0c4106e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
//! }
7373
//! ```
7474
#![no_std]
75+
#![feature(const_fn)]
76+
#![feature(const_if_match)]
7577

7678
use core::sync::atomic::{AtomicPtr, Ordering};
7779
use core::marker::PhantomData;
@@ -174,7 +176,7 @@ macro_rules! static_atomic_ref {
174176

175177
/// An internal helper function for converting `Option<&'a T>` values to
176178
/// `*mut T` for storing in the `AtomicUsize`.
177-
fn from_opt<'a, T>(p: Option<&'a T>) -> *mut T {
179+
const fn from_opt<'a, T>(p: Option<&'a T>) -> *mut T {
178180
match p {
179181
Some(p) => p as *const T as *mut T,
180182
None => null_mut(),
@@ -198,7 +200,7 @@ impl<'a, T> AtomicRef<'a, T> {
198200
/// static VALUE: i32 = 10;
199201
/// let atomic_ref = AtomicRef::new(Some(&VALUE));
200202
/// ```
201-
pub fn new(p: Option<&'a T>) -> AtomicRef<'a, T> {
203+
pub const fn new(p: Option<&'a T>) -> AtomicRef<'a, T> {
202204
AtomicRef {
203205
data: AtomicPtr::new(from_opt(p)),
204206
_marker: PhantomData,

0 commit comments

Comments
 (0)