use crate::object::{self as o, box_, dec, inc, Obj};
use crate::types::{FromLean, ToLean};
use std::marker::PhantomData;
pub trait Codec {
type Rust;
fn to_lean(value: &Self::Rust) -> Obj;
unsafe fn from_lean(object: Obj) -> Self::Rust;
}
pub struct Plain<T>(PhantomData<T>);
impl<T: ToLean + FromLean> Codec for Plain<T> {
type Rust = T;
fn to_lean(value: &T) -> Obj {
value.to_lean()
}
unsafe fn from_lean(object: Obj) -> T {
T::from_lean(object)
}
}
pub struct Array<C>(PhantomData<C>);
impl<C: Codec> Array<C> {
pub fn from_slice(items: &[C::Rust]) -> Obj {
crate::array::from_vec(items.iter().map(C::to_lean).collect())
}
}
impl<C: Codec> Codec for Array<C> {
type Rust = Vec<C::Rust>;
fn to_lean(value: &Vec<C::Rust>) -> Obj {
Self::from_slice(value)
}
unsafe fn from_lean(object: Obj) -> Vec<C::Rust> {
let items = crate::array::elements(object)
.iter()
.map(|&item| {
inc(item);
C::from_lean(item)
})
.collect();
dec(object);
items
}
}
pub struct List<C>(PhantomData<C>);
impl<C: Codec> List<C> {
pub fn from_slice(items: &[C::Rust]) -> Obj {
let mut list = box_(0);
for item in items.iter().rev() {
unsafe {
let cell = o::alloc_ctor(1, 2, 0, 0);
o::ctor_set(cell, 0, C::to_lean(item));
o::ctor_set(cell, 1, list);
list = cell;
}
}
list
}
}
impl<C: Codec> Codec for List<C> {
type Rust = Vec<C::Rust>;
fn to_lean(value: &Vec<C::Rust>) -> Obj {
Self::from_slice(value)
}
unsafe fn from_lean(object: Obj) -> Vec<C::Rust> {
let mut items = Vec::new();
let mut rest = object;
while !o::is_scalar(rest) {
let head = o::ctor_get(rest, 0);
inc(head);
items.push(C::from_lean(head));
rest = o::ctor_get(rest, 1);
}
dec(object);
items
}
}
pub struct Option<C>(PhantomData<C>);
impl<C: Codec> Codec for Option<C> {
type Rust = ::core::option::Option<C::Rust>;
fn to_lean(value: &Self::Rust) -> Obj {
match value {
None => box_(0),
Some(value) => unsafe {
let some = o::alloc_ctor(1, 1, 0, 0);
o::ctor_set(some, 0, C::to_lean(value));
some
},
}
}
unsafe fn from_lean(object: Obj) -> Self::Rust {
if o::is_scalar(object) {
return None;
}
let value = o::ctor_get(object, 0);
inc(value);
dec(object);
Some(C::from_lean(value))
}
}
pub struct Pair<A, B>(PhantomData<(A, B)>);
impl<A: Codec, B: Codec> Codec for Pair<A, B> {
type Rust = (A::Rust, B::Rust);
fn to_lean(value: &Self::Rust) -> Obj {
unsafe {
let pair = o::alloc_ctor(0, 2, 0, 0);
o::ctor_set(pair, 0, A::to_lean(&value.0));
o::ctor_set(pair, 1, B::to_lean(&value.1));
pair
}
}
unsafe fn from_lean(object: Obj) -> Self::Rust {
let (first, second) = (o::ctor_get(object, 0), o::ctor_get(object, 1));
inc(first);
inc(second);
dec(object);
(A::from_lean(first), B::from_lean(second))
}
}
pub struct Boxed<C>(PhantomData<C>);
impl<C: Codec> Codec for Boxed<C> {
type Rust = Box<C::Rust>;
fn to_lean(value: &Self::Rust) -> Obj {
C::to_lean(value)
}
unsafe fn from_lean(object: Obj) -> Self::Rust {
Box::new(C::from_lean(object))
}
}
pub struct ByteArray;
impl Codec for ByteArray {
type Rust = Vec<u8>;
fn to_lean(value: &Vec<u8>) -> Obj {
crate::sarray::byte_array_from_vec(value.clone())
}
unsafe fn from_lean(object: Obj) -> Vec<u8> {
crate::sarray::byte_array_into_vec(object)
}
}
pub struct FloatArray;
impl Codec for FloatArray {
type Rust = Vec<f64>;
fn to_lean(value: &Vec<f64>) -> Obj {
crate::sarray::float_array_from_vec(value.clone())
}
unsafe fn from_lean(object: Obj) -> Vec<f64> {
crate::sarray::float_array_into_vec(object)
}
}
impl<A: Codec, B: Codec> Pair<A, B> {
pub fn from_refs(first: &A::Rust, second: &B::Rust) -> Obj {
unsafe {
let pair = o::alloc_ctor(0, 2, 0, 0);
o::ctor_set(pair, 0, A::to_lean(first));
o::ctor_set(pair, 1, B::to_lean(second));
pair
}
}
}
pub struct Except<E, A>(PhantomData<(E, A)>);
impl<E: Codec, A: Codec> Codec for Except<E, A> {
type Rust = Result<A::Rust, E::Rust>;
fn to_lean(value: &Self::Rust) -> Obj {
let (tag, field) = match value {
Err(error) => (0, E::to_lean(error)),
Ok(value) => (1, A::to_lean(value)),
};
unsafe {
let object = o::alloc_ctor(tag, 1, 0, 0);
o::ctor_set(object, 0, field);
object
}
}
unsafe fn from_lean(object: Obj) -> Self::Rust {
let field = o::ctor_get(object, 0);
inc(field);
let ok = o::obj_tag(object) == 1;
dec(object);
if ok {
Ok(A::from_lean(field))
} else {
Err(E::from_lean(field))
}
}
}