Skip to main content

folk_api/
executor.rs

1//! The `Executor` trait — how plugins send work to PHP workers.
2//!
3//! `folk-core` provides the concrete implementation, backed by the worker pool.
4
5use std::sync::Arc;
6
7use anyhow::Result;
8use async_trait::async_trait;
9use bytes::Bytes;
10
11/// Sends a serialized payload to a PHP worker and returns the response.
12///
13/// Plugins call this; they never see the worker pool directly.
14#[async_trait]
15pub trait Executor: Send + Sync + 'static {
16    /// Send `payload` to a worker and return the response bytes.
17    /// Uses the RPC method name "dispatch" by default.
18    async fn execute(&self, payload: Bytes) -> Result<Bytes> {
19        self.execute_method("dispatch", payload).await
20    }
21
22    /// Send `payload` to a worker using a specific RPC method name.
23    async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes>;
24}
25
26/// Blanket impl: an `Arc<dyn Executor>` is also an `Executor`.
27#[async_trait]
28impl<T: Executor + ?Sized> Executor for Arc<T> {
29    async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes> {
30        (**self).execute_method(method, payload).await
31    }
32}