Skip to content

Define natives

Eva edited this page Mar 14, 2018 · 1 revision

To define a native you should use define_native! macro.

We have next Pawn native definition:

native Memcached_SetString(connection, const key[], const value[], expire_in);

Let's use our macro and define a new function in the Rust library.

define_native!(set_string, connection: usize, key: String, value: String, expire: u32);

struct Memcached {
    clients: Vec<Client>,
}

impl Memcached {
// ...
    pub fn set_string(&mut self, _: &AMX, connection: usize, key: String, value: String, expire: u32) -> ... {
        let client = self.clients[connection];
        client.set(&key, value, expire).unwrap();
    }
}

To use references we can add a ref keyword before a type in a function definition:

// native PassReference(&reference);
define_native!(pass_reference, reference: ref u32);

pub fn pass_reference(&self, _: &AMX, reference: &mut u32) {
    *reference = 152;
}

You cannot use Vec<T> type directly. Look here to know how to use arrays.

Clone this wiki locally