@@ -374,6 +374,20 @@ impl<T> MaybeUninit<T> {
374
374
/// assert_eq!(x, (0, false));
375
375
/// ```
376
376
///
377
+ /// This can be used in const contexts, such as to indicate the end of plugin registration
378
+ /// arrays.
379
+ ///
380
+ /// ```
381
+ /// use std::mem::MaybeUninit;
382
+ ///
383
+ /// struct PluginInfo { id: u32, action: Option<fn(u32) -> u32> }
384
+ ///
385
+ /// static PLUGIN_LIST: [PluginInfo; 2] = [
386
+ /// PluginInfo { id: 1, action: Some(|x| x + 5) },
387
+ /// unsafe { MaybeUninit::zeroed().assume_init() }
388
+ /// ];
389
+ /// ```
390
+ ///
377
391
/// *Incorrect* usage of this function: calling `x.zeroed().assume_init()`
378
392
/// when `0` is not a valid bit-pattern for the type:
379
393
///
@@ -387,17 +401,19 @@ impl<T> MaybeUninit<T> {
387
401
/// // Inside a pair, we create a `NotZero` that does not have a valid discriminant.
388
402
/// // This is undefined behavior. ⚠️
389
403
/// ```
390
- #[ stable( feature = "maybe_uninit" , since = "1.36.0" ) ]
391
- #[ rustc_const_unstable( feature = "const_maybe_uninit_zeroed" , issue = "91850" ) ]
392
- #[ must_use]
393
404
#[ inline]
405
+ #[ must_use]
394
406
#[ rustc_diagnostic_item = "maybe_uninit_zeroed" ]
407
+ #[ stable( feature = "maybe_uninit" , since = "1.36.0" ) ]
408
+ // These are OK to allow since we do not leak &mut to user-visible API
409
+ #[ rustc_allow_const_fn_unstable( const_mut_refs) ]
410
+ #[ rustc_allow_const_fn_unstable( const_ptr_write) ]
411
+ #[ rustc_allow_const_fn_unstable( const_maybe_uninit_as_mut_ptr) ]
412
+ #[ rustc_const_stable( feature = "const_maybe_uninit_zeroed" , since = "CURRENT_RUSTC_VERSION" ) ]
395
413
pub const fn zeroed ( ) -> MaybeUninit < T > {
396
414
let mut u = MaybeUninit :: < T > :: uninit ( ) ;
397
415
// SAFETY: `u.as_mut_ptr()` points to allocated memory.
398
- unsafe {
399
- u. as_mut_ptr ( ) . write_bytes ( 0u8 , 1 ) ;
400
- }
416
+ unsafe { u. as_mut_ptr ( ) . write_bytes ( 0u8 , 1 ) } ;
401
417
u
402
418
}
403
419
0 commit comments