Skip to main content

bb_runtime/roles/
protocol.rs

1//! `ProtocolRuntime` — framework-internal role trait for protocol
2//! implementations.
3//!
4//! Protocols don't share a verb catalog — each one declares its
5//! own atomic opset (e.g. `bb-kademlia.Kademlia.atomic v1`) and
6//! handles every op type in its `dispatch_atomic`. There are NO
7//! fixed role methods.
8//!
9//! `register_protocol!{}` writes this impl for the user — library
10//! authors do not write `ProtocolRuntime` directly.
11
12use crate::atomic::{AtomicOpsetDecl, DispatchResult};
13use crate::runtime::RuntimeResourceRef;
14use crate::slot_value::SlotValue;
15
16/// Role trait for protocol implementations.
17pub trait ProtocolRuntime: Send + Sync {
18    /// Protocol-impl-specific error type.
19    type Error: std::error::Error + Send + Sync + 'static;
20
21    /// Atomic-op opset this protocol declares. Both inbound
22    /// envelope routing AND user-graph DSL ops register here.
23    fn atomic_opset(&self) -> AtomicOpsetDecl;
24
25    /// Single dispatch entry. For inbound envelopes the framework
26    /// synthesizes inputs from the wire envelope:
27    ///
28    /// - `peer_id`:     `Opaque<PeerId>`
29    /// - `payload`:     `Opaque<Bytes>` (raw envelope bytes)
30    /// - `correlation`: `Opaque<WireCorrelation>`
31    ///
32    /// For user-graph DSL ops the inputs come from upstream slot
33    /// values exactly like any role op.
34    fn dispatch_atomic(
35        &mut self,
36        op_type: &str,
37        inputs: &[(&str, &dyn SlotValue)],
38        ctx: &mut RuntimeResourceRef<'_>,
39    ) -> Result<DispatchResult, Self::Error>;
40}