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

davidhewitt
Copy link
Contributor

@davidhewitt davidhewitt commented Apr 10, 2016

No description provided.

@rust-highfive
Copy link
Collaborator

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
Copy link
Contributor Author

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());
Copy link
Contributor

Choose a reason for hiding this comment

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

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

let other_buf = other.as_mut_ptr();

(The transmute is unnecessary.)

@apasel422
Copy link
Contributor

@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);
Copy link
Contributor

Choose a reason for hiding this comment

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

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

@alexcrichton
Copy link
Member

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-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 11, 2016
@apasel422
Copy link
Contributor

@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
Copy link
Contributor Author

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
Copy link
Member

@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
Copy link
Contributor Author

@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);
Copy link
Member

Choose a reason for hiding this comment

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

Looks like a stray println?

@alexcrichton
Copy link
Member

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
Copy link
Contributor

bors commented Apr 15, 2016

✌️ @apasel422 can now approve this pull request

@davidhewitt
Copy link
Contributor Author

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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

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

@apasel422
Copy link
Contributor

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 master branch 2 times, most recently from 95fc8e7 to 89f92cd Compare April 15, 2016 07:40
@davidhewitt
Copy link
Contributor Author

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
Copy link
Contributor

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

@apasel422
Copy link
Contributor

@davidhewitt Looks like this failed with a syntax error.

@davidhewitt
Copy link
Contributor Author

davidhewitt commented Apr 17, 2016

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

@apasel422
Copy link
Contributor

@alexcrichton Is 1.9.0 the right value for since?

@alexcrichton
Copy link
Member

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

@davidhewitt
Copy link
Contributor Author

Ah okay - I'll fix that up tonight!

@apasel422
Copy link
Contributor

@bors r+ 1861951

@bors
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
Implement `From<Vec<T>>` and `Into<Vec<T>>` for `VecDeque<T>`
@bors bors merged commit 1861951 into rust-lang:master Apr 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants