1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//! This crate provides the `#[lazy]` attribute to lazily evaluate functions.
//!
//! This crate is used by the [`lazy-attribute`](https://crates.io/crates/lazy-attribute) crate and is not intended to be used directly.

mod lazy;

use proc_macro::TokenStream;

//--------------------------------------------------------------------------------------------------
// Attribute Procedural Macros
//--------------------------------------------------------------------------------------------------

/// Provides the `#[lazy]` attribute to lazily evaluate functions.
///
/// The macro expands the function into a wrapper that caches the result
/// in a static `OnceCell`.
///
/// Works for both synchronous and asynchronous
/// functions (with `async` feature).
#[proc_macro_attribute]
pub fn lazy(_: TokenStream, item: TokenStream) -> TokenStream {
    let fn_syntax = syn::parse_macro_input!(item as syn::ItemFn);
    lazy::expand(fn_syntax).into()
}