Crate rsrpc

Crate rsrpc 

Source
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§

pub use postcard;
pub use serde;

Structs§

Client
A client connection to a remote RPC server.
RpcStream
A bidirectional RPC stream.
Server
A server that hosts an RPC service implementation.
StreamBuilder
Builder for creating streams on the server side.
StreamFrame
A frame received for a streaming response
StreamSender
Handle for sending items into a stream.

Enums§

DispatchResult
Result from dispatching a method call. Can be either a unary response or a stream of items.
FrameType
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§

ClientEncoding
Trait for making client calls with automatic encoding/decoding.
ErasedStream
Type-erased stream trait for dispatch results.
ServerEncoding
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§

DispatchFn
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.