diff --git a/src/lib.rs b/src/lib.rs index 3facdcd..15a08e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1011,6 +1011,32 @@ impl ArrayDeque { } } + /// Initializes a `MaybeUninit`. This is useful to guarantee + /// that a potantially large `ArrrayDeque` won't overflow the stack + /// when it's intended for the heap. + /// + /// # Examples + /// + /// ```ignore + /// #![feature(new_uninit)] + /// use std::mem::MaybeUninit; + /// use arraydeque::ArrayDeque; + /// + /// // Equivalent to `Box::new(ArrayDeque::new())` but avoids going via the stack + /// + /// let mut buf: Box>> = Box::new_uninit(); + /// ArrayDeque::new_init(&mut buf); + /// let buf: Box> = unsafe { Box::>::assume_init(buf) }; + /// ``` + #[inline] + pub fn new_init(this: &mut MaybeUninit) -> &mut Self { + unsafe { + ptr::addr_of_mut!((*this.as_mut_ptr()).tail).write(0); + ptr::addr_of_mut!((*this.as_mut_ptr()).len).write(0); + this.assume_init_mut() + } + } + /// Return the capacity of the `ArrayDeque`. /// /// # Examples