pub trait ReceiverMacroExpander:
Fn(&MacroExprFactory<'_>, Expr, Vec<Expr>) -> Option<Expr>
+ Send
+ Sync { }Expand description
Trait for receiver macro expansion functions.
A receiver macro expander receives a target expression and a list of arguments, then produces a transformed expression. Receiver macros are triggered by method calls on a target object.
§Type Requirements
The expander function must be:
Fn(&mut MacroExprFactory, Expr, Vec<Expr>) -> Option<Expr>: The expansion signatureSend + Sync: Thread-safe for use in concurrent environments'static: No borrowed references in the closure
§Parameters
factory: Factory for creating new expression nodestarget: The receiver expression (the object before the dot)args: The argument list passed to the method
§Return Value
Some(expr): The macro expansion succeeded andexprreplaces the original callNone: The macro cannot be expanded (keeps the original expression)
§Examples
// Macro for optional chaining: target.get_or(default)
fn get_or_macro(
factory: &mut MacroExprFactory,
target: Expr,
mut args: Vec<Expr>
) -> Option<Expr> {
if args.len() != 1 {
return None;
}
let default_value = args.pop()?;
// Expand to: target != null ? target : default_value
Some(factory.new_call("_?_:_", &[
factory.new_call("_!=_", &[target.clone(), factory.new_const(())]),
target,
default_value,
]))
}