|
| 1 | +use std::iter::Chain; |
| 2 | + |
| 3 | +use crate::stable_hasher::ToStableHashKey; |
| 4 | + |
| 5 | +pub struct StableIterator<I: Iterator> { |
| 6 | + inner: I, |
| 7 | +} |
| 8 | + |
| 9 | +impl<T, I: Iterator<Item = T>> StableIterator<I> { |
| 10 | + #[inline] |
| 11 | + pub fn map<U, F: Fn(T) -> U>(self, f: F) -> StableIterator<impl Iterator<Item = U>> { |
| 12 | + StableIterator { inner: self.inner.map(f) } |
| 13 | + } |
| 14 | + |
| 15 | + #[inline] |
| 16 | + pub fn into_sorted<HCX>(self, hcx: &HCX) -> Vec<T> |
| 17 | + where |
| 18 | + T: ToStableHashKey<HCX>, |
| 19 | + { |
| 20 | + let mut items: Vec<T> = self.inner.collect(); |
| 21 | + items.sort_by_cached_key(|x| x.to_stable_hash_key(hcx)); |
| 22 | + items |
| 23 | + } |
| 24 | + |
| 25 | + #[inline] |
| 26 | + pub fn any<F: Fn(T) -> bool>(&mut self, f: F) -> bool { |
| 27 | + self.inner.any(f) |
| 28 | + } |
| 29 | + |
| 30 | + #[inline] |
| 31 | + pub fn all<F: Fn(T) -> bool>(&mut self, f: F) -> bool { |
| 32 | + self.inner.all(f) |
| 33 | + } |
| 34 | + |
| 35 | + #[inline] |
| 36 | + pub fn chain<J: Iterator<Item = I::Item>>(self, other: StableIterator<J>) -> StableChain<I, J> { |
| 37 | + self.inner.chain(other.inner).into() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +pub trait IntoStableIterator { |
| 42 | + type IntoIter: Iterator; |
| 43 | + fn into_stable_iter(self) -> StableIterator<Self::IntoIter>; |
| 44 | +} |
| 45 | + |
| 46 | +impl<I: Iterator, S: IntoIterator<Item = I::Item, IntoIter = I>> IntoStableIterator for S { |
| 47 | + type IntoIter = I; |
| 48 | + |
| 49 | + #[inline] |
| 50 | + fn into_stable_iter(self) -> StableIterator<I> { |
| 51 | + StableIterator { inner: self.into_iter() } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +pub struct StableChain<I: Iterator, J: Iterator> { |
| 56 | + inner: Chain<I, J>, |
| 57 | +} |
| 58 | + |
| 59 | +impl<I: Iterator, J: Iterator<Item = I::Item>> IntoStableIterator for StableChain<I, J> { |
| 60 | + type IntoIter = Chain<I, J>; |
| 61 | + |
| 62 | + #[inline] |
| 63 | + fn into_stable_iter(self) -> StableIterator<Self::IntoIter> { |
| 64 | + self.inner.into_stable_iter() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<I: Iterator, J: Iterator> From<Chain<I, J>> for StableChain<I, J> { |
| 69 | + #[inline] |
| 70 | + fn from(inner: Chain<I, J>) -> Self { |
| 71 | + Self { inner } |
| 72 | + } |
| 73 | +} |
0 commit comments