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

ambee/giterated

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

Huge refactor to prep for moving the daemon over to the plugin architecture

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨5df753c

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