Attribute Macro rust_sc2::bot[]

#[bot]
Expand description

#[bot] macro implements Deref<Target = Bot> and DerefMut<Target = Bot> for your struct. Implementing this traits allows you to access Bot fields and methods on your struct through self.

Usage:

#[bot]
struct MyBot;

impl MyBot {
    fn my_func(&self) {
        println!("my race: {:?}", self.race);
        println!("current \"game_step\": {}", self.game_step());
    }
    fn my_func_mut(&mut self) {
        self.chat("It works!");
        self.set_game_step(8);
    }
}

What this macro does?

It adds hidden field where data of Bot stored.

Also this adds Deref and DerefMut implementations to access Bot data just through self.whatever instead of self._bot.whatever.

What compiler does?

When you type this:

self.whatever_from_bot

Since Deref is implemented, compiler performs auto dereference to access Bot:

(*self).whatever_from_bot

The way how Deref implemented determines behavior of dereference operation, so actually it becomes:

self._bot.whatever_from_bot

Macro Inside

This:

#[bot]
struct MyBot;

Expands to:

struct MyBot {
    _bot: Bot,
}
impl Deref for MyBot {
    type Target = Bot;

    fn deref(&self) -> &Self::Target {
        &self._bot
    }
}
impl DerefMut for MyBot {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self._bot
    }
}

And this:

#[bot]
struct MyBot {
    field: Type,
    field2: Type2,
}

Expands to:

struct MyBot {
    _bot: Bot,
    field: Type,
    field2: Type2,
}
impl Deref for MyBot {
    type Target = Bot;

    fn deref(&self) -> &Self::Target {
        &self._bot
    }
}
impl DerefMut for MyBot {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self._bot
    }
}