-
Notifications
You must be signed in to change notification settings - Fork 13.4k
implement TrustedLen for Flatten/FlatMap if the U: IntoIterator == [T; N] #87168
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
Changes from all commits
bd1c39d
18a034f
8dd903c
c3ac8d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use crate::fmt; | ||
use crate::iter::{DoubleEndedIterator, Fuse, FusedIterator, Iterator, Map}; | ||
use crate::iter::{DoubleEndedIterator, Fuse, FusedIterator, Iterator, Map, TrustedLen}; | ||
use crate::ops::Try; | ||
|
||
/// An iterator that maps each element to an iterator, and yields the elements | ||
|
@@ -114,6 +114,30 @@ where | |
{ | ||
} | ||
|
||
#[unstable(feature = "trusted_len", issue = "37572")] | ||
unsafe impl<T, I, F, const N: usize> TrustedLen for FlatMap<I, [T; N], F> | ||
where | ||
I: TrustedLen, | ||
F: FnMut(I::Item) -> [T; N], | ||
{ | ||
} | ||
|
||
#[unstable(feature = "trusted_len", issue = "37572")] | ||
unsafe impl<'a, T, I, F, const N: usize> TrustedLen for FlatMap<I, &'a [T; N], F> | ||
where | ||
I: TrustedLen, | ||
F: FnMut(I::Item) -> &'a [T; N], | ||
{ | ||
} | ||
|
||
#[unstable(feature = "trusted_len", issue = "37572")] | ||
unsafe impl<'a, T, I, F, const N: usize> TrustedLen for FlatMap<I, &'a mut [T; N], F> | ||
where | ||
I: TrustedLen, | ||
F: FnMut(I::Item) -> &'a mut [T; N], | ||
{ | ||
} | ||
|
||
/// An iterator that flattens one level of nesting in an iterator of things | ||
/// that can be turned into iterators. | ||
/// | ||
|
@@ -230,6 +254,14 @@ where | |
{ | ||
} | ||
|
||
#[unstable(feature = "trusted_len", issue = "37572")] | ||
unsafe impl<I> TrustedLen for Flatten<I> | ||
where | ||
I: TrustedLen, | ||
<I as Iterator>::Item: TrustedConstSize, | ||
{ | ||
} | ||
|
||
/// Real logic of both `Flatten` and `FlatMap` which simply delegate to | ||
/// this type. | ||
#[derive(Clone, Debug)] | ||
|
@@ -282,6 +314,17 @@ where | |
let (flo, fhi) = self.frontiter.as_ref().map_or((0, Some(0)), U::size_hint); | ||
let (blo, bhi) = self.backiter.as_ref().map_or((0, Some(0)), U::size_hint); | ||
let lo = flo.saturating_add(blo); | ||
|
||
if let Some(fixed_size) = <<I as Iterator>::Item as ConstSizeIntoIterator>::size() { | ||
let (lower, upper) = self.iter.size_hint(); | ||
|
||
let lower = lower.saturating_mul(fixed_size).saturating_add(lo); | ||
let upper = | ||
try { fhi?.checked_add(bhi?)?.checked_add(fixed_size.checked_mul(upper?)?)? }; | ||
|
||
return (lower, upper); | ||
} | ||
|
||
match (self.iter.size_hint(), fhi, bhi) { | ||
((0, Some(0)), Some(a), Some(b)) => (lo, a.checked_add(b)), | ||
_ => (lo, None), | ||
|
@@ -444,3 +487,52 @@ where | |
init | ||
} | ||
} | ||
|
||
trait ConstSizeIntoIterator: IntoIterator { | ||
// FIXME(#31844): convert to an associated const once specialization supports that | ||
fn size() -> Option<usize>; | ||
} | ||
|
||
impl<T> ConstSizeIntoIterator for T | ||
where | ||
T: IntoIterator, | ||
{ | ||
#[inline] | ||
default fn size() -> Option<usize> { | ||
None | ||
} | ||
} | ||
|
||
impl<T, const N: usize> ConstSizeIntoIterator for [T; N] { | ||
#[inline] | ||
fn size() -> Option<usize> { | ||
Some(N) | ||
} | ||
} | ||
|
||
impl<T, const N: usize> ConstSizeIntoIterator for &[T; N] { | ||
#[inline] | ||
fn size() -> Option<usize> { | ||
Some(N) | ||
} | ||
} | ||
|
||
impl<T, const N: usize> ConstSizeIntoIterator for &mut [T; N] { | ||
#[inline] | ||
fn size() -> Option<usize> { | ||
Some(N) | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
#[unstable(feature = "std_internals", issue = "none")] | ||
// FIXME(#20400): Instead of this helper trait there should be multiple impl TrustedLen for Flatten<> | ||
// blocks with different bounds on Iterator::Item but the compiler erroneously considers them overlapping | ||
pub unsafe trait TrustedConstSize: IntoIterator {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I would have expected something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
#[unstable(feature = "std_internals", issue = "none")] | ||
unsafe impl<T, const N: usize> TrustedConstSize for [T; N] {} | ||
#[unstable(feature = "std_internals", issue = "none")] | ||
unsafe impl<T, const N: usize> TrustedConstSize for &'_ [T; N] {} | ||
#[unstable(feature = "std_internals", issue = "none")] | ||
unsafe impl<T, const N: usize> TrustedConstSize for &'_ mut [T; N] {} |
Uh oh!
There was an error while loading. Please reload this page.