Skip to content

Commit 9228f3c

Browse files
author
Clar Fon
committed
Move FusedIterator, TrustedLen to own module
1 parent 4a03614 commit 9228f3c

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

src/libcore/iter/traits/marker.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// An iterator that always continues to yield `None` when exhausted.
2+
///
3+
/// Calling next on a fused iterator that has returned `None` once is guaranteed
4+
/// to return [`None`] again. This trait should be implemented by all iterators
5+
/// that behave this way because it allows optimizing [`Iterator::fuse`].
6+
///
7+
/// Note: In general, you should not use `FusedIterator` in generic bounds if
8+
/// you need a fused iterator. Instead, you should just call [`Iterator::fuse`]
9+
/// on the iterator. If the iterator is already fused, the additional [`Fuse`]
10+
/// wrapper will be a no-op with no performance penalty.
11+
///
12+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
13+
/// [`Iterator::fuse`]: ../../std/iter/trait.Iterator.html#method.fuse
14+
/// [`Fuse`]: ../../std/iter/struct.Fuse.html
15+
#[stable(feature = "fused", since = "1.26.0")]
16+
pub trait FusedIterator: Iterator {}
17+
18+
#[stable(feature = "fused", since = "1.26.0")]
19+
impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
20+
21+
/// An iterator that reports an accurate length using size_hint.
22+
///
23+
/// The iterator reports a size hint where it is either exact
24+
/// (lower bound is equal to upper bound), or the upper bound is [`None`].
25+
/// The upper bound must only be [`None`] if the actual iterator length is
26+
/// larger than [`usize::MAX`]. In that case, the lower bound must be
27+
/// [`usize::MAX`], resulting in a [`.size_hint`] of `(usize::MAX, None)`.
28+
///
29+
/// The iterator must produce exactly the number of elements it reported
30+
/// or diverge before reaching the end.
31+
///
32+
/// # Safety
33+
///
34+
/// This trait must only be implemented when the contract is upheld.
35+
/// Consumers of this trait must inspect [`.size_hint`]’s upper bound.
36+
///
37+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
38+
/// [`usize::MAX`]: ../../std/usize/constant.MAX.html
39+
/// [`.size_hint`]: ../../std/iter/trait.Iterator.html#method.size_hint
40+
#[unstable(feature = "trusted_len", issue = "37572")]
41+
pub unsafe trait TrustedLen : Iterator {}
42+
43+
#[unstable(feature = "trusted_len", issue = "37572")]
44+
unsafe impl<I: TrustedLen + ?Sized> TrustedLen for &mut I {}

src/libcore/iter/traits/mod.rs

+2-45
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,11 @@ mod double_ended;
33
mod exact_size;
44
mod collect;
55
mod accum;
6+
mod marker;
67

78
pub use self::iterator::Iterator;
89
pub use self::double_ended::DoubleEndedIterator;
910
pub use self::exact_size::ExactSizeIterator;
1011
pub use self::collect::{FromIterator, IntoIterator, Extend};
1112
pub use self::accum::{Sum, Product};
12-
13-
/// An iterator that always continues to yield `None` when exhausted.
14-
///
15-
/// Calling next on a fused iterator that has returned `None` once is guaranteed
16-
/// to return [`None`] again. This trait should be implemented by all iterators
17-
/// that behave this way because it allows optimizing [`Iterator::fuse`].
18-
///
19-
/// Note: In general, you should not use `FusedIterator` in generic bounds if
20-
/// you need a fused iterator. Instead, you should just call [`Iterator::fuse`]
21-
/// on the iterator. If the iterator is already fused, the additional [`Fuse`]
22-
/// wrapper will be a no-op with no performance penalty.
23-
///
24-
/// [`None`]: ../../std/option/enum.Option.html#variant.None
25-
/// [`Iterator::fuse`]: ../../std/iter/trait.Iterator.html#method.fuse
26-
/// [`Fuse`]: ../../std/iter/struct.Fuse.html
27-
#[stable(feature = "fused", since = "1.26.0")]
28-
pub trait FusedIterator: Iterator {}
29-
30-
#[stable(feature = "fused", since = "1.26.0")]
31-
impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
32-
33-
/// An iterator that reports an accurate length using size_hint.
34-
///
35-
/// The iterator reports a size hint where it is either exact
36-
/// (lower bound is equal to upper bound), or the upper bound is [`None`].
37-
/// The upper bound must only be [`None`] if the actual iterator length is
38-
/// larger than [`usize::MAX`]. In that case, the lower bound must be
39-
/// [`usize::MAX`], resulting in a [`.size_hint`] of `(usize::MAX, None)`.
40-
///
41-
/// The iterator must produce exactly the number of elements it reported
42-
/// or diverge before reaching the end.
43-
///
44-
/// # Safety
45-
///
46-
/// This trait must only be implemented when the contract is upheld.
47-
/// Consumers of this trait must inspect [`.size_hint`]’s upper bound.
48-
///
49-
/// [`None`]: ../../std/option/enum.Option.html#variant.None
50-
/// [`usize::MAX`]: ../../std/usize/constant.MAX.html
51-
/// [`.size_hint`]: ../../std/iter/trait.Iterator.html#method.size_hint
52-
#[unstable(feature = "trusted_len", issue = "37572")]
53-
pub unsafe trait TrustedLen : Iterator {}
54-
55-
#[unstable(feature = "trusted_len", issue = "37572")]
56-
unsafe impl<I: TrustedLen + ?Sized> TrustedLen for &mut I {}
13+
pub use self::marker::{FusedIterator, TrustedLen};

0 commit comments

Comments
 (0)