From 80638330f29264591ffea866c807851093abb2fd Mon Sep 17 00:00:00 2001 From: The8472 Date: Sat, 4 Apr 2020 18:34:18 +0200 Subject: [PATCH] support in-place collect for MapWhile adapters --- library/alloc/tests/lib.rs | 1 + library/alloc/tests/vec.rs | 1 + library/core/src/iter/adapters/mod.rs | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/library/alloc/tests/lib.rs b/library/alloc/tests/lib.rs index e0e146dc427..b1513d1b056 100644 --- a/library/alloc/tests/lib.rs +++ b/library/alloc/tests/lib.rs @@ -15,6 +15,7 @@ #![feature(split_inclusive)] #![feature(binary_heap_retain)] #![feature(inplace_iteration)] +#![feature(iter_map_while)] use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 3464b751181..47819714cc4 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -819,6 +819,7 @@ fn test_from_iter_specialization_with_iterator_adapters() { .map(|i| i.0 + i.1) .zip(std::iter::repeat(1usize)) .map(|(a, b)| a + b) + .map_while(Option::Some) .peekable() .skip(1) .map(|e| std::num::NonZeroUsize::new(e)); diff --git a/library/core/src/iter/adapters/mod.rs b/library/core/src/iter/adapters/mod.rs index 6013a2f111f..93e0ad3c8ea 100644 --- a/library/core/src/iter/adapters/mod.rs +++ b/library/core/src/iter/adapters/mod.rs @@ -2191,6 +2191,27 @@ where } } +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl SourceIter for MapWhile +where + P: FnMut(I::Item) -> Option, + I: SourceIter, +{ + type Source = S; + + #[inline] + unsafe fn as_inner(&mut self) -> &mut S { + // Safety: unsafe function forwarding to unsafe function with the same requirements + unsafe { SourceIter::as_inner(&mut self.iter) } + } +} + +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl InPlaceIterable for MapWhile where + P: FnMut(I::Item) -> Option +{ +} + /// An iterator that skips over `n` elements of `iter`. /// /// This `struct` is created by the [`skip`] method on [`Iterator`]. See its