Skip to content
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 1 commit into from
Dec 21, 2014

Conversation

cgaebel
Copy link
Contributor

@cgaebel cgaebel commented Dec 17, 2014

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

@alexcrichton
Copy link
Member

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: debug_assert_eq!

@reem
Copy link
Contributor

reem commented Dec 17, 2014

cc @aturon this is what we discussed on IRC yesterday.

@cgaebel
Copy link
Contributor Author

cgaebel commented Dec 18, 2014

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

@cgaebel
Copy link
Contributor Author

cgaebel commented Dec 18, 2014

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"]
Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@Gankra
Copy link
Contributor

Gankra commented Dec 18, 2014

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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/BinarHeap/BinaryHeap

@Gankra
Copy link
Contributor

Gankra commented Dec 18, 2014

I take it back, @apasel422 is in charge of BinaryHeap forever now.

impl<T> ExactSizeIterator<T> for MoveItems<T> {}

// A draining RingBuf iterator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ///

@cgaebel
Copy link
Contributor Author

cgaebel commented Dec 18, 2014

This has been rebased, and I believe all review comments have been addressed.

r? @gankro

@ghost
Copy link

ghost commented Dec 18, 2014

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All hail.

@@ -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
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@Gankra
Copy link
Contributor

Gankra commented Dec 19, 2014

Ok, done my full pass.

@cgaebel
Copy link
Contributor Author

cgaebel commented Dec 19, 2014

Moi aussi.

@Gankra
Copy link
Contributor

Gankra commented Dec 19, 2014

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
@cgaebel
Copy link
Contributor Author

cgaebel commented Dec 19, 2014

bors added a commit that referenced this pull request Dec 21, 2014
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
@bors bors merged commit d57f259 into rust-lang:master Dec 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants