use std::future::Future; use giterated_models::{ error::OperationError, object::GiteratedObject, operation::GiteratedOperation, }; use crate::{ result::FfiResult, state::State, value_ex::FfiValueUntyped, vtable::{operation::Operation, Object}, FfiFuture, FfiValueMut, FfiValueRef, }; use super::{Callback, CallbackPtr}; use std::fmt::Debug; pub struct OperationHandlerCallback(FfiValueUntyped); impl Callback for OperationHandlerCallback { type CallbackFunc = unsafe extern "C" fn( CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> FfiFuture< FfiResult>, >; } pub trait IntoPluginOperationHandler, A> { unsafe extern "C" fn handle( callback_ptr: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> FfiFuture>>; fn callback_ptr(&self) -> CallbackPtr; } impl IntoPluginOperationHandler for F where Fut: Future>> + Send + Sync, F: Fn(O, D) -> Fut + Send + Sync + 'static, O: Debug + GiteratedObject + 'static, D: Debug + GiteratedOperation + 'static, { unsafe extern "C" fn handle( callback: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> FfiFuture>> { 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(O, D, A1) -> Fut, O: Debug + GiteratedObject, D: Debug + GiteratedOperation, { unsafe extern "C" fn handle( _callback_ptr: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> FfiFuture>> { todo!() } fn callback_ptr(&self) -> CallbackPtr { todo!() } } impl IntoPluginOperationHandler for F where Fut: Future>>, F: Fn(O, D, A1, A2) -> Fut, O: Debug + GiteratedObject, D: Debug + GiteratedOperation, { unsafe extern "C" fn handle( _callback_ptr: CallbackPtr, state: FfiValueMut, object: FfiValueRef, operation: FfiValueRef, ) -> FfiFuture>> { todo!() } fn callback_ptr(&self) -> CallbackPtr { todo!() } }