From dad768e906f250fc3a27efa3b1005bca9529479b Mon Sep 17 00:00:00 2001 From: Jan-Niklas Burfeind Date: Thu, 27 Jun 2024 16:12:35 +0200 Subject: [PATCH] refactor: Drop manual default implementations in favor of the corresponding macro. (cherry picked from commit 967d100042c363b1788fe9439f74901cabd83363) --- lib/src/server/session.rs | 10 +----- lib/src/server/session_diagnostics.rs | 11 +------ lib/src/types/service_types/impls.rs | 33 ++----------------- .../server_diagnostics_summary_data_type.rs | 2 +- .../service_counter_data_type.rs | 2 +- lib/src/types/variant.rs | 9 ++--- tools/schema/types.js | 2 +- 7 files changed, 9 insertions(+), 60 deletions(-) diff --git a/lib/src/server/session.rs b/lib/src/server/session.rs index 5be9e38b4..735328182 100644 --- a/lib/src/server/session.rs +++ b/lib/src/server/session.rs @@ -51,20 +51,12 @@ pub enum ServerUserIdentityToken { Invalid(ExtensionObject), } +#[derive(Default)] pub struct SessionManager { pub sessions: HashMap>>, pub sessions_terminated: bool, } -impl Default for SessionManager { - fn default() -> Self { - Self { - sessions: HashMap::new(), - sessions_terminated: false, - } - } -} - impl SessionManager { pub fn len(&self) -> usize { self.sessions.len() diff --git a/lib/src/server/session_diagnostics.rs b/lib/src/server/session_diagnostics.rs index 0dd2f7113..92f7cb4a7 100644 --- a/lib/src/server/session_diagnostics.rs +++ b/lib/src/server/session_diagnostics.rs @@ -9,22 +9,13 @@ use super::{ /// This object tracks session diagnostics for exposure through the address space +#[derive(Default)] pub(crate) struct SessionDiagnostics { total_request_count: u32, unauthorized_request_count: u32, service_counters: HashMap<&'static str, ServiceCounterDataType>, } -impl Default for SessionDiagnostics { - fn default() -> Self { - Self { - total_request_count: 0, - unauthorized_request_count: 0, - service_counters: HashMap::new(), - } - } -} - impl SessionDiagnostics { /// Registers a session object pub(crate) fn register_session(&self, session: &Session, address_space: &mut AddressSpace) { diff --git a/lib/src/types/service_types/impls.rs b/lib/src/types/service_types/impls.rs index 86eb24d41..0e2ec1679 100644 --- a/lib/src/types/service_types/impls.rs +++ b/lib/src/types/service_types/impls.rs @@ -19,9 +19,8 @@ use crate::types::{ enums::DeadbandType, AnonymousIdentityToken, ApplicationDescription, ApplicationType, Argument, CallMethodRequest, DataChangeFilter, DataChangeTrigger, DataSetFieldFlags, EndpointDescription, MessageSecurityMode, MonitoredItemCreateRequest, MonitoringMode, - MonitoringParameters, ReadValueId, ServerDiagnosticsSummaryDataType, - ServiceCounterDataType, ServiceFault, SignatureData, UserNameIdentityToken, - UserTokenPolicy, UserTokenType, + MonitoringParameters, ReadValueId, ServiceCounterDataType, ServiceFault, SignatureData, + UserNameIdentityToken, UserTokenPolicy, UserTokenType, }, status_codes::StatusCode, string::UAString, @@ -346,25 +345,6 @@ impl Into for (NodeId, NodeId, Option>) { } } -impl Default for ServerDiagnosticsSummaryDataType { - fn default() -> Self { - ServerDiagnosticsSummaryDataType { - server_view_count: 0, - current_session_count: 0, - cumulated_session_count: 0, - security_rejected_session_count: 0, - rejected_session_count: 0, - session_timeout_count: 0, - session_abort_count: 0, - current_subscription_count: 0, - cumulated_subscription_count: 0, - publishing_interval_count: 0, - security_rejected_requests_count: 0, - rejected_requests_count: 0, - } - } -} - impl<'a> From<&'a str> for EndpointDescription { fn from(v: &'a str) -> Self { EndpointDescription::from(( @@ -477,15 +457,6 @@ impl From<(&str, DataTypeId)> for Argument { } } -impl Default for ServiceCounterDataType { - fn default() -> Self { - Self { - total_count: 0, - error_count: 0, - } - } -} - impl ServiceCounterDataType { pub fn success(&mut self) { self.total_count += 1; diff --git a/lib/src/types/service_types/server_diagnostics_summary_data_type.rs b/lib/src/types/service_types/server_diagnostics_summary_data_type.rs index 0c43b4d0a..cab9089b4 100644 --- a/lib/src/types/service_types/server_diagnostics_summary_data_type.rs +++ b/lib/src/types/service_types/server_diagnostics_summary_data_type.rs @@ -12,7 +12,7 @@ use crate::types::{ }; use std::io::{Read, Write}; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] #[serde(rename_all = "PascalCase")] pub struct ServerDiagnosticsSummaryDataType { pub server_view_count: u32, diff --git a/lib/src/types/service_types/service_counter_data_type.rs b/lib/src/types/service_types/service_counter_data_type.rs index 17ce703ce..ae34bcea7 100644 --- a/lib/src/types/service_types/service_counter_data_type.rs +++ b/lib/src/types/service_types/service_counter_data_type.rs @@ -12,7 +12,7 @@ use crate::types::{ }; use std::io::{Read, Write}; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct ServiceCounterDataType { pub total_count: u32, pub error_count: u32, diff --git a/lib/src/types/variant.rs b/lib/src/types/variant.rs index f4b4ac48e..3318219d1 100644 --- a/lib/src/types/variant.rs +++ b/lib/src/types/variant.rs @@ -36,9 +36,10 @@ use crate::types::{ /// /// As variants may be passed around a lot on the stack, Boxes are used for more complex types to /// keep the size of this type down a bit, especially when used in arrays. -#[derive(PartialEq, Debug, Clone)] +#[derive(PartialEq, Debug, Clone, Default)] pub enum Variant { /// Empty type has no value. It is equivalent to a Null value (part 6 5.1.6) + #[default] Empty, /// Boolean Boolean(bool), @@ -669,12 +670,6 @@ impl BinaryEncoder for Variant { } } -impl Default for Variant { - fn default() -> Self { - Variant::Empty - } -} - /// This implementation is mainly for debugging / convenience purposes, to eliminate some of the /// noise in common types from using the Debug trait. impl fmt::Display for Variant { diff --git a/tools/schema/types.js b/tools/schema/types.js index faa1e130d..455ebe683 100644 --- a/tools/schema/types.js +++ b/tools/schema/types.js @@ -60,7 +60,7 @@ function makeImportLookupMap(import_map) { } // Types that will be marked as Default constructable -const DEFAULT_TYPES = ["ReadValueId", ]; +const DEFAULT_TYPES = ["ReadValueId", "ServerDiagnosticsSummaryDataType", "ServiceCounterDataType"]; // Types that will be marked as JSON serializable. Serialization is for pubsub, and debugging purposes const JSON_SERIALIZED_TYPES = [