leanfran_runtime/
handle.rs1use crate::marshal::*;
18use crate::object::*;
19use core::fmt;
20use core::marker::PhantomData;
21
22pub unsafe trait LeanRepr {
30 type Value;
32
33 #[doc(hidden)]
39 unsafe fn to_obj(value: Self::Value) -> Obj;
40
41 #[doc(hidden)]
47 unsafe fn from_obj(o: Obj) -> Self::Value;
48}
49
50pub struct List<T>(PhantomData<fn() -> T>);
52
53pub struct Array<T>(PhantomData<fn() -> T>);
55
56pub struct Fin<const N: u64>;
58
59pub struct Lean<L: LeanRepr> {
65 obj: Obj,
66 ty: PhantomData<fn() -> L>,
67}
68
69unsafe impl<L: LeanRepr> Send for Lean<L> {}
73unsafe impl<L: LeanRepr> Sync for Lean<L> {}
74
75impl<L: LeanRepr> Lean<L> {
76 pub fn new(value: L::Value) -> Self {
78 unsafe { Self::from_owned(L::to_obj(value)) }
79 }
80
81 pub fn get(&self) -> L::Value {
83 unsafe { L::from_obj(self.obj) }
84 }
85
86 #[doc(hidden)]
93 pub unsafe fn from_owned(obj: Obj) -> Self {
94 lean_mark_mt(obj);
95 Lean {
96 obj,
97 ty: PhantomData,
98 }
99 }
100
101 #[doc(hidden)]
103 pub fn as_obj(&self) -> Obj {
104 self.obj
105 }
106}
107
108impl<L: LeanRepr> Lean<L> {
109 pub fn to_local(&self) -> Local<L> {
112 unsafe { lean_inc(self.obj) };
113 Local {
114 obj: self.obj,
115 ty: PhantomData,
116 thread: PhantomData,
117 }
118 }
119}
120
121impl<L: LeanRepr> Clone for Lean<L> {
122 fn clone(&self) -> Self {
123 unsafe { lean_inc(self.obj) };
124 Lean {
125 obj: self.obj,
126 ty: PhantomData,
127 }
128 }
129}
130
131impl<L: LeanRepr> Drop for Lean<L> {
132 fn drop(&mut self) {
133 unsafe { lean_dec(self.obj) };
134 }
135}
136
137impl<L: LeanRepr> fmt::Debug for Lean<L>
138where
139 L::Value: fmt::Debug,
140{
141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 f.debug_tuple("Lean").field(&self.get()).finish()
143 }
144}
145
146pub struct Local<L: LeanRepr> {
166 obj: Obj,
167 ty: PhantomData<fn() -> L>,
168 thread: PhantomData<*mut ()>,
169}
170
171impl<L: LeanRepr> Local<L> {
172 pub fn new(value: L::Value) -> Self {
174 unsafe { Self::from_owned(L::to_obj(value)) }
175 }
176
177 pub fn get(&self) -> L::Value {
179 unsafe { L::from_obj(self.obj) }
180 }
181
182 pub fn share(self) -> Lean<L> {
185 let obj = self.obj;
186 core::mem::forget(self);
187 unsafe { Lean::from_owned(obj) }
188 }
189
190 #[doc(hidden)]
196 pub unsafe fn from_owned(obj: Obj) -> Self {
197 Local {
198 obj,
199 ty: PhantomData,
200 thread: PhantomData,
201 }
202 }
203
204 #[doc(hidden)]
206 pub fn as_obj(&self) -> Obj {
207 self.obj
208 }
209}
210
211impl<L: LeanRepr> Clone for Local<L> {
212 fn clone(&self) -> Self {
213 unsafe { lean_inc(self.obj) };
214 Local {
215 obj: self.obj,
216 ty: PhantomData,
217 thread: PhantomData,
218 }
219 }
220}
221
222impl<L: LeanRepr> Drop for Local<L> {
223 fn drop(&mut self) {
224 unsafe { lean_dec(self.obj) };
225 }
226}
227
228impl<L: LeanRepr> fmt::Debug for Local<L>
229where
230 L::Value: fmt::Debug,
231{
232 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
233 f.debug_tuple("Local").field(&self.get()).finish()
234 }
235}
236
237pub unsafe trait IntoLean<L: LeanRepr> {
245 #[doc(hidden)]
251 unsafe fn into_obj(self) -> Obj;
252}
253
254unsafe impl<L: LeanRepr> IntoLean<L> for Lean<L> {
255 unsafe fn into_obj(self) -> Obj {
256 let obj = self.obj;
257 core::mem::forget(self);
258 obj
259 }
260}
261
262unsafe impl<L: LeanRepr> IntoLean<L> for &Lean<L> {
263 unsafe fn into_obj(self) -> Obj {
264 lean_inc(self.obj);
265 self.obj
266 }
267}
268
269unsafe impl<L: LeanRepr> IntoLean<L> for Local<L> {
270 unsafe fn into_obj(self) -> Obj {
271 let obj = self.obj;
272 core::mem::forget(self);
273 obj
274 }
275}
276
277unsafe impl<L: LeanRepr> IntoLean<L> for &Local<L> {
278 unsafe fn into_obj(self) -> Obj {
279 lean_inc(self.obj);
280 self.obj
281 }
282}
283
284macro_rules! prim_repr {
285 ($($t:ty),*) => {$(
286 unsafe impl LeanRepr for $t {
287 type Value = $t;
288 unsafe fn to_obj(value: $t) -> Obj {
289 ToLean::to_lean(value)
290 }
291 unsafe fn from_obj(o: Obj) -> $t {
292 <$t as FromLean>::from_lean(o)
293 }
294 }
295 unsafe impl IntoLean<$t> for $t {
296 unsafe fn into_obj(self) -> Obj {
297 ToLean::to_lean(self)
298 }
299 }
300 )*};
301}
302
303prim_repr!(
304 Nat,
305 Int,
306 bool,
307 char,
308 String,
309 u8,
310 u16,
311 u32,
312 u64,
313 usize,
314 i8,
315 i16,
316 i32,
317 i64,
318 isize,
319 f64,
320 f32,
321 ()
322);
323
324unsafe impl IntoLean<String> for &str {
325 unsafe fn into_obj(self) -> Obj {
326 ToLean::to_lean(self)
327 }
328}
329
330unsafe impl IntoLean<String> for &String {
331 unsafe fn into_obj(self) -> Obj {
332 ToLean::to_lean(self.as_str())
333 }
334}
335
336unsafe impl<T: LeanRepr> LeanRepr for List<T> {
337 type Value = Vec<T::Value>;
338 unsafe fn to_obj(value: Self::Value) -> Obj {
339 list_to_lean(value, |v| T::to_obj(v))
340 }
341 unsafe fn from_obj(o: Obj) -> Self::Value {
342 list_from_lean(o, |v| T::from_obj(v))
343 }
344}
345
346unsafe impl<T: LeanRepr> IntoLean<List<T>> for Vec<T::Value> {
347 unsafe fn into_obj(self) -> Obj {
348 List::<T>::to_obj(self)
349 }
350}
351
352unsafe impl<T: LeanRepr> LeanRepr for Array<T> {
353 type Value = Vec<T::Value>;
354 unsafe fn to_obj(value: Self::Value) -> Obj {
355 array_to_lean(value, |v| T::to_obj(v))
356 }
357 unsafe fn from_obj(o: Obj) -> Self::Value {
358 array_from_lean(o, |v| T::from_obj(v))
359 }
360}
361
362unsafe impl<T: LeanRepr> IntoLean<Array<T>> for Vec<T::Value> {
363 unsafe fn into_obj(self) -> Obj {
364 Array::<T>::to_obj(self)
365 }
366}
367
368unsafe impl<T: LeanRepr> LeanRepr for Option<T> {
369 type Value = Option<T::Value>;
370 unsafe fn to_obj(value: Self::Value) -> Obj {
371 option_to_lean(value, |v| T::to_obj(v))
372 }
373 unsafe fn from_obj(o: Obj) -> Self::Value {
374 option_from_lean(o, |v| T::from_obj(v))
375 }
376}
377
378unsafe impl<T: LeanRepr> IntoLean<Option<T>> for Option<T::Value> {
379 unsafe fn into_obj(self) -> Obj {
380 Option::<T>::to_obj(self)
381 }
382}
383
384unsafe impl<T: LeanRepr, E: LeanRepr> LeanRepr for Result<T, E> {
385 type Value = Result<T::Value, E::Value>;
386 unsafe fn to_obj(value: Self::Value) -> Obj {
387 except_to_lean(value, |v| E::to_obj(v), |v| T::to_obj(v))
388 }
389 unsafe fn from_obj(o: Obj) -> Self::Value {
390 except_from_lean(o, |v| E::from_obj(v), |v| T::from_obj(v))
391 }
392}
393
394unsafe impl<T: LeanRepr, E: LeanRepr> IntoLean<Result<T, E>> for Result<T::Value, E::Value> {
395 unsafe fn into_obj(self) -> Obj {
396 Result::<T, E>::to_obj(self)
397 }
398}
399
400unsafe impl<A: LeanRepr, B: LeanRepr> LeanRepr for (A, B) {
401 type Value = (A::Value, B::Value);
402 unsafe fn to_obj(value: Self::Value) -> Obj {
403 prod_to_lean(value, |v| A::to_obj(v), |v| B::to_obj(v))
404 }
405 unsafe fn from_obj(o: Obj) -> Self::Value {
406 prod_from_lean(o, |v| A::from_obj(v), |v| B::from_obj(v))
407 }
408}
409
410unsafe impl<A: LeanRepr, B: LeanRepr> IntoLean<(A, B)> for (A::Value, B::Value) {
411 unsafe fn into_obj(self) -> Obj {
412 <(A, B)>::to_obj(self)
413 }
414}
415
416unsafe impl<const N: u64> LeanRepr for Fin<N> {
417 type Value = usize;
418 unsafe fn to_obj(value: usize) -> Obj {
419 fin_to_lean(value, N as usize)
420 }
421 unsafe fn from_obj(o: Obj) -> usize {
422 fin_from_lean(o)
423 }
424}
425
426unsafe impl<const N: u64> IntoLean<Fin<N>> for usize {
427 unsafe fn into_obj(self) -> Obj {
428 Fin::<N>::to_obj(self)
429 }
430}