service

Attribute Macro service 

Source
#[service]
Expand description

Proc macro attribute to generate service implementation from function declaration. Will generate a struct which implements micro_tower::Service.

§Usage

A service can be generated by putting #[micro_tower::codegen::service] in front of async functions.

#[micro_tower::codegen::service]
async fn service_name(request: ()) -> &'static str {
    "Hello, World!"
}

Errors can be handled using Result.

#[micro_tower::codegen::service]
async fn service_name(request: ()) -> Result<&'static str, Infallible> {
    Ok("Hello, World!")
}

It is possible to use other services (inner service) in a service by putting an argument with the type of the service.

#[micro_tower::codegen::service]
async fn service_other(request: (), inner: service_name) -> Result<&'static str, BoxError> {
    Ok(inner.call(request).await?)
}

A service generated with this macro can be build using the builder pattern.

let service = service_name::builder().build();

or in case an inner service is used

let inner = service_name::builder().build();
let service = service_other::builder().inner(inner).build();

the setter for inner services will always be named the same as the service argument.

§Attributes

  • crate = "<path>": Use to specify a crate path different from ::micro_tower.
  • name = "<name>": Change log name of service to <name>.
  • extend: Specifies that the service already exists and only tower::Service should be implemented.

§Caveats / Notes

  • non-async functions are not supported
  • for error handling std::result::Result must be used (and no specialized like std::io::Result)
  • the generated service is not clonable