diff options
| author | Sebastian Dröge <sebastian@centricular.com> | 2021-02-04 10:40:20 +0200 |
|---|---|---|
| committer | Sebastian Dröge <sebastian@centricular.com> | 2021-02-04 11:31:31 +0200 |
| commit | f436630ac85504da5c6ef802dbee658fa97a6f37 (patch) | |
| tree | f813dff91d9f9e346f2d1f09143201e44eb72909 | |
| parent | e708cbd91c9cae4426d69270248362b423324556 (diff) | |
| download | rust-f436630ac85504da5c6ef802dbee658fa97a6f37.tar.gz rust-f436630ac85504da5c6ef802dbee658fa97a6f37.zip | |
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.rs | 6 |
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 |
