71
71
//! assert!(res);
72
72
//! }
73
73
//! ```
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
+ //!
74
80
#![ 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) ) ]
77
83
78
84
use core:: sync:: atomic:: { AtomicPtr , Ordering } ;
79
85
use core:: marker:: PhantomData ;
@@ -174,12 +180,29 @@ macro_rules! static_atomic_ref {
174
180
( ) => ( ) ;
175
181
}
176
182
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
+ }
183
206
}
184
207
}
185
208
@@ -190,20 +213,22 @@ unsafe fn to_opt<'a, T>(p: *mut T) -> Option<&'a T> {
190
213
}
191
214
192
215
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
+ }
207
232
}
208
233
}
209
234
0 commit comments