diff options
Diffstat (limited to 'vendor/fallible-iterator')
-rw-r--r-- | vendor/fallible-iterator/.cargo-checksum.json | 2 | ||||
-rw-r--r-- | vendor/fallible-iterator/CHANGELOG.md | 15 | ||||
-rw-r--r-- | vendor/fallible-iterator/Cargo.toml | 20 | ||||
-rw-r--r-- | vendor/fallible-iterator/README.md | 7 | ||||
-rw-r--r-- | vendor/fallible-iterator/src/lib.rs | 488 | ||||
-rw-r--r-- | vendor/fallible-iterator/src/test.rs | 52 |
6 files changed, 412 insertions, 172 deletions
diff --git a/vendor/fallible-iterator/.cargo-checksum.json b/vendor/fallible-iterator/.cargo-checksum.json index 6ca413d57..84bfdcb62 100644 --- a/vendor/fallible-iterator/.cargo-checksum.json +++ b/vendor/fallible-iterator/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"CHANGELOG.md":"cdd0b75aa3081f8318c306024c295681a92be65be942bf0675d5171362f4c915","Cargo.toml":"1709797d7207c821c0006d250b4466651e1a3e9dcf8de22f1ec1721157b53f14","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"0816e154b159ba255c563f7c8c7df5bbb8cc5fc96f5ab8cf9f4743b4f41fe7eb","README.md":"300dac46130dedb0beac58fd21c268f02b9424b2d9954c0d577ffe3712c02f7c","src/lib.rs":"5ccf18d0135d9a78d7d3e8f8b37723a9634f239043595d249750ea9eb39487fd","src/test.rs":"892d47931f81590a2ac1a71125f5613c9af384bfada8c00e0ebbfbc44fa9d486"},"package":"4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"}
\ No newline at end of file +{"files":{"CHANGELOG.md":"ec998938be9e986d87f89f035b561045b9df6a014c889c1e09006e8a1f055504","Cargo.toml":"1eefc77c1092c098db3822fd01220a0015a1e3851320a953665823fb9b46b4c1","LICENSE-APACHE":"c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08","LICENSE-MIT":"0816e154b159ba255c563f7c8c7df5bbb8cc5fc96f5ab8cf9f4743b4f41fe7eb","README.md":"852506da2ff54c00f46b440019585356be034cc35f7908bbb0a80249a8b782f8","src/lib.rs":"58c73ed996f1c80425784b655db890b0bd04c72eeb97ce57f17d610bb28ea78d","src/test.rs":"507ec94f625d6d1b4dbb957f055efe3486ff553b7b7b4a78019018c791b0bf28"},"package":"2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"}
\ No newline at end of file diff --git a/vendor/fallible-iterator/CHANGELOG.md b/vendor/fallible-iterator/CHANGELOG.md index c2cb0c63b..a79150767 100644 --- a/vendor/fallible-iterator/CHANGELOG.md +++ b/vendor/fallible-iterator/CHANGELOG.md @@ -1,6 +1,19 @@ # Change Log -## [Unreleased] +## [v0.3.0] - 2023-05-22 + +### Removed + +* `FromFallibleIterator` trait is removed, `FallibleIterator::collect` now requires `std::iter::FromIterator`, + which has strictly more implementations. + +### Added + +* add convenient wrapper for std::iter::Iterator +* more utilities mirroring std::iter +* Peekable methods +* `IteratorExt` for convenient conversion +* `FallibleIterator::unwrap` ## [v0.2.0] - 2019-03-10 diff --git a/vendor/fallible-iterator/Cargo.toml b/vendor/fallible-iterator/Cargo.toml index b7a15c14b..ed372d5ee 100644 --- a/vendor/fallible-iterator/Cargo.toml +++ b/vendor/fallible-iterator/Cargo.toml @@ -3,25 +3,27 @@ # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g. crates.io) dependencies +# to registry (e.g., crates.io) dependencies. # -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "fallible-iterator" -version = "0.2.0" +version = "0.3.0" authors = ["Steven Fackler <sfackler@gmail.com>"] description = "Fallible iterator traits" readme = "README.md" -categories = ["algorithms", "no-std"] +categories = [ + "algorithms", + "no-std", +] license = "MIT/Apache-2.0" repository = "https://github.com/sfackler/rust-fallible-iterator" [features] alloc = [] -default = ["std"] -std = [] +default = ["alloc"] +std = ["alloc"] diff --git a/vendor/fallible-iterator/README.md b/vendor/fallible-iterator/README.md index e7a4af22b..a0ea216a5 100644 --- a/vendor/fallible-iterator/README.md +++ b/vendor/fallible-iterator/README.md @@ -1,8 +1,9 @@ -# rust-fallible-iterator +[![crates.io](https://img.shields.io/crates/v/fallible-iterator.svg)](https://crates.io/crates/fallible-iterator) +[![docs.rs](https://docs.rs/fallible-iterator/badge.svg)](https://docs.rs/fallible-iterator) -[![CircleCI](https://circleci.com/gh/sfackler/rust-fallible-iterator.svg?style=shield)](https://circleci.com/gh/sfackler/rust-fallible-iterator) +![Continuous integration](https://github.com/sfackler/rust-fallible-iterator/actions/workflows/rust.yml/badge.svg?branch=master&event=push) -[Documentation](https://sfackler.github.io/rust-fallible-iterator/doc/v0.1.3/fallible_iterator) +# rust-fallible-iterator "Fallible" iterators for Rust. diff --git a/vendor/fallible-iterator/src/lib.rs b/vendor/fallible-iterator/src/lib.rs index f5f77b257..88e8d557a 100644 --- a/vendor/fallible-iterator/src/lib.rs +++ b/vendor/fallible-iterator/src/lib.rs @@ -65,40 +65,20 @@ //! ``` #![doc(html_root_url = "https://docs.rs/fallible-iterator/0.2")] #![warn(missing_docs)] -#![cfg_attr(feature = "alloc", feature(alloc))] #![no_std] use core::cmp::{self, Ordering}; +use core::convert::Infallible; use core::iter; +use core::marker::PhantomData; -#[cfg(all(feature = "alloc", not(feature = "std")))] -#[cfg_attr(test, macro_use)] +#[cfg(feature = "alloc")] extern crate alloc; -#[cfg(all(feature = "alloc", not(feature = "std")))] -mod imports { - pub use alloc::boxed::Box; - pub use alloc::collections::btree_map::BTreeMap; - pub use alloc::collections::btree_set::BTreeSet; - pub use alloc::vec::Vec; -} - -#[cfg(feature = "std")] -#[cfg_attr(test, macro_use)] -extern crate std; - -#[cfg(feature = "std")] -mod imports { - pub use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; - pub use std::hash::{BuildHasher, Hash}; - pub use std::prelude::v1::*; -} +#[cfg(feature = "alloc")] +use alloc::boxed::Box; -#[cfg(any(feature = "std", feature = "alloc"))] -use crate::imports::*; - -#[cfg(any(feature = "std", feature = "alloc"))] -#[cfg(test)] +#[cfg(all(test, feature = "alloc"))] mod test; enum FoldStop<T, E> { @@ -141,7 +121,7 @@ pub trait FallibleIterator { /// Returns `Ok(None)` when iteration is finished. /// /// The behavior of calling this method after a previous call has returned - /// `Ok(None)` or `Err` is implemenetation defined. + /// `Ok(None)` or `Err` is implementation defined. fn next(&mut self) -> Result<Option<Self::Item>, Self::Error>; /// Returns bounds on the remaining length of the iterator. @@ -245,7 +225,7 @@ pub trait FallibleIterator { Self: Sized, F: FnMut(Self::Item) -> Result<B, Self::Error>, { - Map { it: self, f: f } + Map { it: self, f } } /// Calls a fallible closure on each element of an iterator. @@ -267,7 +247,7 @@ pub trait FallibleIterator { Self: Sized, F: FnMut(&Self::Item) -> Result<bool, Self::Error>, { - Filter { it: self, f: f } + Filter { it: self, f } } /// Returns an iterator which both filters and maps. The closure may fail; @@ -278,7 +258,7 @@ pub trait FallibleIterator { Self: Sized, F: FnMut(Self::Item) -> Result<Option<B>, Self::Error>, { - FilterMap { it: self, f: f } + FilterMap { it: self, f } } /// Returns an iterator which yields the current iteration count as well @@ -441,10 +421,10 @@ pub trait FallibleIterator { #[inline] fn collect<T>(self) -> Result<T, Self::Error> where - T: FromFallibleIterator<Self::Item>, + T: iter::FromIterator<Self::Item>, Self: Sized, { - T::from_fallible_iter(self) + self.iterator().collect() } /// Transforms the iterator into two collections, partitioning elements by a closure. @@ -731,7 +711,7 @@ pub trait FallibleIterator { Cloned(self) } - /// Returns an iterator which repeas this iterator endlessly. + /// Returns an iterator which repeats this iterator endlessly. #[inline] fn cycle(self) -> Cycle<Self> where @@ -960,7 +940,17 @@ pub trait FallibleIterator { F: FnMut(Self::Error) -> B, Self: Sized, { - MapErr { it: self, f: f } + MapErr { it: self, f } + } + + /// Returns an iterator which unwraps all of its elements. + #[inline] + fn unwrap<T>(self) -> Unwrap<Self> + where + Self: Sized + FallibleIterator<Item = T>, + Self::Error: core::fmt::Debug, + { + Unwrap(self) } } @@ -991,7 +981,7 @@ impl<I: DoubleEndedFallibleIterator + ?Sized> DoubleEndedFallibleIterator for &m } } -#[cfg(any(feature = "std", feature = "alloc"))] +#[cfg(feature = "alloc")] impl<I: FallibleIterator + ?Sized> FallibleIterator for Box<I> { type Item = I::Item; type Error = I::Error; @@ -1012,7 +1002,7 @@ impl<I: FallibleIterator + ?Sized> FallibleIterator for Box<I> { } } -#[cfg(any(feature = "std", feature = "alloc"))] +#[cfg(feature = "alloc")] impl<I: DoubleEndedFallibleIterator + ?Sized> DoubleEndedFallibleIterator for Box<I> { #[inline] fn next_back(&mut self) -> Result<Option<I::Item>, I::Error> { @@ -1052,112 +1042,6 @@ pub trait DoubleEndedFallibleIterator: FallibleIterator { } } -/// Conversion from a fallible iterator. -pub trait FromFallibleIterator<T>: Sized { - /// Creates a value from a fallible iterator. - fn from_fallible_iter<I>(it: I) -> Result<Self, I::Error> - where - I: IntoFallibleIterator<Item = T>; -} - -#[cfg(any(feature = "std", feature = "alloc"))] -impl<T> FromFallibleIterator<T> for Vec<T> { - #[inline] - fn from_fallible_iter<I>(it: I) -> Result<Vec<T>, I::Error> - where - I: IntoFallibleIterator<Item = T>, - { - let it = it.into_fallible_iter(); - let mut vec = Vec::with_capacity(it.size_hint().0); - it.for_each(|v| Ok(vec.push(v)))?; - Ok(vec) - } -} - -#[cfg(feature = "std")] -impl<T, S> FromFallibleIterator<T> for HashSet<T, S> -where - T: Hash + Eq, - S: BuildHasher + Default, -{ - #[inline] - fn from_fallible_iter<I>(it: I) -> Result<HashSet<T, S>, I::Error> - where - I: IntoFallibleIterator<Item = T>, - { - let it = it.into_fallible_iter(); - let mut set = HashSet::default(); - set.reserve(it.size_hint().0); - it.for_each(|v| { - set.insert(v); - Ok(()) - })?; - Ok(set) - } -} - -#[cfg(feature = "std")] -impl<K, V, S> FromFallibleIterator<(K, V)> for HashMap<K, V, S> -where - K: Hash + Eq, - S: BuildHasher + Default, -{ - #[inline] - fn from_fallible_iter<I>(it: I) -> Result<HashMap<K, V, S>, I::Error> - where - I: IntoFallibleIterator<Item = (K, V)>, - { - let it = it.into_fallible_iter(); - let mut map = HashMap::default(); - map.reserve(it.size_hint().0); - it.for_each(|(k, v)| { - map.insert(k, v); - Ok(()) - })?; - Ok(map) - } -} - -#[cfg(any(feature = "std", feature = "alloc"))] -impl<T> FromFallibleIterator<T> for BTreeSet<T> -where - T: Ord, -{ - #[inline] - fn from_fallible_iter<I>(it: I) -> Result<BTreeSet<T>, I::Error> - where - I: IntoFallibleIterator<Item = T>, - { - let it = it.into_fallible_iter(); - let mut set = BTreeSet::new(); - it.for_each(|v| { - set.insert(v); - Ok(()) - })?; - Ok(set) - } -} - -#[cfg(any(feature = "std", feature = "alloc"))] -impl<K, V> FromFallibleIterator<(K, V)> for BTreeMap<K, V> -where - K: Ord, -{ - #[inline] - fn from_fallible_iter<I>(it: I) -> Result<BTreeMap<K, V>, I::Error> - where - I: IntoFallibleIterator<Item = (K, V)>, - { - let it = it.into_fallible_iter(); - let mut map = BTreeMap::new(); - it.for_each(|(k, v)| { - map.insert(k, v); - Ok(()) - })?; - Ok(map) - } -} - /// Conversion into a `FallibleIterator`. pub trait IntoFallibleIterator { /// The elements of the iterator. @@ -1189,12 +1073,18 @@ where /// An iterator which applies a fallible transform to the elements of the /// underlying iterator. -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct Map<T, F> { it: T, f: F, } +impl<I: core::fmt::Debug, F> core::fmt::Debug for Map<I, F> { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("Map").field("iter", &self.it).finish() + } +} + impl<T, F, B> FallibleIterator for Map<T, F> where T: FallibleIterator, @@ -1519,6 +1409,62 @@ where } } +/// A fallible iterator that wraps a normal iterator over `Result`s. +#[derive(Clone, Debug)] +pub struct IntoFallible<I>(I); + +impl<T, I> FallibleIterator for IntoFallible<I> +where + I: iter::Iterator<Item = T>, +{ + type Item = T; + type Error = Infallible; + + #[inline] + fn next(&mut self) -> Result<Option<T>, Self::Error> { + Ok(self.0.next()) + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + self.0.size_hint() + } + + #[inline] + fn try_fold<B, E2, F>(&mut self, init: B, f: F) -> Result<B, E2> + where + E2: From<Infallible>, + F: FnMut(B, T) -> Result<B, E2>, + { + self.0.try_fold(init, f) + } +} + +impl<T, I: iter::Iterator<Item = T>> From<I> for IntoFallible<I> { + fn from(value: I) -> Self { + Self(value) + } +} + +impl<T, I> DoubleEndedFallibleIterator for IntoFallible<I> +where + I: DoubleEndedIterator<Item = T>, +{ + #[inline] + fn next_back(&mut self) -> Result<Option<T>, Infallible> { + Ok(self.0.next_back()) + } + + #[inline] + fn try_rfold<B, E2, F>(&mut self, init: B, f: F) -> Result<B, E2> + where + E2: From<Infallible>, + F: FnMut(B, T) -> Result<B, E2>, + { + self.0.try_rfold(init, f) + } +} + /// An iterator that yields the iteration count as well as the values of the /// underlying iterator. #[derive(Clone, Debug)] @@ -1888,6 +1834,52 @@ where } } +/// Creates an iterator from a fallible function generating values. +/// +/// ``` +/// # use fallible_iterator::{from_fn, FallibleIterator}; +/// let mut count = 0; +/// let counter = from_fn(move || { +/// // Increment our count. This is why we started at zero. +/// count += 1; +/// +/// // Check to see if we've finished counting or not. +/// if count < 6 { +/// Ok(Some(count)) +/// } else if count < 7 { +/// Ok(None) +/// } else { +/// Err(()) +/// } +/// }); +/// assert_eq!(&counter.collect::<Vec<_>>().unwrap(), &[1, 2, 3, 4, 5]); +/// ``` +#[inline] +pub fn from_fn<I, E, F>(fun: F) -> FromFn<F> +where + F: FnMut() -> Result<Option<I>, E>, +{ + FromFn { fun } +} + +/// An iterator using a function to generate new values. +#[derive(Clone, Debug)] +pub struct FromFn<F> { + fun: F, +} + +impl<I, E, F> FallibleIterator for FromFn<F> +where + F: FnMut() -> Result<Option<I>, E>, +{ + type Item = I; + type Error = E; + + fn next(&mut self) -> Result<Option<I>, E> { + (self.fun)() + } +} + /// An iterator that yields `Ok(None)` forever after the underlying iterator /// yields `Ok(None)` once. #[derive(Clone, Debug)] @@ -2199,6 +2191,27 @@ where Ok(self.next.as_ref()) } + + /// Consume and return the next value of this iterator if a condition is true. + /// + /// If func returns true for the next value of this iterator, consume and return it. Otherwise, return None. + #[inline] + pub fn next_if(&mut self, f: impl Fn(&I::Item) -> bool) -> Result<Option<I::Item>, I::Error> { + match self.peek()? { + Some(item) if f(item) => self.next(), + _ => Ok(None), + } + } + + /// Consume and return the next item if it is equal to `expected`. + #[inline] + pub fn next_if_eq<T>(&mut self, expected: &T) -> Result<Option<I::Item>, I::Error> + where + T: ?Sized, + I::Item: PartialEq<T>, + { + self.next_if(|found| found == expected) + } } impl<I> FallibleIterator for Peekable<I> @@ -2603,4 +2616,193 @@ where } } +/// An iterator that unwraps every element yielded by the underlying +/// FallibleIterator +#[derive(Clone, Debug)] +pub struct Unwrap<T>(T); + +impl<T> iter::Iterator for Unwrap<T> +where + T: FallibleIterator, + T::Error: core::fmt::Debug, +{ + type Item = T::Item; + + #[inline] + fn next(&mut self) -> Option<T::Item> { + self.0.next().unwrap() + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + let (_, max) = self.0.size_hint(); + (0, max) + } +} + fn _is_object_safe(_: &dyn DoubleEndedFallibleIterator<Item = (), Error = ()>) {} + +/// An extnsion-trait with set of useful methods to convert [`core::iter::Iterator`] +/// into [`FallibleIterator`] +pub trait IteratorExt { + /// Convert an iterator of `Result`s into `FallibleIterator` by transposition + fn transpose_into_fallible<T, E>(self) -> Convert<Self> + where + Self: iter::Iterator<Item = Result<T, E>> + Sized; + + /// Convert an iterator of anything into `FallibleIterator` by wrapping + /// into `Result<T, Infallible>` where `Infallible` is an error that can never actually + /// happen. + fn into_fallible<T>(self) -> IntoFallible<Self> + where + Self: iter::Iterator<Item = T> + Sized; +} + +impl<I> IteratorExt for I +where + I: iter::Iterator, +{ + /// Convert an iterator of `Result`s into `FallibleIterator` by transposition + fn transpose_into_fallible<T, E>(self) -> Convert<Self> + where + Self: iter::Iterator<Item = Result<T, E>> + Sized, + { + Convert(self) + } + + /// Convert an iterator of anything into `FallibleIterator` by wrapping + /// into `Result<T, Infallible>` where `Infallible` is an error that can never actually + /// happen. + fn into_fallible<T>(self) -> IntoFallible<Self> + where + Self: iter::Iterator<Item = T> + Sized, + { + IntoFallible(self) + } +} + +/// An iterator that yields nothing. +#[derive(Clone, Debug)] +pub struct Empty<T, E>(PhantomData<T>, PhantomData<E>); + +impl<T, E> FallibleIterator for Empty<T, E> { + type Item = T; + type Error = E; + + #[inline] + fn next(&mut self) -> Result<Option<T>, E> { + Ok(None) + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + (0, Some(0)) + } +} + +/// Creates an iterator that yields nothing. +pub fn empty<T, E>() -> Empty<T, E> { + Empty(PhantomData, PhantomData) +} + +/// An iterator that yields something exactly once. +#[derive(Clone, Debug)] +pub struct Once<T, E>(Option<T>, PhantomData<E>); + +/// Creates an iterator that yields an element exactly once. +pub fn once<T, E>(value: T) -> Once<T, E> { + Once(Some(value), PhantomData) +} + +impl<T, E> FallibleIterator for Once<T, E> { + type Item = T; + type Error = E; + + #[inline] + fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> { + Ok(self.0.take()) + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + match self.0 { + Some(_) => (1, Some(1)), + None => (0, Some(0)), + } + } +} + +/// An iterator that fails with a predetermined error exactly once. +#[derive(Clone, Debug)] +pub struct OnceErr<T, E>(PhantomData<T>, Option<E>); + +/// Creates an iterator that fails with a predetermined error exactly once. +pub fn once_err<T, E>(value: E) -> OnceErr<T, E> { + OnceErr(PhantomData, Some(value)) +} + +impl<T, E> FallibleIterator for OnceErr<T, E> { + type Item = T; + type Error = E; + + #[inline] + fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> { + match self.1.take() { + Some(value) => Err(value), + None => Ok(None), + } + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + (0, Some(0)) + } +} + +/// An iterator that endlessly repeats a single element. +#[derive(Clone, Debug)] +pub struct Repeat<T: Clone, E>(T, PhantomData<E>); + +/// Creates an iterator that endlessly repeats a single element. +pub fn repeat<T: Clone, E>(value: T) -> Repeat<T, E> { + Repeat(value, PhantomData) +} + +impl<T: Clone, E> FallibleIterator for Repeat<T, E> { + type Item = T; + type Error = E; + + #[inline] + fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> { + Ok(Some(self.0.clone())) + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + (usize::max_value(), None) + } +} + +/// An iterator that endlessly repeats a single error. +#[derive(Clone, Debug)] +pub struct RepeatErr<T, E: Clone>(PhantomData<T>, E); + +/// Creates an iterator that endlessly repeats a single error. +pub fn repeat_err<T, E: Clone>(value: E) -> RepeatErr<T, E> { + RepeatErr(PhantomData, value) +} + +impl<T, E: Clone> FallibleIterator for RepeatErr<T, E> { + type Item = T; + type Error = E; + + #[inline] + fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> { + Err(self.1.clone()) + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + (0, Some(0)) + } +} diff --git a/vendor/fallible-iterator/src/test.rs b/vendor/fallible-iterator/src/test.rs index f7627c4a3..218c6929f 100644 --- a/vendor/fallible-iterator/src/test.rs +++ b/vendor/fallible-iterator/src/test.rs @@ -1,7 +1,7 @@ -use core::iter; -use core::ops::Range; +use alloc::{vec, vec::Vec}; +use core::{iter, ops::Range}; -use super::{convert, FallibleIterator, Vec}; +use super::{convert, FallibleIterator, IntoFallible}; #[test] fn all() { @@ -190,6 +190,8 @@ fn last() { #[test] fn map() { let it = convert(vec![0, 1, 2, 3, 4].into_iter().map(Ok::<u32, ()>)).map(|n| Ok(n * 2)); + fn assert_debug(_: &impl core::fmt::Debug) {} + assert_debug(&it); assert_eq!(it.clone().collect::<Vec<_>>().unwrap(), [0, 2, 4, 6, 8]); assert_eq!(it.rev().collect::<Vec<_>>().unwrap(), [8, 6, 4, 2, 0]); @@ -241,8 +243,7 @@ fn max_by_key() { // Exercise failure both on the first item, and later. assert_eq!(it.clone().max_by_key(|&i| Err::<i32, _>(i)), Err(0)); assert_eq!( - it.clone() - .max_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }), + it.max_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }), Err(3) ); } @@ -266,8 +267,7 @@ fn min_by_key() { // Exercise failure both on the first item, and later. assert_eq!(it.clone().min_by_key(|&i| Err::<i32, _>(i)), Err(0)); assert_eq!( - it.clone() - .min_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }), + it.min_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }), Err(3) ); } @@ -304,15 +304,14 @@ fn position() { assert_eq!(it.position(|n| Ok(n == 3)).unwrap(), Some(0)); assert_eq!(it.position(|n| Ok(n == 5)).unwrap(), None); - let it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::<i32, i32>)); + let mut it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::<i32, i32>)); assert_eq!( it.clone() .position(|n| if n == 3 { Err(42) } else { Ok(n == 2) }), Ok(Some(1)) ); assert_eq!( - it.clone() - .position(|n| if n == 3 { Err(42) } else { Ok(n == 4) }), + it.position(|n| if n == 3 { Err(42) } else { Ok(n == 4) }), Err(42) ); } @@ -335,7 +334,7 @@ fn skip() { let it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::<i32, ()>)); assert_eq!(it.clone().skip(0).collect::<Vec<_>>(), Ok(vec![1, 2, 3, 4])); assert_eq!(it.clone().skip(2).collect::<Vec<_>>(), Ok(vec![3, 4])); - assert_eq!(it.clone().skip(4).collect::<Vec<_>>(), Ok(vec![])); + assert_eq!(it.skip(4).collect::<Vec<_>>(), Ok(vec![])); } #[test] @@ -350,7 +349,7 @@ fn skip_while() { Ok(vec![3, 4, 1]) ); assert_eq!( - it.clone().skip_while(|x| Ok(*x < 5)).collect::<Vec<_>>(), + it.skip_while(|x| Ok(*x < 5)).collect::<Vec<_>>(), Ok(vec![]) ); } @@ -384,7 +383,7 @@ fn take_while() { Ok(vec![0, 1]) ); assert_eq!( - it.clone().take_while(|x| Ok(*x < 4)).collect::<Vec<_>>(), + it.take_while(|x| Ok(*x < 4)).collect::<Vec<_>>(), Ok(vec![0, 1, 2, 3, 0]) ); } @@ -411,7 +410,10 @@ fn flatten() { #[test] fn inspect() { let mut buf = vec![]; - let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::<i32, ()>)).inspect(|v| Ok(buf.push(*v))); + let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::<i32, ()>)).inspect(|&v| { + buf.push(v); + Ok(()) + }); it.count().unwrap(); assert_eq!(buf, vec![0, 1, 2, 3]); } @@ -451,5 +453,25 @@ fn unzip() { #[test] fn cycle() { let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::<i32, ()>)).cycle(); - assert_eq!(it.take(6).clone().collect::<Vec<_>>(), Ok(vec![0, 1, 2, 3, 0, 1])); + assert_eq!(it.take(6).collect::<Vec<_>>(), Ok(vec![0, 1, 2, 3, 0, 1])); +} + +#[test] +fn unwrap() { + let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::<i32, ()>)).unwrap(); + assert_eq!(it.collect::<Vec<_>>(), vec![0, 1, 2, 3]); +} + +#[test] +#[should_panic] +fn unwrap_panic() { + let _ = convert(vec![Ok(0), Err(())].into_iter()) + .unwrap() + .collect::<Vec<_>>(); +} + +#[test] +fn wrap_std_iter_into_fallible() { + let it = IntoFallible::from(vec![0, 1, 2, 3].into_iter()); + assert_eq!(it.collect::<Vec<_>>().unwrap(), vec![0, 1, 2, 3]); } |