-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OrderBookDeltas FFI and pyo3 interfaces
- Loading branch information
1 parent
753b769
commit 0009f3a
Showing
12 changed files
with
543 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved. | ||
// https://nautechsystems.io | ||
// | ||
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
use std::ops::{Deref, DerefMut}; | ||
|
||
use nautilus_core::{ffi::cvec::CVec, time::UnixNanos}; | ||
|
||
use crate::{ | ||
data::{delta::OrderBookDelta, deltas::OrderBookDeltas}, | ||
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)] | ||
#[allow(non_camel_case_types)] | ||
pub struct OrderBookDeltas_API(Box<OrderBookDeltas>); | ||
|
||
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 | ||
/// - The `deltas` must be a valid pointer to a `CVec` containing `OrderBookDelta` objects | ||
/// - This function clones the data pointed to by `deltas` into Rust-managed memory, then forgets the original `Vec` to prevent Rust from auto-deallocating it | ||
/// - The caller is responsible for managing the memory of `deltas` (including its deallocation) to avoid memory leaks | ||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_new( | ||
instrument_id: InstrumentId, | ||
deltas: &CVec, | ||
) -> OrderBookDeltas_API { | ||
let CVec { ptr, len, cap } = *deltas; | ||
let deltas: Vec<OrderBookDelta> = | ||
unsafe { Vec::from_raw_parts(ptr as *mut OrderBookDelta, len, cap) }; | ||
let cloned_deltas = deltas.clone(); | ||
std::mem::forget(deltas); // Prevents Rust from dropping `deltas` | ||
OrderBookDeltas_API(Box::new(OrderBookDeltas::new(instrument_id, cloned_deltas))) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_drop(deltas: OrderBookDeltas_API) { | ||
drop(deltas); // Memory freed here | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_instrument_id(deltas: &OrderBookDeltas_API) -> InstrumentId { | ||
deltas.instrument_id | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_vec_deltas(deltas: &OrderBookDeltas_API) -> CVec { | ||
deltas.deltas.clone().into() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_is_snapshot(deltas: &OrderBookDeltas_API) -> u8 { | ||
u8::from(deltas.deltas[0].action == BookAction::Clear) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_flags(deltas: &OrderBookDeltas_API) -> u8 { | ||
deltas.flags | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_sequence(deltas: &OrderBookDeltas_API) -> u64 { | ||
deltas.sequence | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_ts_event(deltas: &OrderBookDeltas_API) -> UnixNanos { | ||
deltas.ts_event | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_ts_init(deltas: &OrderBookDeltas_API) -> UnixNanos { | ||
deltas.ts_init | ||
} | ||
|
||
#[allow(clippy::drop_non_drop)] | ||
#[no_mangle] | ||
pub extern "C" fn orderbook_deltas_vec_drop(v: CVec) { | ||
let CVec { ptr, len, cap } = v; | ||
let deltas: Vec<OrderBookDelta> = | ||
unsafe { Vec::from_raw_parts(ptr as *mut OrderBookDelta, len, cap) }; | ||
drop(deltas); // Memory freed here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
pub mod bar; | ||
pub mod delta; | ||
pub mod deltas; | ||
pub mod depth; | ||
pub mod order; | ||
pub mod quote; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved. | ||
// https://nautechsystems.io | ||
// | ||
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
// use std::{ | ||
// collections::{hash_map::DefaultHasher, HashMap}, | ||
// hash::{Hash, Hasher}, | ||
// }; | ||
|
||
use nautilus_core::time::UnixNanos; | ||
use pyo3::prelude::*; | ||
|
||
use crate::{ | ||
data::{delta::OrderBookDelta, deltas::OrderBookDeltas}, | ||
identifiers::instrument_id::InstrumentId, | ||
python::PY_MODULE_MODEL, | ||
}; | ||
|
||
#[pymethods] | ||
impl OrderBookDeltas { | ||
#[new] | ||
fn py_new(instrument_id: InstrumentId, deltas: Vec<OrderBookDelta>) -> Self { | ||
Self::new(instrument_id, deltas) | ||
} | ||
|
||
// TODO: Implement | ||
// fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> { | ||
// match op { | ||
// CompareOp::Eq => self.eq(other).into_py(py), | ||
// CompareOp::Ne => self.ne(other).into_py(py), | ||
// _ => py.NotImplemented(), | ||
// } | ||
// } | ||
|
||
// TODO: Implement | ||
// fn __hash__(&self) -> isize { | ||
// let mut h = DefaultHasher::new(); | ||
// self.hash(&mut h); | ||
// h.finish() as isize | ||
// } | ||
|
||
fn __str__(&self) -> String { | ||
self.to_string() | ||
} | ||
|
||
fn __repr__(&self) -> String { | ||
format!("{self:?}") | ||
} | ||
|
||
#[getter] | ||
#[pyo3(name = "instrument_id")] | ||
fn py_instrument_id(&self) -> InstrumentId { | ||
self.instrument_id | ||
} | ||
|
||
#[getter] | ||
#[pyo3(name = "deltas")] | ||
fn py_deltas(&self) -> Vec<OrderBookDelta> { | ||
// `OrderBookDelta` is `Copy` | ||
self.deltas.clone() | ||
} | ||
|
||
#[getter] | ||
#[pyo3(name = "flags")] | ||
fn py_flags(&self) -> u8 { | ||
self.flags | ||
} | ||
|
||
#[getter] | ||
#[pyo3(name = "sequence")] | ||
fn py_sequence(&self) -> u64 { | ||
self.sequence | ||
} | ||
|
||
#[getter] | ||
#[pyo3(name = "ts_event")] | ||
fn py_ts_event(&self) -> UnixNanos { | ||
self.ts_event | ||
} | ||
|
||
#[getter] | ||
#[pyo3(name = "ts_init")] | ||
fn py_ts_init(&self) -> UnixNanos { | ||
self.ts_init | ||
} | ||
|
||
#[staticmethod] | ||
#[pyo3(name = "fully_qualified_name")] | ||
fn py_fully_qualified_name() -> String { | ||
format!("{}:{}", PY_MODULE_MODEL, stringify!(OrderBookDeltas)) | ||
} | ||
|
||
// /// Creates a `PyCapsule` containing a raw pointer to a `Data::Delta` object. | ||
// /// | ||
// /// This function takes the current object (assumed to be of a type that can be represented as | ||
// /// `Data::Delta`), and encapsulates a raw pointer to it within a `PyCapsule`. | ||
// /// | ||
// /// # Safety | ||
// /// | ||
// /// This function is safe as long as the following conditions are met: | ||
// /// - The `Data::Delta` object pointed to by the capsule must remain valid for the lifetime of the capsule. | ||
// /// - The consumer of the capsule must ensure proper handling to avoid dereferencing a dangling pointer. | ||
// /// | ||
// /// # Panics | ||
// /// | ||
// /// The function will panic if the `PyCapsule` creation fails, which can occur if the | ||
// /// `Data::Delta` object cannot be converted into a raw pointer. | ||
// /// | ||
// #[pyo3(name = "as_pycapsule")] | ||
// fn py_as_pycapsule(&self, py: Python<'_>) -> PyObject { | ||
// data_to_pycapsule(py, Data::Delta(*self)) | ||
// } | ||
|
||
// TODO: Implement `Serializable` and the other methods can be added | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
pub mod bar; | ||
pub mod delta; | ||
pub mod deltas; | ||
pub mod depth; | ||
pub mod order; | ||
pub mod quote; | ||
|
Oops, something went wrong.