Skip to content

Commit

Permalink
Move OrderBookDeltas_API to data module
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Feb 27, 2024
1 parent 647cdf8 commit b9bfe4a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
3 changes: 1 addition & 2 deletions nautilus_core/adapters/src/databento/python/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use nautilus_core::{
time::{get_atomic_clock_realtime, AtomicTime, UnixNanos},
};
use nautilus_model::{
data::{delta::OrderBookDelta, deltas::OrderBookDeltas, Data},
ffi::data::deltas::OrderBookDeltas_API,
data::{delta::OrderBookDelta, deltas::OrderBookDeltas, deltas::OrderBookDeltas_API, Data},
identifiers::{instrument_id::InstrumentId, symbol::Symbol, venue::Venue},
python::data::data_to_pycapsule,
};
Expand Down
35 changes: 35 additions & 0 deletions nautilus_core/model/src/data/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use std::{
fmt::{Display, Formatter},
hash::{Hash, Hasher},
ops::{Deref, DerefMut},
};

use nautilus_core::time::UnixNanos;
Expand Down Expand Up @@ -102,6 +103,40 @@ impl Display for OrderBookDeltas {
}
}

/// Provides a C compatible Foreign Function Interface (FFI) for an underlying [`OrderBookDeltas`].
///
/// This struct wraps `OrderBookDeltas` in a way that makes it compatible with C function
/// calls, enabling interaction with `OrderBookDeltas` in a C environment.
///
/// It implements the `Deref` trait, allowing instances of `OrderBookDeltas_API` to be
/// dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
/// having to manually access the underlying `OrderBookDeltas` instance.
#[repr(C)]
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct OrderBookDeltas_API(Box<OrderBookDeltas>);

impl OrderBookDeltas_API {
#[must_use]
pub fn new(deltas: OrderBookDeltas) -> Self {
Self(Box::new(deltas))
}
}

impl Deref for OrderBookDeltas_API {
type Target = OrderBookDeltas;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for OrderBookDeltas_API {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

////////////////////////////////////////////////////////////////////////////////
// Stubs
////////////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 6 additions & 3 deletions nautilus_core/model/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ pub mod trade;
use nautilus_core::time::UnixNanos;

use self::{
bar::Bar, delta::OrderBookDelta, deltas::OrderBookDeltas, depth::OrderBookDepth10,
quote::QuoteTick, trade::TradeTick,
bar::Bar,
delta::OrderBookDelta,
deltas::{OrderBookDeltas, OrderBookDeltas_API},
depth::OrderBookDepth10,
quote::QuoteTick,
trade::TradeTick,
};
use crate::ffi::data::deltas::OrderBookDeltas_API;

#[repr(C)]
#[derive(Clone, Debug)]
Expand Down
41 changes: 4 additions & 37 deletions nautilus_core/model/src/ffi/data/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,17 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::ops::{Deref, DerefMut};

use nautilus_core::{ffi::cvec::CVec, time::UnixNanos};

use crate::{
data::{delta::OrderBookDelta, deltas::OrderBookDeltas},
data::{
delta::OrderBookDelta,
deltas::{OrderBookDeltas, OrderBookDeltas_API},
},
enums::BookAction,
identifiers::instrument_id::InstrumentId,
};

/// Provides a C compatible Foreign Function Interface (FFI) for an underlying [`OrderBookDeltas`].
///
/// This struct wraps `OrderBookDeltas` in a way that makes it compatible with C function
/// calls, enabling interaction with `OrderBookDeltas` in a C environment.
///
/// It implements the `Deref` trait, allowing instances of `OrderBookDeltas_API` to be
/// dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
/// having to manually access the underlying `OrderBookDeltas` instance.
#[repr(C)]
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct OrderBookDeltas_API(Box<OrderBookDeltas>);

impl OrderBookDeltas_API {
#[must_use]
pub fn new(deltas: OrderBookDeltas) -> Self {
Self(Box::new(deltas))
}
}

impl Deref for OrderBookDeltas_API {
type Target = OrderBookDeltas;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for OrderBookDeltas_API {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

/// Creates a new `OrderBookDeltas` object from a `CVec` of `OrderBookDelta`.
///
/// # Safety
Expand Down
5 changes: 2 additions & 3 deletions nautilus_core/model/src/ffi/orderbook/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ use nautilus_core::ffi::{cvec::CVec, string::str_to_cstr};
use super::{container::OrderBookContainer, level::Level_API};
use crate::{
data::{
delta::OrderBookDelta, depth::OrderBookDepth10, order::BookOrder, quote::QuoteTick,
trade::TradeTick,
delta::OrderBookDelta, deltas::OrderBookDeltas_API, depth::OrderBookDepth10,
order::BookOrder, quote::QuoteTick, trade::TradeTick,
},
enums::{BookType, OrderSide},
ffi::data::deltas::OrderBookDeltas_API,
identifiers::instrument_id::InstrumentId,
types::{price::Price, quantity::Quantity},
};
Expand Down
7 changes: 5 additions & 2 deletions nautilus_core/model/src/python/data/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ use nautilus_core::time::UnixNanos;
use pyo3::{prelude::*, pyclass::CompareOp, types::PyCapsule};

use crate::{
data::{delta::OrderBookDelta, deltas::OrderBookDeltas, Data},
ffi::data::deltas::OrderBookDeltas_API,
data::{
delta::OrderBookDelta,
deltas::{OrderBookDeltas, OrderBookDeltas_API},
Data,
},
identifiers::instrument_id::InstrumentId,
python::common::PY_MODULE_MODEL,
};
Expand Down

0 comments on commit b9bfe4a

Please sign in to comment.