aboutsummaryrefslogtreecommitdiff
path: root/src/yellow/mod.rs
blob: ff3bb221b229ffc59d85ea937053e06a65a7a0a7 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
mod builder;
mod green;
mod red;
mod syntax;

use std::{
    ops::Deref,
    sync::Arc,
    ptr,
};
pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxError};
pub(crate) use self::{
    builder::GreenBuilder,
    green::GreenNode,
    red::RedNode,
};

pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone + Send + Sync {}

#[derive(Debug)]
pub struct SyntaxRoot {
    red: RedNode,
    pub(crate) errors: Vec<SyntaxError>,
}

impl TreeRoot for Arc<SyntaxRoot> {}

impl<'a> TreeRoot for &'a SyntaxRoot {}

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)]
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>();
}