-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add back Vec::from_elem
#832
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
- Feature Name: from_elem_with_love | ||
- Start Date: 2015-02-11 | ||
- RFC PR: (leave this empty) | ||
- Rust Issue: (leave this empty) | ||
|
||
# Summary | ||
|
||
Add back the functionality of `Vec::from_elem` by improving the `vec![x; n]` sugar to work with Clone `x` and runtime `n`. | ||
|
||
# Motivation | ||
|
||
High demand, mostly. There are currently a few ways to achieve the behaviour of `Vec::from_elem(elem, n)`: | ||
|
||
``` | ||
// #1 | ||
let vec = Vec::new(); | ||
for i in range(0, n) { | ||
vec.push(elem.clone()) | ||
} | ||
``` | ||
|
||
``` | ||
// #2 | ||
let vec = vec![elem; n] | ||
``` | ||
|
||
``` | ||
// #3 | ||
let vec = Vec::new(); | ||
vec.resize(elem, n); | ||
``` | ||
|
||
``` | ||
// #4 | ||
let vec: Vec<_> = (0..n).map(|_| elem.clone()).collect() | ||
``` | ||
|
||
``` | ||
// #5 | ||
let vec: Vec<_> = iter::repeat(elem).take(n).collect(); | ||
``` | ||
|
||
None of these quite match the convenience, power, and performance of: | ||
|
||
``` | ||
let vec = Vec::from_elem(elem, n) | ||
``` | ||
|
||
* `#1` is verbose *and* slow, because each `push` requires a capacity check. | ||
* `#2` only works for a Copy `elem` and const `n`. | ||
* `#3` needs a temporary, but should be otherwise identical performance-wise. | ||
* `#4` and `#5` are considered verbose and noisy. They also need to clone one more | ||
time than other methods *strictly* need to. | ||
|
||
However the issues for `#2` are *entirely* artifical. It's simply a side-effect of | ||
forwarding the impl to the identical array syntax. We can just make the code in the | ||
`vec!` macro better. This naturally extends the compile-timey `[x; n]` array sugar | ||
to the more runtimey semantics of Vec, without introducing "another way to do it". | ||
|
||
`vec![100; 10]` is also *slightly* less ambiguous than `from_elem(100, 10)`, | ||
because the `[T; n]` syntax is part of the language that developers should be | ||
familiar with, while `from_elem` is just a function with arbitrary argument order. | ||
|
||
`vec![x; n]` is also known to be 47% more sick-rad than `from_elem`, which was | ||
of course deprecated to due its lack of sick-radness. | ||
|
||
# Detailed design | ||
|
||
Upgrade the current `vec!` macro to have the following definition: | ||
|
||
```rust | ||
macro_rules! vec { | ||
($x:expr; $y:expr) => ( | ||
unsafe { | ||
use std::ptr; | ||
use std::clone::Clone; | ||
|
||
let elem = $x; | ||
let n: usize = $y; | ||
let mut v = Vec::with_capacity(n); | ||
let mut ptr = v.as_mut_ptr(); | ||
for i in range(1, n) { | ||
ptr::write(ptr, Clone::clone(&elem)); | ||
ptr = ptr.offset(1); | ||
v.set_len(i); | ||
} | ||
|
||
// No needless clones | ||
if n > 0 { | ||
ptr::write(ptr, elem); | ||
v.set_len(n); | ||
} | ||
|
||
v | ||
} | ||
); | ||
($($x:expr),*) => ( | ||
<[_] as std::slice::SliceExt>::into_vec( | ||
std::boxed::Box::new([$($x),*])) | ||
); | ||
($($x:expr,)*) => (vec![$($x),*]) | ||
} | ||
``` | ||
|
||
(note: only the `[x; n]` branch is changed) | ||
|
||
Which allows all of the following to work: | ||
|
||
``` | ||
fn main() { | ||
println!("{:?}", vec![1; 10]); | ||
println!("{:?}", vec![Box::new(1); 10]); | ||
let n = 10; | ||
println!("{:?}", vec![1; n]); | ||
} | ||
``` | ||
|
||
# Drawbacks | ||
|
||
Less discoverable than from_elem. All the problems that macros have relative to static methods. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another drawback you may wish to add is inconsistency in APIs. This doesn't also include other list-like collections such as |
||
# Alternatives | ||
|
||
Just un-delete from_elem as it was. | ||
|
||
# Unresolved questions | ||
|
||
No. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 currently unfortunately lack
unsafe
hygiene, allowing unsafe expressions in$x
and$y
without an explicitly declaredunsafe
block. Additionally, I think that this would trigger an "unused unsafe" warning forunsafe { vec![a; b] }
due to the macro-generatedunsafe
block not being necessary.Perhaps a definition like this could be used to solve both these problems?
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.
Oh dang, good point. I was basically expecting to have to split out a fn anyway for inline-countering reasons. Written as-is for simplicity.