A new idea of proc_macro function

I hope Rust can extend function-like macros to struct members, which will make them more developer friendly. example:

// in proc-macro crate

pub struct MyStruct {}

impl MyStruct {
    #[proc_macro]
    pub fn parse_expr2(&mut self, arg1: TokenStream) -> TokenStream {
        quote! {
            my_test_func(self, #arg1)
        }
    }
}

// use this function-like macro

let st = MyStruct {};
let ret2 = st.parse_expr2!(1 + 2);

These are usually called postfix macros. There is an RFC for them you might find interesting. In general, language design is better discussed on IRLO, where you can also find more discussions of postfix macros.

4 Likes

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.