about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-02-07 14:45:51 +0100
committerGitHub <noreply@github.com>2021-02-07 14:45:51 +0100
commitf7062162518f4dde8fa4e08df1dc93840fa99a02 (patch)
treeed4fefe8b1e818401fc80dff6d1829eeb0fb6b63
parent6e1f7139c9bf1bb6d7087a2d7e10fb66df6efab9 (diff)
parentf436630ac85504da5c6ef802dbee658fa97a6f37 (diff)
downloadrust-f7062162518f4dde8fa4e08df1dc93840fa99a02.tar.gz
rust-f7062162518f4dde8fa4e08df1dc93840fa99a02.zip
Rollup merge of #81742 - sdroege:exact-size-iterator-correctness, r=kennytm
Add a note about the correctness and the effect on unsafe code to the `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 https://github.com/rust-lang/rust/issues/81739
-rw-r--r--library/core/src/iter/traits/exact_size.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/library/core/src/iter/traits/exact_size.rs b/library/core/src/iter/traits/exact_size.rs
index 996d62e2b4a..167db3359f2 100644
--- a/library/core/src/iter/traits/exact_size.rs
+++ b/library/core/src/iter/traits/exact_size.rs
@@ -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