1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
mod builder;
mod green;
mod red;
mod syntax;
use std::{
sync::Arc,
ptr,
};
pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxError};
pub(crate) use self::{
builder::GreenBuilder,
green::GreenNode,
red::RedNode,
};
#[derive(Debug)]
pub struct SyntaxRoot {
red: RedNode,
pub(crate) errors: Vec<SyntaxError>,
}
pub trait TreeRoot: Clone + Send + Sync {
fn borrowed(&self) -> RefRoot;
fn owned(&self) -> OwnedRoot;
#[doc(hidden)]
fn syntax_root(&self) -> &SyntaxRoot;
}
#[derive(Clone, Debug)]
pub struct OwnedRoot(Arc<SyntaxRoot>);
#[derive(Clone, Copy, Debug)]
pub struct RefRoot<'a>(&'a OwnedRoot); // TODO: shared_from_this instead of double indirection
impl TreeRoot for OwnedRoot {
fn borrowed(&self) -> RefRoot {
RefRoot(&self)
}
fn owned(&self) -> OwnedRoot {
self.clone()
}
fn syntax_root(&self) -> &SyntaxRoot {
&*self.0
}
}
impl<'a> TreeRoot for RefRoot<'a> {
fn borrowed(&self) -> RefRoot {
*self
}
fn owned(&self) -> OwnedRoot {
self.0.clone()
}
fn syntax_root(&self) -> &SyntaxRoot {
self.0.syntax_root()
}
}
impl SyntaxRoot {
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot {
SyntaxRoot {
red: RedNode::new_root(green),
errors,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub(crate) struct RedPtr(ptr::NonNull<RedNode>);
unsafe impl Send for RedPtr {}
unsafe impl Sync for RedPtr {}
impl RedPtr {
fn new(red: &RedNode) -> RedPtr {
RedPtr(red.into())
}
unsafe fn get<'a>(self, _root: &'a impl TreeRoot) -> &'a RedNode {
&*self.0.as_ptr()
}
}
#[test]
fn assert_send_sync() {
fn f<T: Send + Sync>() {}
f::<GreenNode>();
f::<RedNode>();
f::<SyntaxNode>();
}
|