use crate::{BackendError, EmbeddingBatch, GradientBatch, RustBackend, SpikeBatch, TraceBatch};
pub trait IpcBackend: Send + Sync {
fn process_batch(&mut self, inputs: &[f32]) -> Result<Vec<f32>, BackendError>;
fn initialize(&mut self, model_path: Option<&str>) -> Result<(), BackendError>;
fn save_state(&self, model_path: &str) -> Result<(), BackendError>;
fn get_spike_states(&self) -> Vec<bool>;
fn reset(&mut self) -> Result<(), BackendError>;
}
#[deprecated(since = "0.1.0", note = "renamed to IpcBackend")]
pub use IpcBackend as RuntimeBackend;
pub trait HybridFlowBackend: Send + Sync {
fn send_spikes(&mut self, spikes: SpikeBatch) -> Result<(), BackendError>;
fn send_embeddings(&mut self, embeddings: EmbeddingBatch) -> Result<(), BackendError>;
fn try_receive_gradients(&mut self) -> Result<Option<GradientBatch>, BackendError>;
fn try_receive_traces(&mut self) -> Result<Option<TraceBatch>, BackendError>;
}
#[derive(Debug, Clone, Copy, Default)]
pub enum BackendType {
#[default]
Rust,
#[cfg(feature = "zmq")]
ZmqIpc,
#[cfg(feature = "zmq")]
#[deprecated(since = "0.1.0", note = "renamed to BackendType::ZmqIpc")]
ZmqRuntime,
}
pub struct BackendFactory;
impl BackendFactory {
pub fn create(backend_type: BackendType) -> Box<dyn IpcBackend> {
match backend_type {
BackendType::Rust => Box::new(RustBackend::new()),
#[cfg(feature = "zmq")]
#[allow(deprecated)]
BackendType::ZmqIpc | BackendType::ZmqRuntime => Box::new(crate::ZmqIpcBackend::new()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::RustBackend;
fn assert_ipc_backend<T: IpcBackend>() {}
#[test]
fn rust_backend_implements_ipc_backend() {
assert_ipc_backend::<RustBackend>();
let backend = BackendFactory::create(BackendType::Rust);
let _: Box<dyn IpcBackend> = backend;
}
#[test]
#[allow(deprecated)]
fn runtime_backend_alias_is_ipc_backend() {
fn assert_runtime_backend<T: RuntimeBackend>() {}
assert_runtime_backend::<RustBackend>();
}
#[cfg(feature = "zmq")]
#[test]
fn zmq_backend_implements_ipc_backend() {
assert_ipc_backend::<crate::ZmqIpcBackend>();
let _: Box<dyn IpcBackend> = BackendFactory::create(BackendType::ZmqIpc);
}
#[cfg(feature = "zmq")]
#[test]
#[allow(deprecated)]
fn deprecated_zmq_runtime_selector_creates_zmq_backend() {
let _: Box<dyn IpcBackend> = BackendFactory::create(BackendType::ZmqRuntime);
}
}