use giterated_abi::{ callback::{Callback, CallbackPtr}, result::FfiResult, value_ex::FfiValueUntyped, vtable::{operation::Operation, Object}, FfiValueMut, FfiValueRef, }; use giterated_models::{ error::OperationError, object::GiteratedObject, operation::{GiteratedOperation, OperationState}, }; use crate::{ future::{RuntimeFuture, RuntimeFuturesExt}, new_stack::{handle::RuntimeHandle, PluginState}, state::{FromState, State, StateUUID}, }; use std::{any::type_name, fmt::Debug, future::Future}; use super::RuntimeState; pub struct OperationHandlerCallback(FfiValueUntyped); impl Callback for OperationHandlerCallback { type CallbackFunc = unsafe extern "C" fn( CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> RuntimeFuture< FfiResult>, >; } impl OperationHandlerCallback { // pub fn new< // S, // O: GiteratedObject, // D: GiteratedOperation, // A, // T: IntoPluginOperationHandler, // >( // handler: T, // ) -> Self { // OperationHandlerCallback(unsafe { CallbackPtr::from_raw(&handler, T::handle as _) }) // } } pub trait IntoPluginOperationHandler, A> { unsafe extern "C" fn handle( callback_ptr: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> RuntimeFuture>>; fn callback_ptr(&self) -> CallbackPtr; } impl IntoPluginOperationHandler for F where Fut: Future>> + Send + Sync, F: Fn(S, O, D) -> Fut + Send + Sync + 'static, S: Clone + Debug + Send + Sync + 'static, O: Debug + GiteratedObject + 'static, D: Debug + GiteratedOperation + 'static, { unsafe extern "C" fn handle( callback: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> RuntimeFuture>> { todo!() // let _guard = trace_span!( // "operation handler", // object = type_name::(), // operation = type_name::() // ) // .entered(); // let state = unsafe { state.transmute_ref::() }; // // Since this is Rust code, we know that the AnyObject and AnyOperation are just boxes // let object = unsafe { object.transmute_owned::() }; // let operation = unsafe { operation.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, *operation).await; // match result { // Ok(success) => unsafe { // todo!() // // Ok(AnySuccess::from_raw( // // FFIBox::from_box(Box::new(success)).untyped(), // // OperationVTable::new::(), // // )) // }, // Err(err) => match err { // OperationError::Operation(_) => todo!(), // OperationError::Internal(_) => todo!(), // OperationError::Unhandled => todo!(), // }, // } // }) } fn callback_ptr(&self) -> CallbackPtr { // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } todo!() } } impl IntoPluginOperationHandler for F where Fut: Future>>, F: Fn(S, O, D, A1) -> Fut, S: Clone + Debug, O: Debug + GiteratedObject, D: Debug + GiteratedOperation, { unsafe extern "C" fn handle( _callback_ptr: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> RuntimeFuture>> { todo!() } fn callback_ptr(&self) -> CallbackPtr { todo!() } } impl IntoPluginOperationHandler for F where Fut: Future>>, F: Fn(S, O, D, A1, A2) -> Fut, S: Clone + Debug, O: Debug + GiteratedObject, D: Debug + GiteratedOperation, { unsafe extern "C" fn handle( _callback_ptr: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> RuntimeFuture>> { todo!() } fn callback_ptr(&self) -> CallbackPtr { todo!() } } pub trait FromOperationState: Sized { fn from_operation_state( state: &mut State, object: &O, operation: &D, ) -> Result>; } pub struct StateExtractor(T); impl FromState for StateExtractor { fn from_state(state: &mut State) -> Result { todo!() } } // pub trait FromOperationState: Sized { // fn from_operation_state( // operation_state: &OperationState, // state: &mut State, // object: &O, // operation: &D, // ) -> Result>; // } // impl FromOperationState for RuntimeHandle { // fn from_operation_state( // _operation_state: &OperationState, // state: &mut State, // _object: &O, // _operation: &D, // ) -> Result> { // Ok(unsafe { RuntimeHandle::from_vtable(runtime_state.vtable) }) // } // } // impl FromOperationState for Option // where // T: FromOperationState, // { // fn from_operation_state( // operation_state: &OperationState, // state: &mut State, // object: &O, // operation: &D, // ) -> Result> { // Ok(T::from_operation_state(operation_state, runtime_state, object, operation).ok()) // } // }