use crate::marshal::*;
use crate::object::*;
use core::cell::Cell;
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::from_owned(self.obj)
}
}
}
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,
scope: u64,
ty: PhantomData<fn() -> L>,
thread: PhantomData<*mut ()>,
}
thread_local! {
static SCOPE: Cell<u64> = const { Cell::new(0) };
static SCOPE_LIVE: Cell<usize> = const { Cell::new(0) };
static SCOPE_LAST: Cell<u64> = const { Cell::new(0) };
}
#[inline]
fn enter_scope() -> u64 {
let scope = SCOPE.get();
if scope != 0 {
SCOPE_LIVE.set(SCOPE_LIVE.get() + 1);
}
scope
}
#[inline]
fn leave_scope(scope: u64) {
if scope != 0 && scope == SCOPE.get() {
SCOPE_LIVE.set(SCOPE_LIVE.get() - 1);
}
}
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> {
unsafe { Lean::from_owned(self.release()) }
}
#[inline]
fn settle(&self) {
if self.scope != SCOPE.get() {
unsafe { lean_mark_mt(self.obj) };
}
}
#[inline]
fn release(self) -> Obj {
self.settle();
leave_scope(self.scope);
let obj = self.obj;
core::mem::forget(self);
obj
}
#[doc(hidden)]
pub unsafe fn from_owned(obj: Obj) -> Self {
Local {
obj,
scope: enter_scope(),
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 {
self.settle();
unsafe {
lean_inc(self.obj);
Local::from_owned(self.obj)
}
}
}
impl<L: LeanRepr> Drop for Local<L> {
fn drop(&mut self) {
leave_scope(self.scope);
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 {
self.release()
}
}
unsafe impl<L: LeanRepr> IntoLean<L> for &Local<L> {
unsafe fn into_obj(self) -> Obj {
self.settle();
lean_inc(self.obj);
self.obj
}
}
pub struct Owned<L: LeanRepr> {
obj: Obj,
ty: PhantomData<fn() -> L>,
}
unsafe impl<L: LeanRepr> Send for Owned<L> {}
impl<L: LeanRepr> Owned<L> {
pub fn new(value: L::Value) -> Self {
Owned {
obj: unsafe { L::to_obj(value) },
ty: PhantomData,
}
}
fn obj(&self) -> Obj {
assert!(
!self.obj.is_null(),
"the Owned value was lost: a closure of `with` panicked"
);
self.obj
}
pub fn get(&self) -> L::Value {
unsafe { L::from_obj(self.obj()) }
}
pub fn with<R>(&mut self, f: impl FnOnce(Local<L>) -> (Local<L>, R)) -> R {
struct Restore(u64, usize);
impl Drop for Restore {
fn drop(&mut self) {
SCOPE.set(self.0);
SCOPE_LIVE.set(self.1);
}
}
let obj = core::mem::replace(&mut self.obj, core::ptr::null_mut());
assert!(
!obj.is_null(),
"the Owned value was lost: a closure of `with` panicked"
);
let scope = SCOPE_LAST.get() + 1;
SCOPE_LAST.set(scope);
let restore = Restore(SCOPE.replace(scope), SCOPE_LIVE.replace(0));
let (value, result) = f(unsafe { Local::from_owned(obj) });
let alone = value.scope == scope && SCOPE_LIVE.get() == 1;
let obj = value.obj;
core::mem::forget(value);
if !alone {
unsafe { lean_mark_mt(obj) };
}
drop(restore);
self.obj = obj;
result
}
pub fn share(self) -> Lean<L> {
let obj = self.obj();
core::mem::forget(self);
unsafe { Lean::from_owned(obj) }
}
}
impl<L: LeanRepr> From<Lean<L>> for Owned<L> {
fn from(value: Lean<L>) -> Self {
let obj = value.obj;
core::mem::forget(value);
Owned {
obj,
ty: PhantomData,
}
}
}
impl<L: LeanRepr> Drop for Owned<L> {
fn drop(&mut self) {
if !self.obj.is_null() {
unsafe { lean_dec(self.obj) };
}
}
}
impl<L: LeanRepr> fmt::Debug for Owned<L>
where
L::Value: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Owned").field(&self.get()).finish()
}
}
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)
}
}