Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FlattenOk::{fold, rfold} #927

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/flatten_ok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@
}
}

fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
// Front
let mut acc = match self.inner_front {
Some(x) => x.fold(init, |a, o| f(a, Ok(o))),
None => init,
};

acc = self.iter.fold(acc, |acc, x| match x {
Ok(it) => it.into_iter().fold(acc, |a, o| f(a, Ok(o))),
Err(e) => f(acc, Err(e)),
});

// Back
match self.inner_back {
Some(x) => x.fold(acc, |a, o| f(a, Ok(o))),

Check warning on line 93 in src/flatten_ok.rs

View check run for this annotation

Codecov / codecov/patch

src/flatten_ok.rs#L93

Added line #L93 was not covered by tests
None => acc,
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let inner_hint = |inner: &Option<T::IntoIter>| {
inner
Expand Down Expand Up @@ -130,6 +153,29 @@
}
}
}

fn rfold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
// Back
let mut acc = match self.inner_back {
Some(x) => x.rfold(init, |a, o| f(a, Ok(o))),
None => init,
};

acc = self.iter.rfold(acc, |acc, x| match x {
Ok(it) => it.into_iter().rfold(acc, |a, o| f(a, Ok(o))),
Err(e) => f(acc, Err(e)),
});

// Front
match self.inner_front {
Some(x) => x.rfold(acc, |a, o| f(a, Ok(o))),
None => acc,
}
}
}

impl<I, T, E> Clone for FlattenOk<I, T, E>
Expand Down
64 changes: 62 additions & 2 deletions tests/specializations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![allow(unstable_name_collisions)]

use itertools::Itertools;
use quickcheck::Arbitrary;
use quickcheck::{quickcheck, TestResult};
use rand::Rng;
use std::fmt::Debug;

struct Unspecialized<I>(I);
Expand Down Expand Up @@ -452,8 +454,8 @@ quickcheck! {
test_specializations(&v.into_iter().filter_map_ok(|i| if i < 20 { Some(i * 2) } else { None }));
}

// `Option<u8>` because `Vec<u8>` would be very slow!! And we can't give `[u8; 3]`.
fn flatten_ok(v: Vec<Result<Option<u8>, char>>) -> () {
// `SmallIter2<u8>` because `Vec<u8>` is too slow and we get bad coverage from a singleton like Option<u8>
fn flatten_ok(v: Vec<Result<SmallIter2<u8>, char>>) -> () {
let it = v.into_iter().flatten_ok();
test_specializations(&it);
test_double_ended_specializations(&it);
Expand Down Expand Up @@ -520,3 +522,61 @@ quickcheck! {
}
}
}

/// Like `VecIntoIter<T>` with maximum 2 elements.
#[derive(Debug, Clone, Default)]
enum SmallIter2<T> {
#[default]
Zero,
One(T),
Two(T, T),
}

impl<T: Arbitrary> Arbitrary for SmallIter2<T> {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
match g.gen_range(0u8, 3) {
0 => Self::Zero,
1 => Self::One(T::arbitrary(g)),
2 => Self::Two(T::arbitrary(g), T::arbitrary(g)),
_ => unreachable!(),
}
}
// maybe implement shrink too, maybe not
}

impl<T> Iterator for SmallIter2<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
match std::mem::take(self) {
Self::Zero => None,
Self::One(val) => Some(val),
Self::Two(val, second) => {
*self = Self::One(second);
Some(val)
}
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = match self {
Self::Zero => 0,
Self::One(_) => 1,
Self::Two(_, _) => 2,
};
(len, Some(len))
}
}

impl<T> DoubleEndedIterator for SmallIter2<T> {
fn next_back(&mut self) -> Option<Self::Item> {
match std::mem::take(self) {
Self::Zero => None,
Self::One(val) => Some(val),
Self::Two(first, val) => {
*self = Self::One(first);
Some(val)
}
}
}
}