nautilus_backtest/ffi/
accumulator.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.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::{
17    fmt::Debug,
18    ops::{Deref, DerefMut},
19};
20
21use nautilus_common::ffi::{clock::TestClock_API, timer::TimeEventHandler_API};
22use nautilus_core::{UnixNanos, ffi::parsing::u8_as_bool};
23
24use crate::accumulator::TimeEventAccumulator;
25
26/// FFI wrapper for [`TimeEventAccumulator`].
27#[repr(C)]
28#[allow(non_camel_case_types)]
29pub struct TimeEventAccumulator_API(Box<TimeEventAccumulator>);
30
31impl Deref for TimeEventAccumulator_API {
32    type Target = TimeEventAccumulator;
33
34    fn deref(&self) -> &Self::Target {
35        &self.0
36    }
37}
38
39impl Debug for TimeEventAccumulator_API {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(f, "TimeEventAccumulator_API({:p})", &*self.0)
42    }
43}
44
45impl DerefMut for TimeEventAccumulator_API {
46    fn deref_mut(&mut self) -> &mut Self::Target {
47        &mut self.0
48    }
49}
50
51/// Creates a new [`TimeEventAccumulator_API`] instance.
52#[unsafe(no_mangle)]
53pub extern "C" fn time_event_accumulator_new() -> TimeEventAccumulator_API {
54    TimeEventAccumulator_API(Box::default())
55}
56
57/// Drops a [`TimeEventAccumulator_API`] instance.
58#[unsafe(no_mangle)]
59pub extern "C" fn time_event_accumulator_drop(accumulator: TimeEventAccumulator_API) {
60    drop(accumulator);
61}
62
63/// Advance the clock and push events to the heap.
64#[unsafe(no_mangle)]
65pub extern "C" fn time_event_accumulator_advance_clock(
66    accumulator: &mut TimeEventAccumulator_API,
67    clock: &mut TestClock_API,
68    to_time_ns: UnixNanos,
69    set_time: u8,
70) {
71    accumulator.advance_clock(clock, to_time_ns, u8_as_bool(set_time));
72}
73
74/// Peek at the next event timestamp.
75///
76/// Returns `u64::MAX` if the heap is empty.
77#[unsafe(no_mangle)]
78pub extern "C" fn time_event_accumulator_peek_next_time(
79    accumulator: &TimeEventAccumulator_API,
80) -> u64 {
81    accumulator
82        .peek_next_time()
83        .map_or(u64::MAX, |t| t.as_u64())
84}
85
86/// Pop the next event if its timestamp is at or before `ts`.
87///
88/// Returns a handler with `callback_ptr = NULL` if no event is available.
89#[unsafe(no_mangle)]
90pub extern "C" fn time_event_accumulator_pop_next_at_or_before(
91    accumulator: &mut TimeEventAccumulator_API,
92    ts: UnixNanos,
93) -> TimeEventHandler_API {
94    accumulator
95        .pop_next_at_or_before(ts)
96        .map_or_else(TimeEventHandler_API::null, Into::into)
97}