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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::sync::{Arc, Weak};

use derive_more::{Deref, DerefMut, From};
use qcell::{TLCell, TLCellOwner};

use crate::graph::GenericASTNode;
use crate::graph::utils::OptVec;

type CellImpl<T> = TLCell<Ghost, T>;
type CellOwnerImpl = TLCellOwner<Ghost>;

#[derive(Debug)]
pub struct Ghost;

pub struct OwnedNodeImpl<T> {
    pub data: T,
    pub uid: usize,
    pub parent: Option<BorrowedNode<T>>,
    pub edges: OptVec<BorrowedNode<T>>,
}

#[derive(Deref, From)]
#[deref(forward)]
pub struct OwnedNode<T = GenericASTNode>(Arc<CellImpl<OwnedNodeImpl<T>>>);

#[derive(Deref, From)]
pub struct BorrowedNode<T = GenericASTNode>(Weak<CellImpl<OwnedNodeImpl<T>>>);
#[derive(Deref, DerefMut, From)]
pub struct GhostToken(CellOwnerImpl);

#[derive(Deref)]
pub struct RefOwnedNode<'arena, T = GenericASTNode> {
    #[deref]
    arc: Arc<CellImpl<OwnedNodeImpl<T>>>,
    _phantom: PhantomData<&'arena ()>,
}

pub type RefNode<'arena, T = GenericASTNode> = &'arena CellImpl<OwnedNodeImpl<T>>;

impl<T> OwnedNode<T> {
    pub fn new(data: T, uid: usize) -> Self {
        Self(Arc::new(TLCell::new(OwnedNodeImpl {
            data,
            uid,
            parent: None,
            edges: OptVec::Empty,
        })))
    }

    pub fn with_parent(data: T, uid: usize, parent: BorrowedNode<T>) -> Self {
        Self(Arc::new(TLCell::new(OwnedNodeImpl {
            data,
            uid,
            parent: Some(parent),
            edges: OptVec::Empty,
        })))
    }

    pub fn share(&self) -> BorrowedNode<T> {
        BorrowedNode(Arc::downgrade(&self.0))
    }

    pub fn data<'a>(&'a self, token: &'a GhostToken) -> &T {
        &self.0.ro(token).data
    }

    pub fn edges<'a>(&'a self, token: &'a GhostToken) -> &OptVec<BorrowedNode<T>> {
        &self.0.ro(token).edges
    }

    pub fn id(&self, token: &GhostToken) -> usize {
        self.0.ro(token).uid
    }
}

impl GhostToken {
    pub fn new() -> Self {
        Self(TLCellOwner::new())
    }
}

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

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

impl<T: Debug> Debug for OwnedNode<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OwnedNode")
            .field("strong_count", &Arc::strong_count(&self.0))
            .field("weak_count", &Arc::weak_count(&self.0))
            .finish_non_exhaustive()
    }
}

impl<T: Debug> Debug for BorrowedNode<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RcNode")
            .field("strong_count", &Weak::strong_count(&self.0))
            .field("weak_count", &Weak::weak_count(&self.0))
            .finish_non_exhaustive()
    }
}

impl Debug for GhostToken {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GhostToken").finish_non_exhaustive()
    }
}