From 70b337292117a9bb90e85056dcb4069f8bbc6c0a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 23:20:02 +0300 Subject: Don't allocate when traversing children --- src/yellow/syntax.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src/yellow/syntax.rs') diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index 19a9b8ac2..5c31a3f35 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs @@ -11,8 +11,8 @@ impl TreeRoot for Arc {} impl<'a> TreeRoot for &'a SyntaxRoot {} #[derive(Clone, Copy)] -pub struct SyntaxNode> { - pub(crate) root: ROOT, +pub struct SyntaxNode> { + pub(crate) root: R, // Guaranteed to not dangle, because `root` holds a // strong reference to red's ancestor red: ptr::NonNull, @@ -52,7 +52,7 @@ impl SyntaxNode> { } } -impl SyntaxNode { +impl SyntaxNode { pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { SyntaxNode { root: &*self.root, @@ -73,20 +73,18 @@ impl SyntaxNode { self.red().green().text() } - pub fn children(&self) -> Vec> { + pub fn children<'a>(&'a self) -> impl Iterator> + 'a { let red = self.red(); let n_children = red.n_children(); - let mut res = Vec::with_capacity(n_children); - for i in 0..n_children { - res.push(SyntaxNode { + (0..n_children).map(move |i| { + SyntaxNode { root: self.root.clone(), red: red.nth_child(i), - }); - } - res + } + }) } - pub fn parent(&self) -> Option> { + pub fn parent(&self) -> Option> { let parent = self.red().parent()?; Some(SyntaxNode { root: self.root.clone(), @@ -99,7 +97,7 @@ impl SyntaxNode { } } -impl fmt::Debug for SyntaxNode { +impl fmt::Debug for SyntaxNode { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; if has_short_text(self.kind()) { -- cgit v1.2.3