Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement `From<Vec<T>>` and `Into<Vec<T>>` for `VecDeque<T>` #32866
Conversation
rust-highfive
assigned
alexcrichton
Apr 10, 2016
This comment has been minimized.
This comment has been minimized.
|
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
referenced this pull request
Apr 10, 2016
Closed
Implement `From<Vec<T>>` and `Into<Vec<T>>` for `VecDeque<T>` #32848
This comment has been minimized.
This comment has been minimized.
|
Ah, looks like I got the stability attribute wrong. Do I need to change the feature from rust1 to e.g. vecdeque_vec_conversions ? |
apasel422
reviewed
Apr 10, 2016
| 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.
This comment has been minimized.
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.)
This comment has been minimized.
This comment has been minimized.
|
@davidhewitt These impls should probably start off unstable. You can use |
apasel422
reviewed
Apr 10, 2016
|
|
||
| // 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.
This comment has been minimized.
apasel422
Apr 10, 2016
Member
It'd be good to test for the power-of-two invariant in your test for this method.
apasel422
reviewed
Apr 10, 2016
| @@ -2401,4 +2438,24 @@ mod tests { | |||
| } | |||
| } | |||
| } | |||
|
|
|||
| #[test] | |||
| fn test_from_vec() { | |||
This comment has been minimized.
This comment has been minimized.
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));
}
}
}
apasel422
reviewed
Apr 10, 2016
| } | ||
|
|
||
| #[test] | ||
| fn test_into_vec() { |
This comment has been minimized.
This comment has been minimized.
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).
This comment has been minimized.
This comment has been minimized.
|
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 It's also interesting implementing the |
alexcrichton
added
the
T-libs
label
Apr 11, 2016
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Hmm, I didn't even consider the |
This comment has been minimized.
This comment has been minimized.
|
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? |
This comment has been minimized.
This comment has been minimized.
|
@davidhewitt no worries! Feel free to ping this PR when it's updated. And yeah let's try to put |
This comment has been minimized.
This comment has been minimized.
|
@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 |
alexcrichton
reviewed
Apr 15, 2016
| 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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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 I'll leave the r+ to @apasel422 though :) @bors: delegate=apasel422 |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
Thanks @alexcrichton! @apasel422 - I took "Closes #32848" out of the PR message, just checking that's what you wanted? |
apasel422
reviewed
Apr 15, 2016
| 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.
This comment has been minimized.
apasel422
Apr 15, 2016
Member
Would it be possible to ensure that test_vec_from_vecdeque explicitly tests each of the four cases?
This comment has been minimized.
This comment has been minimized.
|
This looks good, pending some additional tests. Once you've done that, could you squash all the commits into one? |
davidhewitt
force-pushed the
davidhewitt:master
branch
2 times, most recently
from
95fc8e7
to
89f92cd
Apr 15, 2016
This comment has been minimized.
This comment has been minimized.
|
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? |
This comment has been minimized.
This comment has been minimized.
|
Looks like there are a few lines longer than 100 characters. Otherwise, this is ready to merge. |
davidhewitt
force-pushed the
davidhewitt:master
branch
from
89f92cd
to
fefad3d
Apr 16, 2016
This comment has been minimized.
This comment has been minimized.
|
@davidhewitt Looks like this failed with a syntax error. |
davidhewitt
force-pushed the
davidhewitt:master
branch
from
fefad3d
to
985a962
Apr 17, 2016
This comment has been minimized.
This comment has been minimized.
|
Sorry, made a mistake breaking up the lines. :/ |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Is |
This comment has been minimized.
This comment has been minimized.
|
Ah nah we're on to 1.10.0 at this point. Should write a tidy check for that somehow... |
This comment has been minimized.
This comment has been minimized.
|
Ah okay - I'll fix that up tonight! |
davidhewitt commentedApr 10, 2016
•
edited
No description provided.