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(&mut self) -> Option<&T>253 pub fn back(&mut 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: &LinkedHashSet<T, S>) -> bool307 fn eq(&self, other: &LinkedHashSet<T, S>) -> bool { 308 if self.len() != other.len() { 309 return false; 310 } 311 312 self.iter().all(|key| other.contains(key)) 313 } 314 } 315 316 impl<T, S> Hash for LinkedHashSet<T, S> 317 where 318 T: Eq + Hash, 319 S: BuildHasher, 320 { 321 #[inline] hash<H: Hasher>(&self, state: &mut H)322 fn hash<H: Hasher>(&self, state: &mut H) { 323 for e in self { 324 e.hash(state); 325 } 326 } 327 } 328 329 impl<T, S> Eq for LinkedHashSet<T, S> 330 where 331 T: Eq + Hash, 332 S: BuildHasher, 333 { 334 } 335 336 impl<T, S> fmt::Debug for LinkedHashSet<T, S> 337 where 338 T: fmt::Debug, 339 { 340 #[inline] fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result341 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 342 f.debug_set().entries(self.iter()).finish() 343 } 344 } 345 346 impl<T, S> FromIterator<T> for LinkedHashSet<T, S> 347 where 348 T: Eq + Hash, 349 S: BuildHasher + Default, 350 { 351 #[inline] from_iter<I: IntoIterator<Item = T>>(iter: I) -> LinkedHashSet<T, S>352 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> LinkedHashSet<T, S> { 353 let mut set = LinkedHashSet::with_hasher(Default::default()); 354 set.extend(iter); 355 set 356 } 357 } 358 359 impl<T, S> Extend<T> for LinkedHashSet<T, S> 360 where 361 T: Eq + Hash, 362 S: BuildHasher, 363 { 364 #[inline] extend<I: IntoIterator<Item = T>>(&mut self, iter: I)365 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { 366 self.map.extend(iter.into_iter().map(|k| (k, ()))); 367 } 368 } 369 370 impl<'a, T, S> Extend<&'a T> for LinkedHashSet<T, S> 371 where 372 T: 'a + Eq + Hash + Copy, 373 S: BuildHasher, 374 { 375 #[inline] extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)376 fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { 377 self.extend(iter.into_iter().cloned()); 378 } 379 } 380 381 impl<T, S> Default for LinkedHashSet<T, S> 382 where 383 S: Default, 384 { 385 #[inline] default() -> LinkedHashSet<T, S>386 fn default() -> LinkedHashSet<T, S> { 387 LinkedHashSet { 388 map: LinkedHashMap::default(), 389 } 390 } 391 } 392 393 impl<'a, 'b, T, S> BitOr<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S> 394 where 395 T: Eq + Hash + Clone, 396 S: BuildHasher + Default, 397 { 398 type Output = LinkedHashSet<T, S>; 399 400 #[inline] bitor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>401 fn bitor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> { 402 self.union(rhs).cloned().collect() 403 } 404 } 405 406 impl<'a, 'b, T, S> BitAnd<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S> 407 where 408 T: Eq + Hash + Clone, 409 S: BuildHasher + Default, 410 { 411 type Output = LinkedHashSet<T, S>; 412 413 #[inline] bitand(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>414 fn bitand(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> { 415 self.intersection(rhs).cloned().collect() 416 } 417 } 418 419 impl<'a, 'b, T, S> BitXor<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S> 420 where 421 T: Eq + Hash + Clone, 422 S: BuildHasher + Default, 423 { 424 type Output = LinkedHashSet<T, S>; 425 426 #[inline] bitxor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>427 fn bitxor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> { 428 self.symmetric_difference(rhs).cloned().collect() 429 } 430 } 431 432 impl<'a, 'b, T, S> Sub<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S> 433 where 434 T: Eq + Hash + Clone, 435 S: BuildHasher + Default, 436 { 437 type Output = LinkedHashSet<T, S>; 438 439 #[inline] sub(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S>440 fn sub(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> { 441 self.difference(rhs).cloned().collect() 442 } 443 } 444 445 pub struct Iter<'a, K> { 446 iter: linked_hash_map::Keys<'a, K, ()>, 447 } 448 449 pub struct IntoIter<K> { 450 iter: linked_hash_map::IntoIter<K, ()>, 451 } 452 453 pub struct Drain<'a, K: 'a> { 454 iter: linked_hash_map::Drain<'a, K, ()>, 455 } 456 457 pub struct Intersection<'a, T, S> { 458 iter: Iter<'a, T>, 459 other: &'a LinkedHashSet<T, S>, 460 } 461 462 pub struct Difference<'a, T, S> { 463 iter: Iter<'a, T>, 464 other: &'a LinkedHashSet<T, S>, 465 } 466 467 pub struct SymmetricDifference<'a, T, S> { 468 iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>, 469 } 470 471 pub struct Union<'a, T, S> { 472 iter: Chain<Iter<'a, T>, Difference<'a, T, S>>, 473 } 474 475 impl<'a, T, S> IntoIterator for &'a LinkedHashSet<T, S> { 476 type Item = &'a T; 477 type IntoIter = Iter<'a, T>; 478 479 #[inline] into_iter(self) -> Iter<'a, T>480 fn into_iter(self) -> Iter<'a, T> { 481 self.iter() 482 } 483 } 484 485 impl<T, S> IntoIterator for LinkedHashSet<T, S> { 486 type Item = T; 487 type IntoIter = IntoIter<T>; 488 489 #[inline] into_iter(self) -> IntoIter<T>490 fn into_iter(self) -> IntoIter<T> { 491 IntoIter { 492 iter: self.map.into_iter(), 493 } 494 } 495 } 496 497 impl<'a, K> Clone for Iter<'a, K> { 498 #[inline] clone(&self) -> Iter<'a, K>499 fn clone(&self) -> Iter<'a, K> { 500 Iter { 501 iter: self.iter.clone(), 502 } 503 } 504 } 505 impl<'a, K> Iterator for Iter<'a, K> { 506 type Item = &'a K; 507 508 #[inline] next(&mut self) -> Option<&'a K>509 fn next(&mut self) -> Option<&'a K> { 510 self.iter.next() 511 } 512 513 #[inline] size_hint(&self) -> (usize, Option<usize>)514 fn size_hint(&self) -> (usize, Option<usize>) { 515 self.iter.size_hint() 516 } 517 } 518 519 impl<'a, K> ExactSizeIterator for Iter<'a, K> {} 520 521 impl<'a, T> DoubleEndedIterator for Iter<'a, T> { 522 #[inline] next_back(&mut self) -> Option<&'a T>523 fn next_back(&mut self) -> Option<&'a T> { 524 self.iter.next_back() 525 } 526 } 527 528 impl<'a, K: fmt::Debug> fmt::Debug for Iter<'a, K> { 529 #[inline] fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result530 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 531 f.debug_list().entries(self.clone()).finish() 532 } 533 } 534 535 impl<K> Iterator for IntoIter<K> { 536 type Item = K; 537 538 #[inline] next(&mut self) -> Option<K>539 fn next(&mut self) -> Option<K> { 540 self.iter.next().map(|(k, _)| k) 541 } 542 543 #[inline] size_hint(&self) -> (usize, Option<usize>)544 fn size_hint(&self) -> (usize, Option<usize>) { 545 self.iter.size_hint() 546 } 547 } 548 549 impl<K> ExactSizeIterator for IntoIter<K> {} 550 551 impl<K> DoubleEndedIterator for IntoIter<K> { 552 #[inline] next_back(&mut self) -> Option<K>553 fn next_back(&mut self) -> Option<K> { 554 self.iter.next_back().map(|(k, _)| k) 555 } 556 } 557 558 impl<'a, K> Iterator for Drain<'a, K> { 559 type Item = K; 560 561 #[inline] next(&mut self) -> Option<K>562 fn next(&mut self) -> Option<K> { 563 self.iter.next().map(|(k, _)| k) 564 } 565 566 #[inline] size_hint(&self) -> (usize, Option<usize>)567 fn size_hint(&self) -> (usize, Option<usize>) { 568 self.iter.size_hint() 569 } 570 } 571 572 impl<'a, K> DoubleEndedIterator for Drain<'a, K> { 573 #[inline] next_back(&mut self) -> Option<K>574 fn next_back(&mut self) -> Option<K> { 575 self.iter.next_back().map(|(k, _)| k) 576 } 577 } 578 579 impl<'a, K> ExactSizeIterator for Drain<'a, K> {} 580 581 impl<'a, T, S> Clone for Intersection<'a, T, S> { 582 #[inline] clone(&self) -> Intersection<'a, T, S>583 fn clone(&self) -> Intersection<'a, T, S> { 584 Intersection { 585 iter: self.iter.clone(), 586 ..*self 587 } 588 } 589 } 590 591 impl<'a, T, S> Iterator for Intersection<'a, T, S> 592 where 593 T: Eq + Hash, 594 S: BuildHasher, 595 { 596 type Item = &'a T; 597 598 #[inline] next(&mut self) -> Option<&'a T>599 fn next(&mut self) -> Option<&'a T> { 600 loop { 601 match self.iter.next() { 602 None => return None, 603 Some(elt) => { 604 if self.other.contains(elt) { 605 return Some(elt); 606 } 607 } 608 } 609 } 610 } 611 612 #[inline] size_hint(&self) -> (usize, Option<usize>)613 fn size_hint(&self) -> (usize, Option<usize>) { 614 let (_, upper) = self.iter.size_hint(); 615 (0, upper) 616 } 617 } 618 619 impl<'a, T, S> fmt::Debug for Intersection<'a, T, S> 620 where 621 T: fmt::Debug + Eq + Hash, 622 S: BuildHasher, 623 { 624 #[inline] fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result625 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 626 f.debug_list().entries(self.clone()).finish() 627 } 628 } 629 630 impl<'a, T, S> Clone for Difference<'a, T, S> { 631 #[inline] clone(&self) -> Difference<'a, T, S>632 fn clone(&self) -> Difference<'a, T, S> { 633 Difference { 634 iter: self.iter.clone(), 635 ..*self 636 } 637 } 638 } 639 640 impl<'a, T, S> Iterator for Difference<'a, T, S> 641 where 642 T: Eq + Hash, 643 S: BuildHasher, 644 { 645 type Item = &'a T; 646 647 #[inline] next(&mut self) -> Option<&'a T>648 fn next(&mut self) -> Option<&'a T> { 649 loop { 650 match self.iter.next() { 651 None => return None, 652 Some(elt) => { 653 if !self.other.contains(elt) { 654 return Some(elt); 655 } 656 } 657 } 658 } 659 } 660 661 #[inline] size_hint(&self) -> (usize, Option<usize>)662 fn size_hint(&self) -> (usize, Option<usize>) { 663 let (_, upper) = self.iter.size_hint(); 664 (0, upper) 665 } 666 } 667 668 impl<'a, T, S> fmt::Debug for Difference<'a, T, S> 669 where 670 T: fmt::Debug + Eq + Hash, 671 S: BuildHasher, 672 { 673 #[inline] fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result674 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 675 f.debug_list().entries(self.clone()).finish() 676 } 677 } 678 679 impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> { 680 #[inline] clone(&self) -> SymmetricDifference<'a, T, S>681 fn clone(&self) -> SymmetricDifference<'a, T, S> { 682 SymmetricDifference { 683 iter: self.iter.clone(), 684 } 685 } 686 } 687 688 impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S> 689 where 690 T: Eq + Hash, 691 S: BuildHasher, 692 { 693 type Item = &'a T; 694 695 #[inline] next(&mut self) -> Option<&'a T>696 fn next(&mut self) -> Option<&'a T> { 697 self.iter.next() 698 } 699 700 #[inline] size_hint(&self) -> (usize, Option<usize>)701 fn size_hint(&self) -> (usize, Option<usize>) { 702 self.iter.size_hint() 703 } 704 } 705 706 impl<'a, T, S> fmt::Debug for SymmetricDifference<'a, T, S> 707 where 708 T: fmt::Debug + Eq + Hash, 709 S: BuildHasher, 710 { 711 #[inline] fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result712 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 713 f.debug_list().entries(self.clone()).finish() 714 } 715 } 716 717 impl<'a, T, S> Clone for Union<'a, T, S> { 718 #[inline] clone(&self) -> Union<'a, T, S>719 fn clone(&self) -> Union<'a, T, S> { 720 Union { 721 iter: self.iter.clone(), 722 } 723 } 724 } 725 726 impl<'a, T, S> fmt::Debug for Union<'a, T, S> 727 where 728 T: fmt::Debug + Eq + Hash, 729 S: BuildHasher, 730 { 731 #[inline] fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result732 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 733 f.debug_list().entries(self.clone()).finish() 734 } 735 } 736 737 impl<'a, T, S> Iterator for Union<'a, T, S> 738 where 739 T: Eq + Hash, 740 S: BuildHasher, 741 { 742 type Item = &'a T; 743 744 #[inline] next(&mut self) -> Option<&'a T>745 fn next(&mut self) -> Option<&'a T> { 746 self.iter.next() 747 } 748 749 #[inline] size_hint(&self) -> (usize, Option<usize>)750 fn size_hint(&self) -> (usize, Option<usize>) { 751 self.iter.size_hint() 752 } 753 } 754