use giterated_models::{object::GiteratedObject, value::GiteratedObjectValue}; use crate::FFIBox; #[repr(C)] pub struct NewAnyValue { /// A pointer to the plugin-local object type. We are not capable of /// knowing what this type is, we use the provided vtable. inner: FFIBox<()>, vtable: ValueVTable, } impl NewAnyValue { pub fn new>(value: V) -> Self { NewAnyValue { inner: FFIBox::from_box(Box::new(value)).untyped(), vtable: ValueVTable::new::(), } } } #[derive(Clone, Copy)] #[repr(C)] pub struct ValueVTable { pub deserialize: unsafe extern "C" fn(&[u8]) -> Result, pub serialize: unsafe extern "C" fn(NewAnyValue) -> Result, ()>, } impl ValueVTable { pub fn new>() -> Self { Self { deserialize: T::deserialize, serialize: T::serialize, } } } pub trait IntoValueVTable { unsafe extern "C" fn deserialize(src: &[u8]) -> Result; unsafe extern "C" fn serialize(this: NewAnyValue) -> Result, ()>; } impl IntoValueVTable for V where O: GiteratedObject, V: GiteratedObjectValue, { unsafe extern "C" fn deserialize(src: &[u8]) -> Result { let _guard = trace_span!( "deserialize value", object = O::object_name(), value = V::value_name() ); trace!("Deserializing"); let deserialized: V = serde_json::from_slice(src).unwrap(); Ok(NewAnyValue { inner: FFIBox::from_box(Box::new(deserialized)).untyped(), vtable: ValueVTable::new::(), }) } unsafe extern "C" fn serialize(this: NewAnyValue) -> Result, ()> { todo!() } }