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

ambee/giterated

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

Spinning

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨1788060

⁨giterated-runtime/giterated-abi/src/callback/value.rs⁩ - ⁨5461⁩ 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::{FromState, 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, A> {
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 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 impl<F, O, V, Fut, A1> IntoPluginValueGetter<O, V, (A1,)> for F
87 where
88 Fut: Future<Output = Result<V, OperationError<anyhow::Error>>> + Send + Sync,
89 O: GiteratedObject + 'static,
90 V: GiteratedObjectValue<Object = O> + Send + Sync + 'static,
91 F: Fn(O, A1) -> Fut + Send + Sync + 'static,
92 A1: FromState,
93 {
94 unsafe extern "C" fn get_value(
95 callback: CallbackPtr<SettingGetterCallback>,
96 state: FfiValueMut<State>,
97 object: FfiValueRef<Object>,
98 ) -> FfiFuture<FfiResult<Value, FfiError>> {
99 // let _guard = trace_span!(
100 // "get_value handler",
101 // object = O::object_name(),
102 // value = V::value_name()
103 // )
104 // .entered();
105 // let state = unsafe { state.transmute_ref::<S>() };
106
107 // let object = unsafe { object.transmute_owned::<O>() };
108
109 // // Cast the callback ptr to ourselves
110 // let callback: *const F = std::mem::transmute(callback.0);
111 // let callback = callback.as_ref().unwrap();
112
113 // let state = state.clone();
114 // runtime_state.spawn_future(async move {
115 // let result = callback(state, *object).await;
116
117 // match result {
118 // Ok(success) => unsafe { Ok(NewAnyValue::new(success)) },
119 // Err(err) => match err {
120 // OperationError::Operation(_) => todo!(),
121 // OperationError::Internal(_) => todo!(),
122 // OperationError::Unhandled => todo!(),
123 // },
124 // }
125 // })
126
127 todo!()
128 }
129
130 fn callback_ptr(&self) -> CallbackPtr<SettingGetterCallback> {
131 todo!()
132 // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) }
133 }
134 }
135
136 pub struct ValueChangeCallback(CallbackPtr<ValueChangeCallback>);
137
138 impl Callback for ValueChangeCallback {
139 type CallbackFunc = unsafe extern "C" fn(
140 state: FfiValueMut<State>,
141 object: FfiValueRef<Object>,
142 value_name: FfiSliceRef<str>,
143 new_value: Value,
144 ) -> FfiFuture<()>;
145 }
146
147 pub trait IntoValueChangeCallback<S, O> {
148 unsafe extern "C" fn value_changed(
149 callback: CallbackPtr<ValueChangeCallback>,
150 state: FfiValueMut<State>,
151 object: FfiValueRef<Object>,
152 value_name: FfiSliceRef<str>,
153 new_value: Value,
154 ) -> FfiFuture<()>;
155 }
156
157 impl<F, S, O> IntoValueChangeCallback<S, O> for F {
158 unsafe extern "C" fn value_changed(
159 callback: CallbackPtr<ValueChangeCallback>,
160 state: FfiValueMut<State>,
161 _object: FfiValueRef<Object>,
162 _value_name: FfiSliceRef<str>,
163 _new_value: Value,
164 ) -> FfiFuture<()> {
165 todo!()
166 }
167 }
168