Commit Graph

682 Commits

Author SHA1 Message Date
Michael Goulet
434e3e5d45 Rollup merge of #143067 - GrigorenkoPV:142269, r=workingjubilee
Tracking issue number for `iter_macro`

Tracking issue: rust-lang/rust#142269
2025-06-26 20:15:30 -04:00
Michael Goulet
9820197e12 Rollup merge of #142549 - the8472:intersperse-fold-tweak, r=tgross35
small iter.intersperse.fold() optimization

No need to call into fold when the first item is already None, this avoids some redundant work for empty iterators.

"But it uses Fuse" one might want to protest, but Fuse is specialized and may call into the inner iterator anyway.
2025-06-26 20:15:18 -04:00
Pavel Grigorenko
8c15a69290 Tracking issue number for iter_macro 2025-06-26 21:30:02 +03:00
Jubilee
f50da063e8 Rollup merge of #140985 - zachs18:fuse-default-some, r=tgross35
Change `core::iter::Fuse`'s `Default` impl to do what its docs say it does

The [docs on `impl<I: Default> Default for core::iter::Fuse<I>`](https://doc.rust-lang.org/nightly/std/iter/struct.Fuse.html#impl-Default-for-Fuse%3CI%3E) say (as the `I: Default` bound implies) that `Fuse::<I>::default` "Creates a `Fuse` iterator from the default value of `I`". However, the implementation creates a `Fuse` with `Fuse { iter: Default::default() }`, and since the `iter` field is an `Option<I>`, this is actually `Fuse { iter: None }`, not `Fuse { iter: Some(I::default()) }`, so `Fuse::<I>::default()` always returns an empty iterator, even if `I::default()` would not be empty.

This PR changes `Fuse`'s `Default` implementation to match the documentation. This will be a behavior change for anyone currently using `Fuse::<I>::default()` where `I::default()` is not an empty iterator[^1], as `Fuse::<I>::default()` will now also not be an empty iterator.

(Alternately, the docs could be updated to reflect what the current implementation actually does, i.e. returns an always-exhausted iterator that never yields any items (even if `I::default()` would have yielded items). With this option, the `I: Default` bound could also be removed to reflect that no `I` is ever created.)

[Current behavior example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a1e0adc4badca3dc11bfb70a99213249) (maybe an example like this should be added to the docs either way?)

This PR changes publicly observable behavior, so I think requires at least a T-libs-api FCP?

r? libs-api

cc https://github.com/rust-lang/rust/issues/140961

`impl<I: Default> Default for Fuse<I>` was added in 1.70.0 (https://github.com/rust-lang/rust/pull/99929), and it's docs and behavior do not appear to have changed since (`Fuse`'s `iter` field has been an `Option` since before the impl was added).

[^1]: IIUC it is a "de facto" guideline for the stdlib that an iterator type's `default()` should be empty (and for iterators where that would not make sense, they should not implement `Default`): cc https://github.com/rust-lang/libs-team/issues/77#issuecomment-1194681709 , so for stdlib iterators, I don't think this would change anything. However, if a user has a custom `Iterator` type `I`, *and* they are using `Fuse<I>`, *and* they call `Fuse::<I>::default()`, this may change the behavior of their code.
2025-06-23 12:48:18 -07:00
Samuel Tardieu
6a9f223f00 Add diagnostic items for Clippy 2025-06-20 17:53:09 +02:00
Zachary S
f3d4278643 Fix core::iter::Fuse's Default impl to do what it's docs say it does.
Add a doctest with a non-empty-by-default iterator.
2025-06-18 16:28:07 -05:00
The 8472
e6c300892d small iter.intersperse.fold() optimization
No need to call into fold when the first item is already None,
this avoids some redundant work for empty iterators.

"But it uses Fuse" one might want to protest, but Fuse is specialized
and may call into the inner iterator anyway.
2025-06-15 22:40:44 +02:00
Soveu
8f77681656 100% safe implementation of RepeatN 2025-06-13 22:32:15 +02:00
Urgau
9b1cd722ca Fix usage of bootstrap in core 2025-06-07 18:11:53 +02:00
Oli Scherer
5fbdfc3e10 Add iter macro
This adds an `iter!` macro that can be used to create movable
generators.

This also adds a yield_expr feature so the `yield` keyword can be used
within iter! macro bodies. This was needed because several unstable
features each need `yield` expressions, so this allows us to stabilize
them separately from any individual feature.

Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
Co-authored-by: Jieyou Xu <jieyouxu@outlook.com>
Co-authored-by: Travis Cross <tc@traviscross.com>
2025-06-03 10:52:32 -07:00
bors
88b3b520e8 Auto merge of #141086 - a1phyr:spec_advance_by, r=jhpratt
Implement `advance_by` via `try_fold` for `Sized` iterators

When `try_fold` is overriden, it is usually easier for compilers to optimize.

Example difference: https://iter.godbolt.org/z/z8cEfnKro
2025-05-25 11:34:43 +00:00
mejrs
c9b6ccc11c Switch library rustc_unimplemented to use Self and This 2025-05-17 12:50:37 +02:00
Benoît du Garreau
5240cd3a82 Implement advance_by via try_fold for Sized iterators
When `try_fold` is overriden, it is usually easier for compilers to
optimize.
2025-05-16 15:46:44 +02:00
Guillaume Gomez
8929f8a180 Rollup merge of #136183 - hkBst:patch-25, r=Amanieu
Update iterator.rs to use arrays by value

Update examples to no longer avoid iterating arrays for #84513
2025-05-06 19:27:38 +02:00
Marijn Schouten
18a3599fe3 Update iterator.rs to use arrays by value
Update examples to no longer avoid iterating arrays for #84513
2025-05-06 12:19:24 +00:00
Stuart Cook
6238555e72 Rollup merge of #140598 - ShE3py:iter-misc-docs, r=workingjubilee
Steer docs to `utf8_chunks` and `Iterator::take`

- Adds `limit` as an alias of `take` (as this is [what Java calls this operation](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#limit-long-));
- Says that [`Utf8Chunks`](https://doc.rust-lang.org/std/str/struct.Utf8Chunks.html) comes from [`[u8]::utf8_chunks`](https://doc.rust-lang.org/std/primitive.slice.html#method.utf8_chunks).

``@rustbot`` label +A-docs
2025-05-06 16:28:41 +10:00
The 8472
e6030468dd Revert "Avoid unused clones in Cloned<I> and Copied<I>"
This reverts commit ed5f31ab01.
2025-05-04 22:29:57 +02:00
Lieselotte
415a73e3eb docs: alias limit to Iterator::take, cite [u8]::utf8_chunks in Utf8Chunks 2025-05-02 23:41:52 +02:00
Guillaume Gomez
931735a1b1 Rollup merge of #139780 - ongardie:iterator-take-by_ref-example, r=workingjubilee
docs: Add example to `Iterator::take` with `by_ref`

If you want to logically split an iterator after `n` items, you might first discover `take`. Before this change, you'd find that `take` consumes the iterator, and you'd probably be stuck. The answer involves `by_ref`, but that's hard to discover, especially since `by_ref` is a bit abstract and `Iterator` has many methods.

After this change, you'd see the example showing `take` along with `by_ref`, which allows you to continue using the rest of the iterator. `by_ref` had a good example involving `take` already, so this change just duplicates that existing example under `take`.
2025-05-01 22:27:20 +02:00
Matthias Krüger
766340b26e Rollup merge of #139090 - yotamofek:pr/peekable-next-if-docs, r=tgross35
fix docs for `Peekable::next_if{_eq}`

These seem like copy-paste errors

(except `saves the value of` 👉 `retains` which just sounds better to me)
2025-04-27 11:54:58 +02:00
Jonathan Gruner
5a71fabe29 added doctest for Enumerate::next_index 2025-04-19 12:29:47 +02:00
Jonathan Gruner
1f491dccba add next_index to Enumerate 2025-04-19 12:29:47 +02:00
Diego Ongaro
e5fb426a83 docs: Add example to Iterator::take with by_ref
If you want to logically split an iterator after `n` items, you might first
discover `take`. Before this change, you'd find that `take` consumes the
iterator, and you'd probably be stuck. The answer involves `by_ref`, but that's
hard to discover, especially since `by_ref` is a bit abstract and `Iterator`
has many methods.

After this change, you'd see the example showing `take` along with `by_ref`,
which allows you to continue using the rest of the iterator. `by_ref` had a
good example involving `take` already, so this change just duplicates that
existing example under `take`.
2025-04-13 19:52:34 -07:00
Thalia Archibald
ed5f31ab01 Avoid unused clones in Cloned<I> and Copied<I>
Avoid cloning in `Cloned<I>` or copying in `Copied<I>` when elements are
only needed by reference or not at all. There is already some precedent
for this, given that `__iterator_get_unchecked` is implemented, which
can skip elements. The reduced clones are technically observable by a
user impl of `Clone`.
2025-04-13 16:23:12 -07:00
izarma
f21d18063c doc changes 2025-04-06 15:09:59 +05:30
mejrs
41bee761bb use diagnostic::on_unimplemented instead 2025-03-30 15:25:47 +02:00
Yotam Ofek
676e29ba7f fix docs for Peekable::next_if{_eq} 2025-03-28 22:17:31 +00:00
Michael Goulet
145fe2d648 Rollup merge of #136040 - mu001999-contrib:cleanup, r=Mark-Simulacrum
Remove unused trait BoundedSize

Detected by #128637

The usage of this trait is removed in #135104

r? `@the8472`
2025-03-23 14:59:29 -04:00
joboet
51d51c8666 core: optimize RepeatN
...by adding an optimized implementation of `try_fold` and `fold` as well as replacing some unnecessary `mem::replace` calls with `MaybeUninit` helper methods.
2025-03-22 13:35:46 +01:00
bendn
7a8cdf00e6 use then 2025-03-19 10:45:42 +07:00
许杰友 Jieyou Xu (Joe)
01bc95417c Rollup merge of #138329 - scottmcm:assert-hint, r=Mark-Simulacrum
debug-assert that the size_hint is well-formed in `collect`

Closes #137919

In the hopes of helping to catch any future accidentally-incorrect rustc or stdlib iterators (like the ones #137908 accidentally found), this has `Iterator::collect` call `size_hint` and check its `low` doesn't exceed its `Some(high)`.

There's of course a bazillion more places this *could* be checked, but the hope is that this one is a good tradeoff of being likely to catch lots of things while having minimal maintenance cost (especially compared to putting it in *every* container's `from_iter`).
2025-03-16 09:40:07 +08:00
许杰友 Jieyou Xu (Joe)
e0846806db Rollup merge of #138082 - thaliaarchi:slice-cfg-not-test, r=thomcc
Remove `#[cfg(not(test))]` gates in `core`

These gates are unnecessary now that unit tests for `core` are in a separate package, `coretests`, instead of in the same files as the source code. They previously prevented the two `core` versions from conflicting with each other.
2025-03-16 09:40:05 +08:00
Jakub Beránek
bb2324a656 Rollup merge of #135987 - hkBst:patch-20, r=joboet
Clarify iterator by_ref docs

fixes #95143
2025-03-11 13:30:49 +01:00
Scott McMurray
3c74d02319 debug-assert that the size_hint is well-formed in collect 2025-03-10 18:22:28 -07:00
Marijn Schouten
506c304654 Clarify iterator by_ref docs 2025-03-09 13:24:46 +01:00
Matthias Krüger
b834632071 Rollup merge of #138034 - thaliaarchi:use-prelude-size-of, r=tgross35
library: Use `size_of` from the prelude instead of imported

Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them.

These functions were added to all preludes in Rust 1.80.

try-job: test-various
try-job: x86_64-gnu
try-job: x86_64-msvc-1
2025-03-07 10:12:44 +01:00
Thalia Archibald
988eb19970 library: Use size_of from the prelude instead of imported
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the
prelude instead of importing or qualifying them.

These functions were added to all preludes in Rust 1.80.
2025-03-06 20:20:38 -08:00
Thalia Archibald
638b226a6a Remove #[cfg(not(test))] gates in core
These gates are unnecessary now that unit tests for `core` are in a
separate package, `coretests`, instead of in the same files as the
source code. They previously prevented the two `core` versions from
conflicting with each other.
2025-03-06 13:21:59 -08:00
Michael Goulet
5b074125e5 Rollup merge of #135895 - hkBst:patch-15, r=joboet
Document workings of successors more clearly

This is an attempt to fix #135087 together with https://github.com/rust-lang/rust/pull/135886, but I am not sure if I've succeeded in adding much clarity here, so don't be shy with your comments.
2025-03-06 12:22:09 -05:00
Marijn Schouten
6867806f67 Document workings of successors more clearly
This is an attempt to fix #135087 together with https://github.com/rust-lang/rust/pull/135886, but I am not sure if I've succeeded in adding much clarity here, so don't be shy with your comments.
2025-03-02 17:41:42 +01:00
Frank Steffahn
d93926cb5d Fix typo in hidden internal docs of TrustedRandomAccess
I typoed the coercion direction here 4 years ago; fixing it now
2025-02-18 17:54:56 +01:00
Jacob Pratt
afbeefb684 Rollup merge of #135687 - joseluis:feat-reexport_from_coroutine, r=scottmcm
re-export `FromCoroutine` from `core::iter`

tracking issue: https://github.com/rust-lang/rust/issues/43122
fixes: #135686
2025-02-15 02:37:27 -05:00
Michael Goulet
04bbc8340a Rename field in OnceWith from gen to make 2025-02-09 17:10:50 +00:00
Jaken Herman
4457f44065 Document Sum::sum returns additive identities for []
Because the neutral element of `<fNN as iter::Sum>` was changed to
`neg_zero`, the documentation needed to be updated, as it was reporting
inadequate information about what should be expected from the return.

Co-authored-by: Jubilee <workingjubilee@gmail.com>
2025-02-08 14:06:13 -08:00
Marijn Schouten
6770d3ddf4 Fix link in from_fn.rs 2025-02-05 08:52:13 +01:00
Marijn Schouten
d3cd832dda Document purpose of closure in from_fn.rs more clearly 2025-01-26 13:43:55 +01:00
Mu001999
42c9c109fe Remove unused trait BoundedSize 2025-01-25 14:25:34 +08:00
joseLuís
f02c3f82f7 re-export core::iter::FromCoroutine 2025-01-18 16:35:54 +01:00
ltdk
e37daf0c86 Add inherent versions of MaybeUninit methods for slices 2025-01-11 23:57:00 -05:00
Pietro Albini
4ae92b7adb update version placeholders 2025-01-08 20:02:18 +01:00