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

ambee/giterated

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

Fucking whatever there you go

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨57c2ca5

⁨giterated-plugin/src/vtable/setting.rs⁩ - ⁨1629⁩ bytes
Raw
1 use std::mem::transmute;
2
3 use giterated_models::settings::Setting;
4
5 use crate::FFIBox;
6
7 #[repr(C)]
8 pub struct NewAnySetting {
9 /// A pointer to the plugin-local object type. We are not capable of
10 /// knowing what this type is, we use the provided vtable.
11 inner: FFIBox<()>,
12 vtable: SettingVtable,
13 }
14
15 impl NewAnySetting {
16 pub fn new<V: IntoSettingVTable>(value: V) -> Self {
17 Self {
18 inner: FFIBox::from_box(Box::new(value)).untyped(),
19 vtable: SettingVtable::new::<V>(),
20 }
21 }
22
23 pub unsafe fn transmute_owned<T>(self) -> Box<T> {
24 Box::from_raw(self.inner.0 as *mut T)
25 }
26
27 pub unsafe fn transmute_ref<T>(&self) -> &T {
28 let ptr: *const T = transmute(self.inner.0);
29
30 ptr.as_ref().unwrap()
31 }
32 }
33
34 #[derive(Clone, Copy)]
35 #[repr(C)]
36 pub struct SettingVtable {
37 pub deserialize: unsafe extern "C" fn(&[u8]) -> Result<(), ()>,
38 pub serialize: unsafe extern "C" fn(NewAnySetting) -> Result<FFIBox<[u8]>, ()>,
39 }
40
41 impl SettingVtable {
42 pub fn new<T: IntoSettingVTable>() -> Self {
43 Self {
44 deserialize: T::deserialize,
45 serialize: T::serialize,
46 }
47 }
48 }
49
50 pub trait IntoSettingVTable {
51 unsafe extern "C" fn deserialize(src: &[u8]) -> Result<(), ()>;
52 unsafe extern "C" fn serialize(this: NewAnySetting) -> Result<FFIBox<[u8]>, ()>;
53 }
54
55 impl<S> IntoSettingVTable for S
56 where
57 S: Setting,
58 {
59 unsafe extern "C" fn deserialize(src: &[u8]) -> Result<(), ()> {
60 todo!()
61 }
62
63 unsafe extern "C" fn serialize(this: NewAnySetting) -> Result<FFIBox<[u8]>, ()> {
64 todo!()
65 }
66 }
67