repe 13.0.0

Rust implementation of the REPE RPC protocol (JSON-focused)
Documentation

repe-rs

Rust implementation of the REPE RPC protocol with JSON and BEVE body support.

crates.io docs

Features

  • REPE header and message types with correct little-endian wire encoding.
  • Streaming and zero-copy I/O (MessageView, write_message_streaming) for large bodies.
  • JSON and BEVE bodies via structio: one declaration serves both formats, and there is no document model, no derive, and no proc macro in the dependency graph.
  • Sync and async (tokio) clients and servers, with multiplexed in-flight requests, per-call timeouts, batching, and notify support.
  • Dynamic Registry routing: a flat table of functions resolved by JSON Pointer.
  • Fleet APIs for multi-node TCP and UDP fanout.
  • Optional REST gateway (RestGateway) fronting a Registry over HTTP/1.1 and HTTP/2, with ETag revalidation and JSON/BEVE negotiation.
  • Optional WebSocket transport, including a wasm browser client and server-pushed notify subscriptions.
  • Optional stream module for backpressure-controlled bulk transfers with reconnect-resume.
  • Optional repe CLI for talking to any REPE server over TCP or WebSocket.

Installation

Add to your Cargo.toml:

[dependencies]
repe = "13"

Or run cargo add repe.

Quick Start

A body is a type you declare. One structio::object! gives it both formats:

use repe::{BodyFormat, Message, QueryFormat};

#[derive(Default, Debug)]
struct Ping { ping: bool }
structio::object!(Ping { ping });

let msg = Message::builder()
    .id(42)
    .query_str("/status")
    .query_format(QueryFormat::JsonPointer)
    .body_json(&Ping { ping: true })
    .build();

let bytes = msg.to_vec();
let parsed = repe::Message::from_slice(&bytes)?;

assert_eq!(parsed.header.id, 42);
assert_eq!(parsed.header.body_format, BodyFormat::Json as u16);
assert!(parsed.json_body::<Ping>()?.ping);
# Ok::<(), Box<dyn std::error::Error>>(())

Replace body_json with body_beve to encode the same value as BEVE; the declaration is unchanged and everything else is identical.

object! is a macro_rules! macro, so declaring a type costs no proc macro and no build-time dependency. It also works on a type you do not own.

Server and Client

A minimal TCP server with one route, plus a client call:

use repe::{Client, Router, Server};

#[derive(Default, Debug)]
struct Empty;
structio::object!(Empty {});

#[derive(Default, Debug)]
struct Pong { pong: bool }
structio::object!(Pong { pong });

let router = Router::new().with_typed("/ping", |_: Empty| Ok(Pong { pong: true }));
let server = Server::new(router);
let listener = server.listen("127.0.0.1:0")?;
let addr = listener.local_addr()?;
std::thread::spawn(move || { let _ = server.serve(listener); });

let client = Client::connect(addr)?;
let pong: Pong = client.call_typed_json("/ping", &Empty)?;
assert!(pong.pong);
# Ok::<(), Box<dyn std::error::Error>>(())

Every route names the type it takes and the type it returns. A request body is read straight into the handler's parameter and the return value is written straight into the response frame, with no intermediate document either way.

The async (tokio) variant uses AsyncServer and AsyncClient and exposes the same shape. See the Server guide for routers, typed handlers, middleware, struct registration, and peer-aware handlers, and the Client guide for the full client surface (typed and BEVE helpers, multiplexing, timeouts, batches, notifies, error handling).

Bulk numeric arrays

When a request and response are whole contiguous numeric arrays (f32, f64, integer widths), Router::with_typed_slice and Client::call_typed_slice (also AsyncClient::call_typed_slice) carry them on BEVE's typed-slice fast path: one bulk copy each way rather than a per-element walk. The wire bytes are identical to an ordinary Vec<T> body, so a with_typed_slice route and a with_typed / call_typed_beve peer interoperate freely.

use repe::{Client, Router, Server};

let router = Router::new()
    .with_typed_slice::<f64, f64, _>("/scale", |xs| Ok(xs.iter().map(|x| x * 2.0).collect()));
let server = Server::new(router);
let listener = server.listen("127.0.0.1:0")?;
let addr = listener.local_addr()?;
std::thread::spawn(move || { let _ = server.serve(listener); });

let client = Client::connect(addr)?;
let out: Vec<f64> = client.call_typed_slice("/scale", &[1.0, 2.0, 3.0])?;
assert_eq!(out, vec![2.0, 4.0, 6.0]);
# Ok::<(), Box<dyn std::error::Error>>(())

For the request decode, Router::with_typed_slice_ref goes one step further and hands the handler a &[T] borrowed straight out of the receive buffer, with no allocation or element copy, when the client sends the aligned wire form via call_typed_slice_aligned:

use repe::{Client, Router, Server};

let router = Router::new()
    .with_typed_slice_ref::<f64, f64, _>("/sum", |xs| Ok(vec![xs.iter().sum()]));
let server = Server::new(router);
let listener = server.listen("127.0.0.1:0")?;
let addr = listener.local_addr()?;
std::thread::spawn(move || { let _ = server.serve(listener); });

let client = Client::connect(addr)?;
let out: Vec<f64> = client.call_typed_slice_aligned("/sum", &[1.0, 2.0, 3.0])?;
assert_eq!(out, vec![6.0]);
# Ok::<(), Box<dyn std::error::Error>>(())

A with_typed_slice_ref route is a drop-in superset of with_typed_slice: it still accepts the regular call_typed_slice clients (bulk-copying those), and falls back to a bulk copy whenever a borrow isn't possible, so correctness never depends on the alignment landing.

Feature Flags

Flag Effect
rest RestGateway: a REST facade over a Registry, served on HTTP/1.1 and HTTP/2.
plugin #[repe::plugin]: export a Router as a C-ABI plugin a REPE host loads.
plugin-host plugin::host::Plugin: load and drive a plugin, including a C++ one.
websocket Native WebSocketClient, WebSocketServer, and proxy_connection.
websocket-wasm Browser WasmClient on wasm32-unknown-unknown.
fleet-udp UDP fanout via UniUdpFleet.
value-stream Streaming value transfer (SVS), with an optional Zstandard codec (pure Rust, no C toolchain).
parking-lot Lockable impls for parking_lot::Mutex / RwLock.
cli Builds the repe command-line client (pulls in clap and websocket).

fleet-udp, value-stream, rest, plugin, and plugin-host are native-only: on wasm32-unknown-unknown their modules and their non-portable dependencies both compile away, so enabling any of them there is inert rather than a build failure. That matters for a workspace hoisting one shared feature set across a native server and a browser client, since Cargo features are additive and the wasm member cannot subtract one.

Documentation

Full documentation is hosted at repe-org.github.io/repe-rs.

Examples

cargo run --example json_roundtrip
cargo run --example server
cargo run --example client
cargo run --example registry_server
cargo run --example registry_roundtrip
cargo run --example async_server
cargo run --example async_client
cargo run --example typed_numeric_body

Testing

Run cargo test to execute unit and integration tests. The crate denies warnings and includes async tests, so a recent tokio is required. Integration tests cover sync and async server/client calls, unknown routes, handler error mapping, ID mismatches, and timeouts.

License

MIT, see LICENSE.