1 use crate::runtime::vm::{GcStore, VMGcRef};
2 use crate::{
3     AsContext, AsContextMut, GcRef, Result, RootedGcRef,
4     store::{AutoAssertNoGc, StoreOpaque},
5 };
6 use core::convert::Infallible;
7 use core::fmt::{self, Debug};
8 use core::hash::{Hash, Hasher};
9 use core::marker;
10 use core::ops::{Deref, DerefMut};
11 
12 mod sealed {
13     use super::*;
14     pub trait GcRefImpl {}
15     pub trait RootedGcRefImpl<T: GcRef> {
assert_unreachable<U>(&self) -> U16         fn assert_unreachable<U>(&self) -> U;
17 
get_gc_ref<'a>(&self, _store: &'a StoreOpaque) -> Option<&'a VMGcRef>18         fn get_gc_ref<'a>(&self, _store: &'a StoreOpaque) -> Option<&'a VMGcRef> {
19             self.assert_unreachable()
20         }
21 
try_gc_ref<'a>(&self, _store: &'a StoreOpaque) -> Result<&'a VMGcRef>22         fn try_gc_ref<'a>(&self, _store: &'a StoreOpaque) -> Result<&'a VMGcRef> {
23             self.assert_unreachable()
24         }
25 
clone_gc_ref(&self, _store: &mut AutoAssertNoGc<'_>) -> Option<VMGcRef>26         fn clone_gc_ref(&self, _store: &mut AutoAssertNoGc<'_>) -> Option<VMGcRef> {
27             self.assert_unreachable()
28         }
29 
try_clone_gc_ref(&self, _store: &mut AutoAssertNoGc<'_>) -> Result<VMGcRef>30         fn try_clone_gc_ref(&self, _store: &mut AutoAssertNoGc<'_>) -> Result<VMGcRef> {
31             self.assert_unreachable()
32         }
33     }
34 }
35 pub(crate) use sealed::*;
36 
37 #[derive(Debug, Default)]
38 pub(crate) struct RootSet {}
39 
40 impl RootSet {
enter_lifo_scope(&self) -> usize41     pub(crate) fn enter_lifo_scope(&self) -> usize {
42         usize::MAX
43     }
44 
exit_lifo_scope(&mut self, _gc_store: Option<&mut GcStore>, _scope: usize)45     pub(crate) fn exit_lifo_scope(&mut self, _gc_store: Option<&mut GcStore>, _scope: usize) {}
46 }
47 
48 /// This type is disabled because the `gc` cargo feature was not enabled at
49 /// compile time.
50 pub struct Rooted<T: GcRef> {
51     pub(crate) inner: Infallible,
52     _phantom: marker::PhantomData<T>,
53 }
54 
55 impl<T: GcRef> Clone for Rooted<T> {
clone(&self) -> Self56     fn clone(&self) -> Self {
57         match self.inner {}
58     }
59 }
60 
61 impl<T: GcRef> Copy for Rooted<T> {}
62 
63 impl<T: GcRef> Debug for Rooted<T> {
fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result64     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         match self.inner {}
66     }
67 }
68 
69 impl<T: GcRef> PartialEq for Rooted<T> {
eq(&self, _other: &Self) -> bool70     fn eq(&self, _other: &Self) -> bool {
71         match self.inner {}
72     }
73 }
74 
75 impl<T: GcRef> Eq for Rooted<T> {}
76 
77 impl<T: GcRef> Hash for Rooted<T> {
hash<H: Hasher>(&self, _state: &mut H)78     fn hash<H: Hasher>(&self, _state: &mut H) {
79         match self.inner {}
80     }
81 }
82 
83 impl<T: GcRef> RootedGcRefImpl<T> for Rooted<T> {
assert_unreachable<U>(&self) -> U84     fn assert_unreachable<U>(&self) -> U {
85         match self.inner {}
86     }
87 }
88 
89 impl<T: GcRef> Deref for Rooted<T> {
90     type Target = T;
91 
deref(&self) -> &Self::Target92     fn deref(&self) -> &Self::Target {
93         match self.inner {}
94     }
95 }
96 
97 impl<T: GcRef> Rooted<T> {
comes_from_same_store(&self, _store: &StoreOpaque) -> bool98     pub(crate) fn comes_from_same_store(&self, _store: &StoreOpaque) -> bool {
99         match self.inner {}
100     }
101 
to_owned_rooted(&self, _store: impl AsContextMut) -> Result<OwnedRooted<T>>102     pub fn to_owned_rooted(&self, _store: impl AsContextMut) -> Result<OwnedRooted<T>> {
103         match self.inner {}
104     }
105 
rooted_eq(a: Self, _b: Self) -> bool106     pub fn rooted_eq(a: Self, _b: Self) -> bool {
107         match a.inner {}
108     }
109 
ref_eq( _store: impl AsContext, a: &impl RootedGcRef<T>, _b: &impl RootedGcRef<T>, ) -> Result<bool>110     pub fn ref_eq(
111         _store: impl AsContext,
112         a: &impl RootedGcRef<T>,
113         _b: &impl RootedGcRef<T>,
114     ) -> Result<bool> {
115         a.assert_unreachable()
116     }
117 }
118 
119 /// This type has been disabled because the `gc` cargo feature was not enabled
120 /// at compile time.
121 pub struct RootScope<C>
122 where
123     C: AsContextMut,
124 {
125     inner: Infallible,
126     _phantom: marker::PhantomData<C>,
127 }
128 
129 impl<C> RootScope<C>
130 where
131     C: AsContextMut,
132 {
reserve(&mut self, _additional: usize)133     pub fn reserve(&mut self, _additional: usize) {
134         match self.inner {}
135     }
136 }
137 
138 impl<T> AsContext for RootScope<T>
139 where
140     T: AsContextMut,
141 {
142     type Data = T::Data;
143 
as_context(&self) -> crate::StoreContext<'_, Self::Data>144     fn as_context(&self) -> crate::StoreContext<'_, Self::Data> {
145         match self.inner {}
146     }
147 }
148 
149 impl<T> AsContextMut for RootScope<T>
150 where
151     T: AsContextMut,
152 {
as_context_mut(&mut self) -> crate::StoreContextMut<'_, Self::Data>153     fn as_context_mut(&mut self) -> crate::StoreContextMut<'_, Self::Data> {
154         match self.inner {}
155     }
156 }
157 
158 /// This type has been disabled because the `gc` cargo feature was not enabled
159 /// at compile time.
160 pub struct OwnedRooted<T>
161 where
162     T: GcRef,
163 {
164     pub(crate) inner: Infallible,
165     _phantom: marker::PhantomData<T>,
166 }
167 
168 impl<T: GcRef> Debug for OwnedRooted<T> {
fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result169     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
170         match self.inner {}
171     }
172 }
173 
174 impl<T: GcRef> Deref for OwnedRooted<T> {
175     type Target = T;
176 
deref(&self) -> &Self::Target177     fn deref(&self) -> &Self::Target {
178         match self.inner {}
179     }
180 }
181 
182 impl<T> OwnedRooted<T>
183 where
184     T: GcRef,
185 {
clone(&self, _store: impl AsContextMut) -> Self186     pub fn clone(&self, _store: impl AsContextMut) -> Self {
187         match self.inner {}
188     }
189 
to_rooted(&self, _context: impl AsContextMut) -> Rooted<T>190     pub fn to_rooted(&self, _context: impl AsContextMut) -> Rooted<T> {
191         match self.inner {}
192     }
193 
into_rooted(self, _context: impl AsContextMut) -> Rooted<T>194     pub fn into_rooted(self, _context: impl AsContextMut) -> Rooted<T> {
195         match self.inner {}
196     }
197 }
198 
199 impl<T: GcRef> RootedGcRefImpl<T> for OwnedRooted<T> {
assert_unreachable<U>(&self) -> U200     fn assert_unreachable<U>(&self) -> U {
201         match self.inner {}
202     }
203 }
204 
205 pub(crate) struct OpaqueRootScope<S> {
206     store: S,
207 }
208 
209 impl<S> Deref for OpaqueRootScope<S> {
210     type Target = S;
211 
deref(&self) -> &Self::Target212     fn deref(&self) -> &Self::Target {
213         &self.store
214     }
215 }
216 
217 impl<S> DerefMut for OpaqueRootScope<S> {
deref_mut(&mut self) -> &mut Self::Target218     fn deref_mut(&mut self) -> &mut Self::Target {
219         &mut self.store
220     }
221 }
222 
223 impl<S> OpaqueRootScope<S> {
new(store: S) -> Self224     pub(crate) fn new(store: S) -> Self {
225         OpaqueRootScope { store }
226     }
227 }
228