#![allow(clippy::inline_always)]
#![allow(clippy::cast_possible_truncation)]
use core::mem::size_of;
use crate::consts::{LEAN_ARRAY, LEAN_SCALAR_ARRAY};
use crate::object::lean_alloc_object;
use crate::repr::{LeanArrayObjectRepr, LeanObjectRepr, LeanSArrayObjectRepr};
use crate::types::{b_lean_obj_arg, lean_obj_arg, lean_obj_res, lean_object};
unsafe extern "C" {
pub fn lean_array_mk(l: lean_obj_arg) -> *mut lean_object;
pub fn lean_array_to_list(a: lean_obj_arg) -> *mut lean_object;
pub fn lean_array_get_panic(def_val: lean_obj_arg) -> lean_obj_res;
pub fn lean_array_set_panic(a: lean_obj_arg, v: lean_obj_arg) -> lean_obj_res;
pub fn lean_array_push(a: lean_obj_arg, v: lean_obj_arg) -> *mut lean_object;
pub fn lean_mk_array(n: lean_obj_arg, v: lean_obj_arg) -> *mut lean_object;
}
#[inline(always)]
pub unsafe fn lean_alloc_array(size: usize, capacity: usize) -> lean_obj_res {
let total = size_of::<LeanArrayObjectRepr>().strict_add(size_of::<*mut lean_object>().strict_mul(capacity));
unsafe {
let o = lean_alloc_object(total);
let header = o.cast::<LeanObjectRepr>();
(*header).m_rc = 1;
(*header).m_tag = LEAN_ARRAY;
(*header).m_other = 0;
let array = o.cast::<LeanArrayObjectRepr>();
(*array).size = size;
(*array).capacity = capacity;
o
}
}
#[inline(always)]
pub unsafe fn lean_alloc_sarray(elem_size: u32, size: usize, capacity: usize) -> lean_obj_res {
let elem_size_usize = elem_size as usize;
let total = size_of::<LeanSArrayObjectRepr>().strict_add(elem_size_usize.strict_mul(capacity));
unsafe {
let o = lean_alloc_object(total);
let header = o.cast::<LeanObjectRepr>();
(*header).m_rc = 1;
(*header).m_tag = LEAN_SCALAR_ARRAY;
(*header).m_other = elem_size as u8;
let sarray = o.cast::<LeanSArrayObjectRepr>();
(*sarray).size = size;
(*sarray).capacity = capacity;
o
}
}
#[inline(always)]
unsafe fn as_array<'a>(o: *mut lean_object) -> &'a LeanArrayObjectRepr {
unsafe { &*o.cast::<LeanArrayObjectRepr>() }
}
#[inline(always)]
unsafe fn as_sarray<'a>(o: *mut lean_object) -> &'a LeanSArrayObjectRepr {
unsafe { &*o.cast::<LeanSArrayObjectRepr>() }
}
#[inline(always)]
pub unsafe fn lean_array_size(o: b_lean_obj_arg) -> usize {
unsafe { as_array(o).size }
}
#[inline(always)]
pub unsafe fn lean_array_capacity(o: b_lean_obj_arg) -> usize {
unsafe { as_array(o).capacity }
}
#[inline(always)]
pub unsafe fn lean_array_cptr(o: *mut lean_object) -> *mut *mut lean_object {
unsafe { (&raw mut (*o.cast::<LeanArrayObjectRepr>()).data).cast::<*mut lean_object>() }
}
#[inline(always)]
pub unsafe fn lean_array_get_core(o: b_lean_obj_arg, i: usize) -> *mut lean_object {
unsafe { *lean_array_cptr(o).add(i) }
}
#[inline(always)]
pub unsafe fn lean_array_set_core(o: *mut lean_object, i: usize, v: lean_obj_arg) {
unsafe { *lean_array_cptr(o).add(i) = v }
}
#[inline(always)]
pub unsafe fn lean_sarray_elem_size(o: *mut lean_object) -> u8 {
unsafe { crate::object::lean_ptr_other(o) }
}
#[inline(always)]
pub unsafe fn lean_sarray_size(o: b_lean_obj_arg) -> usize {
unsafe { as_sarray(o).size }
}
#[inline(always)]
pub unsafe fn lean_sarray_capacity(o: *mut lean_object) -> usize {
unsafe { as_sarray(o).capacity }
}
#[inline(always)]
pub unsafe fn lean_sarray_cptr(o: *mut lean_object) -> *mut u8 {
unsafe { (&raw mut (*o.cast::<LeanSArrayObjectRepr>()).data).cast::<u8>() }
}