File tree 3 files changed +19
-1
lines changed
3 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -211,8 +211,9 @@ impl<T: ?Sized> NonNull<T> {
211
211
/// }
212
212
/// ```
213
213
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
214
+ #[ rustc_const_unstable( feature = "const_nonnull_new" , issue = "93235" ) ]
214
215
#[ inline]
215
- pub fn new ( ptr : * mut T ) -> Option < Self > {
216
+ pub const fn new ( ptr : * mut T ) -> Option < Self > {
216
217
if !ptr. is_null ( ) {
217
218
// SAFETY: The pointer is already checked and is not null
218
219
Some ( unsafe { Self :: new_unchecked ( ptr) } )
Original file line number Diff line number Diff line change 17
17
#![ feature( const_maybe_uninit_as_mut_ptr) ]
18
18
#![ feature( const_maybe_uninit_assume_init) ]
19
19
#![ feature( const_maybe_uninit_assume_init_read) ]
20
+ #![ feature( const_nonnull_new) ]
20
21
#![ feature( const_num_from_num) ]
22
+ #![ feature( const_ptr_as_ref) ]
21
23
#![ feature( const_ptr_read) ]
22
24
#![ feature( const_ptr_write) ]
23
25
#![ feature( const_ptr_offset) ]
Original file line number Diff line number Diff line change @@ -274,6 +274,21 @@ fn test_unsized_nonnull() {
274
274
assert ! ( ys == zs) ;
275
275
}
276
276
277
+ #[ test]
278
+ fn test_const_nonnull_new ( ) {
279
+ const {
280
+ assert ! ( NonNull :: new( core:: ptr:: null_mut:: <( ) >( ) ) . is_none( ) ) ;
281
+
282
+ let value = & mut 0u32 ;
283
+ let mut ptr = NonNull :: new ( value) . unwrap ( ) ;
284
+ unsafe { * ptr. as_mut ( ) = 42 } ;
285
+
286
+ let reference = unsafe { & * ptr. as_ref ( ) } ;
287
+ assert ! ( * reference == * value) ;
288
+ assert ! ( * reference == 42 ) ;
289
+ } ;
290
+ }
291
+
277
292
#[ test]
278
293
#[ allow( warnings) ]
279
294
// Have a symbol for the test below. It doesn’t need to be an actual variadic function, match the
You can’t perform that action at this time.
0 commit comments