use std::future::Future; use giterated_models::{ error::OperationError, object::GiteratedObject, settings::Setting, value::GiteratedObjectValue, }; use crate::{ new_stack::PluginState, vtable::{AnyObject, NewAnyValue}, }; use super::CallbackPtr; #[derive(Copy, Clone)] pub struct ValueGetterCallback { pub callback_ptr: CallbackPtr, pub func: unsafe extern "C" fn( CallbackPtr, &PluginState, object: AnyObject, ) -> Result, } 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: &PluginState, object: AnyObject, ) -> Result; fn callback_ptr(&self) -> CallbackPtr; } impl IntoPluginValueGetter for F where Fut: Future>>, S: Clone, O: GiteratedObject, V: GiteratedObjectValue, F: Fn(S, O) -> Fut, { unsafe extern "C" fn get_value( callback: CallbackPtr, state: &PluginState, mut object: AnyObject, ) -> Result { 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 result = callback(state.clone(), *object); // match result { // Ok(value) => Ok(NewAnyValue::new(value)), // Err(_) => todo!(), // } todo!() } fn callback_ptr(&self) -> CallbackPtr { unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } } } pub struct ValueChangeCallback { func: unsafe extern "C" fn( &PluginState, object: AnyObject, value_name: &str, new_value: NewAnyValue, ), } pub trait IntoValueChangeCallback { unsafe extern "C" fn value_changed( state: &PluginState, object: AnyObject, value_name: &str, new_value: NewAnyValue, ); } impl IntoValueChangeCallback for F { unsafe extern "C" fn value_changed( state: &PluginState, object: AnyObject, value_name: &str, new_value: NewAnyValue, ) { todo!() } } impl ValueChangeCallback { pub fn new>() -> Self { Self { func: T::value_changed, } } }