aboutsummaryrefslogtreecommitdiff
path: root/src/yellow/mod.rs
blob: 236328a7f220a9abf987bd8963e05322b8cce9ad (plain)
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
mod green;
mod red;
mod syntax;

use std::{
    sync::{Arc, Weak},
    ops::Deref,
    mem
};
pub(crate) use self::{
    green::{GreenNode, TextLen},
    red::RedNode,
    syntax::SError,
};
pub use self::syntax::SyntaxNode;

// This could be just `*const T`, but we use `Weak` for additional checks
#[derive(Debug)]
pub(crate) struct Ptr<T>(Weak<T>);

impl<T> Clone for Ptr<T> {
    fn clone(&self) -> Self {
        Ptr(self.0.clone())
    }
}

impl<T> Ptr<T> {
    fn clone(self_: &Ptr<T>) -> Ptr<T> {
        Ptr(Weak::clone(&self_.0))
    }

    fn new(arc: &Arc<T>) -> Ptr<T> {
        Ptr(Arc::downgrade(arc))
    }

    unsafe fn get(&self) -> &T {
        let t = self.0.upgrade()
            .expect("caller must guarantee that Ptr is not null");
        let t: &T = &*t;
        mem::transmute(t)
    }
}