use core::fmt;
use super::{Iter, Key, Set};
#[must_use = "this returns the intersection as an iterator, \
without modifying either input set"]
pub struct Intersection<'a, T: 'a + Key> {
pub(super) iter: Iter<'a, T>,
pub(super) other: &'a Set<T>,
}
impl<T: Key> Clone for Intersection<'_, T> {
#[inline]
fn clone(&self) -> Self {
Intersection {
iter: self.iter.clone(),
..*self
}
}
}
impl<T> Iterator for Intersection<'_, T>
where
T: Key,
{
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
loop {
let elt = self.iter.next()?;
if self.other.contains(elt) {
return Some(elt);
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper)
}
#[inline]
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.iter.fold(init, |acc, elt| {
if self.other.contains(elt) {
f(acc, elt)
} else {
acc
}
})
}
}
impl<T> fmt::Debug for Intersection<'_, T>
where
T: fmt::Debug + Key,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}