Expose size_hint() for TokenStream's iterator #99703
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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