-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Replace Vec::drain
by a method that accepts a range parameter.
#574
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 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 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,83 @@ | ||
- Start Date: 2015-01-12 | ||
- RFC PR #: (leave this empty) | ||
- Rust Issue #: (leave this empty) | ||
|
||
# Summary | ||
|
||
Replace `Vec::drain` by a method that accepts a range parameter. Add | ||
`String::drain` with similar functionality. | ||
|
||
# Motivation | ||
|
||
Allowing a range parameter is strictly more powerful than the current version. | ||
E.g., see the following implementations of some `Vec` methods via the hypothetical | ||
`drain_range` method: | ||
|
||
```rust | ||
fn truncate(x: &mut Vec<u8>, len: usize) { | ||
if len <= x.len() { | ||
x.drain_range(len..); | ||
} | ||
} | ||
|
||
fn remove(x: &mut Vec<u8>, index: usize) -> u8 { | ||
x.drain_range(index).next().unwrap() | ||
} | ||
|
||
fn pop(x: &mut Vec<u8>) -> Option<u8> { | ||
match x.len() { | ||
0 => None, | ||
n => x.drain_range(n-1).next() | ||
} | ||
} | ||
|
||
fn drain(x: &mut Vec<u8>) -> DrainRange<u8> { | ||
x.drain_range(0..) | ||
} | ||
|
||
fn clear(x: &mut Vec<u8>) { | ||
x.drain_range(0..); | ||
} | ||
``` | ||
|
||
With optimization enabled, those methods will produce code that runs as fast | ||
as the current versions. (They should not be implemented this way.) | ||
|
||
In particular, this method allows the user to remove a slice from a vector in | ||
`O(Vec::len)` instead of `O(Slice::len * Vec::len)`. | ||
|
||
# Detailed design | ||
|
||
Remove `Vec::drain` and add the following method: | ||
|
||
```rust | ||
/// Creates a draining iterator that clears the specified range in the Vec and | ||
/// iterates over the removed items from start to end. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the range is decreasing or if the upper bound is larger than the | ||
/// length of the vector. | ||
pub fn drain<T: Drainer>(&mut self, range: T) -> RangeIter<T> { | ||
range.drain(self) | ||
} | ||
``` | ||
|
||
Where `Drainer` should be implemented for `Range<usize>`, `RangeTo<usize>`, | ||
`RangeFrom<usize>`, `FullRange`, and `usize`. | ||
|
||
Add `String::drain`: | ||
|
||
```rust | ||
/// Creates a draining iterator that clears the specified range in the String | ||
/// and iterates over the characters contained in the range. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the range is decreasing, if the upper bound is larger than the | ||
/// length of the String, or if the start and the end of the range don't lie on | ||
/// character boundaries. | ||
pub fn drain(&mut self, range: /* ? */) -> /* ? */ { | ||
// ? | ||
} | ||
``` |
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.
Why are these omitted?
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.
Assuming that the return value can only be constructed inside the String module, these parts of the signature are merely an implementation detail. If you want to you can assume
or something similar.
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.
The public signature doesn't seem like an implementation detail to me.