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/lib.rs⁩ - ⁨1860⁩ bytes
Raw
1 #![allow(improper_ctypes_definitions)]
2
3 pub mod callback;
4 pub mod future;
5 pub mod handle;
6 pub mod new_stack;
7 pub mod vtable;
8
9 #[macro_use]
10 extern crate tracing;
11
12 use callback::RuntimeState;
13 use dlopen2::wrapper::WrapperApi;
14
15 use handle::PluginInitializationState;
16 use new_stack::{FFIPluginMeta, PluginState};
17
18 pub use vtable::{AnyFailure, AnyObject, AnyOperation, AnySuccess, NewAnySetting, NewAnyValue};
19 use vtable::{HostVTable, InitializationVTable};
20
21 #[derive(WrapperApi)]
22 pub struct GiteratedPluginApi {
23 plugin_meta: unsafe extern "C" fn() -> FFIPluginMeta,
24 load_host_vtable: unsafe extern "C" fn(vtable: &HostVTable),
25 load_initialization_vtable: unsafe extern "C" fn(vtable: &InitializationVTable),
26 initialize: unsafe extern "C" fn(runtime_state: *const RuntimeState) -> PluginState,
27 initialize_registration: unsafe extern "C" fn(
28 init_state: *mut PluginInitializationState,
29 ) -> *mut PluginInitializationState,
30 }
31
32 #[repr(C)]
33 pub struct FFIBox<T: ?Sized>(*mut T);
34
35 impl<T: ?Sized> FFIBox<T> {
36 pub fn from_box(src: Box<T>) -> Self {
37 Self(Box::into_raw(src))
38 }
39
40 pub fn untyped(self) -> FFIBox<()> {
41 FFIBox(self.0 as *mut ())
42 }
43
44 pub unsafe fn retype<N>(self) -> FFIBox<N> {
45 FFIBox(self.0 as *mut N)
46 }
47
48 pub unsafe fn into_box(self) -> Box<T> {
49 Box::from_raw(self.0)
50 }
51
52 pub unsafe fn transmute_ref<N>(&self) -> &N {
53 unsafe { (self.0 as *const N).as_ref() }.unwrap()
54 }
55 }
56
57 impl ToString for FFIBox<str> {
58 fn to_string(&self) -> String {
59 todo!()
60 }
61 }
62
63 impl<T: ?Sized> AsRef<T> for FFIBox<T> {
64 fn as_ref(&self) -> &T {
65 unsafe { self.0.as_ref() }.unwrap()
66 }
67 }
68
69 impl<T: ?Sized> std::ops::Deref for FFIBox<T> {
70 type Target = T;
71
72 fn deref(&self) -> &Self::Target {
73 unsafe { self.0.as_ref() }.unwrap()
74 }
75 }
76