use crate::libR::*;
use core::ffi::c_char;
#[inline(always)]
pub fn len(s:SEXP) -> R_xlen_t {
unsafe { Rf_xlength(s) }
}
pub trait RType:Copy {
const SEXPTYPE:SEXPTYPE;
type Data:Copy=Self; type New;
fn new(len: Self::New) -> SEXP;
#[inline(always)]
fn is_type(s:SEXP) -> bool {
unsafe { TYPEOF(s) == Self::SEXPTYPE }
}
#[inline(always)]
unsafe fn data(s:SEXP) -> *const Self::Data {
unsafe { DATAPTR_RO(s) as *const Self::Data }
}
#[inline(always)]
unsafe fn data_mut(s:SEXP) -> *mut Self::Data where Self:RTypeMut {
unsafe { DATAPTR(s) as *mut Self::Data }
}
}
pub unsafe trait RTypeMut:RType {}
#[allow(non_camel_case_types)]
pub type logical=u32;
#[allow(non_camel_case_types)]
pub type integer=i32;
#[allow(non_camel_case_types)]
pub type numeric=f64;
#[allow(non_camel_case_types)]
pub type character=u8;
pub const CHARSXP: SEXPTYPE = 9;
pub const LGLSXP: SEXPTYPE = 10;
pub const INTSXP: SEXPTYPE = 13;
pub const REALSXP: SEXPTYPE = 14;
impl RType for character {
const SEXPTYPE:SEXPTYPE=CHARSXP;
type New=String;
#[inline(always)]
fn new(s: Self::New) -> SEXP {
let s:&str=s.as_ref();
unsafe { Rf_mkCharLenCE(
s.as_ptr() as *const c_char,
s.len() as i32,
cetype_t_CE_UTF8,)
}
}
}
impl RType for logical {
const SEXPTYPE:SEXPTYPE=LGLSXP;
type New=R_xlen_t;
fn new(len: Self::New) -> SEXP {
unsafe { Rf_allocVector(Self::SEXPTYPE, len) }
}
}
impl RType for integer {
const SEXPTYPE:SEXPTYPE=INTSXP;
type New=R_xlen_t;
fn new(len: Self::New) -> SEXP {
unsafe { Rf_allocVector(Self::SEXPTYPE, len) }
}
}
impl RType for numeric {
const SEXPTYPE:SEXPTYPE=REALSXP;
type New=R_xlen_t;
fn new(len: Self::New) -> SEXP {
unsafe { Rf_allocVector(Self::SEXPTYPE, len) }
}
}
unsafe impl RTypeMut for logical{}
unsafe impl RTypeMut for integer{}
unsafe impl RTypeMut for numeric{}