diff options
Diffstat (limited to 'src/yellow/mod.rs')
-rw-r--r-- | src/yellow/mod.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/yellow/mod.rs b/src/yellow/mod.rs new file mode 100644 index 000000000..236328a7f --- /dev/null +++ b/src/yellow/mod.rs | |||
@@ -0,0 +1,42 @@ | |||
1 | mod green; | ||
2 | mod red; | ||
3 | mod syntax; | ||
4 | |||
5 | use std::{ | ||
6 | sync::{Arc, Weak}, | ||
7 | ops::Deref, | ||
8 | mem | ||
9 | }; | ||
10 | pub(crate) use self::{ | ||
11 | green::{GreenNode, TextLen}, | ||
12 | red::RedNode, | ||
13 | syntax::SError, | ||
14 | }; | ||
15 | pub use self::syntax::SyntaxNode; | ||
16 | |||
17 | // This could be just `*const T`, but we use `Weak` for additional checks | ||
18 | #[derive(Debug)] | ||
19 | pub(crate) struct Ptr<T>(Weak<T>); | ||
20 | |||
21 | impl<T> Clone for Ptr<T> { | ||
22 | fn clone(&self) -> Self { | ||
23 | Ptr(self.0.clone()) | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl<T> Ptr<T> { | ||
28 | fn clone(self_: &Ptr<T>) -> Ptr<T> { | ||
29 | Ptr(Weak::clone(&self_.0)) | ||
30 | } | ||
31 | |||
32 | fn new(arc: &Arc<T>) -> Ptr<T> { | ||
33 | Ptr(Arc::downgrade(arc)) | ||
34 | } | ||
35 | |||
36 | unsafe fn get(&self) -> &T { | ||
37 | let t = self.0.upgrade() | ||
38 | .expect("caller must guarantee that Ptr is not null"); | ||
39 | let t: &T = &*t; | ||
40 | mem::transmute(t) | ||
41 | } | ||
42 | } | ||