nautilus_common/ffi/timer.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3// https://poseitrader.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::ffi::c_char;
17
18use nautilus_core::{
19 UUID4,
20 ffi::string::{cstr_to_ustr, str_to_cstr},
21};
22
23use crate::timer::{TimeEvent, TimeEventCallback, TimeEventHandlerV2};
24
25#[repr(C)]
26#[derive(Clone, Debug)]
27/// Legacy time event handler for Cython/FFI inter-operatbility
28///
29/// TODO: Remove once Cython is deprecated
30///
31/// `TimeEventHandler` associates a `TimeEvent` with a callback function that is triggered
32/// when the event's timestamp is reached.
33pub struct TimeEventHandler {
34 /// The time event.
35 pub event: TimeEvent,
36 /// The callable raw pointer.
37 pub callback_ptr: *mut c_char,
38}
39
40// Legacy conversion from TimeEventHandlerV2 to pure-C TimeEventHandler
41// Only supports Python callbacks; available when `python` feature is enabled
42#[cfg(feature = "python")]
43impl From<TimeEventHandlerV2> for TimeEventHandler {
44 /// # Panics
45 ///
46 /// Panics if the provided `TimeEventHandlerV2` contains a Rust callback,
47 /// since only Python callbacks are supported by the legacy `TimeEventHandler`.
48 fn from(value: TimeEventHandlerV2) -> Self {
49 Self {
50 event: value.event,
51 callback_ptr: match value.callback {
52 TimeEventCallback::Python(callback) => callback.as_ptr().cast::<c_char>(),
53 TimeEventCallback::Rust(_) => {
54 panic!("Legacy time event handler is not supported for Rust callback")
55 }
56 },
57 }
58 }
59}
60
61// Fallback conversion for non-Python callbacks: Rust callbacks only
62#[cfg(not(feature = "python"))]
63impl From<TimeEventHandlerV2> for TimeEventHandler {
64 fn from(value: TimeEventHandlerV2) -> Self {
65 // Only Rust callbacks are supported in non-python builds
66 match value.callback {
67 TimeEventCallback::Rust(_) => TimeEventHandler {
68 event: value.event,
69 callback_ptr: std::ptr::null_mut(),
70 },
71 #[cfg(feature = "python")]
72 TimeEventCallback::Python(_) => {
73 unreachable!("Python callback not supported without python feature")
74 }
75 }
76 }
77}
78
79/// # Safety
80///
81/// Assumes `name_ptr` is borrowed from a valid Python UTF-8 `str`.
82#[unsafe(no_mangle)]
83pub unsafe extern "C" fn time_event_new(
84 name_ptr: *const c_char,
85 event_id: UUID4,
86 ts_event: u64,
87 ts_init: u64,
88) -> TimeEvent {
89 TimeEvent::new(
90 unsafe { cstr_to_ustr(name_ptr) },
91 event_id,
92 ts_event.into(),
93 ts_init.into(),
94 )
95}
96
97/// Returns a [`TimeEvent`] as a C string pointer.
98#[unsafe(no_mangle)]
99pub extern "C" fn time_event_to_cstr(event: &TimeEvent) -> *const c_char {
100 str_to_cstr(&event.to_string())
101}
102
103// This function only exists so that `TimeEventHandler` is included in the definitions
104#[unsafe(no_mangle)]
105pub const extern "C" fn dummy(v: TimeEventHandler) -> TimeEventHandler {
106 v
107}