Welcome to the trillium-macros crate!
This crate currently offers a derive macro for Handler that can be used to delegate Handler behavior to a contained Handler type. Currently it only works for structs, but will eventually support enums as well. Note that it will only delegate to a single inner Handler type.
In the case of a newtype struct or named struct with only a single
field, #[derive(Handler)] is all that's required. If there is more
than one field in the struct, annotate exactly one of them with
#[handler].
As of v0.0.2, deriving Handler makes an effort at adding Handler
bounds to generics contained within the #[handler] type. It may be
overzealous in adding those bounds, in which case you'll need to
implement Handler yourself.
// for these examples, we are using a `&'static str` as the handler type.
use Handler;
#
;
assert_handler;
;
assert_handler;
assert_handler;
assert_handler;
;
assert_handler;
assert_handler;
assert_handler;
Overriding a single trait function
Annotate the handler with a
trillium::Handler
function name #[handler(overrride = FN_NAME)] where FN_NAME is one of
run, before_send, name, has_upgrade, or upgrade, and
implement the same signature on Self. The rest of the Handler
interface will be delegated to the inner Handler, but your custom
implementation for the specified function will be called instead.
use Handler;
#
let handler = CustomName ;
assert_eq!;
assert_handler;
Overriding several trait functions
Annotate the handler with any number of
trillium::Handler
function names #[handler(except = [run, before_send, name, has_upgrade, upgrade])] and implement the trillium Handler signature of that name
on Self.
use Handler;
use Handler;
#
let handler = CustomName ;
assert_handler;