rust/library/core/tests
Dylan DPC 8bd4ed9f95
Rollup merge of #76909 - timvermeulen:advance_by, r=Amanieu
Add Iterator::advance_by and DoubleEndedIterator::advance_back_by

This PR adds the iterator method

```rust
fn advance_by(&mut self, n: usize) -> Result<(), usize>
```

that advances the iterator by `n` elements, returning `Ok(())` if this succeeds or `Err(len)` if the length of the iterator was less than `n`.

Currently `Iterator::nth` is the method to override for efficiently advancing an iterator by multiple elements at once. `advance_by` is superior for this purpose because
- it's simpler to implement: instead of advancing the iterator and producing the next element you only need to advance the iterator
- it composes better: iterators like `Chain` and `FlatMap` can implement `advance_by` in terms of `advance_by` on their inner iterators, but they cannot implement `nth` in terms of `nth` on their inner iterators (see #60395)
- the default implementation of `nth` can trivially be implemented in terms of `advance_by` and `next`, which this PR also does

This PR also adds `DoubleEndedIterator::advance_back_by` for all the same reasons.

I'll make a tracking issue if it's decided this is worth merging. Also let me know if anything can be improved, this went through several iterations so there might very well still be room for improvement (especially in the doc comments). I've written overrides of these methods for most iterators that already override `nth`/`nth_back`, but those still need tests so I'll add them in a later PR.

cc @cuviper @scottmcm @Amanieu
2020-10-01 02:13:29 +02:00
..
fmt mv std libs to library/ 2020-07-27 19:51:13 -05:00
hash mv std libs to library/ 2020-07-27 19:51:13 -05:00
num Fix FIXME in core::num test: Check sign of zero in min/max tests. 2020-09-24 22:29:32 +02:00
alloc.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
any.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
array.rs Rollup merge of #76454 - poliorcetics:ui-to-unit-test-1, r=matklad 2020-09-28 18:39:39 +02:00
ascii.rs Move various ui const tests to library 2020-09-04 02:35:27 +02:00
atomic.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
bool.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
cell.rs Move format-ref-cell test 2020-09-21 21:50:27 +02:00
char.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
clone.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
cmp.rs Move const tests for Ordering to library\core 2020-09-04 00:40:11 +02:00
intrinsics.rs move test to intergrated test in library/core 2020-09-24 14:46:57 +00:00
iter.rs Rollup merge of #76909 - timvermeulen:advance_by, r=Amanieu 2020-10-01 02:13:29 +02:00
lazy.rs Make OnceCell<T> transparent to dropck 2020-08-18 00:34:54 +02:00
lib.rs Rollup merge of #76909 - timvermeulen:advance_by, r=Amanieu 2020-10-01 02:13:29 +02:00
manually_drop.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
mem.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
nonzero.rs Move various ui const tests to library 2020-09-04 02:35:27 +02:00
ops.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
option.rs review: fix nits and move panic safety tests to the correct place 2020-09-25 23:10:24 +02:00
pattern.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
pin.rs Make some methods of Pin<&mut T> unstable const 2020-09-18 19:23:50 +02:00
ptr.rs enable align_to tests in Miri 2020-08-19 10:41:51 +02:00
result.rs Auto merge of #76136 - CDirkx:const-result, r=dtolnay 2020-09-20 13:07:11 +00:00
slice.rs review: fix nits and move panic safety tests to the correct place 2020-09-25 23:10:24 +02:00
str_lossy.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
str.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
time.rs Make all methods of Duration const 2020-09-12 15:14:58 +02:00
tuple.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00