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

ambee/giterated

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

Wow!

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨6530104

⁨giterated-runtime/giterated-abi/src/callback/operation.rs⁩ - ⁨4861⁩ 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::{FromState, 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 A1: FromState,
107 {
108 unsafe extern "C" fn handle(
109 _callback_ptr: CallbackPtr<OperationHandlerCallback>,
110 state: FfiValueMut<State>,
111 object: FfiValueRef<Object>,
112 operation: FfiValueRef<Operation>,
113 ) -> FfiFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
114 todo!()
115 }
116
117 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
118 todo!()
119 }
120 }
121
122 impl<F, O, D, Fut, A1, A2> IntoPluginOperationHandler<O, D, (A1, A2)> for F
123 where
124 Fut: Future<Output = Result<D::Success, OperationError<D::Failure>>>,
125 F: Fn(O, D, A1, A2) -> Fut,
126 O: Debug + GiteratedObject,
127 D: Debug + GiteratedOperation<O>,
128 A1: FromState,
129 A2: FromState,
130 {
131 unsafe extern "C" fn handle(
132 _callback_ptr: CallbackPtr<OperationHandlerCallback>,
133 state: FfiValueMut<State>,
134 object: FfiValueRef<Object>,
135 operation: FfiValueRef<Operation>,
136 ) -> FfiFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
137 todo!()
138 }
139
140 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
141 todo!()
142 }
143 }
144