633: use ToOwned trait instead of inherent method r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-24 23:10:09 +00:00
commit 1d4b421aad
4 changed files with 523 additions and 8 deletions

View File

@ -15,12 +15,13 @@ use crate::{
/// 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<Repr = rowan::SyntaxNode<RaTypes>> {
pub trait AstNode:
rowan::TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>> + ToOwned<Owned = TreeArc<Self>>
{
fn cast(syntax: &SyntaxNode) -> Option<&Self>
where
Self: Sized;
fn syntax(&self) -> &SyntaxNode;
fn to_owned(&self) -> TreeArc<Self>;
}
pub trait AstToken: AstNode {

File diff suppressed because it is too large Load Diff

View File

@ -48,6 +48,10 @@ impl AstNode for {{ node }} {
}
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl ToOwned for {{ node }} {
type Owned = TreeArc<{{ node }}>;
fn to_owned(&self) -> TreeArc<{{ node }}> { TreeArc::cast(self.syntax.to_owned()) }
}
@ -79,6 +83,10 @@ impl AstNode for {{ node }} {
}
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl ToOwned for {{ node }} {
type Owned = TreeArc<{{ node }}>;
fn to_owned(&self) -> TreeArc<{{ node }}> { TreeArc::cast(self.syntax.to_owned()) }
}

View File

@ -2,10 +2,11 @@ mod builder;
pub mod syntax_error;
mod syntax_text;
use std::{fmt, borrow::Borrow};
use self::syntax_text::SyntaxText;
use crate::{SmolStr, SyntaxKind, TextRange};
use rowan::{Types, TransparentNewType};
use std::fmt;
pub(crate) use self::builder::GreenBuilder;
pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
@ -25,6 +26,12 @@ pub struct TreeArc<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>>(
pub(crate) rowan::TreeArc<RaTypes, T>,
);
impl<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>> Borrow<T> for TreeArc<T> {
fn borrow(&self) -> &T {
&*self
}
}
impl<T> TreeArc<T>
where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
@ -124,6 +131,14 @@ impl SyntaxNode {
}
}
impl ToOwned for SyntaxNode {
type Owned = TreeArc<SyntaxNode>;
fn to_owned(&self) -> TreeArc<SyntaxNode> {
let ptr = TreeArc(self.0.to_owned());
TreeArc::cast(ptr)
}
}
impl SyntaxNode {
pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
self.0.root_data()
@ -133,11 +148,6 @@ impl SyntaxNode {
self.0.replace_self(replacement)
}
pub fn to_owned(&self) -> TreeArc<SyntaxNode> {
let ptr = TreeArc(self.0.to_owned());
TreeArc::cast(ptr)
}
pub fn kind(&self) -> SyntaxKind {
self.0.kind()
}