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
use toolshed::CopyCell;
use std::ops::Deref;
use std::fmt::{self, Debug};
use crate::ast::Loc;
#[derive(Clone, Copy)]
pub struct Node<'ast, T> {
inner: CopyCell<&'ast Loc<T>>
}
impl<'ast, T: 'ast> Node<'ast, T> {
#[inline]
pub fn new(ptr: &'ast Loc<T>) -> Self {
Node {
inner: CopyCell::new(ptr)
}
}
#[inline]
pub fn set(&self, ptr: &'ast Loc<T>) {
self.inner.set(ptr)
}
#[inline]
pub fn get_mut(&mut self) -> &mut &'ast Loc<T> {
self.inner.get_mut()
}
}
impl<'ast, T: 'ast> Deref for Node<'ast, T> {
type Target = Loc<T>;
#[inline]
fn deref(&self) -> &Self::Target {
self.inner.get()
}
}
impl<'ast, T: 'ast + PartialEq> PartialEq for Node<'ast, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.deref().eq(other.deref())
}
}
impl<'ast, T: 'ast + Debug> Debug for Node<'ast, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(self.deref(), f)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn ptr() {
let one = Loc::new(0, 0, "one");
let two = Loc::new(0, 0, "two");
let one_ptr = Node::new(&one);
let two_ptr = one_ptr;
assert_eq!(*one_ptr, Loc::new(0, 0, "one"));
assert_eq!(*two_ptr, Loc::new(0, 0, "one"));
two_ptr.set(&two);
assert_eq!(*one_ptr, Loc::new(0, 0, "one"));
assert_eq!(*two_ptr, Loc::new(0, 0, "two"));
}
}