diff options
author | Aleksey Kladov <[email protected]> | 2018-08-17 19:10:55 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-17 19:10:55 +0100 |
commit | 70097504f78c4c41368a0b864a94df95fb9c27f7 (patch) | |
tree | c4034cab3c0d0b9488c0be538ae1d4c286570de9 /crates/libsyntax2/src/yellow | |
parent | ed7ae78c6fd9e508f6e959c6a164cf8481f6b377 (diff) |
hide root
Diffstat (limited to 'crates/libsyntax2/src/yellow')
-rw-r--r-- | crates/libsyntax2/src/yellow/mod.rs | 40 | ||||
-rw-r--r-- | crates/libsyntax2/src/yellow/syntax.rs | 16 |
2 files changed, 43 insertions, 13 deletions
diff --git a/crates/libsyntax2/src/yellow/mod.rs b/crates/libsyntax2/src/yellow/mod.rs index ff3bb221b..3c4510fe7 100644 --- a/crates/libsyntax2/src/yellow/mod.rs +++ b/crates/libsyntax2/src/yellow/mod.rs | |||
@@ -4,7 +4,6 @@ mod red; | |||
4 | mod syntax; | 4 | mod syntax; |
5 | 5 | ||
6 | use std::{ | 6 | use std::{ |
7 | ops::Deref, | ||
8 | sync::Arc, | 7 | sync::Arc, |
9 | ptr, | 8 | ptr, |
10 | }; | 9 | }; |
@@ -15,17 +14,48 @@ pub(crate) use self::{ | |||
15 | red::RedNode, | 14 | red::RedNode, |
16 | }; | 15 | }; |
17 | 16 | ||
18 | pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone + Send + Sync {} | ||
19 | |||
20 | #[derive(Debug)] | 17 | #[derive(Debug)] |
21 | pub struct SyntaxRoot { | 18 | pub struct SyntaxRoot { |
22 | red: RedNode, | 19 | red: RedNode, |
23 | pub(crate) errors: Vec<SyntaxError>, | 20 | pub(crate) errors: Vec<SyntaxError>, |
24 | } | 21 | } |
25 | 22 | ||
26 | impl TreeRoot for Arc<SyntaxRoot> {} | 23 | pub trait TreeRoot: Clone + Send + Sync { |
24 | fn borrowed(&self) -> RefRoot; | ||
25 | fn owned(&self) -> OwnedRoot; | ||
26 | |||
27 | #[doc(hidden)] | ||
28 | fn syntax_root(&self) -> &SyntaxRoot; | ||
29 | } | ||
30 | #[derive(Clone, Debug)] | ||
31 | pub struct OwnedRoot(Arc<SyntaxRoot>); | ||
32 | #[derive(Clone, Copy, Debug)] | ||
33 | pub struct RefRoot<'a>(&'a OwnedRoot); // TODO: shared_from_this instead of double indirection | ||
27 | 34 | ||
28 | impl<'a> TreeRoot for &'a SyntaxRoot {} | 35 | impl TreeRoot for OwnedRoot { |
36 | fn borrowed(&self) -> RefRoot { | ||
37 | RefRoot(&self) | ||
38 | } | ||
39 | fn owned(&self) -> OwnedRoot { | ||
40 | self.clone() | ||
41 | } | ||
42 | |||
43 | fn syntax_root(&self) -> &SyntaxRoot { | ||
44 | &*self.0 | ||
45 | } | ||
46 | } | ||
47 | |||
48 | impl<'a> TreeRoot for RefRoot<'a> { | ||
49 | fn borrowed(&self) -> RefRoot { | ||
50 | *self | ||
51 | } | ||
52 | fn owned(&self) -> OwnedRoot { | ||
53 | self.0.clone() | ||
54 | } | ||
55 | fn syntax_root(&self) -> &SyntaxRoot { | ||
56 | self.0.syntax_root() | ||
57 | } | ||
58 | } | ||
29 | 59 | ||
30 | impl SyntaxRoot { | 60 | impl SyntaxRoot { |
31 | pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot { | 61 | pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot { |
diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs index bb390751a..ef82ba408 100644 --- a/crates/libsyntax2/src/yellow/syntax.rs +++ b/crates/libsyntax2/src/yellow/syntax.rs | |||
@@ -3,14 +3,14 @@ use std::{fmt, sync::Arc}; | |||
3 | use smol_str::SmolStr; | 3 | use smol_str::SmolStr; |
4 | 4 | ||
5 | use { | 5 | use { |
6 | yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr}, | 6 | yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr, RefRoot, OwnedRoot}, |
7 | SyntaxKind::{self, *}, | 7 | SyntaxKind::{self, *}, |
8 | TextRange, TextUnit, | 8 | TextRange, TextUnit, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | 11 | ||
12 | #[derive(Clone, Copy)] | 12 | #[derive(Clone, Copy)] |
13 | pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> { | 13 | pub struct SyntaxNode<R: TreeRoot = OwnedRoot> { |
14 | pub(crate) root: R, | 14 | pub(crate) root: R, |
15 | // Guaranteed to not dangle, because `root` holds a | 15 | // Guaranteed to not dangle, because `root` holds a |
16 | // strong reference to red's ancestor | 16 | // strong reference to red's ancestor |
@@ -28,7 +28,7 @@ impl<R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> { | |||
28 | 28 | ||
29 | impl<R: TreeRoot> Eq for SyntaxNode<R> {} | 29 | impl<R: TreeRoot> Eq for SyntaxNode<R> {} |
30 | 30 | ||
31 | pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; | 31 | pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; |
32 | 32 | ||
33 | #[test] | 33 | #[test] |
34 | fn syntax_node_ref_is_copy() { | 34 | fn syntax_node_ref_is_copy() { |
@@ -42,18 +42,18 @@ pub struct SyntaxError { | |||
42 | pub offset: TextUnit, | 42 | pub offset: TextUnit, |
43 | } | 43 | } |
44 | 44 | ||
45 | impl SyntaxNode<Arc<SyntaxRoot>> { | 45 | impl SyntaxNode<OwnedRoot> { |
46 | pub(crate) fn new_owned(root: SyntaxRoot) -> Self { | 46 | pub(crate) fn new_owned(root: SyntaxRoot) -> Self { |
47 | let root = Arc::new(root); | 47 | let root = OwnedRoot(Arc::new(root)); |
48 | let red = RedPtr::new(&root.red); | 48 | let red = RedPtr::new(&root.syntax_root().red); |
49 | SyntaxNode { root, red } | 49 | SyntaxNode { root, red } |
50 | } | 50 | } |
51 | } | 51 | } |
52 | 52 | ||
53 | impl<R: TreeRoot> SyntaxNode<R> { | 53 | impl<R: TreeRoot> SyntaxNode<R> { |
54 | pub fn as_ref<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { | 54 | pub fn as_ref<'a>(&'a self) -> SyntaxNode<RefRoot<'a>> { |
55 | SyntaxNode { | 55 | SyntaxNode { |
56 | root: &*self.root, | 56 | root: self.root.borrowed(), |
57 | red: self.red, | 57 | red: self.red, |
58 | } | 58 | } |
59 | } | 59 | } |