state-macro 0.1.1

Syntax sugar for stateful functions
Documentation
mod stateful;
mod with_state;

use proc_macro::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);
/// ```
#[proc_macro]
pub fn with_state(input: TokenStream) -> TokenStream {
    with_state::with_state(input)
}

/// 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
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn stateful(attr: TokenStream, item: TokenStream) -> TokenStream {
    stateful::stateful(attr, item)
}

/// 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())`.
#[proc_macro_attribute]
pub fn stateful_cloned(attr: TokenStream, item: TokenStream) -> TokenStream {
    stateful::stateful_cloned(attr, item)
}

/// 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
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn stateful_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
    stateful::stateful_expr(attr, item)
}