Skip to content

Commit 97dc28c

Browse files
committed
Make the use of unstable features non-mandatory
1 parent 0c4106e commit 97dc28c

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ readme = "README.md"
1010
license = "MIT"
1111
keywords = ["atomic", "reference", "static"]
1212

13+
[features]
14+
default = []
15+
nightly = []
16+
1317
[dependencies]

src/lib.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@
7171
//! assert!(res);
7272
//! }
7373
//! ```
74+
//!
75+
//! # Cargo Features
76+
//!
77+
//! - `nightly` enables the use of unstable features. Turns [`AtomicRef::new`]
78+
//! into `const fn`, making it callable in constant contexts.
79+
//!
7480
#![no_std]
75-
#![feature(const_fn)]
76-
#![feature(const_if_match)]
81+
#![cfg_attr(feature = "nightly", feature(const_fn))]
82+
#![cfg_attr(feature = "nightly", feature(const_if_match))]
7783

7884
use core::sync::atomic::{AtomicPtr, Ordering};
7985
use core::marker::PhantomData;
@@ -174,12 +180,29 @@ macro_rules! static_atomic_ref {
174180
() => ();
175181
}
176182

177-
/// An internal helper function for converting `Option<&'a T>` values to
178-
/// `*mut T` for storing in the `AtomicUsize`.
179-
const fn from_opt<'a, T>(p: Option<&'a T>) -> *mut T {
180-
match p {
181-
Some(p) => p as *const T as *mut T,
182-
None => null_mut(),
183+
macro_rules! const_fn_if_nightly {
184+
(
185+
$( #[$meta:meta] )*
186+
$vis:vis fn $($rest:tt)*
187+
) => {
188+
$( #[$meta] )*
189+
#[cfg(feature = "nightly")]
190+
$vis const fn $($rest)*
191+
192+
$( #[$meta] )*
193+
#[cfg(not(feature = "nightly"))]
194+
$vis fn $($rest)*
195+
};
196+
}
197+
198+
const_fn_if_nightly! {
199+
/// An internal helper function for converting `Option<&'a T>` values to
200+
/// `*mut T` for storing in the `AtomicUsize`.
201+
fn from_opt<'a, T>(p: Option<&'a T>) -> *mut T {
202+
match p {
203+
Some(p) => p as *const T as *mut T,
204+
None => null_mut(),
205+
}
183206
}
184207
}
185208

@@ -190,20 +213,22 @@ unsafe fn to_opt<'a, T>(p: *mut T) -> Option<&'a T> {
190213
}
191214

192215
impl<'a, T> AtomicRef<'a, T> {
193-
/// Creates a new `AtomicRef`.
194-
///
195-
/// # Examples
196-
///
197-
/// ```
198-
/// use atomic_ref::AtomicRef;
199-
///
200-
/// static VALUE: i32 = 10;
201-
/// let atomic_ref = AtomicRef::new(Some(&VALUE));
202-
/// ```
203-
pub const fn new(p: Option<&'a T>) -> AtomicRef<'a, T> {
204-
AtomicRef {
205-
data: AtomicPtr::new(from_opt(p)),
206-
_marker: PhantomData,
216+
const_fn_if_nightly! {
217+
/// Creates a new `AtomicRef`.
218+
///
219+
/// # Examples
220+
///
221+
/// ```
222+
/// use atomic_ref::AtomicRef;
223+
///
224+
/// static VALUE: i32 = 10;
225+
/// let atomic_ref = AtomicRef::new(Some(&VALUE));
226+
/// ```
227+
pub fn new(p: Option<&'a T>) -> AtomicRef<'a, T> {
228+
AtomicRef {
229+
data: AtomicPtr::new(from_opt(p)),
230+
_marker: PhantomData,
231+
}
207232
}
208233
}
209234

0 commit comments

Comments
 (0)