Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to init MaybeUninit<ArrayDeque> #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,32 @@ impl<T, const CAP: usize, B: Behavior> ArrayDeque<T, CAP, B> {
}
}

/// Initializes a `MaybeUninit<ArrayDeque>`. 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<MaybeUninit<ArrayDeque<usize, 2>>> = Box::new_uninit();
/// ArrayDeque::new_init(&mut buf);
/// let buf: Box<ArrayDeque<usize, 2>> = unsafe { Box::<MaybeUninit<_>>::assume_init(buf) };
/// ```
#[inline]
pub fn new_init(this: &mut MaybeUninit<Self>) -> &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
Expand Down