Expand description
§rsrpc - Ergonomic Rust-to-Rust RPC
A function-forward RPC library where the trait IS the API.
§Overview
rsrpc generates RPC client and server code from a trait definition. The client
implements the same trait as the server, so client.method(args) just works.
No separate client types, no message enums, no schema files.
§Quick Start
ⓘ
use anyhow::Result;
use rsrpc::{async_trait, Client};
#[rsrpc::service]
pub trait Calculator: Send + Sync + 'static {
async fn add(&self, a: i32, b: i32) -> Result<i32>;
}
// Server implementation
struct MyCalculator;
#[async_trait]
impl Calculator for MyCalculator {
async fn add(&self, a: i32, b: i32) -> Result<i32> {
Ok(a + b)
}
}
#[tokio::main]
async fn main() -> Result<()> {
// Server
let server = <dyn Calculator>::serve(MyCalculator);
tokio::spawn(server.listen("0.0.0.0:9000"));
// Client
let client: Client<dyn Calculator> = Client::connect("127.0.0.1:9000").await?;
let result = client.add(2, 3).await?;
assert_eq!(result, 5);
Ok(())
}§Streaming
Methods returning Result<RpcStream<T>> automatically stream data:
ⓘ
#[rsrpc::service]
pub trait LogService: Send + Sync + 'static {
async fn stream_logs(&self, filter: Filter) -> Result<RpcStream<LogEntry>>;
}
// Client usage
let mut stream = client.stream_logs(filter).await?;
while let Some(entry) = stream.next().await {
println!("{:?}", entry?);
}§HTTP/REST Support
Enable the http feature for REST endpoint support:
ⓘ
#[rsrpc::service]
pub trait UserService: Send + Sync + 'static {
#[get("/users/{id}")]
async fn get_user(&self, id: String) -> Result<User>;
#[post("/users")]
async fn create_user(&self, user: CreateUserRequest) -> Result<User>;
}
// Serve via HTTP
let router = <dyn UserService>::http_routes(service);
axum::serve(listener, router).await?;
// Or use HTTP client
let client: HttpClient<dyn UserService> = HttpClient::new("http://localhost:8080");
client.get_user("123".into()).await?;§Features
http- Enable HTTP/REST support with axum and reqwest
Re-exports§
Structs§
- Client
- A client connection to a remote RPC server.
- RpcStream
- A bidirectional RPC stream.
- Server
- A server that hosts an RPC service implementation.
- Stream
Builder - Builder for creating streams on the server side.
- Stream
Frame - A frame received for a streaming response
- Stream
Sender - Handle for sending items into a stream.
Enums§
- Dispatch
Result - Result from dispatching a method call. Can be either a unary response or a stream of items.
- Frame
Type - Frame types for the wire protocol.
Constants§
- STREAM_
HEADER_ SIZE - Wire format for streaming frames. Header: [frame_type: u8][method_id: u16][request_id: u64][payload_len: u32] = 15 bytes
Traits§
- Client
Encoding - Trait for making client calls with automatic encoding/decoding.
- Erased
Stream - Type-erased stream trait for dispatch results.
- Server
Encoding - Trait for encoding server responses into dispatch results.
Functions§
- decode_
stream_ header - Decode a streaming frame header.
- encode_
stream_ header - Encode a streaming frame header.
Type Aliases§
- Dispatch
Fn - Type alias for dispatch handler functions. Takes (service, method_id, payload) and returns either unary or streaming result.
Attribute Macros§
- async_
trait - Re-exports for generated code
- service
- Re-export the service macro Marks a trait as an RPC service.