• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::{
2     borrow::Borrow,
3     fmt,
4     hash::{BuildHasher, Hash, Hasher},
5     iter::{Chain, FromIterator},
6     ops::{BitAnd, BitOr, BitXor, Sub},
7 };
8 
9 use hashbrown::hash_map::DefaultHashBuilder;
10 
11 use crate::linked_hash_map::{self, LinkedHashMap, TryReserveError};
12 
13 pub struct LinkedHashSet<T, S = DefaultHashBuilder> {
14     map: LinkedHashMap<T, (), S>,
15 }
16 
17 impl<T: Hash + Eq> LinkedHashSet<T, DefaultHashBuilder> {
18     #[inline]
new() -> LinkedHashSet<T, DefaultHashBuilder>19     pub fn new() -> LinkedHashSet<T, DefaultHashBuilder> {
20         LinkedHashSet {
21             map: LinkedHashMap::new(),
22         }
23     }
24 
25     #[inline]
with_capacity(capacity: usize) -> LinkedHashSet<T, DefaultHashBuilder>26     pub fn with_capacity(capacity: usize) -> LinkedHashSet<T, DefaultHashBuilder> {
27         LinkedHashSet {
28             map: LinkedHashMap::with_capacity(capacity),
29         }
30     }
31 }
32 
33 impl<T, S> LinkedHashSet<T, S> {
34     #[inline]
capacity(&self) -> usize35     pub fn capacity(&self) -> usize {
36         self.map.capacity()
37     }
38 
39     #[inline]
iter(&self) -> Iter<'_, T>40     pub fn iter(&self) -> Iter<'_, T> {
41         Iter {
42             iter: self.map.keys(),
43         }
44     }
45 
46     #[inline]
len(&self) -> usize47     pub fn len(&self) -> usize {
48         self.map.len()
49     }
50 
51     #[inline]
is_empty(&self) -> bool52     pub fn is_empty(&self) -> bool {
53         self.map.is_empty()
54     }
55 
56     #[inline]
drain(&mut self) -> Drain<T>57     pub fn drain(&mut self) -> Drain<T> {
58         Drain {
59             iter: self.map.drain(),
60         }
61     }
62 
63     #[inline]
clear(&mut self)64     pub fn clear(&mut self) {
65         self.map.clear()
66     }
67 
68     #[inline]
retain<F>(&mut self, mut f: F) where F: FnMut(&T) -> bool,69     pub fn retain<F>(&mut self, mut f: F)
70     where
71         F: FnMut(&T) -> bool,
72     {
73         self.map.retain(|k, _| f(k));
74     }
75 }
76 
77 impl<T, S> LinkedHashSet<T, S>
78 where
79     T: Eq + Hash,
80     S: BuildHasher,
81 {
82     #[inline]
with_hasher(hasher: S) -> LinkedHashSet<T, S>83     pub fn with_hasher(hasher: S) -> LinkedHashSet<T, S> {
84         LinkedHashSet {
85             map: LinkedHashMap::with_hasher(hasher),
86         }
87     }
88 
89     #[inline]
with_capacity_and_hasher(capacity: usize, hasher: S) -> LinkedHashSet<T, S>90     pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> LinkedHashSet<T, S> {
91         LinkedHashSet {
92             map: LinkedHashMap::with_capacity_and_hasher(capacity, hasher),
93         }
94     }
95 
96     #[inline]
hasher(&self) -> &S97     pub fn hasher(&self) -> &S {
98         self.map.hasher()
99     }
100 
101     #[inline]
reserve(&mut self, additional: usize)102     pub fn reserve(&mut self, additional: usize) {
103         self.map.reserve(additional)
104     }
105 
106     #[inline]
try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>107     pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
108         self.map.try_reserve(additional)
109     }
110 
111     #[inline]
shrink_to_fit(&mut self)112     pub fn shrink_to_fit(&mut self) {
113         self.map.shrink_to_fit()
114     }
115 
116     #[inline]
difference<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Difference<'a, T, S>117     pub fn difference<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Difference<'a, T, S> {
118         Difference {
119             iter: self.iter(),
120             other,
121         }
122     }
123 
124     #[inline]
symmetric_difference<'a>( &'a self, other: &'a LinkedHashSet<T, S>, ) -> SymmetricDifference<'a, T, S>125     pub fn symmetric_difference<'a>(
126         &'a self,
127         other: &'a LinkedHashSet<T, S>,
128     ) -> SymmetricDifference<'a, T, S> {
129         SymmetricDifference {
130             iter: self.difference(other).chain(other.difference(self)),
131         }
132     }
133 
134     #[inline]
intersection<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Intersection<'a, T, S>135     pub fn intersection<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Intersection<'a, T, S> {
136         Intersection {
137             iter: self.iter(),
138             other,
139         }
140     }
141 
142     #[inline]
union<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Union<'a, T, S>143     pub fn union<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Union<'a, T, S> {
144         Union {
145             iter: self.iter().chain(other.difference(self)),
146         }
147     }
148 
149     #[inline]
contains<Q: ?Sized>(&self, value: &Q) -> bool where T: Borrow<Q>, Q: Hash + Eq,150     pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
151     where
152         T: Borrow<Q>,
153         Q: Hash + Eq,
154     {
155         self.map.contains_key(value)
156     }
157 
158     #[inline]
get<Q: ?Sized>(&self, value: &Q) -> Option<&T> where T: Borrow<Q>, Q: Hash + Eq,159     pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
160     where
161         T: Borrow<Q>,
162         Q: Hash + Eq,
163     {
164         self.map.raw_entry().from_key(value).map(|p| p.0)
165     }
166 
167     #[inline]
get_or_insert(&mut self, value: T) -> &T168     pub fn get_or_insert(&mut self, value: T) -> &T {
169         self.map
170             .raw_entry_mut()
171             .from_key(&value)
172             .or_insert(value, ())
173             .0
174     }
175 
176     #[inline]
get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T where T: Borrow<Q>, Q: Hash + Eq, F: FnOnce(&Q) -> T,177     pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
178     where
179         T: Borrow<Q>,
180         Q: Hash + Eq,
181         F: FnOnce(&Q) -> T,
182     {
183         self.map
184             .raw_entry_mut()
185             .from_key(value)
186             .or_insert_with(|| (f(value), ()))
187             .0
188     }
189 
190     #[inline]
is_disjoint(&self, other: &LinkedHashSet<T, S>) -> bool191     pub fn is_disjoint(&self, other: &LinkedHashSet<T, S>) -> bool {
192         self.iter().all(|v| !other.contains(v))
193     }
194 
195     #[inline]
is_subset(&self, other: &LinkedHashSet<T, S>) -> bool196     pub fn is_subset(&self, other: &LinkedHashSet<T, S>) -> bool {
197         self.iter().all(|v| other.contains(v))
198     }
199 
200     #[inline]
is_superset(&self, other: &LinkedHashSet<T, S>) -> bool201     pub fn is_superset(&self, other: &LinkedHashSet<T, S>) -> bool {
202         other.is_subset(self)
203     }
204 
205     #[inline]
insert(&mut self, value: T) -> bool206     pub fn insert(&mut self, value: T) -> bool {
207         self.map.insert(value, ()).is_none()
208     }
209 
210     #[inline]
replace(&mut self, value: T) -> Option<T>211     pub fn replace(&mut self, value: T) -> Option<T> {
212         match self.map.entry(value) {
213             linked_hash_map::Entry::Occupied(occupied) => Some(occupied.replace_key()),
214             linked_hash_map::Entry::Vacant(vacant) => {
215                 vacant.insert(());
216                 None
217             }
218         }
219     }
220 
221     #[inline]
remove<Q: ?Sized>(&mut self, value: &Q) -> bool where T: Borrow<Q>, Q: Hash + Eq,222     pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
223     where
224         T: Borrow<Q>,
225         Q: Hash + Eq,
226     {
227         self.map.remove(value).is_some()
228     }
229 
230     #[inline]
take<Q: ?Sized>(&mut self, value: &Q) -> Option<T> where T: Borrow<Q>, Q: Hash + Eq,231     pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
232     where
233         T: Borrow<Q>,
234         Q: Hash + Eq,
235     {
236         match self.map.raw_entry_mut().from_key(value) {
237             linked_hash_map::RawEntryMut::Occupied(occupied) => Some(occupied.remove_entry().0),
238             linked_hash_map::RawEntryMut::Vacant(_) => None,
239         }
240     }
241 
242     #[inline]
front(&self) -> Option<&T>243     pub fn front(&self) -> Option<&T> {
244         self.map.front().map(|(k, _)| k)
245     }
246 
247     #[inline]
pop_front(&mut self) -> Option<T>248     pub fn pop_front(&mut self) -> Option<T> {
249         self.map.pop_front().map(|(k, _)| k)
250     }
251 
252     #[inline]
back(&self) -> Option<&T>253     pub fn back(&self) -> Option<&T> {
254         self.map.back().map(|(k, _)| k)
255     }
256 
257     #[inline]
pop_back(&mut self) -> Option<T>258     pub fn pop_back(&mut self) -> Option<T> {
259         self.map.pop_back().map(|(k, _)| k)
260     }
261 
262     #[inline]
to_front<Q: ?Sized>(&mut self, value: &Q) -> bool where T: Borrow<Q>, Q: Hash + Eq,263     pub fn to_front<Q: ?Sized>(&mut self, value: &Q) -> bool
264     where
265         T: Borrow<Q>,
266         Q: Hash + Eq,
267     {
268         match self.map.raw_entry_mut().from_key(value) {
269             linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
270                 occupied.to_front();
271                 true
272             }
273             linked_hash_map::RawEntryMut::Vacant(_) => false,
274         }
275     }
276 
277     #[inline]
to_back<Q: ?Sized>(&mut self, value: &Q) -> bool where T: Borrow<Q>, Q: Hash + Eq,278     pub fn to_back<Q: ?Sized>(&mut self, value: &Q) -> bool
279     where
280         T: Borrow<Q>,
281         Q: Hash + Eq,
282     {
283         match self.map.raw_entry_mut().from_key(value) {
284             linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
285                 occupied.to_back();
286                 true
287             }
288             linked_hash_map::RawEntryMut::Vacant(_) => false,
289         }
290     }
291 }
292 
293 impl<T: Hash + Eq + Clone, S: BuildHasher + Clone> Clone for LinkedHashSet<T, S> {
294     #[inline]
clone(&self) -> Self295     fn clone(&self) -> Self {
296         let map = self.map.clone();
297         Self { map }
298     }
299 }
300 
301 impl<T, S> PartialEq for LinkedHashSet<T, S>
302 where
303     T: Eq + Hash,
304     S: BuildHasher,
305 {
306     #[inline]
eq(&self, other: &Self) -> bool307     fn eq(&self, other: &Self) -> bool {
308         self.len() == other.len() && self.iter().eq(other)
309     }
310 }
311 
312 impl<T, S> Hash for LinkedHashSet<T, S>
313 where
314     T: Eq + Hash,
315     S: BuildHasher,
316 {
317     #[inline]
hash<H: Hasher>(&self, state: &mut H)318     fn hash<H: Hasher>(&self, state: &mut H) {
319         for e in self {
320             e.hash(state);
321         }
322     }
323 }
324 
325 impl<T, S> Eq for LinkedHashSet<T, S>
326 where
327     T: Eq + Hash,
328     S: BuildHasher,
329 {
330 }
331 
332 impl<T, S> fmt::Debug for LinkedHashSet<T, S>
333 where
334     T: fmt::Debug,
335 {
336     #[inline]
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result337     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
338         f.debug_set().entries(self.iter()).finish()
339     }
340 }
341 
342 impl<T, S> FromIterator<T> for LinkedHashSet<T, S>
343 where
344     T: Eq + Hash,
345     S: BuildHasher + Default,
346 {
347     #[inline]
from_iter<I: IntoIterator<Item = T>>(iter: I) -> LinkedHashSet<T, S>348     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> LinkedHashSet<T, S> {
349         let mut set = LinkedHashSet::with_hasher(Default::default());
350         set.extend(iter);
351         set
352     }
353 }
354 
355 impl<T, S> Extend<T> for LinkedHashSet<T, S>
356 where
357     T: Eq + Hash,
358     S: BuildHasher,
359 {
360     #[inline]
extend<I: IntoIterator<Item = T>>(&mut self, iter: I)361     fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
362         self.map.extend(iter.into_iter().map(|k| (k, ())));
363     }
364 }
365 
366 impl<'a, T, S> Extend<&'a T> for LinkedHashSet<T, S>
367 where
368     T: 'a + Eq + Hash + Copy,
369     S: BuildHasher,
370 {
371     #[inline]
extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)372     fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
373         self.extend(iter.into_iter().cloned());
374     }
375 }
376 
377 impl<T, S> Default for LinkedHashSet<T, S>
378 where
379     S: Default,
380 {
381     #[inline]
default() -> LinkedHashSet<T, S>382     fn default() -> LinkedHashSet<T, S> {
383         LinkedHashSet {
384             map: LinkedHashMap::default(),
385         }
386     }
387 }
388 
389 impl<'a, 'b, T, S> BitOr<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
390 where
391     T: Eq + Hash + Clone,
392     S: BuildHasher + Default,
393 {
394     type Output = LinkedHashSet<T, S>;
395 
396     #[inline]
bitor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>397     fn bitor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
398         self.union(rhs).cloned().collect()
399     }
400 }
401 
402 impl<'a, 'b, T, S> BitAnd<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
403 where
404     T: Eq + Hash + Clone,
405     S: BuildHasher + Default,
406 {
407     type Output = LinkedHashSet<T, S>;
408 
409     #[inline]
bitand(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>410     fn bitand(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
411         self.intersection(rhs).cloned().collect()
412     }
413 }
414 
415 impl<'a, 'b, T, S> BitXor<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
416 where
417     T: Eq + Hash + Clone,
418     S: BuildHasher + Default,
419 {
420     type Output = LinkedHashSet<T, S>;
421 
422     #[inline]
bitxor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>423     fn bitxor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
424         self.symmetric_difference(rhs).cloned().collect()
425     }
426 }
427 
428 impl<'a, 'b, T, S> Sub<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
429 where
430     T: Eq + Hash + Clone,
431     S: BuildHasher + Default,
432 {
433     type Output = LinkedHashSet<T, S>;
434 
435     #[inline]
sub(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>436     fn sub(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
437         self.difference(rhs).cloned().collect()
438     }
439 }
440 
441 pub struct Iter<'a, K> {
442     iter: linked_hash_map::Keys<'a, K, ()>,
443 }
444 
445 pub struct IntoIter<K> {
446     iter: linked_hash_map::IntoIter<K, ()>,
447 }
448 
449 pub struct Drain<'a, K: 'a> {
450     iter: linked_hash_map::Drain<'a, K, ()>,
451 }
452 
453 pub struct Intersection<'a, T, S> {
454     iter: Iter<'a, T>,
455     other: &'a LinkedHashSet<T, S>,
456 }
457 
458 pub struct Difference<'a, T, S> {
459     iter: Iter<'a, T>,
460     other: &'a LinkedHashSet<T, S>,
461 }
462 
463 pub struct SymmetricDifference<'a, T, S> {
464     iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
465 }
466 
467 pub struct Union<'a, T, S> {
468     iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
469 }
470 
471 impl<'a, T, S> IntoIterator for &'a LinkedHashSet<T, S> {
472     type Item = &'a T;
473     type IntoIter = Iter<'a, T>;
474 
475     #[inline]
into_iter(self) -> Iter<'a, T>476     fn into_iter(self) -> Iter<'a, T> {
477         self.iter()
478     }
479 }
480 
481 impl<T, S> IntoIterator for LinkedHashSet<T, S> {
482     type Item = T;
483     type IntoIter = IntoIter<T>;
484 
485     #[inline]
into_iter(self) -> IntoIter<T>486     fn into_iter(self) -> IntoIter<T> {
487         IntoIter {
488             iter: self.map.into_iter(),
489         }
490     }
491 }
492 
493 impl<'a, K> Clone for Iter<'a, K> {
494     #[inline]
clone(&self) -> Iter<'a, K>495     fn clone(&self) -> Iter<'a, K> {
496         Iter {
497             iter: self.iter.clone(),
498         }
499     }
500 }
501 impl<'a, K> Iterator for Iter<'a, K> {
502     type Item = &'a K;
503 
504     #[inline]
next(&mut self) -> Option<&'a K>505     fn next(&mut self) -> Option<&'a K> {
506         self.iter.next()
507     }
508 
509     #[inline]
size_hint(&self) -> (usize, Option<usize>)510     fn size_hint(&self) -> (usize, Option<usize>) {
511         self.iter.size_hint()
512     }
513 }
514 
515 impl<'a, K> ExactSizeIterator for Iter<'a, K> {}
516 
517 impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
518     #[inline]
next_back(&mut self) -> Option<&'a T>519     fn next_back(&mut self) -> Option<&'a T> {
520         self.iter.next_back()
521     }
522 }
523 
524 impl<'a, K: fmt::Debug> fmt::Debug for Iter<'a, K> {
525     #[inline]
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result526     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
527         f.debug_list().entries(self.clone()).finish()
528     }
529 }
530 
531 impl<K> Iterator for IntoIter<K> {
532     type Item = K;
533 
534     #[inline]
next(&mut self) -> Option<K>535     fn next(&mut self) -> Option<K> {
536         self.iter.next().map(|(k, _)| k)
537     }
538 
539     #[inline]
size_hint(&self) -> (usize, Option<usize>)540     fn size_hint(&self) -> (usize, Option<usize>) {
541         self.iter.size_hint()
542     }
543 }
544 
545 impl<K> ExactSizeIterator for IntoIter<K> {}
546 
547 impl<K> DoubleEndedIterator for IntoIter<K> {
548     #[inline]
next_back(&mut self) -> Option<K>549     fn next_back(&mut self) -> Option<K> {
550         self.iter.next_back().map(|(k, _)| k)
551     }
552 }
553 
554 impl<'a, K> Iterator for Drain<'a, K> {
555     type Item = K;
556 
557     #[inline]
next(&mut self) -> Option<K>558     fn next(&mut self) -> Option<K> {
559         self.iter.next().map(|(k, _)| k)
560     }
561 
562     #[inline]
size_hint(&self) -> (usize, Option<usize>)563     fn size_hint(&self) -> (usize, Option<usize>) {
564         self.iter.size_hint()
565     }
566 }
567 
568 impl<'a, K> DoubleEndedIterator for Drain<'a, K> {
569     #[inline]
next_back(&mut self) -> Option<K>570     fn next_back(&mut self) -> Option<K> {
571         self.iter.next_back().map(|(k, _)| k)
572     }
573 }
574 
575 impl<'a, K> ExactSizeIterator for Drain<'a, K> {}
576 
577 impl<'a, T, S> Clone for Intersection<'a, T, S> {
578     #[inline]
clone(&self) -> Intersection<'a, T, S>579     fn clone(&self) -> Intersection<'a, T, S> {
580         Intersection {
581             iter: self.iter.clone(),
582             ..*self
583         }
584     }
585 }
586 
587 impl<'a, T, S> Iterator for Intersection<'a, T, S>
588 where
589     T: Eq + Hash,
590     S: BuildHasher,
591 {
592     type Item = &'a T;
593 
594     #[inline]
next(&mut self) -> Option<&'a T>595     fn next(&mut self) -> Option<&'a T> {
596         loop {
597             match self.iter.next() {
598                 None => return None,
599                 Some(elt) => {
600                     if self.other.contains(elt) {
601                         return Some(elt);
602                     }
603                 }
604             }
605         }
606     }
607 
608     #[inline]
size_hint(&self) -> (usize, Option<usize>)609     fn size_hint(&self) -> (usize, Option<usize>) {
610         let (_, upper) = self.iter.size_hint();
611         (0, upper)
612     }
613 }
614 
615 impl<'a, T, S> fmt::Debug for Intersection<'a, T, S>
616 where
617     T: fmt::Debug + Eq + Hash,
618     S: BuildHasher,
619 {
620     #[inline]
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result621     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
622         f.debug_list().entries(self.clone()).finish()
623     }
624 }
625 
626 impl<'a, T, S> Clone for Difference<'a, T, S> {
627     #[inline]
clone(&self) -> Difference<'a, T, S>628     fn clone(&self) -> Difference<'a, T, S> {
629         Difference {
630             iter: self.iter.clone(),
631             ..*self
632         }
633     }
634 }
635 
636 impl<'a, T, S> Iterator for Difference<'a, T, S>
637 where
638     T: Eq + Hash,
639     S: BuildHasher,
640 {
641     type Item = &'a T;
642 
643     #[inline]
next(&mut self) -> Option<&'a T>644     fn next(&mut self) -> Option<&'a T> {
645         loop {
646             match self.iter.next() {
647                 None => return None,
648                 Some(elt) => {
649                     if !self.other.contains(elt) {
650                         return Some(elt);
651                     }
652                 }
653             }
654         }
655     }
656 
657     #[inline]
size_hint(&self) -> (usize, Option<usize>)658     fn size_hint(&self) -> (usize, Option<usize>) {
659         let (_, upper) = self.iter.size_hint();
660         (0, upper)
661     }
662 }
663 
664 impl<'a, T, S> fmt::Debug for Difference<'a, T, S>
665 where
666     T: fmt::Debug + Eq + Hash,
667     S: BuildHasher,
668 {
669     #[inline]
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result670     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
671         f.debug_list().entries(self.clone()).finish()
672     }
673 }
674 
675 impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> {
676     #[inline]
clone(&self) -> SymmetricDifference<'a, T, S>677     fn clone(&self) -> SymmetricDifference<'a, T, S> {
678         SymmetricDifference {
679             iter: self.iter.clone(),
680         }
681     }
682 }
683 
684 impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S>
685 where
686     T: Eq + Hash,
687     S: BuildHasher,
688 {
689     type Item = &'a T;
690 
691     #[inline]
next(&mut self) -> Option<&'a T>692     fn next(&mut self) -> Option<&'a T> {
693         self.iter.next()
694     }
695 
696     #[inline]
size_hint(&self) -> (usize, Option<usize>)697     fn size_hint(&self) -> (usize, Option<usize>) {
698         self.iter.size_hint()
699     }
700 }
701 
702 impl<'a, T, S> fmt::Debug for SymmetricDifference<'a, T, S>
703 where
704     T: fmt::Debug + Eq + Hash,
705     S: BuildHasher,
706 {
707     #[inline]
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result708     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
709         f.debug_list().entries(self.clone()).finish()
710     }
711 }
712 
713 impl<'a, T, S> Clone for Union<'a, T, S> {
714     #[inline]
clone(&self) -> Union<'a, T, S>715     fn clone(&self) -> Union<'a, T, S> {
716         Union {
717             iter: self.iter.clone(),
718         }
719     }
720 }
721 
722 impl<'a, T, S> fmt::Debug for Union<'a, T, S>
723 where
724     T: fmt::Debug + Eq + Hash,
725     S: BuildHasher,
726 {
727     #[inline]
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result728     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
729         f.debug_list().entries(self.clone()).finish()
730     }
731 }
732 
733 impl<'a, T, S> Iterator for Union<'a, T, S>
734 where
735     T: Eq + Hash,
736     S: BuildHasher,
737 {
738     type Item = &'a T;
739 
740     #[inline]
next(&mut self) -> Option<&'a T>741     fn next(&mut self) -> Option<&'a T> {
742         self.iter.next()
743     }
744 
745     #[inline]
size_hint(&self) -> (usize, Option<usize>)746     fn size_hint(&self) -> (usize, Option<usize>) {
747         self.iter.size_hint()
748     }
749 }
750