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

ambee/giterated

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

MOre pre vtable changes

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨9cfa135

⁨giterated-plugin/src/callback/mod.rs⁩ - ⁨2468⁩ bytes
Raw
1 mod operation;
2
3 use giterated_models::{
4 error::OperationError,
5 object::{GiteratedObject, Object, ObjectRequestError},
6 object_backend::ObjectBackend,
7 operation::{GiteratedOperation, OperationState},
8 };
9 use std::fmt::Debug;
10
11 pub use operation::*;
12 mod value;
13 pub use value::*;
14 mod setting;
15 pub use setting::*;
16
17 use crate::{
18 new_stack::{PluginState, Runtime},
19 vtable::RuntimeVTable,
20 AnyObject, AnyOperation,
21 };
22
23 /// A container for a callback pointer, used to provide an internal callback function or
24 /// state to a plugin when performing a callback.
25 #[derive(Clone, Copy)]
26 #[repr(C)]
27 pub struct CallbackPtr(*const ());
28
29 impl CallbackPtr {
30 pub unsafe fn from_raw(callback: *const ()) -> Self {
31 Self(callback)
32 }
33 }
34
35 #[derive(Clone)]
36 #[repr(C)]
37 pub struct RuntimeState {
38 pub vtable: RuntimeVTable,
39 }
40
41 impl RuntimeState {
42 pub unsafe fn from_static() -> Self {
43 let runtime = giterated_static_runtime::get_runtime_reference();
44
45 let runtime = runtime.cast::<Box<Runtime>>().as_ref();
46
47 runtime.state()
48 }
49
50 pub unsafe fn runtime_state() -> PluginState {
51 let runtime = giterated_static_runtime::get_runtime_reference();
52
53 PluginState::from_raw_ptr(giterated_static_runtime::get_runtime_reference().as_ptr())
54 }
55 }
56
57 #[async_trait::async_trait(?Send)]
58 impl ObjectBackend for RuntimeState {
59 async fn object_operation<O, D>(
60 &self,
61 object: O,
62 _operation: &str,
63 payload: D,
64 _operation_state: &OperationState,
65 ) -> Result<D::Success, OperationError<D::Failure>>
66 where
67 O: GiteratedObject + Debug + 'static,
68 D: GiteratedOperation<O> + Debug + 'static,
69 D::Success: Clone,
70 D::Failure: Clone,
71 {
72 let _object = AnyObject::new(object);
73 let _operation = AnyOperation::new(payload);
74
75 todo!()
76 }
77
78 async fn get_object<O: GiteratedObject + Debug + 'static>(
79 &self,
80 object_str: &str,
81 operation_state: &OperationState,
82 ) -> Result<Object<O, Self>, OperationError<ObjectRequestError>> {
83 let object = unsafe {
84 (self.vtable.get_object)(
85 Self::runtime_state(),
86 object_str,
87 &mut operation_state.clone(),
88 )
89 }?;
90
91 let object = unsafe { object.cast::<O>() };
92
93 panic!("object casted");
94
95 Ok(unsafe { Object::new_unchecked(object, self.clone()) })
96 }
97 }
98