From fa43a690519ca7ea158cd23ba451f27dcef07e46 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 9 Jul 2026 09:25:17 +1000 Subject: [PATCH] Improve `TokenStreamIter`. It's simpler if it's just a wrapper around `slice::Iter`. Also, we might as well implement `size_hint`. It doesn't have any effect on perf but it's trivial and might help one day. --- compiler/rustc_ast/src/tokenstream.rs | 18 ++++++++---------- compiler/rustc_ast/src/tokenstream/tests.rs | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index 1ef91d718a8..98caac2c88f 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -836,21 +836,18 @@ impl StableHash for TokenStream { } #[derive(Clone)] -pub struct TokenStreamIter<'t> { - stream: &'t TokenStream, - index: usize, -} +pub struct TokenStreamIter<'t>(std::slice::Iter<'t, TokenTree>); impl<'t> TokenStreamIter<'t> { fn new(stream: &'t TokenStream) -> Self { - TokenStreamIter { stream, index: 0 } + TokenStreamIter(stream.0.as_slice().iter()) } // Peeking could be done via `Peekable`, but most iterators need peeking, // and this is simple and avoids the need to use `peekable` and `Peekable` // at all the use sites. pub fn peek(&self) -> Option<&'t TokenTree> { - self.stream.0.get(self.index) + self.0.as_slice().first() } } @@ -858,10 +855,11 @@ impl<'t> Iterator for TokenStreamIter<'t> { type Item = &'t TokenTree; fn next(&mut self) -> Option<&'t TokenTree> { - self.stream.0.get(self.index).map(|tree| { - self.index += 1; - tree - }) + self.0.next() + } + + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() } } diff --git a/compiler/rustc_ast/src/tokenstream/tests.rs b/compiler/rustc_ast/src/tokenstream/tests.rs index 9b79e5fabf1..6c7e82a97c5 100644 --- a/compiler/rustc_ast/src/tokenstream/tests.rs +++ b/compiler/rustc_ast/src/tokenstream/tests.rs @@ -9,5 +9,5 @@ fn test_token_stream_iter() { assert_eq!(ts.len(), 1); let iter = ts.iter(); - assert_eq!(iter.size_hint(), (0, None)); + assert_eq!(iter.size_hint(), (1, Some(1))); }