]> git.lizzy.rs Git - rust.git/commit - src/tools/miri
Rollup merge of #99703 - dtolnay:tokenstreamsizehint, r=petrochenkov
authorYuki Okushi <jtitor@2k36.org>
Mon, 25 Jul 2022 22:14:50 +0000 (07:14 +0900)
committerGitHub <noreply@github.com>
Mon, 25 Jul 2022 22:14:50 +0000 (07:14 +0900)
commit5bbdf659965abf2bcc16b4d0f9e487ee570ba999
tree95ee6ee587744ec3703a8998c1d6c09fc6399f18
parent2973b00ca6bc85ba3552282657de81d4baf864cc
parent63e74aba813d2e24ffa094d05c2545b5e882229f
Rollup merge of #99703 - dtolnay:tokenstreamsizehint, r=petrochenkov

Expose size_hint() for TokenStream's iterator

The iterator for `proc_macro::TokenStream` is a wrapper around a `Vec` iterator:

https://github.com/rust-lang/rust/blob/babff2211e3ae9ef52852dc1b01f3eacdd94c12e/library/proc_macro/src/lib.rs#L363-L371

so it can cheaply provide a perfectly precise size hint, with just a pointer subtraction:

https://github.com/rust-lang/rust/blob/babff2211e3ae9ef52852dc1b01f3eacdd94c12e/library/alloc/src/vec/into_iter.rs#L170-L177

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 in `std::vec::IntoIter`'s `Iterator` impl are `advance_by`, `count`, and `__iterator_get_unchecked`. I've included `count` 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 include `advance_by` because that requires `feature(iter_advance_by)` (https://github.com/rust-lang/rust/issues/77404) and I noticed this comment at the top of libproc_macro:

https://github.com/rust-lang/rust/blob/babff2211e3ae9ef52852dc1b01f3eacdd94c12e/library/proc_macro/src/lib.rs#L20-L22