@@ -4774,6 +4774,49 @@ impl<T> [T] {
4774
4774
}
4775
4775
}
4776
4776
4777
+ impl < T > [ MaybeUninit < T > ] {
4778
+ /// Transmutes the mutable uninitialized slice to a mutable uninitialized slice of
4779
+ /// another type, ensuring alignment of the types is maintained.
4780
+ ///
4781
+ /// This is a safe wrapper around [`slice::align_to_mut`], so inherits the same
4782
+ /// guarantees as that method.
4783
+ ///
4784
+ /// # Examples
4785
+ ///
4786
+ /// ```
4787
+ /// #![feature(align_to_uninit_mut)]
4788
+ /// pub struct BumpAllocator<'scope> {
4789
+ /// memory: &'scope mut [MaybeUninit<u8>],
4790
+ /// }
4791
+ ///
4792
+ /// impl<'scope> BumpAllocator<'scope> {
4793
+ /// pub fn new(memory: &'scope mut [MaybeUninit<u8>]) -> Self {
4794
+ /// Self { memory }
4795
+ /// }
4796
+ /// pub fn try_alloc_uninit<T>(&mut self) -> Option<&'scope mut MaybeUninit<T>> {
4797
+ /// let first_end = self.memory.as_ptr().align_offset(align_of::<T>()) + size_of::<T>();
4798
+ /// let prefix = self.memory.split_off_mut(..first_end)?;
4799
+ /// Some(&mut prefix.align_to_uninit_mut::<T>().1[0])
4800
+ /// }
4801
+ /// pub fn try_alloc_u32(&mut self, value: u32) -> Option<&'scope mut u32> {
4802
+ /// let uninit = self.try_alloc_uninit()?;
4803
+ /// Some(uninit.write(value))
4804
+ /// }
4805
+ /// }
4806
+ ///
4807
+ /// let mut memory = [MaybeUninit::<u8>::uninit(); 10];
4808
+ /// let mut allocator = BumpAllocator::new(&mut memory);
4809
+ /// let v = allocator.try_alloc_u32(42);
4810
+ /// assert_eq!(v, Some(&mut 42));
4811
+ /// ```
4812
+ #[ unstable( feature = "align_to_uninit_mut" , issue = "139062" ) ]
4813
+ #[ inline]
4814
+ #[ must_use]
4815
+ pub fn align_to_uninit_mut < U > ( & mut self ) -> ( & mut Self , & mut [ MaybeUninit < U > ] , & mut Self ) {
4816
+ unsafe { self . align_to_mut ( ) }
4817
+ }
4818
+ }
4819
+
4777
4820
impl < T , const N : usize > [ [ T ; N ] ] {
4778
4821
/// Takes a `&[[T; N]]`, and flattens it to a `&[T]`.
4779
4822
///
0 commit comments