Skip to content

Add some Vec <-> VecDeque documentation #61447

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

Merged
merged 6 commits into from
Jun 16, 2019
Merged
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
31 changes: 31 additions & 0 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2709,6 +2709,11 @@ impl<T: fmt::Debug> fmt::Debug for VecDeque<T> {

#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
impl<T> From<Vec<T>> for VecDeque<T> {
/// Turn a [`Vec<T>`] into a [`VecDeque<T>`].
///
/// This avoids reallocating where possible, but the conditions for that are
/// strict, and subject to change, and so shouldn't be relied upon unless the
/// `Vec<T>` came from `From<VecDeque<T>>` and hasn't been reallocated.
fn from(mut other: Vec<T>) -> Self {
unsafe {
let other_buf = other.as_mut_ptr();
Expand All @@ -2735,6 +2740,32 @@ impl<T> From<Vec<T>> for VecDeque<T> {

#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
impl<T> From<VecDeque<T>> for Vec<T> {
/// Turn a [`VecDeque<T>`] into a [`Vec<T>`].
///
/// This never needs to re-allocate, but does need to do O(n) data movement if
/// the circular buffer doesn't happen to be at the beginning of the allocation.
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
///
/// // This one is O(1).
/// let deque: VecDeque<_> = (1..5).collect();
/// let ptr = deque.as_slices().0.as_ptr();
/// let vec = Vec::from(deque);
/// assert_eq!(vec, [1, 2, 3, 4]);
/// assert_eq!(vec.as_ptr(), ptr);
///
/// // This one needs data rearranging.
/// let mut deque: VecDeque<_> = (1..5).collect();
/// deque.push_front(9);
/// deque.push_front(8);
/// let ptr = deque.as_slices().1.as_ptr();
/// let vec = Vec::from(deque);
/// assert_eq!(vec, [8, 9, 1, 2, 3, 4]);
/// assert_eq!(vec.as_ptr(), ptr);
/// ```
fn from(other: VecDeque<T>) -> Self {
unsafe {
let buf = other.buf.ptr();
Expand Down