//! Fallible, streaming iteration. //! //! `FallibleStreamingIterator` differs from the standard library's `Iterator` trait in two ways: //! iteration can fail, resulting in an error, and only one element of the iteration is available at //! any time. //! //! While these iterators cannot be used with Rust `for` loops, `while let` loops offer a similar //! level of ergonomics: //! //! ```ignore //! while let Some(value) = it.next()? { //! // use value //! } //! ``` #![doc(html_root_url = "https://docs.rs/fallible-streaming-iterator/0.1")] #![warn(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")] externcrate core;
use core::cmp; use core::marker::PhantomData;
/// A fallible, streaming iterator. pubtrait FallibleStreamingIterator { /// The type being iterated over. type Item: ?Sized;
/// The error type of iteration. type Error;
/// Advances the iterator to the next position. /// /// Iterators start just before the first item, so this method should be called before `get` /// when iterating. /// /// The behavior of calling this method after `get` has returned `None`, or after this method /// has returned an error is unspecified. fn advance(&mutself) -> Result<(), Self::Error>;
/// Returns the current element. /// /// The behavior of calling this method before any calls to `advance` is unspecified. fn get(&self) -> Option<&Self::Item>;
/// Advances the iterator, returning the next element. /// /// The default implementation simply calls `advance` followed by `get`. #[inline] fn next(&mutself) -> Result<Option<&Self::Item>, Self::Error> { self.advance()?;
Ok((*self).get())
}
/// Returns bounds on the number of remaining elements in the iterator. #[inline] fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
/// Determines if all elements of the iterator satisfy a predicate. #[inline] fn all<F>(&mutself, mut f: F) -> Result<bool, Self::Error> where Self: Sized,
F: FnMut(&Self::Item) -> bool,
{ whilelet Some(e) = self.next()? { if !f(e) { return Ok(false);
}
}
Ok(true)
}
/// Determines if any elements of the iterator satisfy a predicate. #[inline] fn any<F>(&mutself, mut f: F) -> Result<bool, Self::Error> where Self: Sized,
F: FnMut(&Self::Item) -> bool,
{ self.all(|e| !f(e)).map(|r| !r)
}
/// Borrows an iterator, rather than consuming it. /// /// This is useful to allow the application of iterator adaptors while still retaining ownership /// of the original adaptor. #[inline] fn by_ref(&mutself) -> &mutSelf where Self: Sized,
{ self
}
/// Returns the number of remaining elements in the iterator. #[inline] fn count(mutself) -> Result<usize, Self::Error> where Self: Sized,
{ letmut count = 0; whilelet Some(_) = self.next()? {
count += 1;
}
Ok(count)
}
/// Returns an iterator which filters elements by a predicate. #[inline] fn filter<F>(self, f: F) -> Filter<Self, F> where Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
Filter { it: self, f: f }
}
/// Returns the first element of the iterator which satisfies a predicate. #[inline] fn find<F>(&mutself, mut f: F) -> Result<Option<&Self::Item>, Self::Error> where Self: Sized,
F: FnMut(&Self::Item) -> bool,
{ loop { self.advance()?; matchself.get() {
Some(v) => { if f(v) { break;
}
}
None => break,
}
}
Ok((*self).get())
}
/// Calls a closure on each element of an iterator. #[inline] fn for_each<F>(mutself, mut f: F) -> Result<(), Self::Error> where Self: Sized,
F: FnMut(&Self::Item),
{ whilelet Some(value) = self.next()? {
f(value);
}
Ok(())
}
/// Returns an iterator which is well-behaved at the beginning and end of iteration. #[inline] fn fuse(self) -> Fuse<Self> where Self: Sized,
{
Fuse {
it: self,
state: FuseState::Start,
}
}
/// Returns an iterator which applies a transform to elements. #[inline] fn map<F, B>(self, f: F) -> Map<Self, F, B> where Self: Sized,
F: FnMut(&Self::Item) -> B,
{
Map {
it: self,
f: f,
value: None,
}
}
/// Returns an iterator which applies a transform to elements. /// /// Unlike `map`, the the closure provided to this method returns a reference into the original /// value. #[inline] fn map_ref<F, B: ?Sized>(self, f: F) -> MapRef<Self, F> where Self: Sized,
F: Fn(&Self::Item) -> &B,
{
MapRef { it: self, f: f }
}
/// Returns an iterator that applies a transform to errors. #[inline] fn map_err<F, B>(self, f: F) -> MapErr<Self, F> where Self: Sized,
F: Fn(Self::Error) -> B,
{
MapErr { it: self, f: f }
}
/// Returns the `nth` element of the iterator. #[inline] fn nth(&mutself, n: usize) -> Result<Option<&>Self::Item>, Self::Error> { for _ in0..n { self.advance()?; iflet None = self.get() { return Ok(None);
}
} self.next()
}
/// Returns the position of the first element matching a predicate. #[inline] fn position<F>(&mutself, mut f: F) -> Result<Option<usize>, Self::Error> where Self: Sized,
F: FnMut(&Self::Item) -> bool,
{ letmut pos = 0; whilelet Some(v) = self.next()? { if f(v) { return Ok(Some(pos));
}
pos += 1;
}
Ok(None)
}
/// Returns an iterator which skips the first `n` elements. #[inline] fn skip(self, n: usize) -> Skip<Self> where Self: Sized,
{
Skip { it: self, n: n }
}
/// Returns an iterator which skips the first sequence of elements matching a predicate. #[inline] fn skip_while<F>(self, f: F) -> SkipWhile<Self, F> where Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
SkipWhile {
it: self,
f: f,
done: false,
}
}
/// Returns an iterator which only returns the first `n` elements. #[inline] fn take(self, n: usize) -> Take<Self> where Self: Sized,
{
Take {
it: self,
n: n,
done: false,
}
}
/// Returns an iterator which only returns the first sequence of elements matching a predicate. #[inline] fn take_while<F>(self, f: F) -> TakeWhile<Self, F> where Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
TakeWhile {
it: self,
f: f,
done: false,
}
}
}
/// A fallible, streaming iterator which can be advanced from either end. pubtrait DoubleEndedFallibleStreamingIterator: FallibleStreamingIterator { /// Advances the state of the iterator to the next item from the end. /// /// Iterators start just after the last item, so this method should be called before `get` /// when iterating. /// /// The behavior of calling this method after `get` has returned `None`, or after this method /// or `advance` has returned an error is unspecified. fn advance_back(&mutself) -> Result<(), Self::Error>;
/// Advances the back of the iterator, returning the last element. /// /// The default implementation simply calls `advance_back` followed by `get`. #[inline] fn next_back(&mutself) -> Result<Option<&Self::Item>, Self::Error> { self.advance_back()?;
Ok((*self).get())
}
}
impl<'a, I: ?Sized> FallibleStreamingIterator for &'a mut I where
I: FallibleStreamingIterator,
{ type Item = I::Item; type Error = I::Error;
/// Converts a normal `Iterator` over `Results` of references into a /// `FallibleStreamingIterator`. pubfn convert<'a, I, T, E>(it: I) -> Convert<'a, I, T> where
I: Iterator<Item = Result<&'a T, E>>,
{
Convert { it: it, item: None }
}
/// An iterator which wraps a normal `Iterator`. pubstruct Convert<'a, I, T: 'a> {
it: I,
item: Option<&'a T>,
}
impl<'a, I, T, E> FallibleStreamingIterator for Convert<'a, I, T> where
I: Iterator<Item = Result<&'a T, E>>,
{ type Item = T; type Error = E;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.