Add PeekableIterator trait#144935
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Maybe the correct function signature is this? fn peek<T>(&mut self, callback: impl for<'a> FnOnce(Option<&'a Self::Item>) -> T) -> TThis would mean, for example, that |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| // SAFETY: by type invariant, the `end_or_len` field is always | ||
| // non-null for a non-ZST pointee. (This transmute ensures we | ||
| // get `!nonnull` metadata on the load of the field.) | ||
| if ptr == crate::intrinsics::transmute::<$ptr, NonNull<T>>(end_or_len) { |
There was a problem hiding this comment.
| // SAFETY: by type invariant, the `end_or_len` field is always | |
| // non-null for a non-ZST pointee. (This transmute ensures we | |
| // get `!nonnull` metadata on the load of the field.) | |
| if ptr == crate::intrinsics::transmute::<$ptr, NonNull<T>>(end_or_len) { | |
| if ptr.as_ptr() == end_or_len { |
There was a problem hiding this comment.
Hmm, I was not able to write it this way as Rust determines the types differ in their mutability. In general, this code was based on the next() method generic over both Iter and IterMut
There was a problem hiding this comment.
What exactly was the error? Seems possible to resolve by adding a $as_ptr metavar that is either as_ptr or as_mut_ptr depending on which one is being implemented (like into_ref).
next is a bit magical, others should try to be simpler if possible. cloning and advancing should be fine, it's cheap.
There was a problem hiding this comment.
Cloning actually wouldn't work for IterMut since it doesn't implement it. But that actually brings up a good point; the safety comments need to say why it is sound to hand out a&mut T.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
This doesn't seem to be tested at all. Please make sure that even unstable API gets helpful examples, aside from checking the implementation they serve as a very useful smoke test for whether or not the API makes sense in the first place.
Additionally, the type-specific impls need tests in library/coretests
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@wmstack Are you still open to working on this PR? If not, then I’m willing to take over the work on this. |
|
@bluebear94 Feel free to pick this up as I’m tied up with coursework at the moment. |
|
☔ The latest upstream changes (presumably #147353) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Some changes occurred in src/tools/cargo cc @ehuss |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Implement peekable for slice::iter Implement new PeekableIterator for Iter Replace T with U Apply suggestions from code review Improve documentation for `PeekableIterator` to reflect the changes in API Co-authored-by: +merlan #flirora <flirora@flirora.xyz> Update next_if to use the bool Update next_if logic to handle Some(false) correctly Implement PeekableIterator for Chars Peek by cloning on Chars fix input parameter in Chars Implement PeekableIterator for IntoIter Forget temporary in iter_inner Implement PeekableIterator for Peekable Remove as_ref() Remove unneeded transmute Co-authored-by: Tim (Theemathas) Chirananthavat <theemathas@gmail.com> Use assume_init_ref directly from slice fix peek_with in macros Reduce unsafe scope with Iter/IterMut remove peek_map, add examples Fix trailing whitespace, punctuation fix example chars add 0 case fix example syntax
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@tgross35 I believe the doctests should all compile but just a few clarifications about the coretests.
However the crate root at
Edit: Even if I ignore the intentionally empty comment at the crate root, it doesn't really get detected despite being used in a few un-committed tests: |
|
have you tried adding it to https://github.com/rust-lang/rust/blob/main/library/coretests/tests/lib.rs instead? |
|
rustc UX issue: "crate root" file not identified, can be confusing in case of test files. off to file a bug! #157001 |
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |




View all comments
Tracking issue: #132973
Implementation
Todo: Determine which structures need to implement
PeekableIterator:slice::iterstr::Charsvec::IntoIterPeekable<T>Unresolved Issues:
peek()without mutating?