rust-samp 3.3.0

Write SA-MP and open.mp plugins in safe Rust instead of C++. A single binary runs natively on both servers, with proc macros (`#[native]`, `initialize_plugin!`) that hide the FFI boilerplate and ABI-correct marshalling for Linux (Itanium) and Windows (MSVC).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Global runtime state of the Rust plugin — singleton accessible via
//! [`Runtime::get`].
//!
//! Encapsulates pointers received from the server (SA-MP `ppData` or Open Multiplayer `ICore`),
//! the list of active AMXs, the plugin instantiated by the dev and flags (logger,
//! tick enabled, etc).
//!
//! The `Runtime` lives in a global `AtomicPtr` and uses `UnsafeCell` for interior
//! mutation — safe because the server is single-threaded and all callbacks
//! run on the main thread.

use samp_sdk::consts::{ServerData, Supports};
#[cfg(not(feature = "samp-only"))]
use samp_sdk::omp::component::ICore;
#[cfg(not(feature = "samp-only"))]
use samp_sdk::omp::events::PawnEventHandler;
#[cfg(not(feature = "samp-only"))]
use samp_sdk::omp::server::{ServerComponent, ServerComponentList};
#[cfg(not(feature = "samp-only"))]
use samp_sdk::omp::timers::{ITimer, TimerTimeOutHandler};
#[cfg(not(feature = "samp-only"))]
use samp_sdk::raw::types::AMX_NATIVE_INFO;
use samp_sdk::raw::{functions::Logprintf, types::AMX};

use std::cell::UnsafeCell;
use std::collections::HashMap;
use std::ffi::CString;
use std::ptr::NonNull;
use std::sync::atomic::{AtomicPtr, Ordering};
use std::time::{Duration, Instant};

use crate::amx::{Amx, AmxIdent};
use crate::events::{EventHandler, EventInfo};
use crate::plugin::{SampPlugin, TickConfig};

static RUNTIME: AtomicPtr<Runtime> = AtomicPtr::new(std::ptr::null_mut());

struct RuntimeInner {
    plugin: Option<NonNull<dyn SampPlugin + 'static>>,
    /// Set by `samp::plugin::enable_tick` / `enable_tick_with`. `None`
    /// means the tick is disabled on both servers (the default).
    tick_config: Option<TickConfig>,
    /// Wall-clock timestamp of the previous tick dispatch. Used to compute
    /// `TickContext::elapsed`. `None` until the first tick fires.
    last_tick_at: Option<Instant>,
    server_exports: *const usize,
    /// AMX function table obtained from `IPawnComponent` in native Open Multiplayer mode.
    #[cfg(not(feature = "samp-only"))]
    omp_amx_exports: Option<usize>,
    /// Pointer to the Open Multiplayer server's `ICore`, stored in `on_load`.
    #[cfg(not(feature = "samp-only"))]
    omp_core: Option<NonNull<ICore>>,
    /// Open Multiplayer server component list, stored in `on_init`.
    #[cfg(not(feature = "samp-only"))]
    omp_component_list: Option<NonNull<ServerComponentList>>,
    /// Pawn event handler registered in the `IEventDispatcher` of `IPawnComponent`.
    #[cfg(not(feature = "samp-only"))]
    pawn_event_handler: Option<NonNull<PawnEventHandler>>,
    /// Natives to register on the AMX in native Open Multiplayer mode (via `pawn_on_amx_load`).
    /// In SA-MP/legacy mode this Vec stays empty — natives are passed via `AmxLoad()`.
    #[cfg(not(feature = "samp-only"))]
    omp_natives: Vec<AMX_NATIVE_INFO>,
    /// AMXs that arrived via `on_amx_load` before `on_ready` (without `getAmxFunctions`
    /// available). Processed in `on_ready` when the `fn_table` is stored.
    #[cfg(not(feature = "samp-only"))]
    omp_pending_amx: Vec<*mut AMX>,
    /// Timer created via `ITimersComponent` to deliver `on_tick` in Open
    /// Multiplayer mode. Stored so `omp_cleanup` can kill it on shutdown.
    #[cfg(not(feature = "samp-only"))]
    omp_tick_timer: Option<NonNull<ITimer>>,
    /// Tick handler (heap allocated). Released in the timer's `free` callback.
    #[cfg(not(feature = "samp-only"))]
    omp_tick_handler: Option<NonNull<TimerTimeOutHandler>>,
    amx_list: Vec<(AmxIdent, Amx)>,
    /// `#[event]` handlers registered at init via `register_events`. Empty when
    /// the plugin uses no events — the `amx_Exec` detour is then never installed.
    events: Vec<EventInfo>,
    /// Per-AMX resolution of the registered events, keyed by
    /// `(amx, public index)` for O(1) lookup on the `amx_Exec` hot path (a
    /// public runs on every callback/timer tick). Filled on `on_amx_load`,
    /// pruned on `on_amx_unload`.
    resolved_events: HashMap<(AmxIdent, i32), Vec<EventHandler>>,
    logger_enabled: bool,
}

pub struct Runtime {
    inner: UnsafeCell<RuntimeInner>,
}

// SAFETY: SA-MP and Open Multiplayer servers are single-threaded — every access to the
// `Runtime` happens via callbacks on the main thread. The impls here are a
// formality to satisfy the `AtomicPtr<Runtime>`.
unsafe impl Sync for Runtime {}
unsafe impl Send for Runtime {}

impl Runtime {
    /// Mutable access to the inner state via [`UnsafeCell`].
    ///
    /// # Safety
    /// Single-threaded server — concurrency does not happen. `UnsafeCell` is
    /// the canonical Rust path for shared mutation in this regime.
    #[inline]
    #[allow(clippy::mut_from_ref)]
    fn inner(&self) -> &mut RuntimeInner {
        unsafe { &mut *self.inner.get() }
    }

    pub fn initialize() -> &'static Runtime {
        let inner = RuntimeInner {
            plugin: None,
            tick_config: None,
            last_tick_at: None,
            server_exports: std::ptr::null(),
            #[cfg(not(feature = "samp-only"))]
            omp_amx_exports: None,
            #[cfg(not(feature = "samp-only"))]
            omp_core: None,
            #[cfg(not(feature = "samp-only"))]
            omp_component_list: None,
            #[cfg(not(feature = "samp-only"))]
            pawn_event_handler: None,
            #[cfg(not(feature = "samp-only"))]
            omp_natives: Vec::new(),
            #[cfg(not(feature = "samp-only"))]
            omp_pending_amx: Vec::new(),
            #[cfg(not(feature = "samp-only"))]
            omp_tick_timer: None,
            #[cfg(not(feature = "samp-only"))]
            omp_tick_handler: None,
            amx_list: Vec::new(),
            events: Vec::new(),
            resolved_events: HashMap::new(),
            logger_enabled: true,
        };

        let rt = Runtime {
            inner: UnsafeCell::new(inner),
        };

        let boxed = Box::new(rt);

        RUNTIME.store(Box::into_raw(boxed), Ordering::Release);

        Runtime::get()
    }

    pub fn post_initialize(&self) {
        if !self.inner().logger_enabled {
            return;
        }

        let logger = crate::plugin::logger();
        let _ = logger.apply();
    }

    #[inline]
    pub fn amx_exports(&self) -> usize {
        let inner = self.inner();

        // Native Open Multiplayer mode: exports obtained via IPawnComponent::getAmxFunctions()
        #[cfg(not(feature = "samp-only"))]
        if let Some(exports) = inner.omp_amx_exports {
            return exports;
        }

        // SA-MP mode: exports obtained via server_data passed in Load()
        if inner.server_exports.is_null() {
            // Native Open Multiplayer without an AMX table available (on_init failed or has not been called yet).
            return 0;
        }
        unsafe {
            inner
                .server_exports
                .offset(ServerData::AmxExports.into())
                .read()
        }
    }

    #[inline]
    pub fn logger(&self) -> Logprintf {
        let inner = self.inner();
        assert!(
            !inner.server_exports.is_null(),
            "server_exports not initialized"
        );
        unsafe {
            inner
                .server_exports
                .offset(ServerData::Logprintf.into())
                .cast::<Logprintf>()
                .read()
        }
    }

    pub fn disable_default_logger(&self) {
        self.inner().logger_enabled = false;
    }

    /// Default SDK log routing:
    ///
    /// - **SA-MP**: uses the server's `logprintf` (console + log file configured
    ///   by SA-MP).
    /// - **native Open Multiplayer**: uses `ICore::logLn(Message, ...)` — writes to the console and
    ///   to the log file Open Multiplayer has configured, with timestamp + `[Info]` level.
    ///   Equivalent to SA-MP's `logprintf` in terms of destination. If `ICore` is not
    ///   yet available (before `on_load`), falls back to `eprintln!`.
    ///
    /// The file destination is decided by the server — nothing hardcoded here.
    ///
    /// No SDK prefix. To customize (different level, format, separate file,
    /// etc), use `samp::plugin::logger()` in `on_load` and configure a `fern::Dispatch` —
    /// the `log` crate level is mapped to Open Multiplayer's `LogLevel` automatically.
    pub fn log<T: std::fmt::Display>(&self, message: T) {
        // SA-MP mode: routes via the server's logprintf.
        if !self.inner().server_exports.is_null() {
            let log_fn = self.logger();
            let msg = format!("{message}");
            if let Ok(cstr) = CString::new(msg) {
                log_fn(cstr.as_ptr());
            }
            return;
        }

        // Native Open Multiplayer mode: routes via ICore::logLnU8 (the server's UTF-8 pipeline).
        // Rust strings are already UTF-8 — we always use the U8 variant, ensuring that accents
        // and non-ASCII characters pass through correctly regardless of the console locale.
        #[cfg(not(feature = "samp-only"))]
        if let Some(core) = self.omp_core() {
            let msg = format!("{message}");
            if unsafe {
                samp_sdk::omp::core_log_ln_u8(core, samp_sdk::omp::LogLevel::Message, &msg)
            } {
                return;
            }
        }

        // Fallback: stderr (in case the plugin logs before on_load or ICore is unavailable).
        eprintln!("{message}");
    }

    /// Logs with a specific level on the Open Multiplayer server via `ICore::logLnU8`.
    /// In SA-MP mode the level is ignored and the message goes through `logprintf`.
    ///
    /// Available only when the `samp-only` feature is disabled — without Open Multiplayer,
    /// there is no channel that uses `LogLevel`. In pure `samp-only` use `log()`.
    #[cfg(not(feature = "samp-only"))]
    pub fn log_level<T: std::fmt::Display>(&self, level: samp_sdk::omp::LogLevel, message: T) {
        if self.inner().server_exports.is_null() {
            if let Some(core) = self.omp_core() {
                let msg = format!("{message}");
                if unsafe { samp_sdk::omp::core_log_ln_u8(core, level, &msg) } {
                    return;
                }
            }
            eprintln!("{message}");
            return;
        }
        // SA-MP mode: falls back to the standard log (no level).
        self.log(message);
    }

    /// Inserts an AMX in the runtime and returns a reference to the freshly inserted one.
    ///
    /// Push on `Vec` always succeeds (until OOM); the freshly pushed slot always
    /// exists — hence the return by value without `Option`.
    pub fn insert_amx(&self, amx: *mut AMX) -> &Amx {
        let inner = self.inner();
        let ident = AmxIdent::from(amx);
        let amx = Amx::new(amx, self.amx_exports());

        inner.amx_list.push((ident, amx));
        &inner
            .amx_list
            .last()
            .expect("Vec::last() after push() always returns Some")
            .1
    }

    pub fn remove_amx(&self, amx: *mut AMX) -> Option<Amx> {
        let list = &mut self.inner().amx_list;
        let ident = AmxIdent::from(amx);
        list.iter()
            .position(|(k, _)| *k == ident)
            .map(|pos| list.swap_remove(pos).1)
    }

    pub fn supports(&self) -> Supports {
        let mut supports = Supports::VERSION | Supports::AMX_NATIVES;

        if self.tick_enabled_for_sa_mp() {
            supports.insert(Supports::PROCESS_TICK);
        }

        supports
    }

    #[inline]
    pub fn amx_list(&self) -> &[(AmxIdent, Amx)] {
        &self.inner().amx_list
    }

    pub fn set_plugin<T>(&self, plugin: T)
    where
        T: SampPlugin + 'static,
    {
        let boxed = Box::new(plugin);
        self.inner().plugin = NonNull::new(Box::into_raw(boxed));
    }

    pub fn set_server_exports(&self, exports: *const usize) {
        self.inner().server_exports = exports;
    }

    /// Stores the [`TickConfig`] requested by the plugin. Called by
    /// `samp::plugin::enable_tick` / `enable_tick_with` in the constructor.
    pub fn set_tick_config(&self, config: TickConfig) {
        self.inner().tick_config = Some(config);
    }

    /// `Some` only after the plugin opted in via `enable_tick*`.
    #[inline]
    pub fn tick_config(&self) -> Option<TickConfig> {
        self.inner().tick_config
    }

    /// True iff the plugin opted in to the tick **and** allowed SA-MP
    /// delivery. Used by `Supports()` to decide whether to advertise
    /// `Supports::PROCESS_TICK`.
    #[inline]
    pub fn tick_enabled_for_sa_mp(&self) -> bool {
        self.tick_config().is_some_and(|c| c.sa_mp)
    }

    /// Open Multiplayer timer interval, or `None` if the tick is disabled
    /// on that server. Used by `interlayer::omp_on_ready` to decide whether
    /// to create the `ITimersComponent` timer and at which interval.
    #[cfg(not(feature = "samp-only"))]
    #[inline]
    pub fn omp_tick_interval(&self) -> Option<Duration> {
        self.tick_config().filter(|c| c.omp).map(|c| c.omp_interval)
    }

    /// Records the current instant as the latest tick dispatch and returns
    /// the elapsed time since the previous one (zero on the first call).
    pub fn record_tick(&self) -> Duration {
        let now = Instant::now();
        let prev = self.inner().last_tick_at.replace(now);
        prev.map_or(Duration::ZERO, |t| now.duration_since(t))
    }

    #[inline]
    pub fn get() -> &'static Runtime {
        let ptr = RUNTIME.load(Ordering::Acquire);
        assert!(
            !ptr.is_null(),
            "Runtime::get() called before Runtime::initialize()"
        );
        unsafe { &*ptr }
    }

    /// Non-panicking variant of [`get`]. Used by callers that can
    /// reasonably be invoked before [`initialize`] — typically env-var
    /// parsing that happens at config-build time, before the plugin's
    /// `Load`/`on_load` runs.
    ///
    /// [`get`]: Self::get
    /// [`initialize`]: Self::initialize
    #[inline]
    pub fn try_get() -> Option<&'static Runtime> {
        let ptr = RUNTIME.load(Ordering::Acquire);
        if ptr.is_null() {
            None
        } else {
            Some(unsafe { &*ptr })
        }
    }

    #[inline]
    pub fn plugin() -> &'static mut dyn SampPlugin {
        let rt = Runtime::get();
        let inner = rt.inner();
        unsafe {
            inner
                .plugin
                .as_mut()
                .expect("Runtime::plugin() called before set_plugin()")
                .as_mut()
        }
    }

    #[inline]
    pub fn plugin_cast<T: SampPlugin>() -> NonNull<T> {
        let rt = Runtime::get();
        rt.inner()
            .plugin
            .as_ref()
            .expect("Runtime::plugin_cast() called before set_plugin()")
            .cast()
    }

    // -----------------------------------------------------------------------
    // `#[event]` support — callback interception registry.
    // -----------------------------------------------------------------------

    /// Appends the `#[event]` handlers declared in `initialize_plugin!`. Called
    /// once at init (SA-MP `Load` / Open Multiplayer `ComponentEntryPoint`).
    pub fn register_events(&self, mut events: Vec<EventInfo>) {
        self.inner().events.append(&mut events);
    }

    /// True when at least one `#[event]` handler was registered — the gate that
    /// decides whether the `amx_Exec` detour is ever installed.
    #[inline]
    pub fn has_events(&self) -> bool {
        !self.inner().events.is_empty()
    }

    /// Snapshot of the registered events, for per-AMX resolution.
    pub fn events_snapshot(&self) -> Vec<EventInfo> {
        self.inner().events.clone()
    }

    /// Records a resolved handler for a `(amx, public index)` pair, appending to
    /// any already registered for that key (multiple handlers per callback run
    /// in registration order).
    pub fn push_resolved_event(&self, ident: AmxIdent, index: i32, handler: EventHandler) {
        self.inner()
            .resolved_events
            .entry((ident, index))
            .or_default()
            .push(handler);
    }

    /// Handlers registered for a given `(amx, public index)` pair, in
    /// registration order. Empty (no allocation) when the public carries no
    /// event — the common case on the `amx_Exec` hot path.
    ///
    /// Only the `amx_Exec` dispatcher consumes this, and that path exists on
    /// x86/x86_64 alone (the detour library targets no other arch), so the
    /// method is compiled only there — it has no caller elsewhere.
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    pub fn resolved_handlers(&self, ident: AmxIdent, index: i32) -> Vec<EventHandler> {
        self.inner()
            .resolved_events
            .get(&(ident, index))
            .cloned()
            .unwrap_or_default()
    }

    /// Drops every resolved handler bound to an AMX. Called on `on_amx_unload`,
    /// and also before re-resolving an AMX so a second `on_amx_load` for the
    /// same script cannot register duplicate handlers.
    pub fn remove_resolved_events(&self, ident: AmxIdent) {
        self.inner().resolved_events.retain(|(k, _), _| *k != ident);
    }
}

// ---------------------------------------------------------------------------
// Native Open Multiplayer state — available only when `samp-only` is disabled.
// Grouped in a separate `impl` to avoid repeating `#[cfg(...)]` per method.
// ---------------------------------------------------------------------------

#[cfg(not(feature = "samp-only"))]
impl Runtime {
    /// Sets the AMX function table obtained from `IPawnComponent` (native Open Multiplayer mode).
    pub fn set_omp_amx_exports(&self, exports: usize) {
        self.inner().omp_amx_exports = Some(exports);
    }

    /// Indicates whether we already have `getAmxFunctions()` stored — that is, whether `on_ready`
    /// has already been called and native registration can proceed immediately.
    pub fn omp_has_amx_exports(&self) -> bool {
        self.inner().omp_amx_exports.is_some()
    }

    /// Stores the `ICore*` pointer received in `onLoad` (native Open Multiplayer mode).
    pub fn set_omp_core(&self, core: *mut ICore) {
        self.inner().omp_core = NonNull::new(core);
    }

    /// Returns the Open Multiplayer server's `ICore*` pointer, if available.
    pub fn omp_core(&self) -> Option<*mut ICore> {
        self.inner().omp_core.map(std::ptr::NonNull::as_ptr)
    }

    /// Stores the component list received in `onInit` (native Open Multiplayer mode).
    pub fn set_omp_component_list(&self, list: *mut ServerComponentList) {
        self.inner().omp_component_list = NonNull::new(list);
    }

    /// Returns the server's component list, if already received in `onInit`.
    pub fn omp_component_list(&self) -> Option<*mut ServerComponentList> {
        self.inner()
            .omp_component_list
            .map(std::ptr::NonNull::as_ptr)
    }

    /// Looks up a component in the Open Multiplayer server list by UID.
    ///
    /// Returns `None` if the list has not been received yet or the component does not exist.
    pub fn omp_query_component(
        &self,
        uid: samp_sdk::omp::types::UID,
    ) -> Option<*mut ServerComponent> {
        let list = self.inner().omp_component_list?.as_ptr();
        let ptr = unsafe { samp_sdk::omp::server::query_component(list, uid) };
        if ptr.is_null() { None } else { Some(ptr) }
    }

    /// Stores the Pawn event handler (keeps it alive for as long as the plugin exists).
    pub fn set_pawn_event_handler(&self, handler: *mut PawnEventHandler) {
        self.inner().pawn_event_handler = NonNull::new(handler);
    }

    /// Removes and returns the stored Pawn event handler, or `None` if there was none.
    ///
    /// Used in `omp_cleanup` to unregister the handler from the dispatcher before shutdown.
    pub fn take_pawn_event_handler(&self) -> Option<*mut PawnEventHandler> {
        self.inner()
            .pawn_event_handler
            .take()
            .map(std::ptr::NonNull::as_ptr)
    }

    /// Stores the list of natives to register on the AMX in native Open Multiplayer mode.
    ///
    /// Called by the generated `ComponentEntryPoint` before registering the `PawnEventHandler`,
    /// ensuring natives are available when `pawn_on_amx_load` fires.
    pub fn set_omp_natives(&self, natives: Vec<AMX_NATIVE_INFO>) {
        self.inner().omp_natives = natives;
    }

    /// Returns the list of natives to register on the AMX in native Open Multiplayer mode.
    pub fn omp_natives(&self) -> &[AMX_NATIVE_INFO] {
        &self.inner().omp_natives
    }

    /// Enqueues an AMX that arrived via `on_amx_load` before `on_ready`.
    /// It will be processed when `on_ready` stores the `fn_table`.
    pub fn enqueue_pending_amx(&self, amx: *mut AMX) {
        self.inner().omp_pending_amx.push(amx);
    }

    /// Drains the pending AMX queue. Called in `on_ready` after `set_omp_amx_exports`.
    pub fn take_pending_amx(&self) -> Vec<*mut AMX> {
        std::mem::take(&mut self.inner().omp_pending_amx)
    }

    /// Stores references to the timer/handler created in `on_ready` for the tick abstraction.
    pub fn set_omp_tick(&self, timer: *mut ITimer, handler: *mut TimerTimeOutHandler) {
        self.inner().omp_tick_timer = NonNull::new(timer);
        self.inner().omp_tick_handler = NonNull::new(handler);
    }

    /// Returns and clears the timer pointer (for `kill` on shutdown).
    pub fn take_omp_tick_timer(&self) -> Option<*mut ITimer> {
        self.inner()
            .omp_tick_timer
            .take()
            .map(std::ptr::NonNull::as_ptr)
    }

    /// Returns and clears the timer handler (in case the `free` callback does not fire and
    /// we need to release manually).
    pub fn take_omp_tick_handler(&self) -> Option<*mut TimerTimeOutHandler> {
        self.inner()
            .omp_tick_handler
            .take()
            .map(std::ptr::NonNull::as_ptr)
    }
}