diff options
| author | bors <bors@rust-lang.org> | 2015-11-08 22:01:44 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-11-08 22:01:44 +0000 |
| commit | 5b4986fa5753b9662c8baab1c31b9b79bc84ca19 (patch) | |
| tree | 9ddef42e54f9b53e80d2a5fc725ccb2f21ad8445 /src/libcore | |
| parent | 9eb099daa4760901b5c3c094809fa6761849b8ad (diff) | |
| parent | f6f99ce7a3754a8ab052347d2d27bbb65b2e0560 (diff) | |
| download | rust-5b4986fa5753b9662c8baab1c31b9b79bc84ca19.tar.gz rust-5b4986fa5753b9662c8baab1c31b9b79bc84ca19.zip | |
Auto merge of #29684 - stepancheg:size-hint, r=Gankro
* `size_hint()` cannot be relied upon to perform memory unsafe operations Same applies to `len()` function of `ExactSizeIterator` trait.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index e6dae771792..da7d673cd96 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -369,6 +369,25 @@ pub trait Iterator { /// `None` here means that either there is no known upper bound, or the /// upper bound is larger than `usize`. /// + /// # Implementation notes + /// + /// It is not enforced that an iterator implementation yields the + /// declared number of elements. A buggy iterator may yield less + /// than the lower bound or more than the upper bound of elements. + /// + /// `size_hint()` is primarily intended to be used for optimizations + /// such as reserving space for the elements of the iterator, but + /// must not be trusted to e.g. omit bounds checks in unsafe code. + /// An incorrect implementation of `size_hint()` should not lead to + /// memory safety violations. + /// + /// That said, the implementation should provide a correct + /// estimation, because otherwise it would be a violation of the + /// trait's protocol. + /// + /// The default implementation returns `(0, None)` which is correct + /// for any iterator. + /// /// # Examples /// /// Basic usage: @@ -2731,7 +2750,11 @@ pub trait ExactSizeIterator: Iterator { /// implementation, you can do so. See the [trait-level] docs for an /// example. /// + /// This function has the same safety guarantees as [`size_hint()`] + /// function. + /// /// [trait-level]: trait.ExactSizeIterator.html + /// [`size_hint()`]: trait.Iterator.html#method.size_hint /// /// # Examples /// |
