bb_runtime/roles/codec.rs
1//! `CodecRuntime` - role trait for codec implementations.
2//!
3//! Per `docs/ROLES.md` §8. The trait carries the universal pair
4//! (`atomic_opset` + `dispatch_atomic`); the engine routes through
5//! `dispatch_atomic`. Author Contract impls
6//! (`crate::contracts::Codec`) define the user-facing surface;
7//! `#[derive(bb::Codec)]` emits the bridge into
8//! `CodecRuntime::dispatch_atomic`.
9//!
10//! The opset declares three ops: `Train`, `Encode`, `Decode`.
11//! `Train` carries the optional calibration pass; impls that skip
12//! training keep the default `Contract::train` no-op and the derive
13//! routes the op through `dispatch_atomic` like any other arm.
14
15use crate::atomic::{AtomicOpsetDecl, DispatchResult};
16use crate::runtime::RuntimeResourceRef;
17use crate::slot_value::SlotValue;
18
19/// Role trait for codec implementations.
20pub trait CodecRuntime: Send + Sync {
21 /// Codec-impl-specific error type.
22 type Error: std::error::Error + Send + Sync + 'static;
23
24 /// Atomic-op opset this impl owns.
25 fn atomic_opset(&self) -> AtomicOpsetDecl;
26
27 /// Rust-dispatch entry point for atomic ops.
28 fn dispatch_atomic(
29 &mut self,
30 op_type: &str,
31 inputs: &[(&str, &dyn SlotValue)],
32 ctx: &mut RuntimeResourceRef<'_>,
33 ) -> Result<DispatchResult, Self::Error>;
34}