Crate essrpc

source ·
Expand description

Electron’s Super Simple RPC (ESSRPC) is a lightweight RPC library which aims to enable RPC calls as transparently as possible through calls to ordinary trait methods.

The magic is performed by the essrpc attribute macro which may be applied to any trait whose functions each meet the following conditions:

  • Returns a Result whose error type implements From<RPCError>.
  • Uses only parameter and returns types which implement Serialize
  • Is not unsafe

The essrpc macro generates for a trait an RPC client and a server. For a trait named Foo, the macro will generate FooRPCClient which implements both RPCClient and Foo as well as FooRPCServer which implements RPCServer

Examples

A trait can apply the essrpc attribute like this.

#[essrpc]
pub trait Foo {
   fn bar(&self, a: String, b: i32) -> Result<String, SomeError>;
}

For example purposes, assume we’re using a unix socket to communicate between a parent and child process. Anything else implementing Read+Write would work just as well.

let (s1, s2) = UnixStream::pair().unwrap();

We can spin up a server like this

let mut s = FooRPCServer::new(FooImpl::new(), BincodeTransport::new(s2));
s.serve()

Then, if we have some type FooImpl which implements Foo, we can do RPC like this.

let client = FooRPCClient::new(BincodeTransport::new(s1));
match client.bar("the answer".to_string(), 42) {
    Ok(result) => assert_eq!("the answer is 42", result),
    Err(e) => panic!("error: {:?}", e)
}

Modules

Transport implementations and helpers.

Structs

Generic serializable error with a description and optional cause. Used in conjunction with RPCError.
Identifies a method by both a name and an index. The Indices are automatically generated in the order methods are listed on the trait. Used when implementing Transport.
RPC error. All functions in RPC traits must return an error type which implements From<RPCError>.

Enums

Identifies a method by either a name or an index. Used when implementing Transport.

Traits

Trait implemented by all RPC clients generated by the essrpc macro. For a trait named Foo, the macro will generate FooRPCClient which implements both RPCClient and Foo.
Trait implemented by all RPC servers generated by the essrpc macro. For a trait named Foo, the macro will generate FooRPCServer which implements RPCServer. An RPCServer generated for a trait ‘Foo’ will have a new method
Trait for RPC transport. ESSRPC attempts to make as few assumptions about the transport as possible. A transport may work across a network, via any IPC mechanism, or purely in memory within a single process.

Attribute Macros