nautilus_model/ffi/data/
quote.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{
17    collections::hash_map::DefaultHasher,
18    ffi::c_char,
19    hash::{Hash, Hasher},
20};
21
22use nautilus_core::{UnixNanos, ffi::string::str_to_cstr};
23
24use crate::{
25    data::QuoteTick,
26    identifiers::InstrumentId,
27    types::{Price, Quantity},
28};
29
30#[unsafe(no_mangle)]
31#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
32pub extern "C" fn quote_tick_new(
33    instrument_id: InstrumentId,
34    bid_price: Price,
35    ask_price: Price,
36    bid_size: Quantity,
37    ask_size: Quantity,
38    ts_event: UnixNanos,
39    ts_init: UnixNanos,
40) -> QuoteTick {
41    QuoteTick::new(
42        instrument_id,
43        bid_price,
44        ask_price,
45        bid_size,
46        ask_size,
47        ts_event,
48        ts_init,
49    )
50}
51
52/// # Panics
53///
54/// Panics if any field of the two `QuoteTick` instances differs.
55#[unsafe(no_mangle)]
56pub extern "C" fn quote_tick_eq(lhs: &QuoteTick, rhs: &QuoteTick) -> u8 {
57    assert_eq!(lhs.ask_price, rhs.ask_price);
58    assert_eq!(lhs.ask_size, rhs.ask_size);
59    assert_eq!(lhs.bid_price, rhs.bid_price);
60    assert_eq!(lhs.bid_size, rhs.bid_size);
61    assert_eq!(lhs.ts_event, rhs.ts_event);
62    assert_eq!(lhs.ts_init, rhs.ts_init);
63    assert_eq!(lhs.instrument_id.symbol, rhs.instrument_id.symbol);
64    assert_eq!(lhs.instrument_id.venue, rhs.instrument_id.venue);
65    u8::from(lhs == rhs)
66}
67
68#[unsafe(no_mangle)]
69pub extern "C" fn quote_tick_hash(delta: &QuoteTick) -> u64 {
70    let mut hasher = DefaultHasher::new();
71    delta.hash(&mut hasher);
72    hasher.finish()
73}
74
75/// Returns a [`QuoteTick`] as a C string pointer.
76#[unsafe(no_mangle)]
77pub extern "C" fn quote_tick_to_cstr(quote: &QuoteTick) -> *const c_char {
78    str_to_cstr(&quote.to_string())
79}