use async_trait::async_trait;
use torvyn_types::{BackpressureSignal, ComponentId, ProcessError};
use crate::error::EngineError;
use crate::types::{
CompiledComponent, ComponentInstance, ImportBindings, OutputElement, ProcessResult,
StreamElement,
};
#[async_trait]
pub trait WasmEngine: Send + Sync + 'static {
fn compile_component(&self, bytes: &[u8]) -> Result<CompiledComponent, EngineError>;
fn serialize_component(&self, compiled: &CompiledComponent) -> Result<Vec<u8>, EngineError>;
unsafe fn deserialize_component(
&self,
bytes: &[u8],
) -> Result<Option<CompiledComponent>, EngineError>;
async fn instantiate(
&self,
compiled: &CompiledComponent,
imports: ImportBindings,
component_id: ComponentId,
) -> Result<ComponentInstance, EngineError>;
fn set_fuel(&self, instance: &mut ComponentInstance, fuel: u64) -> Result<(), EngineError>;
fn fuel_remaining(&self, instance: &ComponentInstance) -> Option<u64>;
fn memory_usage(&self, instance: &ComponentInstance) -> usize;
}
#[async_trait]
pub trait ComponentInvoker: Send + Sync + 'static {
async fn invoke_pull(
&self,
instance: &mut ComponentInstance,
component_id: ComponentId,
) -> Result<Option<OutputElement>, ProcessError>;
async fn invoke_process(
&self,
instance: &mut ComponentInstance,
component_id: ComponentId,
element: StreamElement,
) -> Result<ProcessResult, ProcessError>;
async fn invoke_push(
&self,
instance: &mut ComponentInstance,
component_id: ComponentId,
element: StreamElement,
) -> Result<BackpressureSignal, ProcessError>;
async fn invoke_init(
&self,
instance: &mut ComponentInstance,
component_id: ComponentId,
config: &str,
) -> Result<(), ProcessError>;
async fn invoke_teardown(&self, instance: &mut ComponentInstance, component_id: ComponentId);
}
#[cfg(test)]
mod tests {
#[test]
fn test_trait_bounds_compile() {
fn _assert_send_sync<T: Send + Sync + 'static>() {}
}
}