Skip to main content

define_service

Macro define_service 

define_service!() { /* proc-macro */ }
Available on crate features grpc and http and std only.
Expand description

Define a gRPC service inline, without a .proto file or a build script.

This generates the same client and server stubs as the .proto driven codegen does, but from a definition written directly in Rust, using the message types you already have, serialized by the codec of your choice.

A service Echo in package rama.examples.echo.v1 defines the routes /rama.examples.echo.v1.Echo/<Method>, and generates an echo_client module with an EchoClient, together with an echo_server module containing an Echo trait to implement and an EchoServer to serve it.

The grpc_json_echo example of the rama repository defines, serves and calls a service this way.

§Syntax

The macro takes any number of settings, followed by any number of services:

  • package = "<name>";: the gRPC package, used as the first part of every route (required)
  • codec = <path>;: the codec used by services which do not define one themselves
  • client = <bool>;: generate the client stubs, true by default
  • server = <bool>;: generate the server stubs, true by default

A service is defined as service <Name> { ... }, containing an optional codec = <path>; used by its methods, and a method per RPC, written as rpc <Name>(<request type>) -> <response type>;.

Prefix either type with stream to make that side of the RPC streaming. Doc comments on a service or method end up on the generated items, #[deprecated] on a method marks the generated methods as deprecated, and #[codec(<path>)] on a method overrules the codec of its service.

A codec path is used as <path>::default(), so a generic codec needs all of its type parameters: write _ for the ones to be inferred, as in SerdeCodec<JsonFormat, _, _>, or use a type alias which fixes them.

§Paths

The generated stubs live in modules of their own, so every path you write is resolved as if it were written in the module which invokes this macro: EchoRequest refers to the type next to the macro, or to whatever you imported under that name.

Paths which start with crate or :: are used as-is, which is how you refer to types that are not in scope, e.g. ::std::string::String.