bors 81601cd3a3 Auto merge of #37306 - bluss:trusted-len, r=alexcrichton
Add Iterator trait TrustedLen to enable better FromIterator / Extend

This trait attempts to improve FromIterator / Extend code by enabling it to trust the iterator to produce an exact number of elements, which means that reallocation needs to happen only once and is moved out of the loop.

`TrustedLen` differs from `ExactSizeIterator` in that it attempts to include _more_ iterators by allowing for the case that the iterator's len does not fit in `usize`. Consumers must check for this case (for example they could panic, since they can't allocate a collection of that size).

For example, chain can be TrustedLen and all numerical ranges can be TrustedLen. All they need to do is to report an exact size if it fits in `usize`, and `None` as the upper bound otherwise.

The trait describes its contract like this:

```
An iterator that reports an accurate length using size_hint.

The iterator reports a size hint where it is either exact
(lower bound is equal to upper bound), or the upper bound is `None`.
The upper bound must only be `None` if the actual iterator length is
larger than `usize::MAX`.

The iterator must produce exactly the number of elements it reported.

This trait must only be implemented when the contract is upheld.
Consumers of this trait must inspect `.size_hint()`’s upper bound.
```

Fixes #37232
2016-11-04 10:40:30 -07:00
..
2016-10-25 20:33:03 +02:00
2016-10-12 23:57:46 +05:30
2016-09-30 06:13:14 +02:00
2016-10-17 17:49:07 -07:00
2016-09-28 23:17:56 +00:00
2016-10-21 00:49:47 +01:00
2016-10-22 20:43:28 +02:00
2016-10-21 00:49:47 +01:00
2016-09-27 23:29:01 +00:00