Skip to content

Commit

Permalink
Auto merge of #542 - clarfonthey:default-iters, r=Amanieu
Browse files Browse the repository at this point in the history
Implement Default for iterators

See rust-lang/rust#128261 for a similar PR for some libstd iterators. In order to do a similar PR for the `HashSet` and `HashMap` types, we need to make these changes to `hashbrown` first to actually implement them.

One small caveat is that I chose to only implement `Default` for `RawIter` and not `RawIterRange`, since it really exists as an implementation detail anyway, and the best implementation would involve just using the `RawIter` implementation anyway.
  • Loading branch information
bors committed Aug 5, 2024
2 parents ac00a0b + abd706e commit f677701
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,14 @@ pub struct IntoKeys<K, V, A: Allocator = Global> {
inner: IntoIter<K, V, A>,
}

impl<K, V, A: Allocator> Default for IntoKeys<K, V, A> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<K, V, A: Allocator> Iterator for IntoKeys<K, V, A> {
type Item = K;

Expand Down Expand Up @@ -2517,6 +2525,14 @@ pub struct IntoValues<K, V, A: Allocator = Global> {
inner: IntoIter<K, V, A>,
}

impl<K, V, A: Allocator> Default for IntoValues<K, V, A> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<K, V, A: Allocator> Iterator for IntoValues<K, V, A> {
type Item = V;

Expand Down Expand Up @@ -4720,6 +4736,15 @@ impl<K, V, S, A: Allocator> IntoIterator for HashMap<K, V, S, A> {
}
}

impl<'a, K, V> Default for Iter<'a, K, V> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
marker: PhantomData,
}
}
}
impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);

Expand Down Expand Up @@ -4759,6 +4784,15 @@ impl<K, V> ExactSizeIterator for Iter<'_, K, V> {

impl<K, V> FusedIterator for Iter<'_, K, V> {}

impl<'a, K, V> Default for IterMut<'a, K, V> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
marker: PhantomData,
}
}
}
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);

Expand Down Expand Up @@ -4807,6 +4841,14 @@ where
}
}

impl<K, V, A: Allocator> Default for IntoIter<K, V, A> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<K, V, A: Allocator> Iterator for IntoIter<K, V, A> {
type Item = (K, V);

Expand Down Expand Up @@ -4841,6 +4883,14 @@ impl<K: Debug, V: Debug, A: Allocator> fmt::Debug for IntoIter<K, V, A> {
}
}

impl<'a, K, V> Default for Keys<'a, K, V> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<'a, K, V> Iterator for Keys<'a, K, V> {
type Item = &'a K;

Expand Down Expand Up @@ -4873,6 +4923,14 @@ impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
}
impl<K, V> FusedIterator for Keys<'_, K, V> {}

impl<'a, K, V> Default for Values<'a, K, V> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;

Expand Down Expand Up @@ -4905,6 +4963,14 @@ impl<K, V> ExactSizeIterator for Values<'_, K, V> {
}
impl<K, V> FusedIterator for Values<'_, K, V> {}

impl<'a, K, V> Default for ValuesMut<'a, K, V> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;

Expand Down
16 changes: 16 additions & 0 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4112,6 +4112,13 @@ impl<T> Clone for RawIter<T> {
}
}
}
impl<T> Default for RawIter<T> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
// SAFETY: Because the table is static, it always outlives the iter.
unsafe { RawTableInner::NEW.iter() }
}
}

impl<T> Iterator for RawIter<T> {
type Item = Bucket<T>;
Expand Down Expand Up @@ -4330,6 +4337,15 @@ impl<T, A: Allocator> Drop for RawIntoIter<T, A> {
}
}

impl<T, A: Allocator> Default for RawIntoIter<T, A> {
fn default() -> Self {
Self {
iter: Default::default(),
allocation: None,
marker: PhantomData,
}
}
}
impl<T, A: Allocator> Iterator for RawIntoIter<T, A> {
type Item = T;

Expand Down
16 changes: 16 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,14 @@ impl<K> Clone for Iter<'_, K> {
}
}
}
impl<K> Default for Iter<'_, K> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Iter {
iter: Default::default(),
}
}
}
impl<'a, K> Iterator for Iter<'a, K> {
type Item = &'a K;

Expand Down Expand Up @@ -1866,6 +1874,14 @@ impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> {
}
}

impl<K, A: Allocator> Default for IntoIter<K, A> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
IntoIter {
iter: Default::default(),
}
}
}
impl<K, A: Allocator> Iterator for IntoIter<K, A> {
type Item = K;

Expand Down
26 changes: 26 additions & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,15 @@ pub struct Iter<'a, T> {
marker: PhantomData<&'a T>,
}

impl<'a, T> Default for Iter<'a, T> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Iter {
inner: Default::default(),
marker: PhantomData,
}
}
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

Expand Down Expand Up @@ -1881,6 +1890,15 @@ pub struct IterMut<'a, T> {
marker: PhantomData<&'a mut T>,
}

impl<'a, T> Default for IterMut<'a, T> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
IterMut {
inner: Default::default(),
marker: PhantomData,
}
}
}
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;

Expand Down Expand Up @@ -1931,6 +1949,14 @@ where
inner: RawIntoIter<T, A>,
}

impl<T, A: Allocator> Default for IntoIter<T, A> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
IntoIter {
inner: Default::default(),
}
}
}
impl<T, A> Iterator for IntoIter<T, A>
where
A: Allocator,
Expand Down

0 comments on commit f677701

Please # to comment.