diff options
| author | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-20 14:34:34 +0200 |
|---|---|---|
| committer | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-20 14:40:09 +0200 |
| commit | 69b9400b796f545226415feae36b9ea4f8cc70c0 (patch) | |
| tree | f37e16a590159d2e8ecdff491c8ea565a258f4c1 | |
| parent | 49557112d64f5877093bfe43acaf5dc8d5b947a7 (diff) | |
| download | rust-69b9400b796f545226415feae36b9ea4f8cc70c0.tar.gz rust-69b9400b796f545226415feae36b9ea4f8cc70c0.zip | |
Implement TrustedLen for more iterators
| -rw-r--r-- | src/libcore/iter/range.rs | 26 | ||||
| -rw-r--r-- | src/libcore/option.rs | 11 | ||||
| -rw-r--r-- | src/libcore/result.rs | 11 |
3 files changed, 43 insertions, 5 deletions
diff --git a/src/libcore/iter/range.rs b/src/libcore/iter/range.rs index 39da578d549..ab55ee9d9d7 100644 --- a/src/libcore/iter/range.rs +++ b/src/libcore/iter/range.rs @@ -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> diff --git a/src/libcore/option.rs b/src/libcore/option.rs index cb18feff734..28845ef5e8c 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -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 ///////////////////////////////////////////////////////////////////////////// diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 96845259299..8985e7c8251 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -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 ///////////////////////////////////////////////////////////////////////////// |
