From f874d372bb7f756dfa6ebc22ca838657c8ff1539 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 2 Apr 2019 10:09:52 +0300 Subject: simplify --- crates/ra_syntax/src/ast.rs | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) (limited to 'crates') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 3e81fa990..9950ab12d 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -30,6 +30,25 @@ pub trait AstNode: fn syntax(&self) -> &SyntaxNode; } +#[derive(Debug)] +pub struct AstChildren<'a, N> { + inner: SyntaxNodeChildren<'a>, + ph: PhantomData, +} + +impl<'a, N> AstChildren<'a, N> { + fn new(parent: &'a SyntaxNode) -> Self { + AstChildren { inner: parent.children(), ph: PhantomData } + } +} + +impl<'a, N: AstNode + 'a> Iterator for AstChildren<'a, N> { + type Item = &'a N; + fn next(&mut self) -> Option<&'a N> { + self.inner.by_ref().find_map(N::cast) + } +} + impl Attr { pub fn is_inner(&self) -> bool { let tt = match self.value() { @@ -331,29 +350,6 @@ fn children(parent: &P) -> AstChildren { AstChildren::new(parent.syntax()) } -#[derive(Debug)] -pub struct AstChildren<'a, N> { - inner: SyntaxNodeChildren<'a>, - ph: PhantomData, -} - -impl<'a, N> AstChildren<'a, N> { - fn new(parent: &'a SyntaxNode) -> Self { - AstChildren { inner: parent.children(), ph: PhantomData } - } -} - -impl<'a, N: AstNode + 'a> Iterator for AstChildren<'a, N> { - type Item = &'a N; - fn next(&mut self) -> Option<&'a N> { - loop { - if let Some(n) = N::cast(self.inner.next()?) { - return Some(n); - } - } - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub enum StructFlavor<'a> { Tuple(&'a PosFieldDefList), -- cgit v1.2.3