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

ambee/giterated

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

So. Much. Work.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨b05f964

⁨giterated-plugins/giterated-plugin/src/callback/operation.rs⁩ - ⁨2180⁩ bytes
Raw
1 use giterated_models::error::OperationError;
2
3 use crate::{new_stack::PluginState, AnyObject, AnyOperation};
4
5 use std::{any::type_name, fmt::Debug, future::Future};
6
7 use super::CallbackPtr;
8
9 #[derive(Clone, Copy)]
10 pub struct OperationHandlerCallback {
11 pub callback_ptr: CallbackPtr,
12 pub func:
13 unsafe extern "C" fn(CallbackPtr, &PluginState, object: AnyObject, operation: AnyOperation),
14 }
15
16 impl OperationHandlerCallback {
17 pub fn new<S, O, D, DS, DF, T: IntoPluginOperationHandler<S, O, D, DS, DF>>(
18 handler: T,
19 ) -> Self {
20 OperationHandlerCallback {
21 func: T::handle,
22 callback_ptr: T::callback_ptr(&handler),
23 }
24 }
25 }
26
27 pub trait IntoPluginOperationHandler<S, O, D, DS, DF> {
28 unsafe extern "C" fn handle(
29 callback_ptr: CallbackPtr,
30 state: &PluginState,
31 object: AnyObject,
32 operation: AnyOperation,
33 );
34 fn callback_ptr(&self) -> CallbackPtr;
35 }
36
37 impl<F, S, O, D, DS, DF, Fut> IntoPluginOperationHandler<S, O, D, DS, DF> for F
38 where
39 Fut: Future<Output = Result<DS, OperationError<DF>>>,
40 F: Fn(S, O, D) -> Fut,
41 S: Clone + Debug,
42 O: Debug,
43 D: Debug,
44 {
45 unsafe extern "C" fn handle(
46 callback: CallbackPtr,
47 state: &PluginState,
48 mut object: AnyObject,
49 mut operation: AnyOperation,
50 ) {
51 let _guard = trace_span!(
52 "operation handler",
53 object = type_name::<O>(),
54 operation = type_name::<D>()
55 )
56 .entered();
57 let state = unsafe { state.transmute_ref::<S>() };
58
59 // Since this is Rust code, we know that the AnyObject and AnyOperation are just boxes
60 let object = unsafe { object.transmute_owned::<O>() };
61 let operation = unsafe { operation.transmute_owned::<D>() };
62
63 // Cast the callback ptr to ourselves
64 let callback: *const F = std::mem::transmute(callback.0);
65 let callback = callback.as_ref().unwrap();
66
67 // callback(state.clone(), *object, *operation)
68
69 todo!()
70 }
71
72 fn callback_ptr(&self) -> CallbackPtr {
73 unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
74 }
75 }
76