-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add Vec::resize_default. #41771
Add Vec::resize_default. #41771
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
src/libcollections/vec.rs
Outdated
/// | ||
/// If `new_len` is greater than `len()`, the `Vec` is extended by the | ||
/// difference, with each additional slot filled with `Default::default()`. | ||
/// If `new_len` is less than `len()`, the `Vec` is simply truncated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a note "See also Vec::resize
, which uses clone."? I don't know what policy is on that, though (cc @rust-lang/docs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will fix the docs later today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linking to our methods is good, we do that in other places. What does 'cloning' have to do with this method? Seems like it should say something along the lines of: "If you'd like to specify your own value if the Vec
is extended, use Vec::resize
".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the docs to reference both methods in each other.
src/libcollections/vec.rs
Outdated
/// assert_eq!(vec, [1, 2]); | ||
/// ``` | ||
#[unstable(feature = "vec_resize_default", issue = "41758")] | ||
pub fn resize_default(&mut self, new_len: usize, value: T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surely it's not supposed to take a value
argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is what I get for making a PR right before running to catch the bus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I fixed this btw)
src/libcollections/vec.rs
Outdated
@@ -1301,6 +1301,41 @@ impl<T: Clone> Vec<T> { | |||
} | |||
} | |||
|
|||
impl<T: Default> Vec<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought, separate impl
blocks for different bounds seems archaic (from the time before where
clauses), so we probably don't want to continue that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I agree, although for now I think it'd be best to continue the pattern and refactor the entire file in a future PR.
Also, this seems like a good candidate for a fmt RFC.
src/libcollections/vec.rs
Outdated
@@ -1301,6 +1306,47 @@ impl<T: Clone> Vec<T> { | |||
} | |||
} | |||
|
|||
impl<T: Default> Vec<T> { | |||
/// Resizes the `Vec` in-place so that `len()` is equal to `new_len`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we (the docs team) for the time being, don't add ()
to the end of method names in documentation, so this len()
should be len
, ideally linking to the associated method. alternatively, you could just say "...so that its length is equal to new_len
." not a big deal though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will modify on both methods! This was copy-pasted from the old docs.
Looks like there's a test failure:
|
/// | ||
/// [`resize`]: #method.resize | ||
#[unstable(feature = "vec_resize_default", issue = "41758")] | ||
pub fn resize_default(&mut self, new_len: usize) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, should we just implement this with self.resize(new_len, T::default())
? I guess it would invoke default
even when you don't need it...but it seems odd for this implementation to stray from the other resize
(e.g., that version calls self.extend_with_element()
, maybe we should do the same?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, but I decided we couldn't since that would require T: Clone
, right? Same for calling self.extend_with_element()
. Ideally we'd have some trait that is "produce more of the same value" which would be be implemented for both T: Default
and T: Clone
. Unless we have an impl impl Clone for T where T: Default
, which could work (i.e., calling Default::default
each time, but might be impossible to add today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was mentioning about a more efficient implementation is that we could specialise this to just call extend_with_element
for Clone + Default
.
I can add this before merge if it's desired for the initial PR, although I might not be free this week to do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also potentially add a clippy lint that warns if a type implements Default
but not Clone
, but IDK if that's too niche.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, that's true, I forgot that Default
doesn't imply Clone
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, ok, I give up =) I was going to suggest maybe combining extend_with_{element,default}
into one thing that takes a closure, but that seems to require an extra clone. Seems ok as is I suppose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try and figure something out; I have a feeling that I know what to do.
6daa187
to
a03ff4e
Compare
A few warnings to remove, and then probably ready for re-review.
|
a03ff4e
to
951647e
Compare
So, I hopefully fixed the errors and also generalised |
Travis is failing. @clarcharr Just to check, are you able to build locally? Is there something we can help resolve to assist with that? (It's fine if you use travis for tests, just want to make sure that there's nothing on our side blocking your ability to build locally).
|
src/libcollections/vec.rs
Outdated
// This code generalises `extend_with_{element,default}`. | ||
trait ExtendWith<T> { | ||
fn next(&self) -> T; | ||
fn last(self) -> T { self.next() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Given that this is a private trait with two impls, I'd say remove the default impl and just move this method to the ExtendDefault
trait.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is clever.
951647e
to
c2c0641
Compare
/// difference, with each additional slot filled with `value`. | ||
/// If `new_len` is less than `len()`, the `Vec` is simply truncated. | ||
/// If `new_len` is less than `len`, the `Vec` is simply truncated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since len
is a method, not a variable, I think it makes sense for it to be new_len
(variable) and len()
(method) in the comment, as it was before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is based upon the earlier suggestion by @frewsxcv; the docs team has decided to not put the parentheses on the methods, so, I will go with the convention says.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I was unaware. Sorry for the noise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record, I filed Manishearth/rust-clippy#1750 so that we can maybe lint for this convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I find that new convention pretty confusing! I agree that this PR ought to follow it, but I think we ought to consider changing it.
@Mark-Simulacrum There isn't actually anything preventing me from testing locally, other than the fact that I need to remember to set my computer down for an hour and build once so that I can actually test quickly. I've been moving around a lot the past few days and haven't had time. :P |
Note: tests pass now. Is there anything left to do before merge? @nikomatsakis |
@bors r+ |
📌 Commit c2c0641 has been approved by |
⌛ Testing commit c2c0641 with merge b89bcf5... |
💔 Test failed - status-appveyor |
@bors: retry
|
…sakis Add Vec::resize_default. As suggested by rust-lang#41758.
⌛ Testing commit c2c0641 with merge 4d09a0e... |
Add Vec::resize_default. As suggested by #41758.
☀️ Test successful - status-appveyor, status-travis |
As suggested by #41758.