File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -454,6 +454,36 @@ impl<T> MaybeUninit<T> {
454
454
/// // x is initialized now:
455
455
/// let s = unsafe { x.assume_init() };
456
456
/// ```
457
+ ///
458
+ /// This method can be used to avoid unsafe in some cases. The example below
459
+ /// shows a part of an implementation of a fixed sized arena that lends out
460
+ /// pinned references.
461
+ /// With `write`, we can avoid the need to write through a raw pointer:
462
+ ///
463
+ /// ```rust
464
+ /// #![feature(maybe_uninit_extra)]
465
+ /// use core::pin::Pin;
466
+ /// use core::mem::MaybeUninit;
467
+ ///
468
+ /// struct PinArena<T> {
469
+ /// memory: Box<[MaybeUninit<T>]>,
470
+ /// len: usize,
471
+ /// }
472
+ ///
473
+ /// impl <T> PinArena<T> {
474
+ /// pub fn capacity(&self) -> usize {
475
+ /// self.memory.len()
476
+ /// }
477
+ /// pub fn push(&mut self, val: T) -> Pin<&mut T> {
478
+ /// if self.len >= self.capacity() {
479
+ /// panic!("Attempted to push to a full pin arena!");
480
+ /// }
481
+ /// let ref_ = self.memory[self.len].write(val);
482
+ /// self.len += 1;
483
+ /// unsafe { Pin::new_unchecked(ref_) }
484
+ /// }
485
+ /// }
486
+ /// ```
457
487
#[ stable( feature = "maybe_uninit_write" , since = "1.55.0" ) ]
458
488
#[ rustc_const_unstable( feature = "const_maybe_uninit_write" , issue = "63567" ) ]
459
489
#[ inline( always) ]
You can’t perform that action at this time.
0 commit comments