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

ambee/giterated

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

insanity

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨6ea28ab

⁨giterated-abi/src/callback/operation.rs⁩ - ⁨4791⁩ bytes
Raw
1 use std::future::Future;
2
3 use giterated_models::{
4 error::OperationError, object::GiteratedObject, operation::GiteratedOperation,
5 };
6
7 use crate::{
8 result::FfiResult,
9 state::State,
10 value_ex::FfiValueUntyped,
11 vtable::{operation::Operation, Object},
12 FfiFuture, FfiValueMut, FfiValueRef,
13 };
14
15 use super::{Callback, CallbackPtr};
16
17 use std::fmt::Debug;
18
19 pub struct OperationHandlerCallback(FfiValueUntyped);
20
21 impl Callback for OperationHandlerCallback {
22 type CallbackFunc = unsafe extern "C" fn(
23 CallbackPtr<OperationHandlerCallback>,
24 state: FfiValueMut<State>,
25 object: FfiValueRef<Object>,
26 operation: FfiValueRef<Operation>,
27 ) -> FfiFuture<
28 FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>,
29 >;
30 }
31
32 pub trait IntoPluginOperationHandler<O: GiteratedObject, D: GiteratedOperation<O>, A> {
33 unsafe extern "C" fn handle(
34 callback_ptr: CallbackPtr<OperationHandlerCallback>,
35 state: FfiValueMut<State>,
36 object: FfiValueRef<Object>,
37 operation: FfiValueRef<Operation>,
38 ) -> FfiFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>>;
39 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback>;
40 }
41
42 impl<F, O, D, Fut> IntoPluginOperationHandler<O, D, ()> for F
43 where
44 Fut: Future<Output = Result<D::Success, OperationError<D::Failure>>> + Send + Sync,
45 F: Fn(O, D) -> Fut + Send + Sync + 'static,
46 O: Debug + GiteratedObject + 'static,
47 D: Debug + GiteratedOperation<O> + 'static,
48 {
49 unsafe extern "C" fn handle(
50 callback: CallbackPtr<OperationHandlerCallback>,
51 state: FfiValueMut<State>,
52 object: FfiValueRef<Object>,
53 operation: FfiValueRef<Operation>,
54 ) -> FfiFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
55 todo!()
56 // let _guard = trace_span!(
57 // "operation handler",
58 // object = type_name::<O>(),
59 // operation = type_name::<D>()
60 // )
61 // .entered();
62 // let state = unsafe { state.transmute_ref::<S>() };
63
64 // // Since this is Rust code, we know that the AnyObject and AnyOperation are just boxes
65 // let object = unsafe { object.transmute_owned::<O>() };
66 // let operation = unsafe { operation.transmute_owned::<D>() };
67
68 // // Cast the callback ptr to ourselves
69 // let callback: *const F = std::mem::transmute(callback.0);
70 // let callback = callback.as_ref().unwrap();
71
72 // let state = state.clone();
73 // runtime_state.spawn_future(async move {
74 // let result = callback(state, *object, *operation).await;
75
76 // match result {
77 // Ok(success) => unsafe {
78 // todo!()
79 // // Ok(AnySuccess::from_raw(
80 // // FFIBox::from_box(Box::new(success)).untyped(),
81 // // OperationVTable::new::<O, D>(),
82 // // ))
83 // },
84 // Err(err) => match err {
85 // OperationError::Operation(_) => todo!(),
86 // OperationError::Internal(_) => todo!(),
87 // OperationError::Unhandled => todo!(),
88 // },
89 // }
90 // })
91 }
92
93 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
94 // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
95
96 todo!()
97 }
98 }
99
100 impl<F, O, D, Fut, A1> IntoPluginOperationHandler<O, D, (A1,)> for F
101 where
102 Fut: Future<Output = Result<D::Success, OperationError<D::Failure>>>,
103 F: Fn(O, D, A1) -> Fut,
104 O: Debug + GiteratedObject,
105 D: Debug + GiteratedOperation<O>,
106 {
107 unsafe extern "C" fn handle(
108 _callback_ptr: CallbackPtr<OperationHandlerCallback>,
109 state: FfiValueMut<State>,
110 object: FfiValueRef<Object>,
111 operation: FfiValueRef<Operation>,
112 ) -> FfiFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
113 todo!()
114 }
115
116 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
117 todo!()
118 }
119 }
120
121 impl<F, O, D, Fut, A1, A2> IntoPluginOperationHandler<O, D, (A1, A2)> for F
122 where
123 Fut: Future<Output = Result<D::Success, OperationError<D::Failure>>>,
124 F: Fn(O, D, A1, A2) -> Fut,
125 O: Debug + GiteratedObject,
126 D: Debug + GiteratedOperation<O>,
127 {
128 unsafe extern "C" fn handle(
129 _callback_ptr: CallbackPtr<OperationHandlerCallback>,
130 state: FfiValueMut<State>,
131 object: FfiValueRef<Object>,
132 operation: FfiValueRef<Operation>,
133 ) -> FfiFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
134 todo!()
135 }
136
137 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
138 todo!()
139 }
140 }
141