Commit Graph

100 Commits

Author SHA1 Message Date
Linus Färnstrand cf1c7edd2d Use assoc float consts in libcore 2020-04-06 22:44:51 +02:00
Linus Färnstrand a88b36b93b Stop importing int/float modules in libcore 2020-04-05 11:22:01 +02:00
Tim Vermeulen 8cf33b0d9d Add tests 2020-03-17 00:03:43 +01:00
bors 6cad7542da Auto merge of #68358 - matthewjasper:spec-fix, r=nikomatsakis
Remove some unsound specializations

This removes the unsound and exploitable specializations in the standard library

* The `PartialEq` and `Hash` implementations for  `RangeInclusive` are changed to avoid specialization.
* The `PartialOrd` specialization for slices now specializes on a limited set of concrete types.
* Added some tests for the soundness problems.
2020-02-08 03:46:56 +00:00
Matthew Jasper a81c59f9b8 Remove some unsound specializations 2020-02-01 09:11:41 +00:00
Waffle 1aff08010d Add `Iterator::map_while` method and corresponding `MapWhile` adapter 2020-01-28 21:30:34 +03:00
MOZGIII 5446cc99bb Add Iterator::try_find 2020-01-02 00:59:26 +03:00
Mark Rousskov a06baa56b9 Format the world 2019-12-22 17:42:47 -05:00
Mazdak Farrokhzad 7e98ec5079
Rollup merge of #64121 - timvermeulen:iter_step_by_internal, r=scottmcm
Override `StepBy::{try_fold, try_rfold}`

Previous PR: https://github.com/rust-lang/rust/pull/51435

The previous PR was closed in favor of https://github.com/rust-lang/rust/pull/51601, which was later reverted. I don't think these implementations will make it harder to specialize `StepBy<Range<_>>` later, so we should be able to land this without any consequences.

This should fix https://github.com/rust-lang/rust/issues/57517 – in my benchmarks `iter` and `iter.step_by(1)` now perform equally well, provided internal iteration is used.
2019-09-09 17:42:24 +02:00
Tim Vermeulen 58ba1f51ef Add Iterator comparison methods that take a comparison function 2019-09-06 15:30:17 +02:00
Tim Vermeulen 78908f2e09 Override `StepBy::{try_fold, try_rfold}` 2019-09-04 14:12:04 +02:00
Xiang Fan 0e597d4c47 Rev::rposition counts from the wrong end
Because of a compiler bug that adding `Self: ExactSizeIterator` makes
the compiler forget `Self::Item` is `<I as Iterator>::Item`, we remove
this specialization for now.
2019-08-30 21:17:36 +08:00
Tim Vermeulen ec54340756 Fix bug in iter::Chain::size_hint 2019-08-18 21:47:23 +02:00
Mazdak Farrokhzad 477db05066
Rollup merge of #62737 - timvermeulen:cycle_try_fold, r=scottmcm
Override Cycle::try_fold

It's not very pretty, but I believe this is the simplest way to correctly implement `Cycle::try_fold`. The following may seem correct:
```rust
loop {
    acc = self.iter.try_fold(acc, &mut f)?;
    self.iter = self.orig.clone();
}
```
...but this loops infinitely in case `self.orig` is empty, as opposed to returning `acc`. So we first have to fully iterate `self.orig` to check whether it is empty or not, and before _that_, we have to iterate the remaining elements of `self.iter`.

This should always call `self.orig.clone()` the same amount of times as repeated `next()` calls would.

r? @scottmcm
2019-08-17 11:13:42 +02:00
Mazdak Farrokhzad e632dafba2
Rollup merge of #60492 - acrrd:issues/54054_chain, r=SimonSapin
Add custom nth_back for Chain

Implementation of nth_back for Chain.
Part of #54054
2019-08-16 18:22:20 +02:00
Mazdak Farrokhzad c32735d03c
Rollup merge of #62459 - timvermeulen:result_sum_internal_iteration, r=scottmcm
Use internal iteration in the Sum and Product impls of Result and Option

This PR adds internal iteration to the `ResultShunt` iterator type underlying the `Sum` and `Product` impls of `Result`. I had to change `ResultShunt` to hold a mutable reference to an error instead, similar to `itertools::ProcessResults`, in order to be able to pass the `ResultShunt` itself by value (which is necessary for internal iteration).

`ResultShunt::process` can unfortunately no longer be an associated function because that would make it generic over the lifetime of the error reference, which wouldn't work, so I turned it into the free function `process_results`.

I removed the `OptionShunt` type and forwarded the `Sum` and `Product` impls of `Option` to their respective impls of `Result` instead, to avoid having to repeat the internal iteration logic.
2019-08-06 15:36:27 +02:00
Mazdak Farrokhzad fe998dbfe4
Rollup merge of #61457 - timvermeulen:double_ended_iters, r=scottmcm
Implement DoubleEndedIterator for iter::{StepBy, Peekable, Take}

Now that `DoubleEndedIterator::nth_back` has landed, `StepBy` and `Take` can have an efficient `DoubleEndedIterator` implementation. I don't know if there was any particular reason for `Peekable` not having a `DoubleEndedIterator` implementation, but it's quite trivial and I don't see any drawbacks to having it.

I'm not very happy about the implementation of `Peekable::try_rfold`, but I didn't see another way to only take the value out of `self.peeked` in case `self.iter.try_rfold` didn't exit early.

I only added `Peekable::rfold` (in addition to `try_rfold`) because its `Iterator` implementation has both `fold` and `try_fold` (and for similar reasons I added `Take::try_rfold` but not `Take::rfold`). Do we have any guidelines on whether we want both? If we do want both, maybe we should investigate which iterator adaptors override `try_fold` but not `fold` and add the missing implementations. At the moment I think that it's better to always have iterator adaptors implement both, because some iterators have a simpler `fold` implementation than their `try_fold` implementation.

The tests that I added may not be sufficient because they're all just existing tests where `next`/`nth`/`fold`/`try_fold` are replaced by their DEI counterparts, but I do think all paths are covered. Is there anything in particular that I should probably also test?
2019-08-06 08:17:31 +02:00
Tim Vermeulen 2e41ba8742 Use internal iteration in the Sum and Product impls of Result and Option 2019-07-29 02:40:50 +02:00
Tim Vermeulen 688c11216a Override Cycle::try_fold 2019-07-17 01:16:49 +02:00
Tim Vermeulen 56ebfb185b Implement DoubleEndedIterator for iter::{StepBy, Peekable, Take} 2019-07-09 23:38:57 +02:00
Josh Stone 265e3a6230 Unit test Iterator::partition_in_place and is_partitioned 2019-07-09 12:39:25 -07:00
Mazdak Farrokhzad 3e08f1b57e
Rollup merge of #60454 - acrrd:issues/54054_skip, r=scottmcm
Add custom nth_back to Skip

Implementation of nth_back for Skip.
Part of #54054
2019-06-20 08:35:58 +02:00
Adrian Friedli 8590074a01
implement nth_back for RangeInclusive 2019-06-09 22:45:11 +02:00
Adrian Friedli 26d4c8f01c
implement nth_back for Range 2019-06-08 22:30:45 +02:00
Andrea Corradi e80a37558d Add custom nth_back for Skip 2019-05-29 22:33:38 +02:00
Mazdak Farrokhzad d67d1f24dc
Rollup merge of #58975 - jtdowney:iter_arith_traits_option, r=dtolnay
Implement `iter::Sum` and `iter::Product` for `Option`

This is similar to the existing implementation for `Result`. It will take each item into the accumulator unless a `None` is returned.

I based a lot of this on #38580. From that discussion it didn't seem like this addition would be too controversial or difficult. One thing I still don't understand is picking the values for the `stable` attribute. This is my first non-documentation PR for rust so I am open to any feedback on improvements.
2019-05-29 08:15:48 +02:00
Andrea Corradi 7b6ad60672 Add custom nth_back for Chain 2019-05-03 00:01:41 +02:00
Philipp Hansch c9ae68812f
Deny rust_2018_idioms in libcore tests 2019-04-20 18:44:29 +02:00
Adrian Friedli 2605537012
implement nth_back for Enumerate 2019-04-16 23:45:59 +02:00
Josh Stone 3d389f244a Test the size_hint of empty ranges too 2019-03-26 10:39:03 -07:00
Josh Stone 01162d86c5 Implement useful steps_between for all integers
We can use `usize::try_from` to convert steps from any size of integer.
This enables a meaningful `size_hint()` for larger ranges, rather than
always just `(0, None)`. Now they return the true `(len, Some(len))`
when it fits, otherwise `(usize::MAX, None)` for overflow.
2019-03-26 10:13:48 -07:00
kennytm 0e57b7230d
Rollup merge of #59072 - RalfJung:miri-alloc-tests, r=kennytm
we can now skip should_panic tests with the libtest harness
2019-03-16 22:39:44 +08:00
Tim Vermeulen 1819250556 Add tests to ensure that Iterator::min and Iterator::max are stable 2019-03-12 19:25:44 +01:00
Ralf Jung 4888b1fb99 we can now skip should_panic tests with the libtest harness 2019-03-10 17:47:42 +01:00
John Downey a35cdd4e41 Implement `iter::Sum` and `iter::Product` for `Option`
This is similar to the existing implementation for `Result`. It will
take each item into the accumulator unless a `None` is returned.
2019-03-06 12:17:26 -06:00
Mazdak Farrokhzad f19bec89d7
Rollup merge of #58122 - matthieu-m:range_incl_perf, r=dtolnay
RangeInclusive internal iteration performance improvement.

Specialize `Iterator::try_fold` and `DoubleEndedIterator::try_rfold` to improve code generation in all internal iteration scenarios.

This changes brings the performance of internal iteration with `RangeInclusive` on par with the performance of iteration with `Range`:

 - Single conditional jump in hot loop,
 - Unrolling and vectorization,
 - And even Closed Form substitution.

Unfortunately, it only applies to internal iteration. Despite various attempts at stream-lining the implementation of `next` and `next_back`, LLVM has stubbornly refused to optimize external iteration appropriately, leaving me with a choice between:

 - The current implementation, for which Closed Form substitution is performed, but which uses 2 conditional jumps in the hot loop when optimization fail.
 - An implementation using a `is_done` boolean, which uses 1 conditional jump in the hot loop when optimization fail, allowing unrolling and vectorization, but for which Closed Form substitution fails.

In the absence of any conclusive evidence as to which usecase matters most, and with no assurance that the lack of Closed Form substitution is not indicative of other optimizations being foiled, there is no way
to pick one implementation over the other, and thus I defer to the statu quo as far as `next` and `next_back` are concerned.
2019-02-23 09:25:12 +01:00
Ralf Jung 72be9a607b review or fix remaining miri failures in libcore 2019-02-13 18:21:13 +01:00
Ralf Jung 7f5dc49214 review or fix miri failures in iter, slice, cell, time 2019-02-13 17:56:43 +01:00
Ralf Jung 26ade1cfaa mark failures expected due to panics 2019-02-13 17:56:43 +01:00
Alexander Regueiro 99ed06eb88 libs: doc comments 2019-02-10 23:57:25 +00:00
Matthieu M 4fed67f942 Fix exhaustion of inclusive range try_fold and try_rfold 2019-02-09 18:42:34 +01:00
Ralf Jung 81613ad7cf disable tests in Miri 2019-02-07 18:24:10 +01:00
Kevin Leimkuhler 8dea0d0172 Add initial impl of is_sorted to Iterator 2019-01-17 22:34:42 -08:00
Stjepan Glavina 7c083a8fed Remove unnecessary mut 2019-01-14 12:23:50 +01:00
Stjepan Glavina e449f3d629 Fix failing test 2019-01-14 00:45:57 +01:00
Stjepan Glavina 04c74f46f0 Add core::iter::once_with 2019-01-13 16:58:08 +01:00
bors a7be40c65a Auto merge of #56534 - xfix:copied, r=@SimonSapin
Add unstable Iterator::copied()

Initially suggested at https://github.com/bluss/rust-itertools/pull/289, however the maintainers of itertools suggested this may be better of in a standard library.

The intent of `copied` is to avoid accidentally cloning iterator elements after doing a code refactoring which causes a structure to be no longer `Copy`. This is a relatively common pattern, as it can be seen by calling `rg --pcre2 '[.]map[(][|](?:(\w+)[|] [*]\1|&(\w+)[|] \2)[)]'` on Rust main repository. Additionally, many uses of `cloned` actually want to simply `Copy`, and changing something to be no longer copyable may introduce unnoticeable performance penalty.

Also, this makes sense because the standard library includes `[T].copy_from_slice` to pair with `[T].clone_from_slice`.

This also adds `Option::copied`, because it makes sense to pair it with `Iterator::copied`. I don't think this feature is particularly important, but it makes sense to update `Option` along with `Iterator` for consistency.
2018-12-26 19:39:19 +00:00
Mark Rousskov 2a663555dd Remove licenses 2018-12-25 21:08:33 -07:00
Konrad Borowski 8ac5380ea0
Merge branch 'master' into copied 2018-12-23 16:47:11 +01:00
Clar Fon fb18ddaaaa Add DoubleEndedIterator::nth_back 2018-12-20 01:18:04 -05:00