From d91a98ec843ec8562c58ccb01a1e29d00cc744dc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 7 Jan 2019 16:15:47 +0300 Subject: switch ra_syntax to new rowan API --- crates/ra_syntax/src/ast/generated.rs.tera | 93 +++++++++++++++--------------- 1 file changed, 48 insertions(+), 45 deletions(-) (limited to 'crates/ra_syntax/src/ast/generated.rs.tera') diff --git a/crates/ra_syntax/src/ast/generated.rs.tera b/crates/ra_syntax/src/ast/generated.rs.tera index 131ee09ec..0a20fc78e 100644 --- a/crates/ra_syntax/src/ast/generated.rs.tera +++ b/crates/ra_syntax/src/ast/generated.rs.tera @@ -11,89 +11,92 @@ the below applies to the result of this template #![cfg_attr(rustfmt, rustfmt_skip)] -use std::hash::{Hash, Hasher}; +use rowan::TransparentNewType; use crate::{ - ast, - SyntaxNode, SyntaxNodeRef, AstNode, - yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot}, - SyntaxKind::*, + SyntaxNode, SyntaxKind::*, + yellow::{RaTypes, TreePtr}, + ast::{self, AstNode}, }; {% for node, methods in ast %} // {{ node }} {%- if methods.enum %} +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct {{ node }} { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for {{ node }} { + type Repr = rowan::SyntaxNode; +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum {{ node }}<'a> { +pub enum {{ node }}Kind<'a> { {%- for kind in methods.enum %} - {{ kind }}({{ kind }}<'a>), + {{ kind }}(&'a {{ kind }}), {%- endfor %} } -impl<'a> AstNode<'a> for {{ node }}<'a> { - fn cast(syntax: SyntaxNodeRef<'a>) -> Option { +impl AstNode for {{ node }} { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { match syntax.kind() { -{%- for kind in methods.enum %} - {{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })), -{%- endfor %} + {%- for kind in methods.enum %} + | {{ kind | SCREAM }} + {%- endfor %} => Some({{ node }}::from_repr(syntax.into_repr())), _ => None, } } - fn syntax(self) -> SyntaxNodeRef<'a> { - match self { -{%- for kind in methods.enum %} - {{ node }}::{{ kind }}(inner) => inner.syntax(), -{%- endfor %} + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) } +} + +impl {{ node }} { + pub fn kind(&self) -> {{ node }}Kind { + match self.syntax.kind() { + {%- for kind in methods.enum %} + {{ kind | SCREAM }} => {{ node }}Kind::{{ kind }}({{ kind }}::cast(&self.syntax).unwrap()), + {%- endfor %} + _ => unreachable!(), } } } {% else %} -#[derive(Debug, Clone, Copy,)] -pub struct {{ node }}Node = OwnedRoot> { - pub(crate) syntax: SyntaxNode, +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct {{ node }} { + pub(crate) syntax: SyntaxNode, } -pub type {{ node }}<'a> = {{ node }}Node>; - -impl, R2: TreeRoot> PartialEq<{{node}}Node> for {{node}}Node { - fn eq(&self, other: &{{node}}Node) -> bool { self.syntax == other.syntax } -} -impl> Eq for {{node}}Node {} -impl> Hash for {{node}}Node { - fn hash(&self, state: &mut H) { self.syntax.hash(state) } +unsafe impl TransparentNewType for {{ node }} { + type Repr = rowan::SyntaxNode; } -impl<'a> AstNode<'a> for {{ node }}<'a> { - fn cast(syntax: SyntaxNodeRef<'a>) -> Option { +impl AstNode for {{ node }} { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { match syntax.kind() { - {{ node | SCREAM }} => Some({{ node }} { syntax }), + {{ node | SCREAM }} => Some({{ node }}::from_repr(syntax.into_repr())), _ => None, } } - fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } -} - -impl> {{ node }}Node { - pub fn borrowed(&self) -> {{ node }} { - {{ node }}Node { syntax: self.syntax.borrowed() } - } - pub fn owned(&self) -> {{ node }}Node { - {{ node }}Node { syntax: self.syntax.owned() } - } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) } } {% endif %} {% if methods.traits -%} + {%- for t in methods.traits -%} -impl<'a> ast::{{ t }}<'a> for {{ node }}<'a> {} +impl ast::{{ t }} for {{ node }} {} {% endfor -%} + {%- endif -%} -impl<'a> {{ node }}<'a> { +impl {{ node }} { {%- if methods.collections -%} {%- for m in methods.collections -%} {%- set method_name = m.0 -%} {%- set ChildName = m.1 %} - pub fn {{ method_name }}(self) -> impl Iterator> + 'a { + pub fn {{ method_name }}(&self) -> impl Iterator { super::children(self) } {% endfor -%} @@ -109,7 +112,7 @@ impl<'a> {{ node }}<'a> { {%- set method_name = m.0 -%} {%- set ChildName = m.1 %} {%- endif %} - pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> { + pub fn {{ method_name }}(&self) -> Option<&{{ ChildName }}> { super::child_opt(self) } {% endfor -%} -- cgit v1.2.3