From 439f63019fccbbdcc00074856f67ce6af2abcac2 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Sat, 29 Apr 2023 15:53:26 +0200 Subject: [PATCH] support in-place collecting additional FlatMap shapes --- library/alloc/tests/vec.rs | 11 ++++ library/core/src/iter/adapters/flatten.rs | 74 ++++++++++++++++++----- 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 33dd2658e1e5..c545f47a5d15 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1212,6 +1212,17 @@ fn test_in_place_specialization_step_up_down() { let sink_bytes = sink.capacity() * 4; assert_ne!(src_bytes, sink_bytes); assert_eq!(sink.len(), 2); + + let src = vec![[0u8; 4]; 256]; + let srcptr = src.as_ptr(); + let iter = src + .into_iter() + .flat_map(|a| { + a.into_iter().map(|b| b.wrapping_add(1)) + }); + assert_in_place_trait(&iter); + let sink = iter.collect::>(); + assert_eq!(srcptr as *const u8, sink.as_ptr()); } #[test] diff --git a/library/core/src/iter/adapters/flatten.rs b/library/core/src/iter/adapters/flatten.rs index 60d5418716b9..09428350fd92 100644 --- a/library/core/src/iter/adapters/flatten.rs +++ b/library/core/src/iter/adapters/flatten.rs @@ -1,12 +1,13 @@ use crate::iter::adapters::SourceIter; use crate::iter::{ - DoubleEndedIterator, Fuse, FusedIterator, InPlaceIterable, Iterator, Map, TrustedFused, - TrustedLen, + Cloned, Copied, DoubleEndedIterator, Filter, FilterMap, Fuse, FusedIterator, InPlaceIterable, + Iterator, Map, TrustedFused, TrustedLen, }; +use crate::iter::{Once, OnceWith}; use crate::num::NonZeroUsize; use crate::ops::{ControlFlow, Try}; -use crate::{fmt, option}; -use core::iter::Once; +use crate::result; +use crate::{array, fmt, option}; /// An iterator that maps each element to an iterator, and yields the elements /// of the produced iterators. @@ -154,10 +155,10 @@ where unsafe impl InPlaceIterable for FlatMap where I: InPlaceIterable, - U: KnownExpansionFactor + IntoIterator, + U: BoundedSize + IntoIterator, { const EXPAND_BY: Option = const { - match (I::EXPAND_BY, U::FACTOR) { + match (I::EXPAND_BY, U::UPPER_BOUND) { (Some(m), Some(n)) => m.checked_mul(n), _ => None, } @@ -180,16 +181,59 @@ where } } +/// Marker trait for iterators/iterables which have a statically known upper +/// bound of the number of items they can produce. +/// +/// # Safety +/// +/// Implementations must not yield more elements than indicated by UPPER_BOUND if it is `Some`. +/// Used in specializations. Implementations must not be conditional on lifetimes or +/// user-implementable traits. #[rustc_specialization_trait] -trait KnownExpansionFactor { - const FACTOR: Option = NonZeroUsize::new(1); +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe trait BoundedSize { + const UPPER_BOUND: Option = NonZeroUsize::new(1); } -impl KnownExpansionFactor for Option {} -impl KnownExpansionFactor for option::IntoIter {} -impl KnownExpansionFactor for Once {} -impl KnownExpansionFactor for [T; N] { - const FACTOR: Option = NonZeroUsize::new(N); +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for Option {} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for option::IntoIter {} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for Result {} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for result::IntoIter {} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for Once {} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for OnceWith {} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for [T; N] { + const UPPER_BOUND: Option = NonZeroUsize::new(N); +} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for array::IntoIter { + const UPPER_BOUND: Option = NonZeroUsize::new(N); +} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for Filter { + const UPPER_BOUND: Option = I::UPPER_BOUND; +} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for FilterMap { + const UPPER_BOUND: Option = I::UPPER_BOUND; +} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for Map { + const UPPER_BOUND: Option = I::UPPER_BOUND; +} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for Copied { + const UPPER_BOUND: Option = I::UPPER_BOUND; +} +#[unstable(issue = "none", feature = "inplace_iteration")] +unsafe impl BoundedSize for Cloned { + const UPPER_BOUND: Option = I::UPPER_BOUND; } /// An iterator that flattens one level of nesting in an iterator of things @@ -340,10 +384,10 @@ where unsafe impl InPlaceIterable for Flatten where I: InPlaceIterable + Iterator, - ::Item: IntoIterator + KnownExpansionFactor, + ::Item: IntoIterator + BoundedSize, { const EXPAND_BY: Option = const { - match (I::EXPAND_BY, I::Item::FACTOR) { + match (I::EXPAND_BY, I::Item::UPPER_BOUND) { (Some(m), Some(n)) => m.checked_mul(n), _ => None, }