nautilus_model/ffi/orderbook/
level.rsuse std::ops::{Deref, DerefMut};
use nautilus_core::ffi::cvec::CVec;
use crate::{
data::order::BookOrder,
enums::OrderSide,
orderbook::{BookLevel, BookPrice},
types::Price,
};
#[repr(C)]
#[derive(Clone)]
#[allow(non_camel_case_types)]
pub struct BookLevel_API(Box<BookLevel>);
impl BookLevel_API {
#[must_use]
pub fn new(level: BookLevel) -> Self {
Self(Box::new(level))
}
}
impl Deref for BookLevel_API {
type Target = BookLevel;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for BookLevel_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) -> BookLevel_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 = BookLevel::new(price);
level.add_bulk(orders);
BookLevel_API::new(level)
}
#[no_mangle]
pub extern "C" fn level_drop(level: BookLevel_API) {
drop(level); }
#[no_mangle]
pub extern "C" fn level_clone(level: &BookLevel_API) -> BookLevel_API {
level.clone()
}
#[no_mangle]
pub extern "C" fn level_price(level: &BookLevel_API) -> Price {
level.price.value
}
#[no_mangle]
pub extern "C" fn level_orders(level: &BookLevel_API) -> CVec {
let orders_vec: Vec<BookOrder> = level.orders.values().copied().collect();
orders_vec.into()
}
#[no_mangle]
pub extern "C" fn level_size(level: &BookLevel_API) -> f64 {
level.size()
}
#[no_mangle]
pub extern "C" fn level_exposure(level: &BookLevel_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<BookLevel_API> =
unsafe { Vec::from_raw_parts(ptr.cast::<BookLevel_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); }