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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
mod cell;
mod gc_box;
mod heap;
mod trace;
use std::borrow::Borrow;
use std::convert::AsRef;
use std::fmt::{Debug, Display, Pointer};
use std::hash::Hash;
use std::mem;
use std::num::NonZeroUsize;
use std::ops::Deref;
use std::ptr::NonNull;
pub use cell::*;
pub(crate) use gc_box::GcBox;
pub(crate) use heap::Heap;
pub use trace::Trace;
pub struct Gc<T: Trace + ?Sized> {
pub(crate) ptr: NonNull<GcBox<T>>,
}
impl<T: Trace> Gc<T> {
#[inline]
pub(crate) fn new(value: T) -> Self {
unsafe {
Gc {
ptr: NonNull::new_unchecked(Box::into_raw(Box::new(GcBox::new(value)))),
}
}
}
#[inline]
pub fn addr(&self) -> NonZeroUsize {
unsafe { NonZeroUsize::new_unchecked(mem::transmute(self.ptr)) }
}
}
impl<T: Trace + ?Sized> Gc<T> {
#[inline]
fn inner(&self) -> &GcBox<T> {
unsafe { self.ptr.as_ref() }
}
#[inline]
pub fn is_error(&self) -> bool {
self.inner().is_error.get()
}
#[inline]
pub fn set_error(&self) {
self.inner().is_error.set(true)
}
}
unsafe impl<T: Trace + ?Sized> Trace for Gc<T> {
unsafe fn trace(&self) {
self.inner().marked.set(true);
self.inner().trace()
}
unsafe fn marked(&self) -> bool {
self.inner().marked()
}
}
impl<T: Trace + ?Sized> Deref for Gc<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner().data
}
}
impl<T: Trace + ?Sized> Borrow<T> for Gc<T> {
fn borrow(&self) -> &T {
self
}
}
impl<T: Trace + ?Sized> AsRef<T> for Gc<T> {
fn as_ref(&self) -> &T {
self
}
}
impl<T: Trace> Clone for Gc<T> {
fn clone(&self) -> Self {
Self { ptr: self.ptr }
}
}
impl<T: Trace> Copy for Gc<T> {}
impl<T: Trace + ?Sized> Pointer for Gc<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Pointer::fmt(&self.inner(), f)
}
}
impl<T: Trace + Debug> Debug for Gc<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.inner().data)
}
}
impl<T: Trace + Display> Display for Gc<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner().data)
}
}
impl<T: Trace + PartialEq> PartialEq for Gc<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.inner().data.eq(&other.inner().data)
}
}
impl<T: Trace> Hash for Gc<T> {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.ptr.hash(state);
}
}
impl<T: Trace + Eq> Eq for Gc<T> {}
impl<T: Trace + PartialOrd> PartialOrd for Gc<T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.inner().data.partial_cmp(&other.inner().data)
}
}
impl<T: Trace + Ord> Ord for Gc<T> {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.inner().data.cmp(&other.inner().data)
}
}