use core::{
ffi::{CStr, c_char, c_void},
slice,
};
use nsi_ffi_wrap::{Arg, nsi_sys::NSIParamFlags};
use nsi_trait::{ParamValue, Type};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct HostPointer(pub *const c_void);
unsafe impl Send for HostPointer {}
unsafe impl Sync for HostPointer {}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum OwnedData {
F32(Vec<f32>),
F64(Vec<f64>),
I32(Vec<i32>),
I64(Vec<i64>),
String(Vec<Vec<u8>>),
Reference(Vec<HostPointer>),
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OwnedArgument {
pub name: String,
pub type_tag: Type,
pub array_length: usize,
pub flags: i32,
pub data: OwnedData,
}
impl OwnedArgument {
pub fn new(
name: impl Into<String>,
type_tag: Type,
array_length: usize,
flags: i32,
data: OwnedData,
) -> Self {
Self {
name: name.into(),
type_tag,
array_length,
flags,
data,
}
}
pub fn as_f32s(&self) -> Option<&[f32]> {
match &self.data {
OwnedData::F32(values) => Some(values),
_ => None,
}
}
pub fn as_f64s(&self) -> Option<&[f64]> {
match &self.data {
OwnedData::F64(values) => Some(values),
_ => None,
}
}
pub fn as_i32s(&self) -> Option<&[i32]> {
match &self.data {
OwnedData::I32(values) => Some(values),
_ => None,
}
}
pub fn as_i64s(&self) -> Option<&[i64]> {
match &self.data {
OwnedData::I64(values) => Some(values),
_ => None,
}
}
pub fn as_strings(&self) -> Option<&[Vec<u8>]> {
match &self.data {
OwnedData::String(values) => Some(values),
_ => None,
}
}
pub fn as_f32(&self) -> Option<f32> {
match self.as_f32s() {
Some([value]) => Some(*value),
_ => None,
}
}
pub fn as_i32(&self) -> Option<i32> {
match self.as_i32s() {
Some([value]) => Some(*value),
_ => None,
}
}
pub fn element_count(&self) -> usize {
let per = match self.type_tag {
Type::Color | Type::Point | Type::Vector | Type::Normal => 3,
Type::MatrixF32 | Type::MatrixF64 => 16,
_ => 1,
};
let scalars = match &self.data {
OwnedData::F32(values) => values.len(),
OwnedData::F64(values) => values.len(),
OwnedData::I32(values) => values.len(),
OwnedData::I64(values) => values.len(),
OwnedData::String(values) => values.len(),
OwnedData::Reference(values) => values.len(),
};
scalars / per / self.array_length.max(1)
}
pub fn as_matrix(&self) -> Option<[f64; 16]> {
match self.as_matrices()? {
[] => None,
values if values.len() == 16 => {
Some(values[..16].try_into().expect("length checked"))
}
_ => None,
}
}
pub fn as_matrices(&self) -> Option<&[f64]> {
if self.type_tag != Type::MatrixF64 || self.is_array() {
return None;
}
match &self.data {
OwnedData::F64(values) => Some(values),
_ => None,
}
}
pub fn is_array(&self) -> bool {
self.flags & NSIParamFlags::IsArray.bits() != 0
}
pub(crate) fn from_param(param: &Arg<'_, '_>) -> Self {
let type_tag = param.type_tag();
let array_length = param.array_length().max(1);
let elements = param.len() / array_length * array_length;
let scalars = elements * components_per_element(type_tag);
let c = param
.as_c_param()
.expect("nsi-ffi-wrap Arg always yields a C view");
let data = unsafe {
match type_tag {
Type::F32
| Type::Color
| Type::Point
| Type::Vector
| Type::Normal
| Type::MatrixF32 => OwnedData::F32(
slice::from_raw_parts(c.data as *const f32, scalars)
.to_vec(),
),
Type::F64 | Type::MatrixF64 => OwnedData::F64(
slice::from_raw_parts(c.data as *const f64, scalars)
.to_vec(),
),
Type::I32 => OwnedData::I32(
slice::from_raw_parts(c.data as *const i32, scalars)
.to_vec(),
),
Type::I64 => OwnedData::I64(
slice::from_raw_parts(c.data as *const i64, scalars)
.to_vec(),
),
Type::String => {
let ptrs = slice::from_raw_parts(
c.data as *const *const c_char,
scalars,
);
OwnedData::String(
ptrs.iter()
.map(|p| CStr::from_ptr(*p).to_bytes().to_vec())
.collect(),
)
}
Type::Reference => OwnedData::Reference(
slice::from_raw_parts(
c.data as *const *const c_void,
scalars,
)
.iter()
.map(|p| HostPointer(*p))
.collect(),
),
Type::Invalid => {
unreachable!("nsi-ffi-wrap never yields Type::Invalid")
}
}
};
Self {
name: param.name().to_string(),
type_tag,
array_length: param.array_length(),
flags: param.flags(),
data,
}
}
}
#[inline]
const fn components_per_element(type_tag: Type) -> usize {
match type_tag {
Type::Color | Type::Point | Type::Vector | Type::Normal => 3,
Type::MatrixF32 | Type::MatrixF64 => 16,
_ => 1,
}
}
#[cfg(test)]
mod tests;