Choreographic Programming in Rust
Hibana: Runtime Enforcement for Choreographic Programming in Rust
Hibana is a specialized runtime enforcement kernel designed for Rust. It allows developers to execute complex, multiparty protocols derived from a single, global choreography.
By projecting a global choreography into streamlined programs for each participating role, Hibana ensures that every send, receive, or route operation is validated against the projected program before any progress is committed.
🛠 Technical Architecture
Hibana is engineered for maximum efficiency and portability:
- Environment: It is unconditionally
no_std. - Memory: It requires
no dynamic allocatorzero allocation, relying instead on storage provided by the caller. - State Management: Rather than creating a unique Rust continuation type for every possible state (which would lead to "type explosion"), protocol state is managed via compact descriptors.
Core Capabilities
| Feature | Description |
|---|---|
| Role Capacity | Supports up to roles using a one-byte domain. |
| Primitives | Asynchronous send, seq (sequential), par (parallel), binary route, and guarded roll. |
| Endpoint Logic | Affine endpoints: they can be dropped, but cannot be cloned or used to trigger progress twice. |
| Integration | Transport-neutral via a unified Transport trait. |
| Targeting | Identical public API for both embedded and hosted environments. |
System Flow
⚠️ Scope and Boundaries
It is important to understand what Hibana is and is not:
- Enforces the protocol at each attached endpoint.
- Defines the necessary conditions (carrier, codec, scheduling) to achieve distributed guarantees.
-
Implementsa network stack. -
Implementsdistributed algorithms.
Note on Sessions: If a protocol is split across multiple sessions, the guarantee boundary shifts. Hibana validates each session individually rather than treating the entire set as one global protocol.
📖 Documentation Roadmap
The project is organized by the following responsibilities:
- The Model: Understanding enforcement, external dependencies, and state compactness.
- Writing Choreographies: Using the Protocol Language for messages, routes, and parallel flows.
- Implementing Roles: Handling
send,recv,offer, and branching. - Protocol Integration: Managing the runtime boundary, storage, and resolvers.
- Carrier Implementation: Fulfilling the
Transporttrait's five-operation contract. - Deployment Auditing: Verifying resource usage and local/distributed guarantees.
🚀 Quick Start
Installation
Add the dependency to your project:
cargo add hibana
Requirements: Rust 2024 edition, stable Rust 1.95+.
Example: Ping-Pong
To run the built-in example:
cargo run --example ping_pong ping=7, pong=8
The Protocol Logic
The following snippet demonstrates how a simple choreography is defined and projected:
use hibana::{
g::{self, Msg},
runtime::program::{RoleProgram, project},
};
fn ping_pong() -> (RoleProgram<0>, RoleProgram<1>) {
// Define a sequential flow: 0 sends to 1, then 1 sends to 0
let choreography = g::seq(
g::send::<(0, 1), Msg::Msg1, u32>(),
g::send::<(1, 0), Msg::Msg2, u32>(),
);
// Project the global choreography into role-specific programs
(project(choreography), project(choreography))
}
In a real execution, the developer provides the runtime storage and a Transport implementation (e.g., in-memory for tests), attaches roles to a SessionId, and drives the logic via send() and recv().
🧠 The Conceptual Model
The Lifecycle
Key Definitions
- Multiparty: The identity of the peer, the direction of flow, and the sequence of events are all derived from the single projected choreography.
- Asynchronous: A
sendoperation is considered successful once the frame is handed to the carrier; it does not block for the remote party'sreceive. - Affine: An endpoint owner can advance a projected step exactly once. While the endpoint can be discarded (dropped), it cannot be duplicated.
Public API Surfaces
| Surface | Responsibility | Primary Components |
|---|---|---|
| Audience Surface | Role implementation and driving the protocol. | hibana::g, Endpoint, RouteBranch, EndpointError |
| Integration Surface | Projecting roles, providing storage/transport. | hibana::runtime |
Application logic typically interacts only with the Audience Surface, while the Integration Surface handles the underlying machinery without exposing carrier state to the role code.
Efficiency Note
The choreography structure exists as a temporary Rust value. Once project(choreography) is called, it is converted into a compact RoleProgram. Consequently, Endpoint futures do not need to store the entire choreography tree or every possible payload type, ensuring that reusing a projected artifact across many sessions does not lead to a proliferation of endpoint types.