use crate::marshal::*;
use crate::object::*;
use core::fmt;
use core::marker::PhantomData;
pub unsafe trait LeanRepr {
type Value;
#[doc(hidden)]
unsafe fn to_obj(value: Self::Value) -> Obj;
#[doc(hidden)]
unsafe fn from_obj(o: Obj) -> Self::Value;
}
pub struct List<T>(PhantomData<fn() -> T>);
pub struct Array<T>(PhantomData<fn() -> T>);
pub struct Fin<const N: u64>;
pub struct Lean<L: LeanRepr> {
obj: Obj,
ty: PhantomData<fn() -> L>,
}
unsafe impl<L: LeanRepr> Send for Lean<L> {}
unsafe impl<L: LeanRepr> Sync for Lean<L> {}
impl<L: LeanRepr> Lean<L> {
pub fn new(value: L::Value) -> Self {
unsafe { Self::from_owned(L::to_obj(value)) }
}
pub fn get(&self) -> L::Value {
unsafe { L::from_obj(self.obj) }
}
#[doc(hidden)]
pub unsafe fn from_owned(obj: Obj) -> Self {
lean_mark_mt(obj);
Lean {
obj,
ty: PhantomData,
}
}
#[doc(hidden)]
pub fn as_obj(&self) -> Obj {
self.obj
}
}
impl<L: LeanRepr> Lean<L> {
pub fn to_local(&self) -> Local<L> {
unsafe { lean_inc(self.obj) };
Local {
obj: self.obj,
ty: PhantomData,
thread: PhantomData,
}
}
}
impl<L: LeanRepr> Clone for Lean<L> {
fn clone(&self) -> Self {
unsafe { lean_inc(self.obj) };
Lean {
obj: self.obj,
ty: PhantomData,
}
}
}
impl<L: LeanRepr> Drop for Lean<L> {
fn drop(&mut self) {
unsafe { lean_dec(self.obj) };
}
}
impl<L: LeanRepr> fmt::Debug for Lean<L>
where
L::Value: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Lean").field(&self.get()).finish()
}
}
pub struct Local<L: LeanRepr> {
obj: Obj,
ty: PhantomData<fn() -> L>,
thread: PhantomData<*mut ()>,
}
impl<L: LeanRepr> Local<L> {
pub fn new(value: L::Value) -> Self {
unsafe { Self::from_owned(L::to_obj(value)) }
}
pub fn get(&self) -> L::Value {
unsafe { L::from_obj(self.obj) }
}
pub fn share(self) -> Lean<L> {
let obj = self.obj;
core::mem::forget(self);
unsafe { Lean::from_owned(obj) }
}
#[doc(hidden)]
pub unsafe fn from_owned(obj: Obj) -> Self {
Local {
obj,
ty: PhantomData,
thread: PhantomData,
}
}
#[doc(hidden)]
pub fn as_obj(&self) -> Obj {
self.obj
}
}
impl<L: LeanRepr> Clone for Local<L> {
fn clone(&self) -> Self {
unsafe { lean_inc(self.obj) };
Local {
obj: self.obj,
ty: PhantomData,
thread: PhantomData,
}
}
}
impl<L: LeanRepr> Drop for Local<L> {
fn drop(&mut self) {
unsafe { lean_dec(self.obj) };
}
}
impl<L: LeanRepr> fmt::Debug for Local<L>
where
L::Value: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Local").field(&self.get()).finish()
}
}
pub unsafe trait IntoLean<L: LeanRepr> {
#[doc(hidden)]
unsafe fn into_obj(self) -> Obj;
}
unsafe impl<L: LeanRepr> IntoLean<L> for Lean<L> {
unsafe fn into_obj(self) -> Obj {
let obj = self.obj;
core::mem::forget(self);
obj
}
}
unsafe impl<L: LeanRepr> IntoLean<L> for &Lean<L> {
unsafe fn into_obj(self) -> Obj {
lean_inc(self.obj);
self.obj
}
}
unsafe impl<L: LeanRepr> IntoLean<L> for Local<L> {
unsafe fn into_obj(self) -> Obj {
let obj = self.obj;
core::mem::forget(self);
obj
}
}
unsafe impl<L: LeanRepr> IntoLean<L> for &Local<L> {
unsafe fn into_obj(self) -> Obj {
lean_inc(self.obj);
self.obj
}
}
macro_rules! prim_repr {
($($t:ty),*) => {$(
unsafe impl LeanRepr for $t {
type Value = $t;
unsafe fn to_obj(value: $t) -> Obj {
ToLean::to_lean(value)
}
unsafe fn from_obj(o: Obj) -> $t {
<$t as FromLean>::from_lean(o)
}
}
unsafe impl IntoLean<$t> for $t {
unsafe fn into_obj(self) -> Obj {
ToLean::to_lean(self)
}
}
)*};
}
prim_repr!(
Nat,
Int,
bool,
char,
String,
u8,
u16,
u32,
u64,
usize,
i8,
i16,
i32,
i64,
isize,
f64,
f32,
()
);
unsafe impl IntoLean<String> for &str {
unsafe fn into_obj(self) -> Obj {
ToLean::to_lean(self)
}
}
unsafe impl IntoLean<String> for &String {
unsafe fn into_obj(self) -> Obj {
ToLean::to_lean(self.as_str())
}
}
unsafe impl<T: LeanRepr> LeanRepr for List<T> {
type Value = Vec<T::Value>;
unsafe fn to_obj(value: Self::Value) -> Obj {
list_to_lean(value, |v| T::to_obj(v))
}
unsafe fn from_obj(o: Obj) -> Self::Value {
list_from_lean(o, |v| T::from_obj(v))
}
}
unsafe impl<T: LeanRepr> IntoLean<List<T>> for Vec<T::Value> {
unsafe fn into_obj(self) -> Obj {
List::<T>::to_obj(self)
}
}
unsafe impl<T: LeanRepr> LeanRepr for Array<T> {
type Value = Vec<T::Value>;
unsafe fn to_obj(value: Self::Value) -> Obj {
array_to_lean(value, |v| T::to_obj(v))
}
unsafe fn from_obj(o: Obj) -> Self::Value {
array_from_lean(o, |v| T::from_obj(v))
}
}
unsafe impl<T: LeanRepr> IntoLean<Array<T>> for Vec<T::Value> {
unsafe fn into_obj(self) -> Obj {
Array::<T>::to_obj(self)
}
}
unsafe impl<T: LeanRepr> LeanRepr for Option<T> {
type Value = Option<T::Value>;
unsafe fn to_obj(value: Self::Value) -> Obj {
option_to_lean(value, |v| T::to_obj(v))
}
unsafe fn from_obj(o: Obj) -> Self::Value {
option_from_lean(o, |v| T::from_obj(v))
}
}
unsafe impl<T: LeanRepr> IntoLean<Option<T>> for Option<T::Value> {
unsafe fn into_obj(self) -> Obj {
Option::<T>::to_obj(self)
}
}
unsafe impl<T: LeanRepr, E: LeanRepr> LeanRepr for Result<T, E> {
type Value = Result<T::Value, E::Value>;
unsafe fn to_obj(value: Self::Value) -> Obj {
except_to_lean(value, |v| E::to_obj(v), |v| T::to_obj(v))
}
unsafe fn from_obj(o: Obj) -> Self::Value {
except_from_lean(o, |v| E::from_obj(v), |v| T::from_obj(v))
}
}
unsafe impl<T: LeanRepr, E: LeanRepr> IntoLean<Result<T, E>> for Result<T::Value, E::Value> {
unsafe fn into_obj(self) -> Obj {
Result::<T, E>::to_obj(self)
}
}
unsafe impl<A: LeanRepr, B: LeanRepr> LeanRepr for (A, B) {
type Value = (A::Value, B::Value);
unsafe fn to_obj(value: Self::Value) -> Obj {
prod_to_lean(value, |v| A::to_obj(v), |v| B::to_obj(v))
}
unsafe fn from_obj(o: Obj) -> Self::Value {
prod_from_lean(o, |v| A::from_obj(v), |v| B::from_obj(v))
}
}
unsafe impl<A: LeanRepr, B: LeanRepr> IntoLean<(A, B)> for (A::Value, B::Value) {
unsafe fn into_obj(self) -> Obj {
<(A, B)>::to_obj(self)
}
}
unsafe impl<const N: u64> LeanRepr for Fin<N> {
type Value = usize;
unsafe fn to_obj(value: usize) -> Obj {
fin_to_lean(value, N as usize)
}
unsafe fn from_obj(o: Obj) -> usize {
fin_from_lean(o)
}
}
unsafe impl<const N: u64> IntoLean<Fin<N>> for usize {
unsafe fn into_obj(self) -> Obj {
Fin::<N>::to_obj(self)
}
}