Implement StreamFunction in Rust
🚧
This feature is currently in alpha and subject to change.
Install CLI
$ curl -fsSL "https://get.yomo.run" | sh
Write a StreamFunction in Rust
#[yomo::init]
fn init() -> anyhow::Result<()> {
println!("wasm rust sfn init");
Ok(())
}
#[yomo::observe_datatags]
fn observe_datatags() -> Vec<u32> {
vec![0x33]
}
#[yomo::handler]
fn handler(ctx: yomo::Context) -> anyhow::Result<()> {
// load input tag & data
let tag = ctx.get_tag();
let input = ctx.load_input();
println!(
"wasm rust sfn received {} bytes with tag[{:#x}]",
input.len(),
tag
);
// parse input from bytes
let input = String::from_utf8(input.to_vec())?;
// your app logic goes here
let output = input.to_uppercase();
// return the datatag and output bytes
ctx.dump_output(0x34, output.into_bytes());
Ok(())
}
Compile to WASI (opens in a new tab)
Cargo.toml
:
[package]
name = "sfn"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
anyhow = "1.0"
yomo = "0.3"
Compile to wasm:
$ rustup target add wasm32-wasi
$ cargo build --release --target wasm32-wasi
Run Streaming Serverless Function
yomo run /path/to/sfn.wasm