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

Implement `From<Vec<T>>` and `Into<Vec<T>>` for `VecDeque<T>` #32866

Merged
merged 1 commit into from Apr 19, 2016

Conversation

Projects
None yet
5 participants
@davidhewitt
Copy link
Contributor

davidhewitt commented Apr 10, 2016

No description provided.

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Apr 10, 2016

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@davidhewitt

This comment has been minimized.

Copy link
Contributor Author

davidhewitt commented Apr 10, 2016

Ah, looks like I got the stability attribute wrong. Do I need to change the feature from rust1 to e.g. vecdeque_vec_conversions ?

impl<T> From<Vec<T>> for VecDeque<T> {
fn from(other: Vec<T>) -> Self {
unsafe {
let other_buf: *mut T = mem::transmute(other.as_ptr());

This comment has been minimized.

@apasel422

apasel422 Apr 10, 2016

Member

If you declare other to be mut, this can just be:

let other_buf = other.as_mut_ptr();

(The transmute is unnecessary.)

@apasel422

This comment has been minimized.

Copy link
Member

apasel422 commented Apr 10, 2016

@davidhewitt These impls should probably start off unstable. You can use vecdeque_vec_conversions as the feature name, and #32848 as the issue number. I forgot that we'd need to keep a tracking issue open, so let's actually remove "Closes #32848" from the PR message.


// Do this to force resize to correct size;
// otherwise we may get not_power_of_two assert failures!
out.reserve(0);

This comment has been minimized.

@apasel422

apasel422 Apr 10, 2016

Member

It'd be good to test for the power-of-two invariant in your test for this method.

@@ -2401,4 +2438,24 @@ mod tests {
}
}
}

#[test]
fn test_from_vec() {

This comment has been minimized.

@apasel422

apasel422 Apr 10, 2016

Member

Can you add some additional test cases here with varying vec lengths and capacities? It might be easiest to do something like:

#[test]
fn test_from_vec() {
    for cap in 0..35 {
        for len in 0..cap + 1 {
            let mut vec = Vec::with_capacity(cap);
            vec.extend(0..len);

            let vd = VecDeque::from(vec.clone());
            assert!(vd.cap().is_power_of_two());
            assert_eq!(vd.len(), vec.len());
            assert!(vd.into_iter().eq(vec));
        }
    }
}
}

#[test]
fn test_into_vec() {

This comment has been minimized.

@apasel422

apasel422 Apr 10, 2016

Member

This could also benefit from some other test cases to ensure that the use of wrap_copy is correct, with varying item layouts (contiguous, wrapping, etc).

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 11, 2016

Thanks for the PR @davidhewitt! @apasel422 unfortunately we don't actually read stability annotations on trait impls right now, so these'll be insta-stable if they land (so #[stable] is fine).

It's also interesting implementing the Into trait instead of the From trait as From<VecDeque<T>> for Vec<T>. Right now we pretty consistently implement From rather than Into (as it also gives you the into method). Would that be possible to do, or is that not allowed because of privacy just yet?

@alexcrichton alexcrichton added the T-libs label Apr 11, 2016

@apasel422

This comment has been minimized.

Copy link
Member

apasel422 commented Apr 11, 2016

@alexcrichton Hmm, I didn't even consider the From/Into distinction when I filed the issue, but yes, we should use whatever convention we've been going with.

@davidhewitt

This comment has been minimized.

Copy link
Contributor Author

davidhewitt commented Apr 11, 2016

Hi guys, I'm working on some corrections to this PR, might be a couple of evenings before they land if that's ok?

R.E. the From / Into - shall I rework the Into into a From on the Vec class?

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 11, 2016

@davidhewitt no worries! Feel free to ping this PR when it's updated. And yeah let's try to put From on Vec directly, but if it's too hard we can discuss that at libs triage.

@davidhewitt

This comment has been minimized.

Copy link
Contributor Author

davidhewitt commented Apr 14, 2016

@apasel422 @alexcrichton So I've taken the first round of comments on board and rewritten this patch with better tests, and covered some corner cases uncovered during testing. Thanks for the feedback, I feel I learned some good stuff with how to make better tests there!

RE. putting the From on Vec, I can do this as long as it's implemented in vec_deque.rs, and not vec.rs. That feels a little weird to me, but privacy stops it working the other way around. Does that feel okay with you guys?

for i in left_edge..right_edge {
right_offset = (i - left_edge) % (cap - right_edge);
let src: isize = (right_edge + right_offset) as isize;
println!("swap {} to {}; cap {}; left {}; len: {}; right {};", src, i, cap, left_edge, len, right_edge);

This comment has been minimized.

@alexcrichton

alexcrichton Apr 15, 2016

Member

Looks like a stray println?

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 15, 2016

Thanks for the update @davidhewitt! The libs team discussed this in triage yesterday and the conclusion was that this is good-to-go (especially now that both impls are From rather than one of each).

I'll leave the r+ to @apasel422 though :)

@bors: delegate=apasel422

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Apr 15, 2016

✌️ @apasel422 can now approve this pull request

@davidhewitt

This comment has been minimized.

Copy link
Contributor Author

davidhewitt commented Apr 15, 2016

Thanks @alexcrichton!

@apasel422 - I took "Closes #32848" out of the PR message, just checking that's what you wanted?

let cap = other.cap();

// Need to move the ring to the front of the buffer, as vec will expect this.
if other.is_contiguous() {

This comment has been minimized.

@apasel422

apasel422 Apr 15, 2016

Member

Would it be possible to ensure that test_vec_from_vecdeque explicitly tests each of the four cases?

@apasel422

This comment has been minimized.

Copy link
Member

apasel422 commented Apr 15, 2016

This looks good, pending some additional tests. Once you've done that, could you squash all the commits into one?

@davidhewitt davidhewitt force-pushed the davidhewitt:master branch 2 times, most recently from 95fc8e7 to 89f92cd Apr 15, 2016

@davidhewitt

This comment has been minimized.

Copy link
Contributor Author

davidhewitt commented Apr 15, 2016

I rewrote test_vec_from_vecdeque to expand out the loops, to label which configurations of the loop variables will be testing which bits of the ring-straightening algorithm. Also squashed the commits.

Is there other tests you have in mind?

@apasel422

This comment has been minimized.

Copy link
Member

apasel422 commented Apr 15, 2016

Looks like there are a few lines longer than 100 characters. Otherwise, this is ready to merge.

@davidhewitt davidhewitt force-pushed the davidhewitt:master branch from 89f92cd to fefad3d Apr 16, 2016

@apasel422

This comment has been minimized.

Copy link
Member

apasel422 commented Apr 16, 2016

@davidhewitt Looks like this failed with a syntax error.

@davidhewitt davidhewitt force-pushed the davidhewitt:master branch from fefad3d to 985a962 Apr 17, 2016

@davidhewitt

This comment has been minimized.

Copy link
Contributor Author

davidhewitt commented Apr 17, 2016

Sorry, made a mistake breaking up the lines. :/

@apasel422

This comment has been minimized.

Copy link
Member

apasel422 commented Apr 17, 2016

@alexcrichton Is 1.9.0 the right value for since?

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Apr 18, 2016

Ah nah we're on to 1.10.0 at this point. Should write a tidy check for that somehow...

@davidhewitt

This comment has been minimized.

Copy link
Contributor Author

davidhewitt commented Apr 18, 2016

Ah okay - I'll fix that up tonight!

@davidhewitt davidhewitt force-pushed the davidhewitt:master branch from 985a962 to 1861951 Apr 18, 2016

@apasel422

This comment has been minimized.

Copy link
Member

apasel422 commented Apr 19, 2016

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Apr 19, 2016

⌛️ Testing commit 1861951 with merge 14f61c8...

bors added a commit that referenced this pull request Apr 19, 2016

Auto merge of #32866 - davidhewitt:master, r=apasel422
Implement `From<Vec<T>>` and `Into<Vec<T>>` for `VecDeque<T>`

@bors bors merged commit 1861951 into rust-lang:master Apr 19, 2016

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.