Rollup merge of #111548 - calebcartwright:by-ref-tokentree2, r=compiler-errors

add util function to TokenStream to eliminate some clones

Another proposed change in the same vein as #111492 trying to get rid of some clones.

This adds a TokenStream helper function so that rustdoc can directly get a chunks iterator over the underlying token trees so that it no longer needs the clones and vec.
This commit is contained in:
Matthias Krüger 2023-05-15 10:58:40 +02:00 committed by GitHub
commit 916ba6dec1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View file

@ -551,6 +551,10 @@ impl TokenStream {
vec_mut.extend(stream_iter);
}
}
pub fn chunks(&self, chunk_size: usize) -> core::slice::Chunks<'_, TokenTree> {
self.0.chunks(chunk_size)
}
}
/// By-reference iterator over a [`TokenStream`], that produces `&TokenTree`

View file

@ -594,9 +594,8 @@ pub(super) fn display_macro_source(
def_id: DefId,
vis: ty::Visibility<DefId>,
) -> String {
let tts: Vec<_> = def.body.tokens.clone().into_trees().collect();
// Extract the spans of all matchers. They represent the "interface" of the macro.
let matchers = tts.chunks(4).map(|arm| &arm[0]);
let matchers = def.body.tokens.chunks(4).map(|arm| &arm[0]);
if def.macro_rules {
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(cx.tcx, matchers, ";"))