Is it possible wrote custom Attribute macros without procedure macro?

Currently, I want to wrote an attribute macro which is equivlent to Python's wrapper, @lru_cache

But what I could found is, wrote procedure macros, which needs another crate, seems ugly.

further, if we could add custom dependencies (e.g., in playground), it is hard for us to use procedure macros.

Is it possible to write attribute macro without procedure macros?

Is there any macros which could convert

#[cache]
fn test(a:i32)->i32{...}

to

static mut _test_cache:std::collections::HashMap<i32,i32>=HashMap::new();// suppose we could wrote it. Actually it should be something under an Arc.
fn test(a:i32){
   /// SAFETY: suppose we wrote a Arc thus there is no data racing...
   unsafe{*_test_cache.entry(a).or_insert({/*body of the original test function*/})}
}

Currently there's not a way for a normal macro_rules macro to be used as a normal attribute without a workaround of some sort. The macro_rules_attribute crate is one example of a workaround.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.