derive
Source: compiler/src/builtin_docs/keywords.md Declares a derive macro: derive Name(TypeDecl t) -> Code { … }, used as #[derive(Name)].
It runs at compile time on the class or enum it is attached to, and its
output (usually an impl) is added after the type. attrs(h, …) lists the
field and variant attributes it owns. The built-in derives (Show, Eq,
Ord, Hash, String, Default, Send, Sensitive) need no import.
Contextual: elsewhere derive is an ordinary name.
use macro::{TypeDecl, Code, lit};
/// `T::type_name()` returns the type's name.
derive TypeName(TypeDecl t) -> Code {
return quote items {
impl ${t.name} {
pub static fn type_name() -> string {
return ${lit(t.name.str())};
}
}
};
}
See also#
ShowEqOrdfn: Declares a function, a method, or (written in an expression) an anonymous function.let: Declares a mutable local variable.const: Declares a binding that cannot be reassigned: a local, a module constant, or a class field that is set only bynew.static: Declares a module-level or class-level slot shared by the whole program, or a method withoutself.readonly: A read-only view of a value: nothing reachable through it can be changed.