Skip to main content

optional_service

Attribute Macro optional_service 

Source
#[optional_service]
Expand description

The Java @OptionalService("condition") analog — a first-class attribute that makes a composable function (#[preload]), a websocket server function (#[websocket_service]), a #[before_application] or a #[main_application] conditional on application configuration: the item registers only when the condition holds at startup (Java Feature.isRequired semantics — comma-separated OR, !key negation, key=value / key / key= forms, case-insensitive).

Works in either stacking order:

#[optional_service("app.env=dev")]      // Java order — condition on top
#[preload(route = "dev.only.service")]
struct DevOnly;

#[preload(route = "also.dev.only")]     // marker order — condition below
#[optional_service("app.env=dev")]
struct AlsoDevOnly;

When written above the primary attribute, this macro expands first and re-attaches the condition below it, where the primary attribute consumes it; when written below, the primary attribute consumes it directly. Using it without one of the four primary attributes is a compile error.