nautilus_backtest/ffi/
accumulator.rs1use 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#[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#[unsafe(no_mangle)]
53pub extern "C" fn time_event_accumulator_new() -> TimeEventAccumulator_API {
54 TimeEventAccumulator_API(Box::default())
55}
56
57#[unsafe(no_mangle)]
59pub extern "C" fn time_event_accumulator_drop(accumulator: TimeEventAccumulator_API) {
60 drop(accumulator);
61}
62
63#[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#[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#[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}