1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use TokenStream;
/// A macro for decluttering mutable state computations.
///
/// Takes a state expression and code block, and transforms function calls
/// prefixed with `::` to include the state as their first argument.
///
/// # Example
///
/// ```ignore
/// with_state! { state;
/// let x = ::mat_mul(::param(), y) + ::param();
/// }
/// ```
///
/// Expands to:
///
/// ```ignore
/// let x = mat_mul(state, param(state), y) + param(state);
/// ```
/// An attribute macro that transforms a function to be stateful.
///
/// Adds a state parameter to the function and wraps the function body
/// in the `with_state!` macro.
///
/// # Example
///
/// ```ignore
/// #[stateful(State<S>)]
/// fn dense(x: i32) -> i32 {
/// let y = ::mat_mul(::param(), x);
/// y
/// }
/// ```
///
/// Expands to:
///
/// ```ignore
/// fn dense(state: State<S>, x: i32) -> i32 {
/// with_state! { state;
/// let y = ::mat_mul(::param(), x);
/// y
/// }
/// }
/// ```
/// Like [`stateful`], but passes `state.clone()` to state functions instead of `state`.
/// Useful when the state type is like `Rc<RefCell<T>>`.
/// Equivalent to `stateful_expr(State, state.clone())`.
/// Like [`stateful`], but allows specifying a custom expression when passing the state variable.
/// A more general version of `stateful_cloned`.
///
/// # Example
///
/// ```ignore
/// #[stateful(State<S>, state.clone())]
/// fn dense(x: i32) -> i32 {
/// let y = ::mat_mul(::param(), x);
/// y
/// }
/// ```
///
/// Expands to:
///
/// ```ignore
/// fn dense(state: State<S>, x: i32) -> i32 {
/// with_state! { state.clone();
/// let y = ::mat_mul(::param(), x);
/// y
/// }
/// }
/// ```