diff options
Diffstat (limited to 'crates/libsyntax2/src/yellow/mod.rs')
-rw-r--r-- | crates/libsyntax2/src/yellow/mod.rs | 40 |
1 files changed, 35 insertions, 5 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 { |