-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Expose size_hint() for TokenStream's iterator #99703
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
(rust-highfive has picked a reviewer for you, use r? to override) |
@bors r+ rollup |
I'm definitely considering changing the iterator to not be a thin wrapper around Vec's |
Rollup of 9 pull requests Successful merges: - rust-lang#92390 (Constify a few `(Partial)Ord` impls) - rust-lang#97077 (Simplify some code that depend on Deref) - rust-lang#98710 (correct the output of a `capacity` method example) - rust-lang#99084 (clarify how write_bytes can lead to UB due to invalid values) - rust-lang#99178 (Lighten up const_prop_lint, reusing const_prop) - rust-lang#99673 (don't ICE on invalid dyn calls) - rust-lang#99703 (Expose size_hint() for TokenStream's iterator) - rust-lang#99709 (`Inherited` always has `TypeckResults` available) - rust-lang#99713 (Fix sidebar background) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
The iterator for
proc_macro::TokenStream
is a wrapper around aVec
iterator:rust/library/proc_macro/src/lib.rs
Lines 363 to 371 in babff22
so it can cheaply provide a perfectly precise size hint, with just a pointer subtraction:
rust/library/alloc/src/vec/into_iter.rs
Lines 170 to 177 in babff22
I need the size hint in syn (https://github.com/dtolnay/syn/blob/1.0.98/src/buffer.rs) to reduce allocations when converting TokenStream into syn's internal TokenBuffer representation.
Aside from
size_hint
, the other non-default methods instd::vec::IntoIter
'sIterator
impl areadvance_by
,count
, and__iterator_get_unchecked
. I've includedcount
in this PR since it is trivial. I did not include__iterator_get_unchecked
because it is spoopy and I did not feel like dealing with that. Lastly, I did not includeadvance_by
because that requiresfeature(iter_advance_by)
(#77404) and I noticed this comment at the top of libproc_macro:rust/library/proc_macro/src/lib.rs
Lines 20 to 22 in babff22