Skip to main content

ruda_model_macros/
lib.rs

1#![warn(missing_docs)]
2
3//! The derive crate of Ruda.
4
5use proc_macro::TokenStream;
6
7/// Derive macro for the `Module` trait.
8///
9/// # Sub-modules
10///
11/// By default, the macro automatically detects sub-modules and parameters as module types.
12///
13/// Any field not recognized as a module type is assumed to be a non-module
14/// and is skipped by the module system (not persistent, not visited).
15///
16/// ## Generics
17///
18/// Generic type parameters (e.g., `field: M`) are assumed to be sub-modules by default.
19/// If a generic field represents some other runtime state or configuration, you can use
20/// the `#[module(skip)]` attribute to provide a hint.
21///
22/// # Field Attributes
23///
24/// ## `#[module(skip)]`
25///
26/// Explicitly marks a field to be ignored by the module derive.
27///
28/// Skipped fields are not parameters, not modules, and are not persistent.
29/// This is equivalent to the deprecated `Ignored<T>` wrapper.
30///
31/// ### Requirements
32///
33/// The field must implement: `Debug + Clone + Send`.
34///
35/// # Example
36///
37/// ```ignore
38/// #[derive(Module, Debug)]
39/// pub struct MyModule<B: Backend, M, N: NonModuleTrait> {
40///     /// A normal parameter.
41///     weights: Param<Tensor<B, 2>>,
42///     /// A field configured at runtime.
43///     dropout_prob: f64,
44///     /// A field that is recomputed at runtime.
45///     cached_mask: Option<Tensor<B, 2>>,
46///     /// A field that contains some debug state.
47///     debug_state: String,
48///     /// Treated as a module (default for generics).
49///     inner: M,
50///     /// Hint required: this generic is NOT a module.
51///     #[module(skip)]
52///     other: N,
53/// }
54/// ```
55#[proc_macro_derive(Module, attributes(module))]
56pub fn module_derive(input: TokenStream) -> TokenStream {
57    let input = syn::parse(input).unwrap();
58    ruda_model_codegen::derive_module_native(&input).into()
59}
60
61/// Derive macro for the record.
62#[proc_macro_derive(Record)]
63pub fn record_derive(input: TokenStream) -> TokenStream {
64    let input = syn::parse(input).unwrap();
65    ruda_model_codegen::derive_record_native(&input).into()
66}
67
68/// Derive macro for the config.
69#[proc_macro_derive(Config, attributes(config))]
70pub fn config_derive(input: TokenStream) -> TokenStream {
71    let item = syn::parse(input).unwrap();
72    ruda_model_codegen::derive_config_native(&item).into()
73}