1
Fork 0

use CursorRef more, to not to clone Trees

This commit is contained in:
klensy 2022-05-16 18:58:15 +03:00
parent 10b3a0d209
commit cc5f3e21ac
10 changed files with 35 additions and 24 deletions

View file

@ -442,8 +442,8 @@ impl TokenStream {
}
}
pub fn trees(&self) -> Cursor {
self.clone().into_trees()
pub fn trees(&self) -> CursorRef<'_> {
CursorRef::new(self)
}
pub fn into_trees(self) -> Cursor {
@ -538,12 +538,21 @@ pub struct CursorRef<'t> {
}
impl<'t> CursorRef<'t> {
fn new(stream: &'t TokenStream) -> Self {
CursorRef { stream, index: 0 }
}
#[inline]
fn next_with_spacing(&mut self) -> Option<&'t TreeAndSpacing> {
self.stream.0.get(self.index).map(|tree| {
self.index += 1;
tree
})
}
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
}
}
impl<'t> Iterator for CursorRef<'t> {