Skip to main content

Rust API

The crate is published as raio-rs and imported as raio. Its complete API is documented on docs.rs.

cargo add raio-rs
core/examples/hello.rs
use raio::app::{App, Builder, InputPort, Node, OutputPort, Ports, node};
use raio::time::MS;

#[derive(Ports)]
struct Producer {
node: Node,
out: OutputPort<String>,
}

#[node]
impl Producer {
fn new(builder: &mut Builder) -> Self {
Self {
node: builder.node(),
out: builder.output(),
}
}

#[task]
async fn start(&self) {
self.node.wait(1 * MS).await;
self.out.set("hello world".to_string());
}
}

#[derive(Ports)]
struct Sink {
input: InputPort<String>,
}

#[node]
impl Sink {
fn new(builder: &mut Builder) -> Self {
Self {
input: builder.input(),
}
}

#[task]
async fn start(&self) {
while let Some((value, _)) = self.input.wait().await {
println!("sink received {:?}", *value);
}
}
}

fn main() {
let mut app = App::new();
let producer = app.create_node(Producer::new);
let sink = app.create_node(Sink::new);
app.connect(&producer.out, &sink.input);
app.run();
}

A node is a struct that derives Ports, and a function that takes a Builder builds it. Its impl block is marked #[node], and the methods in it that are marked #[task], #[reaction(...)] or #[shutdown] are what the node runs. App::create_node takes the function that builds the node, App::connect joins two ports, and App::run starts one process per node.

$ cargo run --example hello
sink received "hello world"

Rust and Python Side by Side​

The two APIs have the same concepts under slightly different names:

RustPython
Wait in logical timenode.wait(10 * MS).awaitawait raio.wait(10 * MS)
Next value of an inputinput.wait().awaitawait port, async for value in port
Value at the current timestampinput.get()port.value when port.present
Set an outputout.set(value)out.set(value)
Declare a task#[task] on an async method@task on an async method
React to inputs#[reaction(a, b)] on a method@reaction(a, b) on a method
Wait for several thingsselect and join! of the futures crateraio.select, raio.gather, raio.timeout
Start another tasknode.spawn(future)raio.spawn(coroutine)
Current timestampnode.now()raio.now()
Events from threadsAction<T> and its handle()Action[T] and its send
Connectapp.connect(&a.out, &b.input).delay(d)raio.connect(a.out, b.inp, delay=d)
Connect physicallyapp.connect(&a.out, &b.input).physical()raio.connect(a.out, b.inp, physical=True)
Shut the app downnode.request_shutdown()raio.request_shutdown()
Run at shutdown#[shutdown] on a methoddef shutdown(self)
Draw the appapp.diagram("app.svg")raio.diagram("app.svg", a, b)

Differences from Python​

Port values are serialized with bincode by default, where Python uses pickle. A port that reaches a Python node takes MessagePackSerializer; see Rust and Python in One App.

A Rust task may await a future that Raio does not provide, such as one of another runtime. Its result arrives as a physical event, like a value sent to an action.

The Rust App has no fast-forward mode.

The version of the crate is raio::VERSION.