mm1_proc_macros/lib.rs
1// #![warn(missing_docs)]
2#![warn(rust_2018_idioms)]
3#![warn(unreachable_pub)]
4
5use proc_macro::TokenStream;
6
7mod dispatch;
8mod message;
9
10#[proc_macro_attribute]
11pub fn message(attr: TokenStream, item: TokenStream) -> TokenStream {
12 message::message(attr, item)
13}
14
15/// Dispatch an `Envelope` over a `match`-like set of typed arms.
16///
17/// `dispatch!(match envelope { Foo(f) => .., Bar { .. } => .. })` tests the
18/// envelope's message against each arm's message type in order and runs the
19/// body of the first that matches.
20///
21/// # Supported patterns
22///
23/// Typed arms support unit-struct paths, tuple-struct patterns, struct
24/// patterns, and bindings such as `message @ Foo { .. }`. Guards are supported
25/// on typed arms, but they inspect the message before the envelope is consumed.
26/// Their bindings are therefore borrowed: for example, write
27/// `Ping { seq } if *seq > 10` when `seq` is a `u64`.
28///
29/// Use `_` to ignore every remaining message, or `message @ _` to bind the
30/// catch-all message. Catch-all arms cannot have guards; put the condition in
31/// the arm body instead. A bare lowercase identifier is rejected as ambiguous
32/// with the unit-struct pattern syntax, so binding catch-alls must use
33/// `message @ _`.
34///
35/// # Unmatched messages
36///
37/// If no arm matches and there is no catch-all arm (e.g. `_ => ..` or
38/// `other @ _ => ..`),
39/// the message is **logged at `WARN` and dropped** (via
40/// `Envelope::log_unhandled`); the actor is not crashed. This mirrors the
41/// fire-and-forget semantics elsewhere in the runtime — any peer that learns an
42/// address can send an unexpected message, and that must not be able to kill
43/// the actor.
44///
45/// Because the generated fallback evaluates to `()`, a `dispatch!` used in
46/// value position (its result assigned or returned) must supply its own
47/// catch-all arm to produce that value.
48#[proc_macro]
49pub fn dispatch(input: TokenStream) -> TokenStream {
50 dispatch::dispatch(input)
51}