From b73c51ff9bdf96ea22af0b9c431f201dcc8ddcd3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 7 Jan 2019 16:42:10 +0300 Subject: wrap TreePtr --- crates/ra_syntax/src/yellow.rs | 52 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 7 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/yellow.rs b/crates/ra_syntax/src/yellow.rs index 6dc846f33..38e680a9c 100644 --- a/crates/ra_syntax/src/yellow.rs +++ b/crates/ra_syntax/src/yellow.rs @@ -18,19 +18,56 @@ impl Types for RaTypes { type RootData = Vec; } -pub type GreenNode = ::rowan::GreenNode; -pub type TreePtr = ::rowan::TreePtr; +pub type GreenNode = rowan::GreenNode; + +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct TreePtr>>( + pub(crate) rowan::TreePtr, +); + +impl TreePtr +where + T: TransparentNewType>, +{ + pub(crate) fn cast(this: TreePtr) -> TreePtr + where + U: TransparentNewType>, + { + TreePtr(rowan::TreePtr::cast(this.0)) + } +} + +impl std::ops::Deref for TreePtr +where + T: TransparentNewType>, +{ + type Target = T; + fn deref(&self) -> &T { + self.0.deref() + } +} + +impl fmt::Debug for TreePtr +where + T: TransparentNewType>, + T: fmt::Debug, +{ + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&self.0, fmt) + } +} #[derive(PartialEq, Eq, Hash)] #[repr(transparent)] -pub struct SyntaxNode(pub(crate) ::rowan::SyntaxNode); +pub struct SyntaxNode(pub(crate) rowan::SyntaxNode); unsafe impl TransparentNewType for SyntaxNode { - type Repr = ::rowan::SyntaxNode; + type Repr = rowan::SyntaxNode; } impl SyntaxNode { pub(crate) fn new(green: GreenNode, errors: Vec) -> TreePtr { - TreePtr::cast(::rowan::SyntaxNode::new(green, errors)) + let ptr = TreePtr(rowan::SyntaxNode::new(green, errors)); + TreePtr::cast(ptr) } } @@ -75,7 +112,8 @@ impl SyntaxNode { self.0.replace_self(replacement) } pub fn to_owned(&self) -> TreePtr { - TreePtr::cast(self.0.to_owned()) + let ptr = TreePtr(self.0.to_owned()); + TreePtr::cast(ptr) } pub fn kind(&self) -> SyntaxKind { self.0.kind() @@ -120,7 +158,7 @@ impl fmt::Debug for SyntaxNode { } #[derive(Debug)] -pub struct SyntaxNodeChildren<'a>(::rowan::SyntaxNodeChildren<'a, RaTypes>); +pub struct SyntaxNodeChildren<'a>(rowan::SyntaxNodeChildren<'a, RaTypes>); impl<'a> Iterator for SyntaxNodeChildren<'a> { type Item = &'a SyntaxNode; -- cgit v1.2.3