Skip to content

Commit

Permalink
Use ustr serde implementation (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
twitu authored Aug 5, 2023
1 parent d7e156b commit 69b77d3
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ examples/backtest/notebooks/catalog
nautilus_trader/**/.gitignore
nautilus_trader/test_kit/mocks/.nautilus/
tests/test_data/catalog/
bench_data/

!tests/integration_tests/adapters/betfair/responses/*.log
4 changes: 2 additions & 2 deletions nautilus_core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nautilus_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ strum = { version = "0.25.0", features = ["derive"] }
thiserror = "1.0.44"
tracing = "0.1.37"
tokio = { version = "1.29.1", features = ["full"] }
ustr = "0.10.0"
ustr = { git = "https://github.com/anderslanglands/ustr", features = ["serde"] }
uuid = { version = "1.4.1", features = ["v4"] }

# dev-dependencies
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/common/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rename_variants = "ScreamingSnakeCase"

[export.rename]
"bool" = "uint8_t"
"SerializableUstr" = "char*"
"Ustr" = "char*"
"UnixNanos" = "uint64_t"
"TimedeltaNanos" = "int64_t"
"TimeEvent" = "TimeEvent_t"
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/common/cbindgen_cython.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rename_variants = "ScreamingSnakeCase"

[export.rename]
"bool" = "bint"
"SerializableUstr" = "char*"
"Ustr" = "char*"
"UnixNanos" = "uint64_t"
"TimedeltaNanos" = "int64_t"
"TimeEvent" = "TimeEvent_t"
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/common/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl TestClock {
.map(|event| {
let callback_py = self
.callbacks_py
.get(event.name.0.as_str())
.get(event.name.as_str())
.cloned()
.unwrap_or_else(|| {
// If callback_py is None, use the default_callback_py
Expand Down
9 changes: 4 additions & 5 deletions nautilus_core/common/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::{

use nautilus_core::{
correctness,
string::SerializableUstr,
time::{TimedeltaNanos, UnixNanos},
uuid::UUID4,
};
Expand All @@ -33,7 +32,7 @@ use ustr::Ustr;
/// Represents a time event occurring at the event timestamp.
pub struct TimeEvent {
/// The event name.
pub name: SerializableUstr,
pub name: Ustr,
/// The event ID.
pub event_id: UUID4,
/// The message category
Expand All @@ -48,7 +47,7 @@ impl TimeEvent {
correctness::valid_string(&name, "`TimeEvent` name");

TimeEvent {
name: SerializableUstr(Ustr::from(&name)),
name: Ustr::from(&name),
event_id,
ts_event,
ts_init,
Expand Down Expand Up @@ -151,7 +150,7 @@ impl TestTimer {

pub fn pop_event(&self, event_id: UUID4, ts_init: UnixNanos) -> TimeEvent {
TimeEvent {
name: SerializableUstr(Ustr::from(&self.name)),
name: Ustr::from(&self.name),
event_id,
ts_event: self.next_time_ns,
ts_init,
Expand Down Expand Up @@ -182,7 +181,7 @@ impl Iterator for TestTimer {
} else {
let item = (
TimeEvent {
name: SerializableUstr(Ustr::from(&self.name)),
name: Ustr::from(&self.name),
event_id: UUID4::new(),
ts_event: self.next_time_ns,
ts_init: self.next_time_ns,
Expand Down
1 change: 0 additions & 1 deletion nautilus_core/core/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ rename_variants = "ScreamingSnakeCase"
[export.rename]
"bool" = "uint8_t"
"Ustr" = "char*"
"SerializableUstr" = "char*"
"UnixNanos" = "uint64_t"
"TimedeltaNanos" = "int64_t"
"TimeEvent" = "TimeEvent_t"
Expand Down
1 change: 0 additions & 1 deletion nautilus_core/core/cbindgen_cython.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ rename_variants = "ScreamingSnakeCase"
[export.rename]
"bool" = "bint"
"Ustr" = "char*"
"SerializableUstr" = "char*"
"UnixNanos" = "uint64_t"
"TimedeltaNanos" = "int64_t"
"TimeEvent" = "TimeEvent_t"
Expand Down
46 changes: 1 addition & 45 deletions nautilus_core/core/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,12 @@

use std::{
ffi::{c_char, CStr, CString},
fmt, str,
str,
};

use pyo3::{ffi, types::PyString, FromPyPointer, Python};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use ustr::Ustr;

#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct SerializableUstr(pub Ustr);

impl Serialize for SerializableUstr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = String::new();
fmt::write(&mut s, format_args!("{}", self.0)).map_err(serde::ser::Error::custom)?;
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for SerializableUstr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(Self(Ustr::from(s.as_str())))
}
}

impl Default for SerializableUstr {
fn default() -> Self {
Self(Ustr::from(""))
}
}

impl fmt::Debug for SerializableUstr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}

impl fmt::Display for SerializableUstr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

/// Returns an owned string from a valid Python object pointer.
///
/// # Safety
Expand Down
1 change: 0 additions & 1 deletion nautilus_core/model/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ exclude = [
[export.rename]
"bool" = "uint8_t"
"Ustr" = "char*"
"SerializableUstr" = "char*"
"AccountId" = "AccountId_t"
"Bar" = "Bar_t"
"BarAggregation" = "uint8_t"
Expand Down
1 change: 0 additions & 1 deletion nautilus_core/model/cbindgen_cython.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ exclude = [
[export.rename]
"bool" = "bint"
"Ustr" = "char*"
"SerializableUstr" = "char*"
"AccountId" = "AccountId_t"
"Bar" = "Bar_t"
"BarAggregation" = "uint8_t"
Expand Down
11 changes: 6 additions & 5 deletions nautilus_core/model/src/events/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
use std::collections::HashMap;

use derive_builder::{self, Builder};
use nautilus_core::{string::SerializableUstr, time::UnixNanos, uuid::UUID4};
use nautilus_core::{time::UnixNanos, uuid::UUID4};
use serde::{Deserialize, Serialize};
use ustr::Ustr;

use crate::{
enums::{
Expand Down Expand Up @@ -141,7 +142,7 @@ pub struct OrderDenied {
pub strategy_id: StrategyId,
pub instrument_id: InstrumentId,
pub client_order_id: ClientOrderId,
pub reason: SerializableUstr,
pub reason: Ustr,
pub event_id: UUID4,
pub ts_event: UnixNanos,
pub ts_init: UnixNanos,
Expand Down Expand Up @@ -189,7 +190,7 @@ pub struct OrderRejected {
pub instrument_id: InstrumentId,
pub client_order_id: ClientOrderId,
pub account_id: AccountId,
pub reason: SerializableUstr,
pub reason: Ustr,
pub event_id: UUID4,
pub ts_event: UnixNanos,
pub ts_init: UnixNanos,
Expand Down Expand Up @@ -292,7 +293,7 @@ pub struct OrderModifyRejected {
pub client_order_id: ClientOrderId,
pub venue_order_id: Option<VenueOrderId>,
pub account_id: Option<AccountId>,
pub reason: SerializableUstr,
pub reason: Ustr,
pub event_id: UUID4,
pub ts_event: UnixNanos,
pub ts_init: UnixNanos,
Expand All @@ -310,7 +311,7 @@ pub struct OrderCancelRejected {
pub client_order_id: ClientOrderId,
pub venue_order_id: Option<VenueOrderId>,
pub account_id: Option<AccountId>,
pub reason: SerializableUstr,
pub reason: Ustr,
pub event_id: UUID4,
pub ts_event: UnixNanos,
pub ts_init: UnixNanos,
Expand Down
10 changes: 3 additions & 7 deletions nautilus_core/model/src/events/order_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

use std::ffi::c_char;

use nautilus_core::{
string::{cstr_to_ustr, SerializableUstr},
time::UnixNanos,
uuid::UUID4,
};
use nautilus_core::{string::cstr_to_ustr, time::UnixNanos, uuid::UUID4};

// use crate::types::price::Price;
// use crate::types::quantity::Quantity;
Expand Down Expand Up @@ -109,7 +105,7 @@ pub unsafe extern "C" fn order_denied_new(
strategy_id,
instrument_id,
client_order_id,
reason: SerializableUstr(cstr_to_ustr(reason_ptr)),
reason: cstr_to_ustr(reason_ptr),
event_id,
ts_event,
ts_init,
Expand Down Expand Up @@ -188,7 +184,7 @@ pub unsafe extern "C" fn order_rejected_new(
instrument_id,
client_order_id,
account_id,
reason: SerializableUstr(cstr_to_ustr(reason_ptr)),
reason: cstr_to_ustr(reason_ptr),
event_id,
ts_event,
ts_init,
Expand Down

0 comments on commit 69b77d3

Please sign in to comment.