nautilus_model/ffi/orderbook/
level.rsuse std::ops::{Deref, DerefMut};
use nautilus_core::ffi::cvec::CVec;
use crate::{
data::order::BookOrder,
enums::OrderSide,
orderbook::{ladder::BookPrice, level::Level},
types::price::Price,
};
#[repr(C)]
#[derive(Clone)]
#[allow(non_camel_case_types)]
pub struct Level_API(Box<Level>);
impl Level_API {
#[must_use]
pub fn new(level: Level) -> Self {
Self(Box::new(level))
}
}
impl Deref for Level_API {
type Target = Level;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Level_API {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[no_mangle]
pub extern "C" fn level_new(order_side: OrderSide, price: Price, orders: CVec) -> Level_API {
let CVec { ptr, len, cap } = orders;
let orders: Vec<BookOrder> = unsafe { Vec::from_raw_parts(ptr.cast::<BookOrder>(), len, cap) };
let price = BookPrice {
value: price,
side: order_side,
};
let mut level = Level::new(price);
level.add_bulk(orders);
Level_API::new(level)
}
#[no_mangle]
pub extern "C" fn level_drop(level: Level_API) {
drop(level); }
#[no_mangle]
pub extern "C" fn level_clone(level: &Level_API) -> Level_API {
level.clone()
}
#[no_mangle]
pub extern "C" fn level_price(level: &Level_API) -> Price {
level.price.value
}
#[no_mangle]
pub extern "C" fn level_orders(level: &Level_API) -> CVec {
let orders_vec: Vec<BookOrder> = level.orders.values().copied().collect();
orders_vec.into()
}
#[no_mangle]
pub extern "C" fn level_size(level: &Level_API) -> f64 {
level.size()
}
#[no_mangle]
pub extern "C" fn level_exposure(level: &Level_API) -> f64 {
level.exposure()
}
#[allow(clippy::drop_non_drop)]
#[no_mangle]
pub extern "C" fn vec_levels_drop(v: CVec) {
let CVec { ptr, len, cap } = v;
let data: Vec<Level_API> = unsafe { Vec::from_raw_parts(ptr.cast::<Level_API>(), len, cap) };
drop(data); }
#[allow(clippy::drop_non_drop)]
#[no_mangle]
pub extern "C" fn vec_orders_drop(v: CVec) {
let CVec { ptr, len, cap } = v;
let orders: Vec<BookOrder> = unsafe { Vec::from_raw_parts(ptr.cast::<BookOrder>(), len, cap) };
drop(orders); }