From ff1c82216cc05f2621a301e30ab7a1102dea9d2b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 8 Sep 2018 01:16:07 +0300 Subject: Remove dyn dispatch --- crates/libsyntax2/src/yellow/syntax.rs | 37 ++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'crates/libsyntax2/src/yellow/syntax.rs') diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs index 444dbeb30..1d99cab4a 100644 --- a/crates/libsyntax2/src/yellow/syntax.rs +++ b/crates/libsyntax2/src/yellow/syntax.rs @@ -1,6 +1,7 @@ use std::{ fmt, sync::Arc, hash::{Hasher, Hash}, + ops::Range, }; use smol_str::SmolStr; @@ -93,17 +94,11 @@ impl SyntaxNode { SyntaxText::new(self.borrowed()) } - pub fn children(&self) -> impl Iterator> { - let red = self.red; - let n_children = self.red().n_children(); - let root = self.root.clone(); - (0..n_children).map(move |i| { - let red = unsafe { red.get(root.syntax_root()) }; - SyntaxNode { - root: root.clone(), - red: red.get_child(i).unwrap(), - } - }) + pub fn children(&self) -> SyntaxNodeChildren { + SyntaxNodeChildren { + parent: self.clone(), + iter: (0..self.red().n_children()) + } } pub fn parent(&self) -> Option> { @@ -192,6 +187,26 @@ impl fmt::Debug for SyntaxNode { } } +#[derive(Debug)] +pub struct SyntaxNodeChildren { + parent: SyntaxNode, + iter: Range, +} + +impl Iterator for SyntaxNodeChildren { + type Item = SyntaxNode; + + fn next(&mut self) -> Option> { + self.iter.next().map(|i| { + let red = self.parent.red(); + SyntaxNode { + root: self.parent.root.clone(), + red: red.get_child(i).unwrap(), + } + }) + } +} + fn has_short_text(kind: SyntaxKind) -> bool { match kind { IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true, -- cgit v1.2.3