Skip to content

Commit f677701

Browse files
committed
Auto merge of #542 - clarfonthey:default-iters, r=Amanieu
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.
2 parents ac00a0b + abd706e commit f677701

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

src/map.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,14 @@ pub struct IntoKeys<K, V, A: Allocator = Global> {
24472447
inner: IntoIter<K, V, A>,
24482448
}
24492449

2450+
impl<K, V, A: Allocator> Default for IntoKeys<K, V, A> {
2451+
#[cfg_attr(feature = "inline-more", inline)]
2452+
fn default() -> Self {
2453+
Self {
2454+
inner: Default::default(),
2455+
}
2456+
}
2457+
}
24502458
impl<K, V, A: Allocator> Iterator for IntoKeys<K, V, A> {
24512459
type Item = K;
24522460

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

2528+
impl<K, V, A: Allocator> Default for IntoValues<K, V, A> {
2529+
#[cfg_attr(feature = "inline-more", inline)]
2530+
fn default() -> Self {
2531+
Self {
2532+
inner: Default::default(),
2533+
}
2534+
}
2535+
}
25202536
impl<K, V, A: Allocator> Iterator for IntoValues<K, V, A> {
25212537
type Item = V;
25222538

@@ -4720,6 +4736,15 @@ impl<K, V, S, A: Allocator> IntoIterator for HashMap<K, V, S, A> {
47204736
}
47214737
}
47224738

4739+
impl<'a, K, V> Default for Iter<'a, K, V> {
4740+
#[cfg_attr(feature = "inline-more", inline)]
4741+
fn default() -> Self {
4742+
Self {
4743+
inner: Default::default(),
4744+
marker: PhantomData,
4745+
}
4746+
}
4747+
}
47234748
impl<'a, K, V> Iterator for Iter<'a, K, V> {
47244749
type Item = (&'a K, &'a V);
47254750

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

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

4787+
impl<'a, K, V> Default for IterMut<'a, K, V> {
4788+
#[cfg_attr(feature = "inline-more", inline)]
4789+
fn default() -> Self {
4790+
Self {
4791+
inner: Default::default(),
4792+
marker: PhantomData,
4793+
}
4794+
}
4795+
}
47624796
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
47634797
type Item = (&'a K, &'a mut V);
47644798

@@ -4807,6 +4841,14 @@ where
48074841
}
48084842
}
48094843

4844+
impl<K, V, A: Allocator> Default for IntoIter<K, V, A> {
4845+
#[cfg_attr(feature = "inline-more", inline)]
4846+
fn default() -> Self {
4847+
Self {
4848+
inner: Default::default(),
4849+
}
4850+
}
4851+
}
48104852
impl<K, V, A: Allocator> Iterator for IntoIter<K, V, A> {
48114853
type Item = (K, V);
48124854

@@ -4841,6 +4883,14 @@ impl<K: Debug, V: Debug, A: Allocator> fmt::Debug for IntoIter<K, V, A> {
48414883
}
48424884
}
48434885

4886+
impl<'a, K, V> Default for Keys<'a, K, V> {
4887+
#[cfg_attr(feature = "inline-more", inline)]
4888+
fn default() -> Self {
4889+
Self {
4890+
inner: Default::default(),
4891+
}
4892+
}
4893+
}
48444894
impl<'a, K, V> Iterator for Keys<'a, K, V> {
48454895
type Item = &'a K;
48464896

@@ -4873,6 +4923,14 @@ impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
48734923
}
48744924
impl<K, V> FusedIterator for Keys<'_, K, V> {}
48754925

4926+
impl<'a, K, V> Default for Values<'a, K, V> {
4927+
#[cfg_attr(feature = "inline-more", inline)]
4928+
fn default() -> Self {
4929+
Self {
4930+
inner: Default::default(),
4931+
}
4932+
}
4933+
}
48764934
impl<'a, K, V> Iterator for Values<'a, K, V> {
48774935
type Item = &'a V;
48784936

@@ -4905,6 +4963,14 @@ impl<K, V> ExactSizeIterator for Values<'_, K, V> {
49054963
}
49064964
impl<K, V> FusedIterator for Values<'_, K, V> {}
49074965

4966+
impl<'a, K, V> Default for ValuesMut<'a, K, V> {
4967+
#[cfg_attr(feature = "inline-more", inline)]
4968+
fn default() -> Self {
4969+
Self {
4970+
inner: Default::default(),
4971+
}
4972+
}
4973+
}
49084974
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
49094975
type Item = &'a mut V;
49104976

src/raw/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,6 +4112,13 @@ impl<T> Clone for RawIter<T> {
41124112
}
41134113
}
41144114
}
4115+
impl<T> Default for RawIter<T> {
4116+
#[cfg_attr(feature = "inline-more", inline)]
4117+
fn default() -> Self {
4118+
// SAFETY: Because the table is static, it always outlives the iter.
4119+
unsafe { RawTableInner::NEW.iter() }
4120+
}
4121+
}
41154122

41164123
impl<T> Iterator for RawIter<T> {
41174124
type Item = Bucket<T>;
@@ -4330,6 +4337,15 @@ impl<T, A: Allocator> Drop for RawIntoIter<T, A> {
43304337
}
43314338
}
43324339

4340+
impl<T, A: Allocator> Default for RawIntoIter<T, A> {
4341+
fn default() -> Self {
4342+
Self {
4343+
iter: Default::default(),
4344+
allocation: None,
4345+
marker: PhantomData,
4346+
}
4347+
}
4348+
}
43334349
impl<T, A: Allocator> Iterator for RawIntoIter<T, A> {
43344350
type Item = T;
43354351

src/set.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,14 @@ impl<K> Clone for Iter<'_, K> {
18321832
}
18331833
}
18341834
}
1835+
impl<K> Default for Iter<'_, K> {
1836+
#[cfg_attr(feature = "inline-more", inline)]
1837+
fn default() -> Self {
1838+
Iter {
1839+
iter: Default::default(),
1840+
}
1841+
}
1842+
}
18351843
impl<'a, K> Iterator for Iter<'a, K> {
18361844
type Item = &'a K;
18371845

@@ -1866,6 +1874,14 @@ impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> {
18661874
}
18671875
}
18681876

1877+
impl<K, A: Allocator> Default for IntoIter<K, A> {
1878+
#[cfg_attr(feature = "inline-more", inline)]
1879+
fn default() -> Self {
1880+
IntoIter {
1881+
iter: Default::default(),
1882+
}
1883+
}
1884+
}
18691885
impl<K, A: Allocator> Iterator for IntoIter<K, A> {
18701886
type Item = K;
18711887

src/table.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,15 @@ pub struct Iter<'a, T> {
18351835
marker: PhantomData<&'a T>,
18361836
}
18371837

1838+
impl<'a, T> Default for Iter<'a, T> {
1839+
#[cfg_attr(feature = "inline-more", inline)]
1840+
fn default() -> Self {
1841+
Iter {
1842+
inner: Default::default(),
1843+
marker: PhantomData,
1844+
}
1845+
}
1846+
}
18381847
impl<'a, T> Iterator for Iter<'a, T> {
18391848
type Item = &'a T;
18401849

@@ -1881,6 +1890,15 @@ pub struct IterMut<'a, T> {
18811890
marker: PhantomData<&'a mut T>,
18821891
}
18831892

1893+
impl<'a, T> Default for IterMut<'a, T> {
1894+
#[cfg_attr(feature = "inline-more", inline)]
1895+
fn default() -> Self {
1896+
IterMut {
1897+
inner: Default::default(),
1898+
marker: PhantomData,
1899+
}
1900+
}
1901+
}
18841902
impl<'a, T> Iterator for IterMut<'a, T> {
18851903
type Item = &'a mut T;
18861904

@@ -1931,6 +1949,14 @@ where
19311949
inner: RawIntoIter<T, A>,
19321950
}
19331951

1952+
impl<T, A: Allocator> Default for IntoIter<T, A> {
1953+
#[cfg_attr(feature = "inline-more", inline)]
1954+
fn default() -> Self {
1955+
IntoIter {
1956+
inner: Default::default(),
1957+
}
1958+
}
1959+
}
19341960
impl<T, A> Iterator for IntoIter<T, A>
19351961
where
19361962
A: Allocator,

0 commit comments

Comments
 (0)