From d402974aa0af6de290245a9d2a69a5d56c4fa610 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 18 Jul 2019 19:23:05 +0300 Subject: migrate ra_syntax to the new rowan API --- crates/ra_syntax/src/ast.rs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'crates/ra_syntax/src/ast.rs') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 3dcf39f7e..fe00e78d1 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -9,7 +9,7 @@ mod expr_extensions; use std::marker::PhantomData; use crate::{ - syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken, TreeArc}, + syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken}, SmolStr, }; @@ -25,51 +25,49 @@ pub use self::{ /// conversion itself has zero runtime cost: ast and syntax nodes have exactly /// the same representation: a pointer to the tree root and a pointer to the /// node itself. -pub trait AstNode: - rowan::TransparentNewType + ToOwned> -{ - fn cast(syntax: &SyntaxNode) -> Option<&Self> +pub trait AstNode { + fn cast(syntax: SyntaxNode) -> Option where Self: Sized; fn syntax(&self) -> &SyntaxNode; } /// Like `AstNode`, but wraps tokens rather than interior nodes. -pub trait AstToken<'a> { - fn cast(token: SyntaxToken<'a>) -> Option +pub trait AstToken { + fn cast(token: SyntaxToken) -> Option where Self: Sized; - fn syntax(&self) -> SyntaxToken<'a>; - fn text(&self) -> &'a SmolStr { + fn syntax(&self) -> &SyntaxToken; + fn text(&self) -> &SmolStr { self.syntax().text() } } /// An iterator over `SyntaxNode` children of a particular AST type. #[derive(Debug)] -pub struct AstChildren<'a, N> { - inner: SyntaxNodeChildren<'a>, +pub struct AstChildren { + inner: SyntaxNodeChildren, ph: PhantomData, } -impl<'a, N> AstChildren<'a, N> { - fn new(parent: &'a SyntaxNode) -> Self { +impl AstChildren { + fn new(parent: &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> { +impl Iterator for AstChildren { + type Item = N; + fn next(&mut self) -> Option { self.inner.by_ref().find_map(N::cast) } } -fn child_opt(parent: &P) -> Option<&C> { +fn child_opt(parent: &P) -> Option { children(parent).next() } -fn children(parent: &P) -> AstChildren { +fn children(parent: &P) -> AstChildren { AstChildren::new(parent.syntax()) } @@ -123,7 +121,7 @@ fn test_doc_comment_preserves_indents() { #[test] fn test_where_predicates() { - fn assert_bound(text: &str, bound: Option<&TypeBound>) { + fn assert_bound(text: &str, bound: Option) { assert_eq!(text, bound.unwrap().syntax().text().to_string()); } -- cgit v1.2.3