diff options
| author | Valerii Lashmanov <vflashm@gmail.com> | 2020-09-24 01:21:31 -0500 |
|---|---|---|
| committer | Valerii Lashmanov <vflashm@gmail.com> | 2020-09-26 14:30:05 -0500 |
| commit | 0600b178aa0e9f310067bf8ccaf736e77a03eb1d (patch) | |
| tree | cabfe4537ed3bcec07dc66cfe64675993584a989 /compiler/rustc_data_structures/src/sso/mod.rs | |
| parent | 5c224a484dc6ba2a70c9cd0d73a04849f6d7aa68 (diff) | |
| download | rust-0600b178aa0e9f310067bf8ccaf736e77a03eb1d.tar.gz rust-0600b178aa0e9f310067bf8ccaf736e77a03eb1d.zip | |
SsoHashSet/SsoHashMap API greatly expanded
Now both provide almost complete API of their non-SSO counterparts.
Diffstat (limited to 'compiler/rustc_data_structures/src/sso/mod.rs')
| -rw-r--r-- | compiler/rustc_data_structures/src/sso/mod.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/sso/mod.rs b/compiler/rustc_data_structures/src/sso/mod.rs index ef634b9adce..bcc4240721e 100644 --- a/compiler/rustc_data_structures/src/sso/mod.rs +++ b/compiler/rustc_data_structures/src/sso/mod.rs @@ -1,3 +1,75 @@ +use std::fmt; +use std::iter::ExactSizeIterator; +use std::iter::FusedIterator; +use std::iter::Iterator; + +/// Iterator which may contain instance of +/// one of two specific implementations. +/// +/// Used by both SsoHashMap and SsoHashSet. +#[derive(Clone)] +pub enum EitherIter<L, R> { + Left(L), + Right(R), +} + +impl<L, R> Iterator for EitherIter<L, R> +where + L: Iterator, + R: Iterator<Item = L::Item>, +{ + type Item = L::Item; + + fn next(&mut self) -> Option<Self::Item> { + match self { + EitherIter::Left(l) => l.next(), + EitherIter::Right(r) => r.next(), + } + } + + fn size_hint(&self) -> (usize, Option<usize>) { + match self { + EitherIter::Left(l) => l.size_hint(), + EitherIter::Right(r) => r.size_hint(), + } + } +} + +impl<L, R> ExactSizeIterator for EitherIter<L, R> +where + L: ExactSizeIterator, + R: ExactSizeIterator, + EitherIter<L, R>: Iterator, +{ + fn len(&self) -> usize { + match self { + EitherIter::Left(l) => l.len(), + EitherIter::Right(r) => r.len(), + } + } +} + +impl<L, R> FusedIterator for EitherIter<L, R> +where + L: FusedIterator, + R: FusedIterator, + EitherIter<L, R>: Iterator, +{ +} + +impl<L, R> fmt::Debug for EitherIter<L, R> +where + L: fmt::Debug, + R: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + EitherIter::Left(l) => l.fmt(f), + EitherIter::Right(r) => r.fmt(f), + } + } +} + mod map; mod set; |
