diff --git a/nautilus_core/core/src/python/casing.rs b/nautilus_core/core/src/python/casing.rs index 62cad36afa19..77c10ab675e7 100644 --- a/nautilus_core/core/src/python/casing.rs +++ b/nautilus_core/core/src/python/casing.rs @@ -16,6 +16,7 @@ use heck::ToSnakeCase; use pyo3::prelude::*; +#[must_use] #[pyfunction(name = "convert_to_snake_case")] pub fn py_convert_to_snake_case(s: String) -> String { s.to_snake_case() diff --git a/nautilus_core/core/src/python/datetime.rs b/nautilus_core/core/src/python/datetime.rs index 843b7282732c..dba1c2077c8c 100644 --- a/nautilus_core/core/src/python/datetime.rs +++ b/nautilus_core/core/src/python/datetime.rs @@ -20,36 +20,43 @@ use crate::datetime::{ secs_to_millis, secs_to_nanos, unix_nanos_to_iso8601, }; +#[must_use] #[pyfunction(name = "secs_to_nanos")] pub fn py_secs_to_nanos(secs: f64) -> u64 { secs_to_nanos(secs) } +#[must_use] #[pyfunction(name = "secs_to_millis")] pub fn py_secs_to_millis(secs: f64) -> u64 { secs_to_millis(secs) } +#[must_use] #[pyfunction(name = "millis_to_nanos")] pub fn py_millis_to_nanos(millis: f64) -> u64 { millis_to_nanos(millis) } +#[must_use] #[pyfunction(name = "micros_to_nanos")] pub fn py_micros_to_nanos(micros: f64) -> u64 { micros_to_nanos(micros) } +#[must_use] #[pyfunction(name = "nanos_to_secs")] pub fn py_nanos_to_secs(nanos: u64) -> f64 { nanos_to_secs(nanos) } +#[must_use] #[pyfunction(name = "nanos_to_millis")] pub fn py_nanos_to_millis(nanos: u64) -> u64 { nanos_to_millis(nanos) } +#[must_use] #[pyfunction(name = "nanos_to_micros")] pub fn py_nanos_to_micros(nanos: u64) -> u64 { nanos_to_micros(nanos) diff --git a/nautilus_core/indicators/src/average/mod.rs b/nautilus_core/indicators/src/average/mod.rs index 8623cc669d84..fc3c3e6a8ef4 100644 --- a/nautilus_core/indicators/src/average/mod.rs +++ b/nautilus_core/indicators/src/average/mod.rs @@ -55,6 +55,7 @@ pub enum MovingAverageType { pub struct MovingAverageFactory; impl MovingAverageFactory { + #[must_use] pub fn create( moving_average_type: MovingAverageType, period: usize, diff --git a/nautilus_core/indicators/src/average/wma.rs b/nautilus_core/indicators/src/average/wma.rs index 4bff6366cad5..03aa1acaf6d9 100644 --- a/nautilus_core/indicators/src/average/wma.rs +++ b/nautilus_core/indicators/src/average/wma.rs @@ -69,11 +69,11 @@ impl WeightedMovingAverage { fn weighted_average(&self) -> f64 { let mut sum = 0.0; let mut weight_sum = 0.0; - let reverse_weights: Vec = self.weights.iter().cloned().rev().collect(); + let reverse_weights: Vec = self.weights.iter().copied().rev().collect(); for (index, input) in self.inputs.iter().rev().enumerate() { let weight = reverse_weights.get(index).unwrap(); sum += input * weight; - weight_sum += weight + weight_sum += weight; } sum / weight_sum } @@ -152,7 +152,7 @@ mod tests { #[rstest] fn test_wma_initialized(indicator_wma_10: WeightedMovingAverage) { - let display_str = format!("{}", indicator_wma_10); + let display_str = format!("{indicator_wma_10}"); assert_eq!( display_str, "WeightedMovingAverage(10,[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])" @@ -196,7 +196,7 @@ mod tests { fn test_value_with_two_inputs(mut indicator_wma_10: WeightedMovingAverage) { indicator_wma_10.update_raw(1.0); indicator_wma_10.update_raw(2.0); - let result = (2.0 * 1.0 + 1.0 * 0.9) / 1.9; + let result = 2.0f64.mul_add(1.0, 1.0 * 0.9) / 1.9; assert_eq!(indicator_wma_10.value, result); } @@ -205,14 +205,14 @@ mod tests { indicator_wma_10.update_raw(1.0); indicator_wma_10.update_raw(2.0); indicator_wma_10.update_raw(3.0); - let result = (3.0 * 1.0 + 2.0 * 0.9 + 1.0 * 0.8) / (1.0 + 0.9 + 0.8); + let result = 1.0f64.mul_add(0.8, 3.0f64.mul_add(1.0, 2.0 * 0.9)) / (1.0 + 0.9 + 0.8); assert_eq!(indicator_wma_10.value, result); } #[rstest] fn test_value_expected_with_exact_period(mut indicator_wma_10: WeightedMovingAverage) { for i in 1..11 { - indicator_wma_10.update_raw(i as f64); + indicator_wma_10.update_raw(f64::from(i)); } assert_eq!(indicator_wma_10.value, 7.0); } @@ -220,9 +220,9 @@ mod tests { #[rstest] fn test_value_expected_with_more_inputs(mut indicator_wma_10: WeightedMovingAverage) { for i in 1..=11 { - indicator_wma_10.update_raw(i as f64); + indicator_wma_10.update_raw(f64::from(i)); } - assert_eq!(indicator_wma_10.value(), 8.0000000000000018); + assert_eq!(indicator_wma_10.value(), 8.000_000_000_000_002); } #[rstest] diff --git a/nautilus_core/indicators/src/momentum/rsi.rs b/nautilus_core/indicators/src/momentum/rsi.rs index 48621c0b76df..0dba6ac89abf 100644 --- a/nautilus_core/indicators/src/momentum/rsi.rs +++ b/nautilus_core/indicators/src/momentum/rsi.rs @@ -104,7 +104,7 @@ impl RelativeStrengthIndex { pub fn update_raw(&mut self, value: f64) { if !self._has_inputs { self._last_value = value; - self._has_inputs = true + self._has_inputs = true; } let gain = value - self._last_value; if gain > 0.0 { @@ -153,40 +153,40 @@ mod tests { #[rstest] fn test_rsi_initialized(rsi_10: RelativeStrengthIndex) { - let display_str = format!("{}", rsi_10); + let display_str = format!("{rsi_10}"); assert_eq!(display_str, "RelativeStrengthIndex(10,EXPONENTIAL)"); assert_eq!(rsi_10.period, 10); - assert_eq!(rsi_10.is_initialized, false) + assert!(!rsi_10.is_initialized); } #[rstest] fn test_initialized_with_required_inputs_returns_true(mut rsi_10: RelativeStrengthIndex) { for i in 0..12 { - rsi_10.update_raw(i as f64); + rsi_10.update_raw(f64::from(i)); } - assert_eq!(rsi_10.is_initialized, true) + assert!(rsi_10.is_initialized); } #[rstest] fn test_value_with_one_input_returns_expected_value(mut rsi_10: RelativeStrengthIndex) { rsi_10.update_raw(1.0); - assert_eq!(rsi_10.value, 1.0) + assert_eq!(rsi_10.value, 1.0); } #[rstest] fn test_value_all_higher_inputs_returns_expected_value(mut rsi_10: RelativeStrengthIndex) { for i in 1..4 { - rsi_10.update_raw(i as f64); + rsi_10.update_raw(f64::from(i)); } - assert_eq!(rsi_10.value, 1.0) + assert_eq!(rsi_10.value, 1.0); } #[rstest] fn test_value_with_all_lower_inputs_returns_expected_value(mut rsi_10: RelativeStrengthIndex) { for i in (1..4).rev() { - rsi_10.update_raw(i as f64); + rsi_10.update_raw(f64::from(i)); } - assert_eq!(rsi_10.value, 0.0) + assert_eq!(rsi_10.value, 0.0); } #[rstest] @@ -198,7 +198,7 @@ mod tests { rsi_10.update_raw(7.0); rsi_10.update_raw(6.0); - assert_eq!(rsi_10.value, 0.6837363325825265) + assert_eq!(rsi_10.value, 0.683_736_332_582_526_5); } #[rstest] @@ -212,7 +212,7 @@ mod tests { rsi_10.update_raw(6.0); rsi_10.update_raw(7.0); - assert_eq!(rsi_10.value, 0.7615344667662725); + assert_eq!(rsi_10.value, 0.761_534_466_766_272_5); } #[rstest] @@ -220,28 +220,28 @@ mod tests { rsi_10.update_raw(1.0); rsi_10.update_raw(2.0); rsi_10.reset(); - assert_eq!(rsi_10.is_initialized(), false); - assert_eq!(rsi_10.count, 0) + assert!(!rsi_10.is_initialized()); + assert_eq!(rsi_10.count, 0); } #[rstest] fn test_handle_quote_tick(mut rsi_10: RelativeStrengthIndex, quote_tick: QuoteTick) { rsi_10.handle_quote_tick("e_tick); assert_eq!(rsi_10.count, 1); - assert_eq!(rsi_10.value, 1.0) + assert_eq!(rsi_10.value, 1.0); } #[rstest] fn test_handle_trade_tick(mut rsi_10: RelativeStrengthIndex, trade_tick: TradeTick) { rsi_10.handle_trade_tick(&trade_tick); assert_eq!(rsi_10.count, 1); - assert_eq!(rsi_10.value, 1.0) + assert_eq!(rsi_10.value, 1.0); } #[rstest] fn test_handle_bar(mut rsi_10: RelativeStrengthIndex, bar_ethusdt_binance_minute_bid: Bar) { rsi_10.handle_bar(&bar_ethusdt_binance_minute_bid); assert_eq!(rsi_10.count, 1); - assert_eq!(rsi_10.value, 1.0) + assert_eq!(rsi_10.value, 1.0); } } diff --git a/nautilus_core/network/src/websocket.rs b/nautilus_core/network/src/websocket.rs index 1b2098ff2745..2c5189320b51 100644 --- a/nautilus_core/network/src/websocket.rs +++ b/nautilus_core/network/src/websocket.rs @@ -121,11 +121,11 @@ impl WebSocketClientInner { let mut request = url.into_client_request()?; let req_headers = request.headers_mut(); - headers.into_iter().for_each(|(key, val)| { + for (key, val) in headers { let header_value = HeaderValue::from_str(&val).unwrap(); let header_name = HeaderName::from_str(&key).unwrap(); req_headers.insert(header_name, header_value); - }); + } connect_async(request).await.map(|resp| resp.0.split()) } diff --git a/nautilus_core/persistence/src/backend/kmerge_batch.rs b/nautilus_core/persistence/src/backend/kmerge_batch.rs index 837c6aa2693a..ff3fe562ff6f 100644 --- a/nautilus_core/persistence/src/backend/kmerge_batch.rs +++ b/nautilus_core/persistence/src/backend/kmerge_batch.rs @@ -46,7 +46,7 @@ impl EagerStream { .await; }); - EagerStream { rx, task, runtime } + Self { rx, task, runtime } } } @@ -85,7 +85,7 @@ where match iter.next() { Some(mut batch) => match batch.next() { Some(item) => { - break Some(ElementBatchIter { item, batch, iter }); + break Some(Self { item, batch, iter }); } None => continue, }, @@ -276,7 +276,7 @@ mod tests { let mut vec: Vec = Arbitrary::arbitrary(g); // Sort the vector - vec.sort(); + vec.sort_unstable(); // Recreate nested Vec structure by splitting the flattened_sorted_vec into sorted chunks let mut nested_sorted_vec = Vec::new(); @@ -292,7 +292,7 @@ mod tests { } // Wrap the sorted nested vector in the SortedNestedVecU64 struct - SortedNestedVec(nested_sorted_vec) + Self(nested_sorted_vec) } // Optionally, implement the `shrink` method if you want to shrink the generated data on test failures @@ -306,18 +306,17 @@ mod tests { let mut kmerge: KMerge<_, u64, _> = KMerge::new(OrdComparator); let copy_data = all_data.clone(); - copy_data.into_iter().for_each(|stream| { - let input = stream.0.into_iter().map(|batch| batch.into_iter()); + for stream in copy_data.into_iter() { + let input = stream.0.into_iter().map(std::iter::IntoIterator::into_iter); kmerge.push_iter(input); - }); + } let merged_data: Vec = kmerge.collect(); let mut sorted_data: Vec = all_data .into_iter() - .map(|stream| stream.0.into_iter().flatten()) - .flatten() + .flat_map(|stream| stream.0.into_iter().flatten()) .collect(); - sorted_data.sort(); + sorted_data.sort_unstable(); merged_data.len() == sorted_data.len() && merged_data.eq(&sorted_data) } diff --git a/nautilus_core/persistence/src/backend/session.rs b/nautilus_core/persistence/src/backend/session.rs index a02542468d1c..214f1d6b00e2 100644 --- a/nautilus_core/persistence/src/backend/session.rs +++ b/nautilus_core/persistence/src/backend/session.rs @@ -95,14 +95,14 @@ impl DataBackendSession { /// Query a file for its records. the caller must specify `T` to indicate /// the kind of data expected from this query. /// - /// table_name: Logical table_name assigned to this file. Queries to this file should address the + /// `table_name`: Logical `table_name` assigned to this file. Queries to this file should address the /// file by its table name. - /// file_path: Path to file - /// sql_query: A custom sql query to retrieve records from file. If no query is provided a default - /// query "SELECT * FROM " is run. + /// `file_path`: Path to file + /// `sql_query`: A custom sql query to retrieve records from file. If no query is provided a default + /// query "SELECT * FROM <`table_name`>" is run. /// /// # Safety - /// The file data must be ordered by the ts_init in ascending order for this + /// The file data must be ordered by the `ts_init` in ascending order for this /// to work correctly. pub fn add_file( &mut self,