diff options
author | Aleksey Kladov <[email protected]> | 2019-01-07 13:42:10 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-08 08:20:15 +0000 |
commit | b73c51ff9bdf96ea22af0b9c431f201dcc8ddcd3 (patch) | |
tree | 6708f69cb7de64fe0d5a48f2bb2220f4743eede2 /crates | |
parent | 5618c8ade1885ca17a56d9303a7559b332ae3e9e (diff) |
wrap TreePtr
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_syntax/src/yellow.rs | 52 |
1 files changed, 45 insertions, 7 deletions
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 { | |||
18 | type RootData = Vec<SyntaxError>; | 18 | type RootData = Vec<SyntaxError>; |
19 | } | 19 | } |
20 | 20 | ||
21 | pub type GreenNode = ::rowan::GreenNode<RaTypes>; | 21 | pub type GreenNode = rowan::GreenNode<RaTypes>; |
22 | pub type TreePtr<T> = ::rowan::TreePtr<RaTypes, T>; | 22 | |
23 | #[derive(Clone, PartialEq, Eq, Hash)] | ||
24 | pub struct TreePtr<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>>( | ||
25 | pub(crate) rowan::TreePtr<RaTypes, T>, | ||
26 | ); | ||
27 | |||
28 | impl<T> TreePtr<T> | ||
29 | where | ||
30 | T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>, | ||
31 | { | ||
32 | pub(crate) fn cast<U>(this: TreePtr<T>) -> TreePtr<U> | ||
33 | where | ||
34 | U: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>, | ||
35 | { | ||
36 | TreePtr(rowan::TreePtr::cast(this.0)) | ||
37 | } | ||
38 | } | ||
39 | |||
40 | impl<T> std::ops::Deref for TreePtr<T> | ||
41 | where | ||
42 | T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>, | ||
43 | { | ||
44 | type Target = T; | ||
45 | fn deref(&self) -> &T { | ||
46 | self.0.deref() | ||
47 | } | ||
48 | } | ||
49 | |||
50 | impl<T> fmt::Debug for TreePtr<T> | ||
51 | where | ||
52 | T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>, | ||
53 | T: fmt::Debug, | ||
54 | { | ||
55 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
56 | fmt::Debug::fmt(&self.0, fmt) | ||
57 | } | ||
58 | } | ||
23 | 59 | ||
24 | #[derive(PartialEq, Eq, Hash)] | 60 | #[derive(PartialEq, Eq, Hash)] |
25 | #[repr(transparent)] | 61 | #[repr(transparent)] |
26 | pub struct SyntaxNode(pub(crate) ::rowan::SyntaxNode<RaTypes>); | 62 | pub struct SyntaxNode(pub(crate) rowan::SyntaxNode<RaTypes>); |
27 | unsafe impl TransparentNewType for SyntaxNode { | 63 | unsafe impl TransparentNewType for SyntaxNode { |
28 | type Repr = ::rowan::SyntaxNode<RaTypes>; | 64 | type Repr = rowan::SyntaxNode<RaTypes>; |
29 | } | 65 | } |
30 | 66 | ||
31 | impl SyntaxNode { | 67 | impl SyntaxNode { |
32 | pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SyntaxNode> { | 68 | pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SyntaxNode> { |
33 | TreePtr::cast(::rowan::SyntaxNode::new(green, errors)) | 69 | let ptr = TreePtr(rowan::SyntaxNode::new(green, errors)); |
70 | TreePtr::cast(ptr) | ||
34 | } | 71 | } |
35 | } | 72 | } |
36 | 73 | ||
@@ -75,7 +112,8 @@ impl SyntaxNode { | |||
75 | self.0.replace_self(replacement) | 112 | self.0.replace_self(replacement) |
76 | } | 113 | } |
77 | pub fn to_owned(&self) -> TreePtr<SyntaxNode> { | 114 | pub fn to_owned(&self) -> TreePtr<SyntaxNode> { |
78 | TreePtr::cast(self.0.to_owned()) | 115 | let ptr = TreePtr(self.0.to_owned()); |
116 | TreePtr::cast(ptr) | ||
79 | } | 117 | } |
80 | pub fn kind(&self) -> SyntaxKind { | 118 | pub fn kind(&self) -> SyntaxKind { |
81 | self.0.kind() | 119 | self.0.kind() |
@@ -120,7 +158,7 @@ impl fmt::Debug for SyntaxNode { | |||
120 | } | 158 | } |
121 | 159 | ||
122 | #[derive(Debug)] | 160 | #[derive(Debug)] |
123 | pub struct SyntaxNodeChildren<'a>(::rowan::SyntaxNodeChildren<'a, RaTypes>); | 161 | pub struct SyntaxNodeChildren<'a>(rowan::SyntaxNodeChildren<'a, RaTypes>); |
124 | 162 | ||
125 | impl<'a> Iterator for SyntaxNodeChildren<'a> { | 163 | impl<'a> Iterator for SyntaxNodeChildren<'a> { |
126 | type Item = &'a SyntaxNode; | 164 | type Item = &'a SyntaxNode; |