Search code examples
rustrust-macros

Rust macro accept call to member function


I'm trying to replace a bunch of snippets like

if let Some(thing) = some_t {
    thing.doSomething();
}

with a macro, resulting in something like

if_some(some_t, doSomething());

so basically replacing

if_some(/* option */, /* member function call(s) */);

with

if let Some(thing) = /* option */ {
    thing./* member function call(s) */
}

The attempt below does however not seem to work (the compiler reports "error: unexpected token: doSomething()", which does not help me in understanding what is wrong here. Anyone has an idea on how to handle this?

#[macro_export]
macro_rules! if_some {
    ( $option:ident, $function:expr ) => {{
        if let Some(thing) = $option {
            thing.$function
        }
    }};
}

struct Thing {}

impl Thing {
    fn doSomething(&self) {}
}

fn main() {
    let some_t = Some(Thing {});

    if let Some(thing) = some_t {
        thing.doSomething();
    }

    if_some!(some_t, doSomething());
}

Solution

  • Once doSomething() is captured as expr, it will always stay so. The compiler does not see thing.doSomething(), it sees thing.<expr> and this is invalid Rust. You cannot write an expression after the dot, it needs to be an identifier.

    The easiest way to fix that is to instead capture it as list of tts. tts can be inspected after they are captured:

    #[macro_export]
    macro_rules! if_some {
        ( $option:ident, $($function:tt)* ) => {{
            if let Some(thing) = $option {
                thing.$($function)*
            }
        }};
    }