Skip to content

Commit

Permalink
Merge pull request #36 from dpc/fix-fix
Browse files Browse the repository at this point in the history
 add convenient wrapper for std::iter::Iterator
  • Loading branch information
dpc committed May 12, 2023
2 parents 3f081ec + fa17b60 commit f181098
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
44 changes: 23 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
#![no_std]

use core::cmp::{self, Ordering};
use core::convert::Infallible;
use core::iter;
use core::marker::PhantomData;


#[cfg(feature = "alloc")]
extern crate alloc;

Expand Down Expand Up @@ -1409,7 +1409,6 @@ where
}
}


/// A fallible iterator that wraps a normal iterator over `Result`s.
#[derive(Clone, Debug)]
pub struct IntoFallible<I>(I);
Expand All @@ -1419,7 +1418,7 @@ where
I: iter::Iterator<Item = T>,
{
type Item = T;
type Error = Never;
type Error = Infallible;

#[inline]
fn next(&mut self) -> Result<Option<T>, Self::Error> {
Expand All @@ -1432,12 +1431,18 @@ where
}

#[inline]
fn try_fold<B, E2, F>(&mut self, init: B, mut f: F) -> Result<B, E2>
fn try_fold<B, E2, F>(&mut self, init: B, f: F) -> Result<B, E2>
where
E2: From<Never>,
E2: From<Infallible>,
F: FnMut(B, T) -> Result<B, E2>,
{
self.0.try_fold(init, |acc, v| f(acc, v))
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)
}
}

Expand All @@ -1446,21 +1451,20 @@ where
I: DoubleEndedIterator<Item = T>,
{
#[inline]
fn next_back(&mut self) -> Result<Option<T>, Never> {
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, mut f: F) -> Result<B, E2>
fn try_rfold<B, E2, F>(&mut self, init: B, f: F) -> Result<B, E2>
where
E2: From<Never>,
E2: From<Infallible>,
F: FnMut(B, T) -> Result<B, E2>,
{
self.0.try_rfold(init, |acc, v| f(acc, v))
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)]
Expand Down Expand Up @@ -2638,28 +2642,26 @@ where

fn _is_object_safe(_: &dyn DoubleEndedFallibleIterator<Item = (), Error = ()>) {}

/// An error that can't ever happen. Might be replaced with `!`
/// when stabilized
pub enum Never {}

/// 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, Never>` where `Never` is an error that can never actually
/// 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 {
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
Expand All @@ -2669,7 +2671,7 @@ impl<I> IteratorExt for I where I : iter::Iterator {
}

/// Convert an iterator of anything into `FallibleIterator` by wrapping
/// into `Result<T, Never>` where `Never` is an error that can never actually
/// into `Result<T, Infallible>` where `Infallible` is an error that can never actually
/// happen.
fn into_fallible<T>(self) -> IntoFallible<Self>
where
Expand Down Expand Up @@ -2742,7 +2744,7 @@ pub fn once_err<T, E>(value: E) -> OnceErr<T, E> {
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() {
Expand Down Expand Up @@ -2803,4 +2805,4 @@ impl<T, E: Clone> FallibleIterator for RepeatErr<T, E> {
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(0))
}
}
}
8 changes: 7 additions & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::{vec, vec::Vec};
use core::{iter, ops::Range};

use super::{convert, FallibleIterator};
use super::{convert, FallibleIterator, IntoFallible};

#[test]
fn all() {
Expand Down Expand Up @@ -469,3 +469,9 @@ fn unwrap_panic() {
.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]);
}

0 comments on commit f181098

Please sign in to comment.