diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
commit | 9918693037dce8aa4bb6f08741b6812923486c18 (patch) | |
tree | 21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/itertools-0.11.0/src/exactly_one_err.rs | |
parent | Releasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff) | |
download | rustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip |
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/itertools-0.11.0/src/exactly_one_err.rs')
-rw-r--r-- | vendor/itertools-0.11.0/src/exactly_one_err.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/vendor/itertools-0.11.0/src/exactly_one_err.rs b/vendor/itertools-0.11.0/src/exactly_one_err.rs new file mode 100644 index 000000000..c54ae77ca --- /dev/null +++ b/vendor/itertools-0.11.0/src/exactly_one_err.rs @@ -0,0 +1,110 @@ +#[cfg(feature = "use_std")] +use std::error::Error; +use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; + +use std::iter::ExactSizeIterator; + +use either::Either; + +use crate::size_hint; + +/// Iterator returned for the error case of `IterTools::exactly_one()` +/// This iterator yields exactly the same elements as the input iterator. +/// +/// During the execution of `exactly_one` the iterator must be mutated. This wrapper +/// effectively "restores" the state of the input iterator when it's handed back. +/// +/// This is very similar to `PutBackN` except this iterator only supports 0-2 elements and does not +/// use a `Vec`. +#[derive(Clone)] +pub struct ExactlyOneError<I> +where + I: Iterator, +{ + first_two: Option<Either<[I::Item; 2], I::Item>>, + inner: I, +} + +impl<I> ExactlyOneError<I> +where + I: Iterator, +{ + /// Creates a new `ExactlyOneErr` iterator. + pub(crate) fn new(first_two: Option<Either<[I::Item; 2], I::Item>>, inner: I) -> Self { + Self { first_two, inner } + } + + fn additional_len(&self) -> usize { + match self.first_two { + Some(Either::Left(_)) => 2, + Some(Either::Right(_)) => 1, + None => 0, + } + } +} + +impl<I> Iterator for ExactlyOneError<I> +where + I: Iterator, +{ + type Item = I::Item; + + fn next(&mut self) -> Option<Self::Item> { + match self.first_two.take() { + Some(Either::Left([first, second])) => { + self.first_two = Some(Either::Right(second)); + Some(first) + }, + Some(Either::Right(second)) => { + Some(second) + } + None => { + self.inner.next() + } + } + } + + fn size_hint(&self) -> (usize, Option<usize>) { + size_hint::add_scalar(self.inner.size_hint(), self.additional_len()) + } +} + + +impl<I> ExactSizeIterator for ExactlyOneError<I> where I: ExactSizeIterator {} + +impl<I> Display for ExactlyOneError<I> + where I: Iterator, +{ + fn fmt(&self, f: &mut Formatter) -> FmtResult { + let additional = self.additional_len(); + if additional > 0 { + write!(f, "got at least 2 elements when exactly one was expected") + } else { + write!(f, "got zero elements when exactly one was expected") + } + } +} + +impl<I> Debug for ExactlyOneError<I> + where I: Iterator + Debug, + I::Item: Debug, +{ + fn fmt(&self, f: &mut Formatter) -> FmtResult { + match &self.first_two { + Some(Either::Left([first, second])) => { + write!(f, "ExactlyOneError[First: {:?}, Second: {:?}, RemainingIter: {:?}]", first, second, self.inner) + }, + Some(Either::Right(second)) => { + write!(f, "ExactlyOneError[Second: {:?}, RemainingIter: {:?}]", second, self.inner) + } + None => { + write!(f, "ExactlyOneError[RemainingIter: {:?}]", self.inner) + } + } + } +} + +#[cfg(feature = "use_std")] +impl<I> Error for ExactlyOneError<I> where I: Iterator + Debug, I::Item: Debug, {} + + |