Skip to content

Commit e47e4dc

Browse files
bhgomesJohnTitor
authored andcommitted
add trait implementations to iterators mirroring std::collections
1 parent d71851e commit e47e4dc

4 files changed

Lines changed: 166 additions & 0 deletions

File tree

‎phf/src/map.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! An immutable map constructed at compile time.
22
use core::fmt;
3+
use core::iter::FusedIterator;
34
use core::iter::IntoIterator;
45
use core::ops::Index;
56
use core::slice;
@@ -148,6 +149,25 @@ pub struct Entries<'a, K, V> {
148149
iter: slice::Iter<'a, (K, V)>,
149150
}
150151

152+
impl<'a, K, V> Clone for Entries<'a, K, V> {
153+
#[inline]
154+
fn clone(&self) -> Self {
155+
Self {
156+
iter: self.iter.clone(),
157+
}
158+
}
159+
}
160+
161+
impl<'a, K, V> fmt::Debug for Entries<'a, K, V>
162+
where
163+
K: fmt::Debug,
164+
V: fmt::Debug,
165+
{
166+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
167+
f.debug_list().entries(self.clone()).finish()
168+
}
169+
}
170+
151171
impl<'a, K, V> Iterator for Entries<'a, K, V> {
152172
type Item = (&'a K, &'a V);
153173

@@ -168,11 +188,31 @@ impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {
168188

169189
impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {}
170190

191+
impl<'a, K, V> FusedIterator for Entries<'a, K, V> {}
192+
171193
/// An iterator over the keys in a `Map`.
172194
pub struct Keys<'a, K, V> {
173195
iter: Entries<'a, K, V>,
174196
}
175197

198+
impl<'a, K, V> Clone for Keys<'a, K, V> {
199+
#[inline]
200+
fn clone(&self) -> Self {
201+
Self {
202+
iter: self.iter.clone(),
203+
}
204+
}
205+
}
206+
207+
impl<'a, K, V> fmt::Debug for Keys<'a, K, V>
208+
where
209+
K: fmt::Debug,
210+
{
211+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
212+
f.debug_list().entries(self.clone()).finish()
213+
}
214+
}
215+
176216
impl<'a, K, V> Iterator for Keys<'a, K, V> {
177217
type Item = &'a K;
178218

@@ -193,11 +233,31 @@ impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
193233

194234
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {}
195235

236+
impl<'a, K, V> FusedIterator for Keys<'a, K, V> {}
237+
196238
/// An iterator over the values in a `Map`.
197239
pub struct Values<'a, K, V> {
198240
iter: Entries<'a, K, V>,
199241
}
200242

243+
impl<'a, K, V> Clone for Values<'a, K, V> {
244+
#[inline]
245+
fn clone(&self) -> Self {
246+
Self {
247+
iter: self.iter.clone(),
248+
}
249+
}
250+
}
251+
252+
impl<'a, K, V> fmt::Debug for Values<'a, K, V>
253+
where
254+
V: fmt::Debug,
255+
{
256+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
257+
f.debug_list().entries(self.clone()).finish()
258+
}
259+
}
260+
201261
impl<'a, K, V> Iterator for Values<'a, K, V> {
202262
type Item = &'a V;
203263

@@ -217,3 +277,5 @@ impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
217277
}
218278

219279
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}
280+
281+
impl<'a, K, V> FusedIterator for Values<'a, K, V> {}

‎phf/src/ordered_map.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! An order-preserving immutable map constructed at compile time.
22
use core::fmt;
3+
use core::iter::FusedIterator;
34
use core::iter::IntoIterator;
45
use core::ops::Index;
56
use core::slice;
@@ -179,6 +180,25 @@ pub struct Entries<'a, K, V> {
179180
iter: slice::Iter<'a, (K, V)>,
180181
}
181182

183+
impl<'a, K, V> Clone for Entries<'a, K, V> {
184+
#[inline]
185+
fn clone(&self) -> Self {
186+
Self {
187+
iter: self.iter.clone(),
188+
}
189+
}
190+
}
191+
192+
impl<'a, K, V> fmt::Debug for Entries<'a, K, V>
193+
where
194+
K: fmt::Debug,
195+
V: fmt::Debug,
196+
{
197+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
198+
f.debug_list().entries(self.clone()).finish()
199+
}
200+
}
201+
182202
impl<'a, K, V> Iterator for Entries<'a, K, V> {
183203
type Item = (&'a K, &'a V);
184204

@@ -199,11 +219,31 @@ impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {
199219

200220
impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {}
201221

222+
impl<'a, K, V> FusedIterator for Entries<'a, K, V> {}
223+
202224
/// An iterator over the keys in a `OrderedMap`.
203225
pub struct Keys<'a, K, V> {
204226
iter: Entries<'a, K, V>,
205227
}
206228

229+
impl<'a, K, V> Clone for Keys<'a, K, V> {
230+
#[inline]
231+
fn clone(&self) -> Self {
232+
Self {
233+
iter: self.iter.clone(),
234+
}
235+
}
236+
}
237+
238+
impl<'a, K, V> fmt::Debug for Keys<'a, K, V>
239+
where
240+
K: fmt::Debug,
241+
{
242+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
243+
f.debug_list().entries(self.clone()).finish()
244+
}
245+
}
246+
207247
impl<'a, K, V> Iterator for Keys<'a, K, V> {
208248
type Item = &'a K;
209249

@@ -224,11 +264,31 @@ impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
224264

225265
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {}
226266

267+
impl<'a, K, V> FusedIterator for Keys<'a, K, V> {}
268+
227269
/// An iterator over the values in a `OrderedMap`.
228270
pub struct Values<'a, K, V> {
229271
iter: Entries<'a, K, V>,
230272
}
231273

274+
impl<'a, K, V> Clone for Values<'a, K, V> {
275+
#[inline]
276+
fn clone(&self) -> Self {
277+
Self {
278+
iter: self.iter.clone(),
279+
}
280+
}
281+
}
282+
283+
impl<'a, K, V> fmt::Debug for Values<'a, K, V>
284+
where
285+
V: fmt::Debug,
286+
{
287+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
288+
f.debug_list().entries(self.clone()).finish()
289+
}
290+
}
291+
232292
impl<'a, K, V> Iterator for Values<'a, K, V> {
233293
type Item = &'a V;
234294

@@ -248,3 +308,5 @@ impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
248308
}
249309

250310
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}
311+
312+
impl<'a, K, V> FusedIterator for Values<'a, K, V> {}

‎phf/src/ordered_set.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! An order-preserving immutable set constructed at compile time.
22
use crate::{ordered_map, OrderedMap, PhfHash};
33
use core::fmt;
4+
use core::iter::FusedIterator;
45
use core::iter::IntoIterator;
56
use phf_shared::PhfBorrow;
67

@@ -125,6 +126,24 @@ pub struct Iter<'a, T> {
125126
iter: ordered_map::Keys<'a, T, ()>,
126127
}
127128

129+
impl<'a, T> Clone for Iter<'a, T> {
130+
#[inline]
131+
fn clone(&self) -> Self {
132+
Self {
133+
iter: self.iter.clone(),
134+
}
135+
}
136+
}
137+
138+
impl<'a, T> fmt::Debug for Iter<'a, T>
139+
where
140+
T: fmt::Debug,
141+
{
142+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
143+
f.debug_list().entries(self.clone()).finish()
144+
}
145+
}
146+
128147
impl<'a, T> Iterator for Iter<'a, T> {
129148
type Item = &'a T;
130149

@@ -147,3 +166,5 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
147166
}
148167

149168
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
169+
170+
impl<'a, T> FusedIterator for Iter<'a, T> {}

‎phf/src/set.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! An immutable set constructed at compile time.
22
use core::fmt;
3+
use core::iter::FusedIterator;
34
use core::iter::IntoIterator;
45

56
use phf_shared::{PhfBorrow, PhfHash};
@@ -105,6 +106,24 @@ pub struct Iter<'a, T: 'static> {
105106
iter: map::Keys<'a, T, ()>,
106107
}
107108

109+
impl<'a, T> Clone for Iter<'a, T> {
110+
#[inline]
111+
fn clone(&self) -> Self {
112+
Self {
113+
iter: self.iter.clone(),
114+
}
115+
}
116+
}
117+
118+
impl<'a, T> fmt::Debug for Iter<'a, T>
119+
where
120+
T: fmt::Debug,
121+
{
122+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123+
f.debug_list().entries(self.clone()).finish()
124+
}
125+
}
126+
108127
impl<'a, T> Iterator for Iter<'a, T> {
109128
type Item = &'a T;
110129

@@ -124,3 +143,5 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
124143
}
125144

126145
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
146+
147+
impl<'a, T> FusedIterator for Iter<'a, T> {}

0 commit comments

Comments
 (0)