Skip to content

Add Pair adaptor for Iterator to return elements two at a time #14528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

apoelstra
Copy link
Contributor

I need this adaptor to read hexadecimal strings (as seen in the example in the doc comment), and I think it'd be more generally useful.

@lilyball
Copy link
Contributor

@Ryman You should comment on the Files Changed tab instead of the commit directly. That way we can see the context of your comment on the Conversation tab, and also track older comments after the PR has been updated.

@lilyball
Copy link
Contributor

This seems like an absurdly specialized iterator, especially for a single use-case. If we had integral generic parameters, then an iterator that returns [T, ..N] would be a more generically useful thing, but a specialized iterator just for adjacent pairs?

ImmutableVector defines a method chunks() that does basically what you want, though it returns a &[T]. You could try writing the same thing for StrSlice, having it return a given number of chars at a time (with &[char]). Or you could just call .as_slice() and use .chunks(), as hex parsing means you only care about ASCII chars. You could also use my open PR #14425 for a .filter_scan() adaptor to recreate your Pair as needed:

let mut char_iter = data.chars().filter_scan(None, |st, x| st.take().map(|a| (a,x)).or_else(|| { *st = Some(x); None }));

/// # Example
///
/// ```rust
/// let xs = "10ab20cde".chars().pair();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this let-binding doing here?

@kud1ing
Copy link

kud1ing commented May 30, 2014

@kballard: I think if i was the committer, i'd be happier to read something like This seems like a very specialized iterator rather than This seems like an absurdly specialized iterator.

@apoelstra
Copy link
Contributor Author

Hi guys, thanks for the comments. The efficiency issues and read-after-None are minor issues that I'm happy to fix if the PR would be accepted.

As for the specialization of this adaptor, I had thought that I had more use-cases than just hexadecimal parsing, but in fact I can't think of one outside of string parsing, in which case it would be much more generally useful to implement .chunks() on StrSlice. So I'm going to close this and maybe come back with that later this weekend.

@apoelstra apoelstra closed this May 30, 2014
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 5, 2023
…v, r=Veykril

Add bounds for fields in derive macro, second try
flip1995 pushed a commit to flip1995/rust that referenced this pull request Apr 3, 2025
Not all host tools platforms support `f16`/`f128` builtins yet due to
LLVM assertion failures and miscompilations. Until them, Clippy should
avoid using `f16`/`f128` at runtime itself. See
rust-lang#137630.

cc @tgross35

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants