JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

More progress :)

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨92c3f32

⁨giterated-plugin/src/vtable/value.rs⁩ - ⁨2497⁩ bytes
Raw
1 use giterated_models::{
2 object::GiteratedObject,
3 value::{GetValue, GiteratedObjectValue},
4 };
5
6 use crate::{AnySuccess, FFIBox};
7
8 use super::OperationVTable;
9
10 #[repr(C)]
11 pub struct NewAnyValue {
12 /// A pointer to the plugin-local object type. We are not capable of
13 /// knowing what this type is, we use the provided vtable.
14 inner: FFIBox<()>,
15 vtable: ValueVTable,
16 }
17
18 unsafe impl Send for NewAnyValue {}
19 unsafe impl Sync for NewAnyValue {}
20
21 impl NewAnyValue {
22 pub fn new<O, V: IntoValueVTable<O> + Send + Sync>(value: V) -> Self {
23 NewAnyValue {
24 inner: FFIBox::from_box(Box::new(value)).untyped(),
25 vtable: ValueVTable::new::<O, V>(),
26 }
27 }
28 }
29
30 impl From<NewAnyValue> for AnySuccess {
31 fn from(val: NewAnyValue) -> Self {
32 unsafe { AnySuccess::from_raw(val.inner, (val.vtable.get_value_vtable)()) }
33 }
34 }
35
36 #[derive(Clone, Copy)]
37 #[repr(C)]
38 pub struct ValueVTable {
39 pub get_value_vtable: unsafe extern "C" fn() -> OperationVTable,
40
41 pub deserialize: unsafe extern "C" fn(&[u8]) -> Result<NewAnyValue, ()>,
42 pub serialize: unsafe extern "C" fn(NewAnyValue) -> Result<FFIBox<[u8]>, ()>,
43 }
44
45 impl ValueVTable {
46 pub fn new<O, T: IntoValueVTable<O>>() -> Self {
47 Self {
48 get_value_vtable: T::get_value_vtable,
49 deserialize: T::deserialize,
50 serialize: T::serialize,
51 }
52 }
53 }
54
55 pub trait IntoValueVTable<O> {
56 unsafe extern "C" fn get_value_vtable() -> OperationVTable;
57 unsafe extern "C" fn deserialize(src: &[u8]) -> Result<NewAnyValue, ()>;
58 unsafe extern "C" fn serialize(this: NewAnyValue) -> Result<FFIBox<[u8]>, ()>;
59 }
60
61 impl<O, V> IntoValueVTable<O> for V
62 where
63 O: GiteratedObject,
64 V: GiteratedObjectValue<Object = O>,
65 {
66 unsafe extern "C" fn get_value_vtable() -> OperationVTable {
67 OperationVTable::new::<O, GetValue>()
68 }
69
70 unsafe extern "C" fn deserialize(src: &[u8]) -> Result<NewAnyValue, ()> {
71 let _guard = trace_span!(
72 "deserialize value",
73 object = O::object_name(),
74 value = V::value_name()
75 );
76
77 trace!("Deserializing");
78 let deserialized: V = serde_json::from_slice(src).unwrap();
79
80 Ok(NewAnyValue {
81 inner: FFIBox::from_box(Box::new(deserialized)).untyped(),
82 vtable: ValueVTable::new::<O, V>(),
83 })
84 }
85
86 unsafe extern "C" fn serialize(_this: NewAnyValue) -> Result<FFIBox<[u8]>, ()> {
87 todo!()
88 }
89 }
90