Service

Derive Macro Service 

Source
#[derive(Service)]
Expand description

Derive macro for Service structs - generates ServiceFactory implementation

Automatically recognizes archy types (Res, Client, Emit, Sub, Shutdown) and injects them via app.extract(). All other fields use Default::default().

For custom initialization of state fields, use #[startup] in your #[service] impl.

#[derive(Service)]
struct JobService {
    config: Res<Config>,              // → app.extract()
    metrics: Client<MetricsService>,  // → app.extract()
    cache: RwLock<HashMap<K, V>>,     // → Default::default()
    counter: AtomicU64,               // → Default::default()
}

#[service]
impl JobService {
    #[startup]
    async fn init(&self) {
        // Custom initialization if Default isn't enough
        self.counter.store(1, Ordering::SeqCst);
    }
}

Generates:

impl ::archy::ServiceFactory for JobService {
    fn create(app: &::archy::App) -> Self {
        JobService {
            config: app.extract(),
            metrics: app.extract(),
            cache: Default::default(),
            counter: Default::default(),
        }
    }
}