1
Fork 0

Rollup merge of #110819 - tamird:flattencompat-trustedlen, r=the8472

simplify TrustedLen impls

Implement on FlattenCompat and delegate from Flatten and FlatMap.

/cc ``@the8472``
This commit is contained in:
Matthias Krüger 2023-04-26 18:51:43 +02:00 committed by GitHub
commit 8fe7a4937c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -136,26 +136,12 @@ where
} }
#[unstable(feature = "trusted_len", issue = "37572")] #[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<T, I, F, const N: usize> TrustedLen for FlatMap<I, [T; N], F> unsafe impl<I, U, F> TrustedLen for FlatMap<I, U, F>
where where
I: TrustedLen, I: Iterator,
F: FnMut(I::Item) -> [T; N], U: IntoIterator,
{ F: FnMut(I::Item) -> U,
} FlattenCompat<Map<I, F>, <U as IntoIterator>::IntoIter>: TrustedLen,
#[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],
{ {
} }
@ -298,8 +284,8 @@ where
#[unstable(feature = "trusted_len", issue = "37572")] #[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<I> TrustedLen for Flatten<I> unsafe impl<I> TrustedLen for Flatten<I>
where where
I: TrustedLen, I: Iterator<Item: IntoIterator>,
<I as Iterator>::Item: TrustedConstSize, FlattenCompat<I, <I::Item as IntoIterator>::IntoIter>: TrustedLen,
{ {
} }
@ -660,6 +646,27 @@ where
} }
} }
unsafe impl<const N: usize, I, T> TrustedLen
for FlattenCompat<I, <[T; N] as IntoIterator>::IntoIter>
where
I: TrustedLen<Item = [T; N]>,
{
}
unsafe impl<'a, const N: usize, I, T> TrustedLen
for FlattenCompat<I, <&'a [T; N] as IntoIterator>::IntoIter>
where
I: TrustedLen<Item = &'a [T; N]>,
{
}
unsafe impl<'a, const N: usize, I, T> TrustedLen
for FlattenCompat<I, <&'a mut [T; N] as IntoIterator>::IntoIter>
where
I: TrustedLen<Item = &'a mut [T; N]>,
{
}
trait ConstSizeIntoIterator: IntoIterator { trait ConstSizeIntoIterator: IntoIterator {
// FIXME(#31844): convert to an associated const once specialization supports that // FIXME(#31844): convert to an associated const once specialization supports that
fn size() -> Option<usize>; fn size() -> Option<usize>;
@ -696,19 +703,6 @@ impl<T, const N: usize> ConstSizeIntoIterator for &mut [T; 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 {}
#[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] {}
#[inline] #[inline]
fn and_then_or_clear<T, U>(opt: &mut Option<T>, f: impl FnOnce(&mut T) -> Option<U>) -> Option<U> { fn and_then_or_clear<T, U>(opt: &mut Option<T>, f: impl FnOnce(&mut T) -> Option<U>) -> Option<U> {
let x = f(opt.as_mut()?); let x = f(opt.as_mut()?);