use std::future::Future; use giterated_abi::{ callback::{Callback, CallbackPtr}, result::{FfiError, FfiResult}, vtable::{Object, Value}, FfiSliceRef, FfiValueMut, FfiValueRef, }; use giterated_models::{ error::OperationError, object::GiteratedObject, value::GiteratedObjectValue, }; use crate::{future::RuntimeFuture, new_stack::PluginState, state::State}; use super::{RuntimeState, SettingGetterCallback}; #[derive(Copy, Clone)] pub struct ValueGetterCallback(CallbackPtr); impl Callback for ValueGetterCallback { type CallbackFunc = unsafe extern "C" fn( CallbackPtr, state: FfiValueMut, object: FfiValueRef, ) -> RuntimeFuture>; } impl ValueGetterCallback { // pub fn new>(handler: T) -> Self { // Self { // func: T::get_value, // callback_ptr: handler.callback_ptr(), // } // } } pub trait IntoPluginValueGetter { unsafe extern "C" fn get_value( callback: CallbackPtr, state: FfiValueMut, object: FfiValueRef, ) -> RuntimeFuture>; fn callback_ptr(&self) -> CallbackPtr; } impl IntoPluginValueGetter for F where Fut: Future>> + Send + Sync, S: Clone + Send + Sync + 'static, O: GiteratedObject + 'static, V: GiteratedObjectValue + Send + Sync + 'static, F: Fn(S, O) -> Fut + Send + Sync + 'static, { unsafe extern "C" fn get_value( callback: CallbackPtr, state: FfiValueMut, mut object: FfiValueRef, ) -> RuntimeFuture> { // let _guard = trace_span!( // "get_value handler", // object = O::object_name(), // value = V::value_name() // ) // .entered(); // let state = unsafe { state.transmute_ref::() }; // let object = unsafe { object.transmute_owned::() }; // // Cast the callback ptr to ourselves // let callback: *const F = std::mem::transmute(callback.0); // let callback = callback.as_ref().unwrap(); // let state = state.clone(); // runtime_state.spawn_future(async move { // let result = callback(state, *object).await; // match result { // Ok(success) => unsafe { Ok(NewAnyValue::new(success)) }, // Err(err) => match err { // OperationError::Operation(_) => todo!(), // OperationError::Internal(_) => todo!(), // OperationError::Unhandled => todo!(), // }, // } // }) todo!() } fn callback_ptr(&self) -> CallbackPtr { todo!() // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } } } pub struct ValueChangeCallback(CallbackPtr); impl Callback for ValueChangeCallback { type CallbackFunc = unsafe extern "C" fn( &PluginState, object: FfiValueRef, value_name: FfiSliceRef, new_value: Value, ) -> RuntimeFuture<()>; } pub trait IntoValueChangeCallback { unsafe extern "C" fn value_changed( callback: CallbackPtr, state: FfiValueMut, object: FfiValueRef, value_name: FfiSliceRef, new_value: Value, ) -> RuntimeFuture<()>; } impl IntoValueChangeCallback for F { unsafe extern "C" fn value_changed( callback: CallbackPtr, state: FfiValueMut, _object: FfiValueRef, _value_name: FfiSliceRef, _new_value: Value, ) -> RuntimeFuture<()> { todo!() } } impl ValueChangeCallback { // pub fn new>() -> Self { // Self { // func: T::value_changed, // } // } }