use std::{
fmt::Debug,
ops::{Deref, DerefMut},
};
use nautilus_common::ffi::{clock::TestClock_API, timer::TimeEventHandler_API};
use nautilus_core::{UnixNanos, ffi::parsing::u8_as_bool};
use crate::accumulator::TimeEventAccumulator;
#[repr(C)]
#[allow(non_camel_case_types)]
pub struct TimeEventAccumulator_API(Box<TimeEventAccumulator>);
impl Deref for TimeEventAccumulator_API {
type Target = TimeEventAccumulator;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Debug for TimeEventAccumulator_API {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TimeEventAccumulator_API({:p})", &*self.0)
}
}
impl DerefMut for TimeEventAccumulator_API {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[unsafe(no_mangle)]
pub extern "C" fn time_event_accumulator_new() -> TimeEventAccumulator_API {
TimeEventAccumulator_API(Box::default())
}
#[unsafe(no_mangle)]
pub extern "C" fn time_event_accumulator_drop(accumulator: TimeEventAccumulator_API) {
drop(accumulator);
}
#[unsafe(no_mangle)]
pub extern "C" fn time_event_accumulator_clear(accumulator: &mut TimeEventAccumulator_API) {
accumulator.clear();
}
#[unsafe(no_mangle)]
pub extern "C" fn time_event_accumulator_advance_clock(
accumulator: &mut TimeEventAccumulator_API,
clock: &mut TestClock_API,
to_time_ns: UnixNanos,
set_time: u8,
) {
accumulator.advance_clock(clock, to_time_ns, u8_as_bool(set_time));
}
#[unsafe(no_mangle)]
pub extern "C" fn time_event_accumulator_peek_next_time(
accumulator: &TimeEventAccumulator_API,
) -> u64 {
accumulator
.peek_next_time()
.map_or(u64::MAX, |t| t.as_u64())
}
#[unsafe(no_mangle)]
pub extern "C" fn time_event_accumulator_pop_next_at_or_before(
accumulator: &mut TimeEventAccumulator_API,
ts: UnixNanos,
) -> TimeEventHandler_API {
accumulator
.pop_next_at_or_before(ts)
.map_or_else(TimeEventHandler_API::null, Into::into)
}