summary refs log tree commit diff
path: root/src/libcore/iter
AgeCommit message (Collapse)AuthorLines
2019-08-09Don't use associated type bounds in docs until it is stableIlija Tovilo-3/+2
2019-08-09Add missing #![feature(associated_type_bounds)]Ilija Tovilo-0/+2
2019-08-08Use associated_type_bounds where applicable - closes #61738Ilija Tovilo-26/+39
2019-08-06Rollup merge of #62459 - timvermeulen:result_sum_internal_iteration, r=scottmcmMazdak Farrokhzad-115/+43
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-06Rollup merge of #61457 - timvermeulen:double_ended_iters, r=scottmcmMazdak Farrokhzad-0/+117
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-07-29Use internal iteration in the Sum and Product impls of Result and OptionTim Vermeulen-115/+43
2019-07-27Refactoring use commun code between option, result and accumStargateur-136/+137
2019-07-10Rollup merge of #62481 - czipperz:iterator-last-nth-use-for_each, r=scottmcmMazdak Farrokhzad-3/+1
Use `fold` in `Iterator::last` default implementation We already use it in all the other methods. Consistency + potential perf is a pretty nice win!
2019-07-09Use fold in Iterator::lastChris Gregory-3/+1
Replace last impl with fold
2019-07-09Tracking issue 62544 for iter_is_partitionedJosh Stone-1/+1
2019-07-09Tracking issue 62543 for iter_partition_in_placeJosh Stone-1/+1
2019-07-09Implement DoubleEndedIterator for iter::{StepBy, Peekable, Take}Tim Vermeulen-0/+117
2019-07-09Return the true count from partition_in_placeJosh Stone-6/+35
2019-07-09Rename partition_mut to partition_in_placeJosh Stone-8/+8
2019-07-09Capitalize example commentJosh Stone-1/+1
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
2019-07-09Add Iterator::partition_mut() and is_partitioned()Josh Stone-0/+71
`partition_mut()` swaps `&mut T` items in-place to satisfy the predicate, so all `true` items precede all `false` items. This requires a `DoubleEndedIterator` so we can search from front and back for items that need swapping. `is_partitioned()` checks whether the predicate is already satisfied.
2019-07-08Auto merge of #62473 - timvermeulen:is_sorted_by_key, r=scottmcmbors-3/+3
Only call the closure parameter of Iterator::is_sorted_by_key once per item See https://github.com/rust-lang/rust/issues/53485#issuecomment-472314004. This changes `Iterator::is_sorted_by_key` to only call the given closure once for each item, which allows us to pass the items to the closure by value instead of by reference. **Important**: `is_sorted_by_key` for slices and slice iterators is now no longer implemented in terms of the custom `slice::Iter::is_sorted_by` implementation. It's a trade-off: we could forward `slice::Iter::is_sorted_by_key` to it directly for potential SIMD benefits, but that would mean that the closure is potentially called twice for (almost) every element of the slice.
2019-07-07Only call the closure parameter of Iterator::is_sorted_by_key once per itemTim Vermeulen-3/+3
2019-06-28doc(libcore) Fix CSIvan Enderlin-1/+1
A small PR to fix a small CS typo in `iter/traits/collect.rs`.
2019-06-23Fix meta-variable binding errors in macrosJulien Cretin-2/+2
The errors are either: - The meta-variable used in the right-hand side is not bound (or defined) in the left-hand side. - The meta-variable used in the right-hand side does not repeat with the same kleene operator as its binder in the left-hand side. Either it does not repeat enough, or it uses a different operator somewhere. This change should have no semantic impact.
2019-06-20Rollup merge of #60454 - acrrd:issues/54054_skip, r=scottmcmMazdak Farrokhzad-0/+14
Add custom nth_back to Skip Implementation of nth_back for Skip. Part of #54054
2019-06-12Rollup merge of #61671 - koalatux:nth-back-range, r=KodrAusMazdak Farrokhzad-0/+41
implement nth_back for Range(Inclusive) This is part of #54054.
2019-06-10Use `for_each` in `Iterator::partition`Josh Stone-2/+2
We already use this for `unzip`, but `partition` is not much different.
2019-06-09implement nth_back for RangeInclusiveAdrian Friedli-0/+28
2019-06-08implement nth_back for RangeAdrian Friedli-0/+13
2019-05-30Stabilize iter_nth_back featureLzu Tao-4/+1
2019-05-29Add custom nth_back for SkipAndrea Corradi-0/+14
2019-05-29Rollup merge of #60542 - timvermeulen:step_sub_usize, r=scottmcmMazdak Farrokhzad-0/+35
Add Step::sub_usize Required for #54054. I'm aware that the `Step` trait needs a rework, but this still seems like a reasonable addition? This currently doesn't compile because Chalk contains a type that implement this trait, and this is a breaking change. How can that be fixed?
2019-05-29Rollup merge of #58975 - jtdowney:iter_arith_traits_option, r=dtolnayMazdak Farrokhzad-0/+110
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-28Bump Sum and Product for Option<T> to 1.37David Tolnay-2/+2
2019-05-28Rollup merge of #61237 - DevQps:expand-iterator-docs, r=Mark-SimulacrumMazdak Farrokhzad-0/+6
Updated the Iterator docs with information about overriding methods. # Description Updated the Iterator docs with information about overriding methods. closes #60223
2019-05-27Updated the Iterator docs with information about overriding methods.Christian-0/+6
2019-05-25Add Step::sub_usizeTim Vermeulen-0/+35
2019-05-22Revert "Add implementations of last in terms of next_back on a bunch of ↵Steven Fackler-5/+0
DoubleEndedIterators." This reverts commit 3e86cf36b5114f201868bf459934fe346a76a2d4.
2019-05-17Use iter() for iterating arrays by sliceJosh Stone-16/+16
These `into_iter()` calls will change from iterating references to values if we ever get `IntoIterator` for arrays, which may break the code using that iterator. Calling `iter()` is future proof.
2019-05-14Rollup merge of #60130 - khuey:efficient_last, r=sfacklerMazdak Farrokhzad-0/+5
Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators Provided a `DoubleEndedIterator` has finite length, `Iterator::last` is equivalent to `DoubleEndedIterator::next_back`. But searching forwards through the iterator when it's unnecessary is obviously not good for performance. I ran into this on one of the collection iterators. I tried adding appropriate overloads for a bunch of the iterator adapters like filter, map, etc, but I ran into a lot of type inference failures after doing so. The other interesting case is what to do with `Repeat`. Do we consider it part of the contract that `Iterator::last` will loop forever on it? The docs do say that the iterator will be evaluated until it returns None. This is also relevant for the adapters, it's trivially easy to observe whether a `Map` adapter invoked its closure a zillion times or just once for the last element.
2019-04-27Stabilize Iterator::copied in 1.36.0.Mazdak Farrokhzad-10/+9
2019-04-22Remove double trailing newlinesvarkor-2/+0
2019-04-19Add implementations of last in terms of next_back on a bunch of ↵Kyle Huey-0/+5
DoubleEndedIterators. r?Manishearth
2019-04-19Rollup merge of #60098 - Centril:libcore-deny-more, r=varkorMazdak Farrokhzad-13/+12
libcore: deny `elided_lifetimes_in_paths` r? @varkor
2019-04-19Rollup merge of #60023 - koalatux:nth-back, r=scottmcmMazdak Farrokhzad-0/+26
implement specialized nth_back() for Bytes, Fuse and Enumerate Hi, After my first PR has been successfully merged, here is my second pull request :-) Also this PR contains some specializations for the problem discussed in #54054.
2019-04-19libcore: deny more...Mazdak Farrokhzad-13/+12
2019-04-18libcore => 2018Taiki Endo-26/+30
2019-04-16implement nth_back for EnumerateAdrian Friedli-0/+10
2019-04-16implement nth_back for FuseAdrian Friedli-0/+16
2019-04-02Rollup merge of #59444 - cuviper:steps_between, r=scottmcmMazdak Farrokhzad-51/+10
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-04-02Rollup merge of #59262 - timvermeulen:iterator_cmp_dedup, r=scottmcmMazdak Farrokhzad-98/+14
Remove duplicated code from Iterator::{ne, lt, le, gt, ge} This PR delegates `Iterator::ne` to `Iterator::eq` and `Iterator::{lt, le, gt, ge}` to `Iterator::partial_cmp`. Oddly enough, this change actually simplifies the generated assembly [in some cases](https://rust.godbolt.org/z/riBtNe), although I don't understand assembly well enough to see if the longer assembly is doing something clever. I also added two extremely simple benchmarks: ``` // before test iter::bench_lt ... bench: 98,404 ns/iter (+/- 21,008) test iter::bench_partial_cmp ... bench: 62,437 ns/iter (+/- 5,009) // after test iter::bench_lt ... bench: 61,757 ns/iter (+/- 8,770) test iter::bench_partial_cmp ... bench: 62,151 ns/iter (+/- 13,753) ``` I have no idea why the current `lt`/`le`/`gt`/`ge` implementations don't seem to be compiled optimally, but simply having them call `partial_cmp` seems to be an improvement. See #44729 for a previous discussion.
2019-03-29Fix OnceWith docstring.Geoffry Song-2/+2
This was incorrectly copypasta'd from RepeatWith.
2019-03-26impl TrustedLen for 128-bit ranges tooJosh Stone-2/+2
2019-03-26Implement useful steps_between for all integersJosh Stone-49/+8
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.