-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[hashmap] Adds drain
: a way to sneak out the elements while clearing a map.
#19946
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -443,6 +443,27 @@ impl<T> RingBuf<T> { | |
#[unstable = "matches collection reform specification, waiting for dust to settle"] | ||
pub fn is_empty(&self) -> bool { self.len() == 0 } | ||
|
||
/// Creates a draining iterator that clears the `RingBuf` and iterates over | ||
/// the removed items from start to end. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::RingBuf; | ||
/// | ||
/// let mut v = RingBuf::new(); | ||
/// v.push_back(1i); | ||
/// assert_eq!(v.drain().next(), Some(1)); | ||
/// assert!(v.is_empty()); | ||
/// ``` | ||
#[inline] | ||
#[unstable = "matches collection reform specification, waiting for dust to settle"] | ||
pub fn drain<'a>(&'a mut self) -> Drain<'a, T> { | ||
Drain { | ||
inner: self, | ||
} | ||
} | ||
|
||
/// Clears the buffer, removing all values. | ||
/// | ||
/// # Examples | ||
|
@@ -456,10 +477,9 @@ impl<T> RingBuf<T> { | |
/// assert!(v.is_empty()); | ||
/// ``` | ||
#[unstable = "matches collection reform specification, waiting for dust to settle"] | ||
#[inline] | ||
pub fn clear(&mut self) { | ||
while self.pop_front().is_some() {} | ||
self.head = 0; | ||
self.tail = 0; | ||
self.drain(); | ||
} | ||
|
||
/// Provides a reference to the front element, or `None` if the sequence is | ||
|
@@ -1177,9 +1197,44 @@ impl<T> DoubleEndedIterator<T> for MoveItems<T> { | |
} | ||
} | ||
|
||
|
||
impl<T> ExactSizeIterator<T> for MoveItems<T> {} | ||
|
||
/// A draining RingBuf iterator | ||
pub struct Drain<'a, T: 'a> { | ||
inner: &'a mut RingBuf<T>, | ||
} | ||
|
||
#[unsafe_destructor] | ||
impl<'a, T: 'a> Drop for Drain<'a, T> { | ||
fn drop(&mut self) { | ||
for _ in *self {} | ||
self.inner.head = 0; | ||
self.inner.tail = 0; | ||
} | ||
} | ||
|
||
impl<'a, T: 'a> Iterator<T> for Drain<'a, T> { | ||
#[inline] | ||
fn next(&mut self) -> Option<T> { | ||
self.inner.pop_front() | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (uint, Option<uint>) { | ||
let len = self.inner.len(); | ||
(len, Some(len)) | ||
} | ||
} | ||
|
||
impl<'a, T: 'a> DoubleEndedIterator<T> for Drain<'a, T> { | ||
#[inline] | ||
fn next_back(&mut self) -> Option<T> { | ||
self.inner.pop_back() | ||
} | ||
} | ||
|
||
impl<'a, T: 'a> ExactSizeIterator<T> for Drain<'a, T> {} | ||
|
||
impl<A: PartialEq> PartialEq for RingBuf<A> { | ||
fn eq(&self, other: &RingBuf<A>) -> bool { | ||
self.len() == other.len() && | ||
|
@@ -1789,6 +1844,73 @@ mod tests { | |
} | ||
} | ||
|
||
#[test] | ||
fn test_drain() { | ||
|
||
// Empty iter | ||
{ | ||
let mut d: RingBuf<int> = RingBuf::new(); | ||
|
||
{ | ||
let mut iter = d.drain(); | ||
|
||
assert_eq!(iter.size_hint(), (0, Some(0))); | ||
assert_eq!(iter.next(), None); | ||
assert_eq!(iter.size_hint(), (0, Some(0))); | ||
} | ||
|
||
assert!(d.is_empty()); | ||
} | ||
|
||
// simple iter | ||
{ | ||
let mut d = RingBuf::new(); | ||
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. Any reason this isn't a collect? 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. Ah, symmetry with the other tests. |
||
for i in range(0i, 5) { | ||
d.push_back(i); | ||
} | ||
|
||
assert_eq!(d.drain().collect::<Vec<int>>(), [0, 1, 2, 3, 4]); | ||
assert!(d.is_empty()); | ||
} | ||
|
||
// wrapped iter | ||
{ | ||
let mut d = RingBuf::new(); | ||
for i in range(0i, 5) { | ||
d.push_back(i); | ||
} | ||
for i in range(6, 9) { | ||
d.push_front(i); | ||
} | ||
|
||
assert_eq!(d.drain().collect::<Vec<int>>(), [8,7,6,0,1,2,3,4]); | ||
assert!(d.is_empty()); | ||
} | ||
|
||
// partially used | ||
{ | ||
let mut d = RingBuf::new(); | ||
for i in range(0i, 5) { | ||
d.push_back(i); | ||
} | ||
for i in range(6, 9) { | ||
d.push_front(i); | ||
} | ||
|
||
{ | ||
let mut it = d.drain(); | ||
assert_eq!(it.size_hint(), (8, Some(8))); | ||
assert_eq!(it.next(), Some(8)); | ||
assert_eq!(it.size_hint(), (7, Some(7))); | ||
assert_eq!(it.next_back(), Some(4)); | ||
assert_eq!(it.size_hint(), (6, Some(6))); | ||
assert_eq!(it.next(), Some(7)); | ||
assert_eq!(it.size_hint(), (5, Some(5))); | ||
} | ||
assert!(d.is_empty()); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_from_iter() { | ||
use std::iter; | ||
|
Oops, something went wrong.
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.
<3