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 5 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
53 changes: 53 additions & 0 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2709,6 +2709,33 @@ 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.
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
///
/// // Start with a `VecDeque<i32>`.
/// let deque: VecDeque<_> = (1..5).collect();
///
/// // Turn it into a `Vec<i32>` with no allocation needed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation for spending so much time worrying about reallocation here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a few open issues where people want to know about our allocation strategy for various From impls. In other words, people want to know so they are being explicit about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, the only thing interesting about these two From impls is that they can (sometimes) avoid allocations compared to .into_iter().collect(). So I want to write an example that shows the lack of reallocation, but I also want to write it in such a way that it's a situation where we'd be ok committing to the lack of reallocation. So I want to make an example that doesn't do things like "well, I happen to know that right now it doesn't reallocate if I with_capacity(8) and don't use all the capacity", because I don't want to write text elaborating why those conditions work.

I agree this example is far more complicated than most, though, so would be happy to change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if an example is really helpful at all here, if we don't actually want to guarantee anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfackler Ok, I removed the example from here.

/// let mut vec = Vec::from(deque);
///
/// // Modify it, being careful not to trigger reallocation.
/// vec.pop();
/// vec.push(100);
///
/// // Turn it back into a `VecDeque<i32>` with no allocation needed.
/// let ptr = vec.as_ptr();
/// let deque = VecDeque::from(vec);
/// assert_eq!(deque, [1, 2, 3, 100]);
/// assert_eq!(deque.as_slices().0.as_ptr(), ptr);
/// ```
fn from(mut other: Vec<T>) -> Self {
unsafe {
let other_buf = other.as_mut_ptr();
Expand All @@ -2735,6 +2762,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