use core::fmt;
use lean_rs_sys::lean_object;
use crate::abi::traits::{IntoLean, LeanAbi, TryFromLean, sealed};
use crate::error::LeanResult;
use crate::runtime::LeanRuntime;
use crate::runtime::obj::Obj;
pub struct LeanExpr<'lean> {
obj: Obj<'lean>,
}
impl Clone for LeanExpr<'_> {
fn clone(&self) -> Self {
Self { obj: self.obj.clone() }
}
}
impl fmt::Debug for LeanExpr<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LeanExpr").finish_non_exhaustive()
}
}
impl sealed::SealedAbi for LeanExpr<'_> {}
impl<'lean> LeanAbi<'lean> for LeanExpr<'lean> {
type CRepr = *mut lean_object;
fn into_c(self, _runtime: &'lean LeanRuntime) -> *mut lean_object {
self.obj.into_raw()
}
#[allow(
clippy::not_unsafe_ptr_arg_deref,
reason = "sealed trait — caller invariant documented on LeanAbi::from_c"
)]
fn from_c(c: *mut lean_object, runtime: &'lean LeanRuntime) -> LeanResult<Self> {
#[allow(unsafe_code)]
let obj = unsafe { Obj::from_owned_raw(runtime, c) };
Ok(Self { obj })
}
}
impl<'lean> TryFromLean<'lean> for LeanExpr<'lean> {
fn try_from_lean(obj: Obj<'lean>) -> LeanResult<Self> {
Ok(Self { obj })
}
}
impl<'lean> IntoLean<'lean> for LeanExpr<'lean> {
fn into_lean(self, _runtime: &'lean LeanRuntime) -> Obj<'lean> {
self.obj
}
}