Implement TrustedLen for more iterators

This commit is contained in:
Ulrik Sverdrup 2016-10-20 14:34:34 +02:00
parent 49557112d6
commit 69b9400b79
3 changed files with 43 additions and 5 deletions

View File

@ -480,6 +480,22 @@ macro_rules! range_incl_exact_iter_impl {
)*)
}
macro_rules! range_trusted_len_impl {
($($t:ty)*) => ($(
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl TrustedLen for ops::Range<$t> { }
)*)
}
macro_rules! range_incl_trusted_len_impl {
($($t:ty)*) => ($(
#[unstable(feature = "inclusive_range",
reason = "recently added, follows RFC",
issue = "28237")]
unsafe impl TrustedLen for ops::RangeInclusive<$t> { }
)*)
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: Step> Iterator for ops::Range<A> where
for<'a> &'a A: Add<&'a A, Output = A>
@ -513,6 +529,13 @@ impl<A: Step> Iterator for ops::Range<A> where
range_exact_iter_impl!(usize u8 u16 u32 isize i8 i16 i32);
range_incl_exact_iter_impl!(u8 u16 i8 i16);
// These macros generate `TrustedLen` impls.
//
// They need to guarantee that .size_hint() is either exact, or that
// the upper bound is None when it does not fit the type limits.
range_trusted_len_impl!(usize isize u8 i8 u16 i16 u32 i32 i64 u64);
range_incl_trusted_len_impl!(usize isize u8 i8 u16 i16 u32 i32 i64 u64);
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: Step + Clone> DoubleEndedIterator for ops::Range<A> where
for<'a> &'a A: Add<&'a A, Output = A>,
@ -533,9 +556,6 @@ impl<A: Step + Clone> DoubleEndedIterator for ops::Range<A> where
impl<A> FusedIterator for ops::Range<A>
where A: Step, for<'a> &'a A: Add<&'a A, Output = A> {}
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl TrustedLen for ops::Range<usize> { }
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: Step> Iterator for ops::RangeFrom<A> where
for<'a> &'a A: Add<&'a A, Output = A>

View File

@ -145,7 +145,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
use iter::{FromIterator, FusedIterator};
use iter::{FromIterator, FusedIterator, TrustedLen};
use mem;
// Note that this is not a lang item per se, but it has a hidden dependency on
@ -803,6 +803,7 @@ impl<A> DoubleEndedIterator for Item<A> {
impl<A> ExactSizeIterator for Item<A> {}
impl<A> FusedIterator for Item<A> {}
unsafe impl<A> TrustedLen for Item<A> {}
/// An iterator over a reference of the contained item in an [`Option`].
///
@ -833,6 +834,9 @@ impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, A> FusedIterator for Iter<'a, A> {}
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl<'a, A> TrustedLen for Iter<'a, A> {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, A> Clone for Iter<'a, A> {
fn clone(&self) -> Iter<'a, A> {
@ -868,6 +872,8 @@ impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, A> FusedIterator for IterMut<'a, A> {}
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl<'a, A> TrustedLen for IterMut<'a, A> {}
/// An iterator over the item contained inside an [`Option`].
///
@ -898,6 +904,9 @@ impl<A> ExactSizeIterator for IntoIter<A> {}
#[unstable(feature = "fused", issue = "35602")]
impl<A> FusedIterator for IntoIter<A> {}
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl<A> TrustedLen for IntoIter<A> {}
/////////////////////////////////////////////////////////////////////////////
// FromIterator
/////////////////////////////////////////////////////////////////////////////

View File

@ -249,7 +249,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
use fmt;
use iter::{FromIterator, FusedIterator};
use iter::{FromIterator, FusedIterator, TrustedLen};
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
///
@ -886,6 +886,9 @@ impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for Iter<'a, T> {}
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl<'a, A> TrustedLen for Iter<'a, A> {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Iter<'a, T> { Iter { inner: self.inner } }
@ -924,6 +927,9 @@ impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for IterMut<'a, T> {}
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl<'a, A> TrustedLen for IterMut<'a, A> {}
/// An iterator over the value in a [`Ok`] variant of a [`Result`]. This struct is
/// created by the [`into_iter`] method on [`Result`][`Result`] (provided by
/// the [`IntoIterator`] trait).
@ -961,6 +967,9 @@ impl<T> ExactSizeIterator for IntoIter<T> {}
#[unstable(feature = "fused", issue = "35602")]
impl<T> FusedIterator for IntoIter<T> {}
#[unstable(feature = "trusted_len", issue = "0")]
unsafe impl<A> TrustedLen for IntoIter<A> {}
/////////////////////////////////////////////////////////////////////////////
// FromIterator
/////////////////////////////////////////////////////////////////////////////