Attribute Macro tarpc::server

source · []
#[server]
Expand description

A utility macro that can be used for RPC server implementations.

Syntactic sugar to make using async functions in the server implementation easier. It does this by rewriting code like this, which would normally not compile because async functions are disallowed in trait implementations:

#[tarpc::service]
trait World {
    async fn hello(name: String) -> String;
}

#[derive(Clone)]
struct HelloServer(SocketAddr);

#[tarpc::server]
impl World for HelloServer {
    async fn hello(self, _: context::Context, name: String) -> String {
        format!("Hello, {name}! You are connected from {:?}.", self.0)
    }
}

Into code like this, which matches the service trait definition:

#[derive(Clone)]
struct HelloServer(SocketAddr);

#[tarpc::service]
trait World {
    async fn hello(name: String) -> String;
}

impl World for HelloServer {
    type HelloFut = Pin<Box<dyn Future<Output = String> + Send>>;

    fn hello(self, _: context::Context, name: String) -> Pin<Box<dyn Future<Output = String>
    + Send>> {
        Box::pin(async move {
            format!("Hello, {name}! You are connected from {:?}.", self.0)
        })
    }
}

Note that this won’t touch functions unless they have been annotated with async, meaning that this should not break existing code.