Skip to content

Commit 8ef597b

Browse files
committed
Canonicalize special cases of map (3) MapOk via MapSpecialCase
1 parent 0d6bb9d commit 8ef597b

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

src/adaptors/mod.rs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -893,53 +893,32 @@ where
893893
#[deprecated(note="Use MapOk instead", since="0.10")]
894894
pub type MapResults<I, F> = MapOk<I, F>;
895895

896+
#[derive(Clone)]
897+
pub struct MapSpecialCaseFnOk<F>(F);
898+
899+
impl<F, T, U, E> MapSpecialCaseFn<Result<T, E>> for MapSpecialCaseFnOk<F>
900+
where
901+
F: FnMut(T)->U
902+
{
903+
type Out = Result<U, E>;
904+
fn call(&mut self, t: Result<T, E>) -> Self::Out {
905+
t.map(|v| self.0(v))
906+
}
907+
}
908+
896909
/// An iterator adapter to apply a transformation within a nested `Result::Ok`.
897910
///
898911
/// See [`.map_ok()`](../trait.Itertools.html#method.map_ok) for more information.
899-
#[derive(Clone)]
900-
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
901-
pub struct MapOk<I, F> {
902-
iter: I,
903-
f: F
904-
}
912+
pub type MapOk<I, F> = MapSpecialCase<I, MapSpecialCaseFnOk<F>>;
905913

906914
/// Create a new `MapOk` iterator.
907915
pub fn map_ok<I, F, T, U, E>(iter: I, f: F) -> MapOk<I, F>
908916
where I: Iterator<Item = Result<T, E>>,
909917
F: FnMut(T) -> U,
910918
{
911-
MapOk {
919+
MapSpecialCase {
912920
iter,
913-
f,
914-
}
915-
}
916-
917-
impl<I, F, T, U, E> Iterator for MapOk<I, F>
918-
where I: Iterator<Item = Result<T, E>>,
919-
F: FnMut(T) -> U,
920-
{
921-
type Item = Result<U, E>;
922-
923-
fn next(&mut self) -> Option<Self::Item> {
924-
self.iter.next().map(|v| v.map(&mut self.f))
925-
}
926-
927-
fn size_hint(&self) -> (usize, Option<usize>) {
928-
self.iter.size_hint()
929-
}
930-
931-
fn fold<Acc, Fold>(self, init: Acc, mut fold_f: Fold) -> Acc
932-
where Fold: FnMut(Acc, Self::Item) -> Acc,
933-
{
934-
let mut f = self.f;
935-
self.iter.fold(init, move |acc, v| fold_f(acc, v.map(&mut f)))
936-
}
937-
938-
fn collect<C>(self) -> C
939-
where C: FromIterator<Self::Item>
940-
{
941-
let mut f = self.f;
942-
self.iter.map(move |v| v.map(&mut f)).collect()
921+
f: MapSpecialCaseFnOk(f),
943922
}
944923
}
945924

0 commit comments

Comments
 (0)