#[service]
Expand description
This macro generates the machinery used by both the client and server.
Namely, it produces:
- a serve fn inside the trait
- client stub struct
- Request and Response enums
ยงExample
use tarpc::{client, transport, service, server::{self, Channel}, context::Context};
#[service]
pub trait Calculator {
async fn add(a: i32, b: i32) -> i32;
}
// The request type looks like the following.
// Note, you don't have to interact with this type directly outside
// of testing, it is used by the client and server implementation
let req = CalculatorRequest::Add {a: 5, b: 7};
// This would be the associated response, again you don't ofent use this,
// it is only shown for educational purposes.
let resp = CalculatorResponse::Add(12);
// This could be any transport.
let (client_side, server_side) = transport::channel::unbounded();
// A client can be made like so:
let client = CalculatorClient::new(client::Config::default(), client_side);
// And a server like so:
#[derive(Clone)]
struct CalculatorServer;
impl Calculator for CalculatorServer {
async fn add(self, context: Context, a: i32, b: i32) -> i32 {
a + b
}
}
// You would usually spawn on an async runtime.
let server = server::BaseChannel::with_defaults(server_side);
let _ = server.execute(CalculatorServer.serve());