# The Trusted Iterator Length Problem

**URL:** <https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302>\
**Category:** Uncategorized\
**Created:** [January 13, 2015, 3:17am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302 "2015-01-13T03:17:00Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![reem](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/reem/32/167_2.png) [@reem](https://internals.rust-lang.org/u/reem)\
**Post date:** [February 24, 2015, 6:10am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/22 "2015-02-24T06:10:46Z")

</div>

Oh, I would just have `exact_size` be an unsafe method and implementing it would therefore require typing `unsafe`.

---

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/huon/32/3698_2.png) [@huon](https://internals.rust-lang.org/u/huon)\
**Post date:** [February 24, 2015, 6:22am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/23 "2015-02-24T06:22:43Z")

</div>

@reem, that’s the problem that @P1start (and the rest of the thread 😉 ) is describing: a method declared `unsafe` in the trait doesn’t require the implementer to write `unsafe`.

---

<div class="post-metadata">

**Author:** ![reem](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/reem/32/167_2.png) [@reem](https://internals.rust-lang.org/u/reem)\
**Post date:** [February 25, 2015, 7:29am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/24 "2015-02-25T07:29:10Z")

</div>

That seems wrong then.

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [February 25, 2015, 10:23am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/25 "2015-02-25T10:23:54Z")

</div>

Currently the `unsafe` declaration in the trait just means that the function is `unsafe` to call, so it might have to fulfill a contract. This doesn’t necessarily mean that every implementation of this function has to rely on that contract, and when they don’t they can just omit the `unsafe`.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [February 25, 2015, 1:13pm UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/26 "2015-02-25T13:13:09Z")

</div>

We need real specialization based on optionally available traits. An impl of an unsafe trait TrustedIteratorLength could be present, and functions should be able to specialize for that case.

---

<div class="post-metadata">

**Author:** ![theemathas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/theemathas/32/11817_2.png) [@theemathas](https://internals.rust-lang.org/u/theemathas)\
**Post date:** [March 10, 2015, 1:32am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/27 "2015-03-10T01:32:52Z")

</div>

Filed an issue for making `ExactSizeIterator` unsafe

> <https://github.com/rust-lang/rfcs/issues/958>
>
> I think that ExactSizeIterator should be an unsafe trait, so that its invariant can be relied upon by unsafe code.
> See also...

---

<div class="post-metadata">

**Author:** ![quantheory](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quantheory/32/480_2.png) [@quantheory](https://internals.rust-lang.org/u/quantheory)\
**Post date:** [March 19, 2015, 10:59pm UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/28 "2015-03-19T22:59:36Z")

</div>

I’m not sure if this is sensible, but if not it might inspire someone to have a better idea:

```rust
unsafe trait ExactSizeIterator {}
trait MaybeExactSizeIterator {
    // Edit: The `&self` isn't actually used in the blanket impls, but it
    // seems necessary for object safety?
    fn size_hint_is_exact(&self) -> bool;
}
impl<T> MaybeExactSizeIterator for T where T: ExactSizeIterator {
    fn size_hint_is_exact(&self) -> bool { true }
}
impl<T> MaybeExactSizeIterator for T where T: !ExactSizeIterator {
    fn size_hint_is_exact(&self) -> bool { false }
}
trait Iterator : MaybeExactSizeIterator {
    // ...
}

```

Pros:

- Addresses this issue.
- Everyone who accepts an `Iterator` can get this information without loss of generality.
- `unsafe` is in the right place.

Cons:

- It makes no sense to implement `ExactSizeIterator` without also implementing `Iterator`, so the dependence is a bit weird.
- We have to have specialization that can get something like the above past the coherence rules. If you don’t have negative bounds of some sort, you unfortunately can’t express anything about the absence of an unsafe trait.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [March 21, 2015, 10:11am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/29 "2015-03-21T10:11:36Z")

</div>

> [@tbu](#):
>
> It's even worse than I understood from that comment. The proposed method would be unsafe to call, but you can implement it without unsafe, with a simple fn exact\_size(&self) -\> bool { true }.

I just wanted to note that this is no longer true now that PR [Move fn safety out of the subtyping relationship and into coercions by nikomatsakis · Pull Request #23452 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/23452) has landed. If you have an `unsafe` fn in a trait, the impl must now implement it with an `unsafe` fn as well.

---

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/huon/32/3698_2.png) [@huon](https://internals.rust-lang.org/u/huon)\
**Post date:** [March 21, 2015, 10:37am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/30 "2015-03-21T10:37:34Z")

</div>

Great! I think I’m pretty happy with the `unsafe` method solution now.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:23am UTC](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302/31 "2019-03-25T08:23:37Z")

</div>

Closed on March 25, 2019, 8:23am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/the-trusted-iterator-length-problem/1302.md?page=1)
