-
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
Conversation
Let's please hold off merging this until rust-lang/rfcs#509 is merged as well (but review is fine!) |
} | ||
} | ||
|
||
assert_eq!(self.table.size, 0); |
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: debug_assert_eq!
cc @aturon this is what we discussed on IRC yesterday. |
cc @thestinger for Vec changes. RingBuf/Vec .drain has been added if y'all want to review that too! |
while self.pop_front().is_some() {} | ||
self.head = 0; | ||
self.tail = 0; | ||
self.drain(); |
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
BinaryHeap is done now, too. Not sure who "owns" that (and ringbuf too, for that matter), but someone should cc them. |
#[inline] | ||
/// Drops all items that have not yet been moved and returns the empty vector. | ||
/// Deprecated, use .drain() instead | ||
#[deprecated = "use .drain() insetad"] |
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 don't think this deprecation is in the RFC. It should probably be added.
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.
Unsure if this is strictly desirable. MoveItems in theory lets you move the collection to somewhere else by passing around an iterator.
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 that's why I'm thinking it should be added to the RFC so people can debate it.
RingBuf is primarily authored by @csherratt. BinaryHeap hasn't really had any heavy work done on it in a while. Pretty simple collection relatively speaking. |
@@ -596,6 +605,26 @@ impl<T> DoubleEndedIterator<T> for MoveItems<T> { | |||
|
|||
impl<T> ExactSizeIterator<T> for MoveItems<T> {} | |||
|
|||
/// An iterator that drains a `BinarHeap`. |
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.
s/BinarHeap/BinaryHeap
I take it back, @apasel422 is in charge of BinaryHeap forever now. |
impl<T> ExactSizeIterator<T> for MoveItems<T> {} | ||
|
||
// A draining RingBuf iterator |
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 should be ///
0c721de
to
fcfbefb
Compare
This has been rebased, and I believe all review comments have been addressed. r? @gankro |
fcfbefb
to
9ccd998
Compare
The changes to ringbuf LGTM. |
#[inline] | ||
#[unstable = "matches collection reform specification, waiting for dust to settle"] | ||
pub fn drain<'a>(&'a mut self) -> DrainItems<'a, T> { | ||
DrainItems { |
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.
Word on high from lord @aturon is that these should be called Drain (as Iterators should be named after their creation method -- Iter, IterMut, IntoIter.
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.
All hail.
9ccd998
to
0925212
Compare
@@ -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 is, clears the RingBuf and returns an |
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.
Awkward sentence. Either have a period after iterator
, or just have "... iterator that clears the..."
/// Clears the map, returning all key-value pairs as an iterator. Keeps the | ||
/// allocated memory for reuse. | ||
/// | ||
/// # Example |
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.
Examples
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.
All the other docs in this file say "Example", but in other files it says "Examples" even when there's only one test.
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.
New convention is examples per the rule of @steveklabnik; anything else is out of date.
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.
Even when the english language disagrees? afaik singular forms of nouns should be used when there's only one.
Ok, done my full pass. |
Moi aussi. |
r=me with fixed doc + squash |
…aring. It is useful to move all the elements out of some collections without deallocating the underlying buffer. It came up in IRC, and this patch implements it as `drain`. This has been discussed as part of RFC 509. r? @gankro cc: @frankmcsherry
992eeef
to
d57f259
Compare
It is useful to move all the elements out of a hashmap without deallocating the underlying buffer. It came up in IRC, and this patch implements it as `drain`. r? @gankro cc: @frankmcsherry
It is useful to move all the elements out of a hashmap without deallocating
the underlying buffer. It came up in IRC, and this patch implements it as
drain
.r? @gankro
cc: @frankmcsherry