Skip to content

Commit

Permalink
Add a note about the correctness and the effect on unsafe code to the…
Browse files Browse the repository at this point in the history
… `ExactSizeIterator` docs

As it is a safe trait it does not provide any guarantee that the
returned length is correct and as such unsafe code must not rely on it.

That's why `TrustedLen` exists.

Fixes #81739
  • Loading branch information
sdroege committed Feb 4, 2021
1 parent e708cbd commit f436630
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/core/src/iter/traits/exact_size.rs
Expand Up @@ -13,6 +13,12 @@
/// implement it. However, you may be able to provide a more performant
/// implementation than the default, so overriding it in this case makes sense.
///
/// Note that this trait is a safe trait and as such does *not* and *cannot*
/// guarantee that the returned length is correct. This means that `unsafe`
/// code **must not** rely on the correctness of [`Iterator::size_hint`]. The
/// unstable and unsafe [`TrustedLen`](super::marker::TrustedLen) trait gives
/// this additional guarantee.
///
/// [`len`]: ExactSizeIterator::len
///
/// # Examples
Expand Down

0 comments on commit f436630

Please sign in to comment.