diff options
author | Aleksey Kladov <[email protected]> | 2018-09-07 23:16:07 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-07 23:16:07 +0100 |
commit | ff1c82216cc05f2621a301e30ab7a1102dea9d2b (patch) | |
tree | 393fb79e9fb06afe26eddfa66a59113a10dba26c /crates/libsyntax2/src/yellow/syntax.rs | |
parent | fcfda94664b454f60be2dbc1b564ed63aa4c3ec5 (diff) |
Remove dyn dispatch
Diffstat (limited to 'crates/libsyntax2/src/yellow/syntax.rs')
-rw-r--r-- | crates/libsyntax2/src/yellow/syntax.rs | 37 |
1 files changed, 26 insertions, 11 deletions
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 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fmt, sync::Arc, | 2 | fmt, sync::Arc, |
3 | hash::{Hasher, Hash}, | 3 | hash::{Hasher, Hash}, |
4 | ops::Range, | ||
4 | }; | 5 | }; |
5 | 6 | ||
6 | use smol_str::SmolStr; | 7 | use smol_str::SmolStr; |
@@ -93,17 +94,11 @@ impl<R: TreeRoot> SyntaxNode<R> { | |||
93 | SyntaxText::new(self.borrowed()) | 94 | SyntaxText::new(self.borrowed()) |
94 | } | 95 | } |
95 | 96 | ||
96 | pub fn children(&self) -> impl Iterator<Item = SyntaxNode<R>> { | 97 | pub fn children(&self) -> SyntaxNodeChildren<R> { |
97 | let red = self.red; | 98 | SyntaxNodeChildren { |
98 | let n_children = self.red().n_children(); | 99 | parent: self.clone(), |
99 | let root = self.root.clone(); | 100 | iter: (0..self.red().n_children()) |
100 | (0..n_children).map(move |i| { | 101 | } |
101 | let red = unsafe { red.get(root.syntax_root()) }; | ||
102 | SyntaxNode { | ||
103 | root: root.clone(), | ||
104 | red: red.get_child(i).unwrap(), | ||
105 | } | ||
106 | }) | ||
107 | } | 102 | } |
108 | 103 | ||
109 | pub fn parent(&self) -> Option<SyntaxNode<R>> { | 104 | pub fn parent(&self) -> Option<SyntaxNode<R>> { |
@@ -192,6 +187,26 @@ impl<R: TreeRoot> fmt::Debug for SyntaxNode<R> { | |||
192 | } | 187 | } |
193 | } | 188 | } |
194 | 189 | ||
190 | #[derive(Debug)] | ||
191 | pub struct SyntaxNodeChildren<R: TreeRoot> { | ||
192 | parent: SyntaxNode<R>, | ||
193 | iter: Range<usize>, | ||
194 | } | ||
195 | |||
196 | impl<R: TreeRoot> Iterator for SyntaxNodeChildren<R> { | ||
197 | type Item = SyntaxNode<R>; | ||
198 | |||
199 | fn next(&mut self) -> Option<SyntaxNode<R>> { | ||
200 | self.iter.next().map(|i| { | ||
201 | let red = self.parent.red(); | ||
202 | SyntaxNode { | ||
203 | root: self.parent.root.clone(), | ||
204 | red: red.get_child(i).unwrap(), | ||
205 | } | ||
206 | }) | ||
207 | } | ||
208 | } | ||
209 | |||
195 | fn has_short_text(kind: SyntaxKind) -> bool { | 210 | fn has_short_text(kind: SyntaxKind) -> bool { |
196 | match kind { | 211 | match kind { |
197 | IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true, | 212 | IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true, |