Skip to main content

post_with

Function post_with 

Source
pub fn post_with<T>(job: impl FnOnce(&mut T) + Send + 'static)
where T: SampPlugin + 'static,
Expand description

Queues job to run on the main thread with the plugin instance.

The plugin’s own state is where the result of background work usually has to land, and a worker thread cannot reach it: the plugin is not Sync, and only the main thread may touch it. This posts the job and hands it &mut T at the moment it runs.

std::thread::spawn(move || {
    let delivered = true; // ... the slow work ...

    samp::mainthread::post_with::<Mailer>(move |plugin| {
        if delivered {
            plugin.sent += 1;
        }
    });
});

T must be the type initialize_plugin! creates; naming another logs a warning and skips the job, rather than reinterpreting the plugin’s bytes.

Do not call into Pawn from inside the closure. A public that re-enters one of this plugin’s natives would take a second &mut to the same plugin. Collect what to send, let the closure end, and send it after — or use post_with_amx, which is that shape already.