use std::any::Any;
use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
use bytes::Bytes;
#[async_trait]
pub trait Executor: Send + Sync + 'static {
async fn execute(&self, payload: Bytes) -> Result<Bytes> {
self.execute_method("dispatch", payload).await
}
async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes>;
async fn execute_direct(
&self,
_method: &str,
_request: &(dyn Any + Send + Sync),
) -> Result<Box<dyn Any + Send>> {
Err(anyhow::anyhow!(
"execute_direct not supported by this runtime"
))
}
}
#[async_trait]
impl<T: Executor + ?Sized> Executor for Arc<T> {
async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes> {
(**self).execute_method(method, payload).await
}
async fn execute_direct(
&self,
method: &str,
request: &(dyn Any + Send + Sync),
) -> Result<Box<dyn Any + Send>> {
(**self).execute_direct(method, request).await
}
}