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/value.rs⁩ - ⁨3689⁩ bytes
Raw
1 use std::future::Future;
2
3 use giterated_models::{
4 error::OperationError, object::GiteratedObject, value::GiteratedObjectValue,
5 };
6
7 use crate::{
8 result::{FfiError, FfiResult},
9 state::State,
10 vtable::{Object, Value},
11 FfiFuture, FfiSliceRef, FfiValueMut, FfiValueRef,
12 };
13
14 use super::{setting::SettingGetterCallback, Callback, CallbackPtr};
15
16 #[derive(Copy, Clone)]
17 pub struct ValueGetterCallback(CallbackPtr<ValueGetterCallback>);
18
19 impl Callback for ValueGetterCallback {
20 type CallbackFunc = unsafe extern "C" fn(
21 CallbackPtr<ValueGetterCallback>,
22 state: FfiValueMut<State>,
23 object: FfiValueRef<Object>,
24 ) -> FfiFuture<FfiResult<Value, FfiError>>;
25 }
26
27 pub trait IntoPluginValueGetter<O, V> {
28 unsafe extern "C" fn get_value(
29 callback: CallbackPtr<SettingGetterCallback>,
30 state: FfiValueMut<State>,
31 object: FfiValueRef<Object>,
32 ) -> FfiFuture<FfiResult<Value, FfiError>>;
33
34 fn callback_ptr(&self) -> CallbackPtr<SettingGetterCallback>;
35 }
36
37 impl<F, O, V, Fut> IntoPluginValueGetter<O, V> for F
38 where
39 Fut: Future<Output = Result<V, OperationError<anyhow::Error>>> + Send + Sync,
40 O: GiteratedObject + 'static,
41 V: GiteratedObjectValue<Object = O> + Send + Sync + 'static,
42 F: Fn(O) -> Fut + Send + Sync + 'static,
43 {
44 unsafe extern "C" fn get_value(
45 callback: CallbackPtr<SettingGetterCallback>,
46 state: FfiValueMut<State>,
47 mut object: FfiValueRef<Object>,
48 ) -> FfiFuture<FfiResult<Value, FfiError>> {
49 // let _guard = trace_span!(
50 // "get_value handler",
51 // object = O::object_name(),
52 // value = V::value_name()
53 // )
54 // .entered();
55 // let state = unsafe { state.transmute_ref::<S>() };
56
57 // let object = unsafe { object.transmute_owned::<O>() };
58
59 // // Cast the callback ptr to ourselves
60 // let callback: *const F = std::mem::transmute(callback.0);
61 // let callback = callback.as_ref().unwrap();
62
63 // let state = state.clone();
64 // runtime_state.spawn_future(async move {
65 // let result = callback(state, *object).await;
66
67 // match result {
68 // Ok(success) => unsafe { Ok(NewAnyValue::new(success)) },
69 // Err(err) => match err {
70 // OperationError::Operation(_) => todo!(),
71 // OperationError::Internal(_) => todo!(),
72 // OperationError::Unhandled => todo!(),
73 // },
74 // }
75 // })
76
77 todo!()
78 }
79
80 fn callback_ptr(&self) -> CallbackPtr<SettingGetterCallback> {
81 todo!()
82 // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
83 }
84 }
85
86 pub struct ValueChangeCallback(CallbackPtr<ValueChangeCallback>);
87
88 impl Callback for ValueChangeCallback {
89 type CallbackFunc = unsafe extern "C" fn(
90 state: FfiValueMut<State>,
91 object: FfiValueRef<Object>,
92 value_name: FfiSliceRef<str>,
93 new_value: Value,
94 ) -> FfiFuture<()>;
95 }
96
97 pub trait IntoValueChangeCallback<S, O> {
98 unsafe extern "C" fn value_changed(
99 callback: CallbackPtr<ValueChangeCallback>,
100 state: FfiValueMut<State>,
101 object: FfiValueRef<Object>,
102 value_name: FfiSliceRef<str>,
103 new_value: Value,
104 ) -> FfiFuture<()>;
105 }
106
107 impl<F, S, O> IntoValueChangeCallback<S, O> for F {
108 unsafe extern "C" fn value_changed(
109 callback: CallbackPtr<ValueChangeCallback>,
110 state: FfiValueMut<State>,
111 _object: FfiValueRef<Object>,
112 _value_name: FfiSliceRef<str>,
113 _new_value: Value,
114 ) -> FfiFuture<()> {
115 todo!()
116 }
117 }
118