mod hs;
mod str;
mod vec;
pub use self::{hs::*, str::*, vec::*};
use core::ffi::*;
pub trait ReprC<T>: private::CFFISafe {
fn from(_: T) -> Self;
}
pub trait ReprRust<T: private::CFFISafe> {
fn from(_: T) -> Self;
}
mod private {
pub trait CFFISafe {}
macro_rules! c_ffi_safe {
($($ty:ty),*) => {$(
impl CFFISafe for $ty {}
impl CFFISafe for *const $ty {}
)*};
}
c_ffi_safe![(), i8, i16, i32, i64, u8, u16, u32, u64, f32, f64];
}
macro_rules! transparent {
($($ty:ty),*) => {$(
impl ReprRust<$ty> for $ty {
#[inline]
fn from(x: $ty) -> Self { x }
}
impl ReprC<$ty> for $ty {
#[inline]
fn from(x: $ty) -> Self { x }
}
impl ReprRust<*const $ty> for *const $ty {
#[inline]
fn from(x: *const $ty) -> Self { x }
}
impl ReprC<*const $ty> for *const $ty {
#[inline]
fn from(x: *const $ty) -> Self { x }
}
)*};
}
transparent![i8, i16, i32, i64, u8, u16, u32, u64, f32, f64];
pub trait ReprHs {
fn into() -> HsType;
}
macro_rules! repr_hs {
($($ty:ty => $ident:ident,)*) => {$(
impl ReprHs for $ty {
fn into() -> HsType {
HsType::$ident
}
}
)*};
}
pub(crate) use repr_hs;
repr_hs! {
c_char => CChar,
c_double => CDouble,
c_float => CFloat,
c_int => CInt,
c_long => CLong,
c_short => CShort,
c_uchar => CUChar,
c_uint => CUInt,
c_ulong => CULong,
c_ushort => CUShort,
}
macro_rules! repr_hs_ptr {
($ty:ty) => {
impl<T> ReprHs for $ty
where
T: ReprHs,
{
fn into() -> HsType {
HsType::Ptr(Box::new(T::into()))
}
}
};
}
pub(crate) use repr_hs_ptr;
repr_hs_ptr!(*const T);
impl ReprC<()> for () {
fn from(_: ()) -> Self {}
}
repr_hs! { () => Empty, }