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

ambee/giterated

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

Before

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨e432306

⁨giterated-plugin/src/callback/operation.rs⁩ - ⁨6936⁩ bytes
Raw
1 use giterated_abi::{
2 callback::{Callback, CallbackPtr},
3 result::FfiResult,
4 value_ex::FfiValueUntyped,
5 vtable::{operation::Operation, Object},
6 FfiValueMut, FfiValueRef,
7 };
8 use giterated_models::{
9 error::OperationError,
10 object::GiteratedObject,
11 operation::{GiteratedOperation, OperationState},
12 };
13
14 use crate::{
15 future::{RuntimeFuture, RuntimeFuturesExt},
16 new_stack::{handle::RuntimeHandle, PluginState},
17 state::{FromState, State, StateUUID},
18 };
19
20 use std::{any::type_name, fmt::Debug, future::Future};
21
22 use super::RuntimeState;
23
24 pub struct OperationHandlerCallback(FfiValueUntyped);
25
26 impl Callback for OperationHandlerCallback {
27 type CallbackFunc = unsafe extern "C" fn(
28 CallbackPtr<OperationHandlerCallback>,
29 state: FfiValueMut<State>,
30 object: FfiValueRef<Object>,
31 operation: FfiValueRef<Operation>,
32 ) -> RuntimeFuture<
33 FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>,
34 >;
35 }
36
37 impl OperationHandlerCallback {
38 // pub fn new<
39 // S,
40 // O: GiteratedObject,
41 // D: GiteratedOperation<O>,
42 // A,
43 // T: IntoPluginOperationHandler<S, O, D, A>,
44 // >(
45 // handler: T,
46 // ) -> Self {
47 // OperationHandlerCallback(unsafe { CallbackPtr::from_raw(&handler, T::handle as _) })
48 // }
49 }
50
51 pub trait IntoPluginOperationHandler<S, O: GiteratedObject, D: GiteratedOperation<O>, A> {
52 unsafe extern "C" fn handle(
53 callback_ptr: CallbackPtr<OperationHandlerCallback>,
54 state: FfiValueMut<State>,
55 object: FfiValueRef<Object>,
56 operation: FfiValueRef<Operation>,
57 ) -> RuntimeFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>>;
58 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback>;
59 }
60
61 impl<F, S, O, D, Fut> IntoPluginOperationHandler<S, O, D, ()> for F
62 where
63 Fut: Future<Output = Result<D::Success, OperationError<D::Failure>>> + Send + Sync,
64 F: Fn(S, O, D) -> Fut + Send + Sync + 'static,
65 S: Clone + Debug + Send + Sync + 'static,
66 O: Debug + GiteratedObject + 'static,
67 D: Debug + GiteratedOperation<O> + 'static,
68 {
69 unsafe extern "C" fn handle(
70 callback: CallbackPtr<OperationHandlerCallback>,
71 state: FfiValueMut<State>,
72 object: FfiValueRef<Object>,
73 operation: FfiValueRef<Operation>,
74 ) -> RuntimeFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
75 todo!()
76 // let _guard = trace_span!(
77 // "operation handler",
78 // object = type_name::<O>(),
79 // operation = type_name::<D>()
80 // )
81 // .entered();
82 // let state = unsafe { state.transmute_ref::<S>() };
83
84 // // Since this is Rust code, we know that the AnyObject and AnyOperation are just boxes
85 // let object = unsafe { object.transmute_owned::<O>() };
86 // let operation = unsafe { operation.transmute_owned::<D>() };
87
88 // // Cast the callback ptr to ourselves
89 // let callback: *const F = std::mem::transmute(callback.0);
90 // let callback = callback.as_ref().unwrap();
91
92 // let state = state.clone();
93 // runtime_state.spawn_future(async move {
94 // let result = callback(state, *object, *operation).await;
95
96 // match result {
97 // Ok(success) => unsafe {
98 // todo!()
99 // // Ok(AnySuccess::from_raw(
100 // // FFIBox::from_box(Box::new(success)).untyped(),
101 // // OperationVTable::new::<O, D>(),
102 // // ))
103 // },
104 // Err(err) => match err {
105 // OperationError::Operation(_) => todo!(),
106 // OperationError::Internal(_) => todo!(),
107 // OperationError::Unhandled => todo!(),
108 // },
109 // }
110 // })
111 }
112
113 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
114 // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
115
116 todo!()
117 }
118 }
119
120 impl<F, S, O, D, Fut, A1> IntoPluginOperationHandler<S, O, D, (A1,)> for F
121 where
122 Fut: Future<Output = Result<D::Success, OperationError<D::Failure>>>,
123 F: Fn(S, O, D, A1) -> Fut,
124 S: Clone + Debug,
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 ) -> RuntimeFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
134 todo!()
135 }
136
137 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
138 todo!()
139 }
140 }
141
142 impl<F, S, O, D, Fut, A1, A2> IntoPluginOperationHandler<S, O, D, (A1, A2)> for F
143 where
144 Fut: Future<Output = Result<D::Success, OperationError<D::Failure>>>,
145 F: Fn(S, O, D, A1, A2) -> Fut,
146 S: Clone + Debug,
147 O: Debug + GiteratedObject,
148 D: Debug + GiteratedOperation<O>,
149 {
150 unsafe extern "C" fn handle(
151 _callback_ptr: CallbackPtr<OperationHandlerCallback>,
152 state: FfiValueMut<State>,
153 object: FfiValueRef<Object>,
154 operation: FfiValueRef<Operation>,
155 ) -> RuntimeFuture<FfiResult<FfiValueUntyped, OperationError<FfiValueUntyped>>> {
156 todo!()
157 }
158
159 fn callback_ptr(&self) -> CallbackPtr<OperationHandlerCallback> {
160 todo!()
161 }
162 }
163
164 pub trait FromOperationState<O, D>: Sized {
165 fn from_operation_state(
166 state: &mut State,
167 object: &O,
168 operation: &D,
169 ) -> Result<Self, OperationError<anyhow::Error>>;
170 }
171
172 pub struct StateExtractor<T>(T);
173
174 impl<T: FromState> FromState for StateExtractor<T> {
175 fn from_state(state: &mut State) -> Result<Self, anyhow::Error> {
176 todo!()
177 }
178 }
179
180 // pub trait FromOperationState<O, D>: Sized {
181 // fn from_operation_state(
182 // operation_state: &OperationState,
183 // state: &mut State,
184 // object: &O,
185 // operation: &D,
186 // ) -> Result<Self, OperationError<anyhow::Error>>;
187 // }
188
189 // impl<O, D> FromOperationState<O, D> for RuntimeHandle {
190 // fn from_operation_state(
191 // _operation_state: &OperationState,
192 // state: &mut State,
193 // _object: &O,
194 // _operation: &D,
195 // ) -> Result<Self, OperationError<anyhow::Error>> {
196 // Ok(unsafe { RuntimeHandle::from_vtable(runtime_state.vtable) })
197 // }
198 // }
199
200 // impl<O, D, T> FromOperationState<O, D> for Option<T>
201 // where
202 // T: FromOperationState<O, D>,
203 // {
204 // fn from_operation_state(
205 // operation_state: &OperationState,
206 // state: &mut State,
207 // object: &O,
208 // operation: &D,
209 // ) -> Result<Self, OperationError<anyhow::Error>> {
210 // Ok(T::from_operation_state(operation_state, runtime_state, object, operation).ok())
211 // }
212 // }
213