Skip to main content

controller

Attribute Macro controller 

Source
#[controller]
Expand description

A controller: a type the container builds and its transports reach, through the handlers of its #[handlers] impl. Only another controller injects one; wire() refuses a read of a controller by any other binding.

It writes what #[injectable] writes, from a struct (#[controller(default)] marks a field set from Default) or from an impl block’s new or #[construct] fn, and takes the same arguments: a scope and timeout = <Duration expr>. With no scope it is Auto: a singleton if nothing below it needs an execution, and built per call otherwise. It takes no route prefix; the module mounts a controller under one with m.controller::<T>().at(prefix).

It also writes impl ulo::Controller, the mark that makes a type a controller, holding where the type implements ulo::Handlers, the impl #[handlers] writes, so a #[controller] type with no handler impl fails to compile here. #[module] accepts under controllers only a type implementing Handlers and refuses a Controller under providers, and wire() refuses a controller registered as a provider. A WebSocket gateway is a controller and takes #[controller] too. A #[controller] type whose handlers are mounted by hand, a gateway implementing Gateway among them, implements Handlers itself.

Rust resolves the attribute’s path, and any path that reaches it is accepted, a re-export or a renamed dependency’s name among them. The code it writes names ::ulo: a crate depending on ulo under another name aliases it at its root, extern crate framework as ulo;. The same holds for #[injectable] and #[handlers]; the markers they read are written bare or under ulo.

On an impl block it stacks with #[handlers] in either order, so the constructor and the handlers can share one impl, #[controller] #[handlers] impl Greeter { fn new(..) .. }.