Repository navigation
2.3 BackendFactory and BackendType
Relevant source files
The following files were used as context for generating this wiki page:
This page describes the factory pattern used in corpus-ipc to abstract the instantiation of compute processing engines. It details the BackendType enumeration, the use of conditional compilation for the ZeroMQ transport, and how the BackendFactory enables runtime selection of backend implementations. This reflects the completed rename conventions per CHANGELOG guidelines src/trait_def.rs:10-12.
The corpus-ipc library uses a factory pattern to decouple application logic from specific backend implementations src/trait_def.rs:16-23. This allows the system to switch between a native Rust environment and an external process (via ZeroMQ) without altering the calling code's signature src/trait_def.rs:17-23.
The factory returns a Box<dyn IpcBackend>, ensuring that callers interact exclusively through the unified trait interface src/trait_def.rs:119.
The following diagram bridges the high-level concepts of backend selection to the specific code entities responsible for implementation.
Backend Selection Logic
graph TD
subgraph "Natural Language Space"
A["Selection Logic"]
B["Native Engine"]
C["External Engine"]
end
subgraph "Code Entity Space"
direction LR
A1["BackendType"]
B1["RustBackend"]
C1["ZmqIpcBackend"]
F1["BackendFactory::create"]
end
A --> A1
B --> B1
C --> C1
A1 --> F1
F1 -- "instantiates" --> B1
F1 -- "instantiates" --> C1
Sources: src/trait_def.rs:29-55, src/trait_def.rs:90-127
The BackendType enum serves as the discriminator for the factory src/trait_def.rs:90-106. It determines which concrete struct will be instantiated and boxed src/trait_def.rs:119-126.
| Variant | Description | Feature Requirement |
|---|---|---|
Rust |
Pure-Rust native backend (default) src/trait_def.rs:96-98. | Always available |
ZmqIpc |
IPC backend via ZMQ SUB socket src/trait_def.rs:99-101. | feature = "zmq" |
ZmqRuntime |
Deprecated compatibility selector for ZmqIpc src/trait_def.rs:102-105. |
feature = "zmq" |
Sources: src/trait_def.rs:90-106
The ZmqIpc variant and the ZmqRuntime alias are gated behind the zmq feature flag src/trait_def.rs:99-106. This minimizes the dependency tree for users who only require the native RustBackend.
-
In
BackendTypedefinition: Variants involving ZeroMQ are conditionally compiled using#[cfg(feature = "zmq")]src/trait_def.rs:99-106. -
In
BackendFactory::create: The match arms handling ZeroMQ backends are similarly guarded, preventing compilation errors when thezmqfeature is omitted src/trait_def.rs:122-125.
Sources: src/trait_def.rs:99-106, src/trait_def.rs:122-125
The BackendFactory is a unit struct providing a static create method src/trait_def.rs:108-119. It maps the BackendType enum to the construction of concrete types such as RustBackend and ZmqIpcBackend src/trait_def.rs:120-126.
The sequence diagram below illustrates how a BackendType is transformed into a trait object for downstream processing.
Backend Instantiation Flow
sequenceDiagram
participant App as "Caller"
participant BF as "BackendFactory"
participant RB as "RustBackend"
participant ZB as "ZmqIpcBackend"
App->>BF: "create(BackendType::Rust)"
BF->>RB: "new()"
RB-->>BF: "Instance"
BF-->>App: "Box<dyn IpcBackend>"
Note over App, ZB: If feature "zmq" is enabled
App->>BF: "create(BackendType::ZmqIpc)"
BF->>ZB: "new()"
ZB-->>BF: "Instance"
BF-->>App: "Box<dyn IpcBackend>"
-
BackendFactory::create(backend_type: BackendType) -> Box<dyn IpcBackend>:- Accepts a
BackendTypediscriminator src/trait_def.rs:119. - Matches on the variant to instantiate the underlying engine src/trait_def.rs:120-125.
- Returns a heap-allocated trait object wrapped in a
Boxsrc/trait_def.rs:119.
- Accepts a
Sources: src/trait_def.rs:108-127
Backends produced by BackendFactory must implement IpcBackend, which enforces thread safety via Send + Sync bounds src/trait_def.rs:29. This is critical for integration into asynchronous runtimes like tokio or shared concurrent state contexts.
Unit tests within src/trait_def.rs validate factory correctness and trait conformance src/trait_def.rs:129-164:
-
rust_backend_implements_ipc_backendchecks thatRustBackendsatisfiesIpcBackendsrc/trait_def.rs:136-142. -
zmq_backend_implements_ipc_backend(whenzmqfeature is active) verifiesZmqIpcBackendconformance src/trait_def.rs:151-156. - Deprecation aliases (
ZmqRuntime,RuntimeBackend) are explicitly tested for backward compatibility src/trait_def.rs:144-149, src/trait_def.rs:158-163.
- 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