Problem using macros inside same macros repetition section

Cant get this to work:

enum Event {
    Before,
    Connect(usize),
    Close,
}

macro_rules! callback_gen {
    (Before => $code:block) => { Event::Before => $code, };
    (Connect => $code:block) => { Event::Connect(response) => $code, };
    (Close => $code:block) => { Event::Close => $code, };
    ($($event:ident => $code:block),+) => { 
        |incoming_event| {
            match incoming_event {
                $(callback_gen!($event => $code))*
            }
        }
    };
}

fn main() {

    let cb = callback_gen!(
        Before => {println!("before")},
        Connect => {println!("connect")},
        Close => {println!("close")}
    );

    cb(Event::Before);
}

(Playground)

You can't create entire match arms in a macro, you'll have to split up the pattern and the block.

How could i create pattern in macro?

Rust doesn't check macros before they are expanded (in fact it can't check macros before expansion)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.