Repository navigation
2.1 IpcBackend Trait
Relevant source files
The following files were used as context for generating this wiki page:
The IpcBackend trait defined in [src/trait_def.rs:29-55]() serves as the primary abstraction layer in corpus-ipc. It decouples application execution logic from concrete compute engines, enabling seamless interchangeability between native Rust implementations and external IPC-driven backends.
Every backend implementation must conform to the IpcBackend trait contract and satisfy Send + Sync requirements [[src/trait_def.rs:29]()]. This ensures that backend instances can be transferred across thread boundaries and accessed concurrently—typically encapsulated behind a mutex. The REST server uses AppState (Mutex<Box<dyn IpcBackend>> plus an AtomicBool initialized flag) in src/server.rs.
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>;
}Sources: [[src/trait_def.rs:29-55]()]
-
Signature:
fn initialize(&mut self, model_path: Option<&str>) -> Result<(), BackendError>[[src/trait_def.rs:39]()] -
Purpose: Prepares the backend for processing by loading model weights, initializing internal buffers, or establishing IPC transport socket connections [
[src/trait_def.rs:36-38]()]. -
Contract: Must be called before
process_batch. Successful executions are idempotent [[src/trait_def.rs:38()].
-
Signature:
fn process_batch(&mut self, inputs: &[f32]) -> Result<Vec<f32>, BackendError>[[src/trait_def.rs:34]()] -
Purpose: Executes a forward processing pass over a dynamic slice of
f32input signals [[src/trait_def.rs:31-34]()]. -
Output Contract: Returns a
Vec<f32>containing the processed outputs, where the dimensionality depends on the specific concrete backend implementation [[src/trait_def.rs:25-28]()].
-
Signature:
fn save_state(&self, model_path: &str) -> Result<(), BackendError>[[src/trait_def.rs:42]()] -
Purpose: Persists the current model state or weights to the designated file path [
[src/trait_def.rs:41]()].
-
Signature:
fn get_spike_states(&self) -> Vec<bool>[[src/trait_def.rs:45]()] -
Purpose: Retrieves a vector of boolean flags representing per-channel spike states (where
trueindicates a spike occurred on the most recent evaluation tick) [[src/trait_def.rs:44-45]()].
-
Signature:
fn reset(&mut self) -> Result<(), BackendError>[[src/trait_def.rs:54]()] -
Purpose: Resets internal volatile state, such as membrane potentials and temporal caches [
[src/trait_def.rs:47-48]()]. -
Transport Behavior: For connection-oriented backends like
ZmqIpcBackend,reset()may drop the underlying transport socket and clear initialization flags, requiring a subsequentinitialize()call before futureprocess_batch()calls [[src/trait_def.rs:49-53]()].
Sources: [[src/trait_def.rs:29-55]()]
The lifecycle of an IpcBackend transitions through uninitialized, initialized, processing, and reset states as depicted below.
IpcBackend Lifecycle State Machine
stateDiagram-v2
direction TB
Uninitialized["Uninitialized"]
Initialized["Initialized"]
Processing["Processing"]
Resetting["Resetting"]
Saving["Saving"]
ErrorState["Error"]
Uninitialized --> Initialized : "initialize(model_path)"
Initialized --> Processing : "process_batch(inputs)"
Processing --> Initialized : "Ok(Vec<f32>)"
Initialized --> Resetting : "reset()"
Resetting --> Initialized : "State Cleared"
Initialized --> Saving : "save_state(model_path)"
Saving --> Initialized : "IO Complete"
Uninitialized --> ErrorState : "process_batch without init"
ErrorState --> [*]
Sources: [[src/trait_def.rs:29-55]()]
To guarantee that all concrete backends adhere to the trait requirements, unit test suites employ compile-time type assertions using helper functions [[src/trait_def.rs:134-156]()]:
fn assert_ipc_backend<T: IpcBackend>() {}Furthermore, the codebase preserves the RuntimeBackend alias for backward compatibility following rename refactors. Marked as deprecated, it maps directly to IpcBackend [[src/trait_def.rs:57-63]()]:
#[deprecated(since = "0.1.0", note = "renamed to IpcBackend")]
pub use IpcBackend as RuntimeBackend;Sources: [[src/trait_def.rs:57-63](), [src/trait_def.rs:129-164]()]
This mapping bridges high-level architectural concepts to the concrete Rust code symbols defined in the codebase.
| Natural Language Concept | Code Symbol / Entity | File Reference |
|---|---|---|
| Compute Backend Trait | trait IpcBackend |
[[src/trait_def.rs:29]()] |
| Deprecated Alias | type RuntimeBackend |
[[src/trait_def.rs:63]()] |
| Native Implementation | struct RustBackend |
[[src/trait_def.rs:14]()] |
| ZMQ IPC Implementation | struct ZmqIpcBackend |
[[src/trait_def.rs:124]()] |
| Factory Selector | enum BackendType |
[[src/trait_def.rs:95]()] |
| Backend Instantiator | struct BackendFactory |
[[src/trait_def.rs:109]()] |
Trait-to-Implementation Architecture
classDiagram
class IpcBackend {
<<trait>>
+process_batch(inputs)
+initialize(model_path)
+save_state(model_path)
+get_spike_states()
+reset()
}
class RuntimeBackend {
<<deprecated alias>>
}
class RustBackend {
+new()
+process_batch(inputs)
}
class ZmqIpcBackend {
+new()
+process_batch(inputs)
}
class BackendFactory {
+create(backend_type) Box<dyn IpcBackend>
}
class BackendType {
<<enumeration>>
Rust
ZmqIpc
ZmqRuntime
}
IpcBackend <|.. RustBackend : "implements"
IpcBackend <|.. ZmqIpcBackend : "implements"
BackendFactory ..> IpcBackend : "returns trait object"
BackendFactory ..> BackendType : "accepts"
RuntimeBackend ..> IpcBackend : "alias"
Sources: [[src/trait_def.rs:29-127]()]
- Home
- 1. Overview
- 1.1. Getting Started
- 1.2. Architecture Overview
- 1.3. Crate Exports and Module Map
- 2. Core Abstractions
- 2.1. IpcBackend Trait
- 2.2. HybridFlowBackend Trait
- 2.3. BackendFactory and BackendType
- 2.4. Error Handling
- 3. Backend Implementations
- 3.1. RustBackend
- 3.2. ZmqIpcBackend
- 3.3. ZMQ Readout Parsing and Golden Frames
- 3.4. Structured ZeroMQ IpcMessage Transport
- 4. IPC Protocol and Data Models
- 4.1. IpcMessage Protocol
- 4.2. Batch Data Types
- 4.3. NeuromodulatorSnapshot and Neuromodulator Packet
- 4.4. StimulusBatch and Runtime Ingress
- 4.5. Deserialization Hooks and Model Compat Aliases
- 5. Validation and Wire Compatibility
- 5.1. Validation Framework and ProtocolLimits
- 5.2. ValidationError and ValidationKind
- 5.3. WireCompatibility and Envelope Decoding
- 5.4. Canonical Wire-v1 Encoding
- 6. corpus_ipc_server HTTP Server
- 6.1. REST API Endpoints
- 6.2. Server Configuration and Deployment
- 7. Build, Dependencies, and Quality Gates
- 7.1. Cargo Features and Dependency Isolation
- 7.2. Testing Strategy
- 7.2.1. Wire-v1 Golden Test Vectors
- 7.2.2. Property-Based Wire Tests
- 7.3. CI, Coverage, and Release Qualification
- 8. Project History and Licensing
- 8.1. Naming History and Completed Renames
- 8.2. Licensing
- 9. Glossary