From 601b7f862e59ab57744b1ec6ee6dd4b0051e0268 Mon Sep 17 00:00:00 2001 From: Olivier Schyns Date: Wed, 4 Sep 2024 16:07:48 +0200 Subject: [PATCH] feat: Removed Default trait from the example implementations of Adaptor Signed-off-by: Olivier Schyns --- prosa/src/core/adaptor.rs | 2 +- prosa/src/core/proc.rs | 13 ++++++------- prosa/src/inj/adaptor.rs | 16 +++++++++------- prosa/src/inj/proc.rs | 7 +++---- prosa/src/lib.rs | 6 +++--- prosa/src/stub/adaptor.rs | 29 ++++++++++++----------------- prosa/src/stub/proc.rs | 5 ++--- 7 files changed, 36 insertions(+), 42 deletions(-) diff --git a/prosa/src/core/adaptor.rs b/prosa/src/core/adaptor.rs index 259a215..15897e7 100644 --- a/prosa/src/core/adaptor.rs +++ b/prosa/src/core/adaptor.rs @@ -36,7 +36,7 @@ pub use prosa_macros::Adaptor; /// ``` /// use prosa::core::adaptor::Adaptor; /// -/// #[derive(Default, Adaptor)] +/// #[derive(Adaptor)] /// struct MyAdaptor {} /// ``` pub trait Adaptor { diff --git a/prosa/src/core/proc.rs b/prosa/src/core/proc.rs index 9cd3ca5..5369076 100644 --- a/prosa/src/core/proc.rs +++ b/prosa/src/core/proc.rs @@ -27,12 +27,12 @@ //! { //! /// Method called when the processor spawns //! /// This method is called only once so the processing will be thread safe -//! fn init(&mut self, proc: &MyProc) -> Result<(), Box>; +//! fn new(proc: &MyProc) -> Result> where Self: Sized; //! /// Method to process incomming requests //! fn process_request(&self, service_name: &str, request: &M) -> M; //! } //! -//! #[derive(Default, Adaptor)] +//! #[derive(Adaptor)] //! pub struct MyAdaptor { //! // your adaptor vars here //! } @@ -48,9 +48,9 @@ //! + prosa_utils::msg::tvf::Tvf //! + std::default::Default, //! { -//! fn init(&mut self, proc: &MyProc) -> Result<(), Box> { +//! fn new(proc: &MyProc) -> Result> { //! // Init your adaptor from processor parameters -//! Ok(()) +//! Ok(Self {}) //! } //! //! fn process_request(&self, service_name: &str, request: &M) -> M { @@ -98,12 +98,11 @@ //! #[proc] //! impl Proc for MyProc //! where -//! A: Default + Adaptor + MyAdaptorTrait + std::marker::Send + std::marker::Sync, +//! A: Adaptor + MyAdaptorTrait + std::marker::Send + std::marker::Sync, //! { //! async fn internal_run(&mut self, name: String) -> Result<(), Box> { //! // Initiate an adaptor for the stub processor -//! let mut adaptor = A::default(); -//! adaptor.init(self)?; +//! let mut adaptor = A::new(self)?; //! //! // Declare the processor //! self.proc.add_proc().await?; diff --git a/prosa/src/inj/adaptor.rs b/prosa/src/inj/adaptor.rs index ca4385e..1612e4d 100644 --- a/prosa/src/inj/adaptor.rs +++ b/prosa/src/inj/adaptor.rs @@ -14,7 +14,7 @@ extern crate self as prosa; /// use prosa::core::adaptor::Adaptor; /// use prosa::inj::adaptor::InjAdaptor; /// -/// #[derive(Default, Adaptor)] +/// #[derive(Adaptor)] /// pub struct MyInjAdaptor { } /// /// impl InjAdaptor for MyInjAdaptor @@ -28,8 +28,8 @@ extern crate self as prosa; /// + prosa_utils::msg::tvf::Tvf /// + std::default::Default, /// { -/// fn init(&mut self, _proc: &InjProc) -> Result<(), Box> { -/// Ok(()) +/// fn new(_proc: &InjProc) -> Result> { +/// Ok(Self {}) /// } /// fn build_transaction(&mut self) -> M { /// let mut msg = M::default(); @@ -51,7 +51,9 @@ where { /// Method called when the processor spawns /// This method is called only once so the processing will be thread safe - fn init(&mut self, proc: &InjProc) -> Result<(), Box>; + fn new(proc: &InjProc) -> Result> + where + Self: Sized; /// Method to build a transaction to inject fn build_transaction(&mut self) -> M; /// Method to process transaction response of the injection (to check the return code for example) @@ -67,7 +69,7 @@ where } /// Dummy adaptor for the inj processor. Use to send a very basic message with _DUMMY_ in it. -#[derive(Default, Adaptor)] +#[derive(Adaptor)] pub struct InjDummyAdaptor {} impl InjAdaptor for InjDummyAdaptor @@ -81,8 +83,8 @@ where + prosa_utils::msg::tvf::Tvf + std::default::Default, { - fn init(&mut self, _proc: &InjProc) -> Result<(), Box> { - Ok(()) + fn new(_proc: &InjProc) -> Result> { + Ok(Self {}) } fn build_transaction(&mut self) -> M { diff --git a/prosa/src/inj/proc.rs b/prosa/src/inj/proc.rs index 21111f0..6dfd081 100644 --- a/prosa/src/inj/proc.rs +++ b/prosa/src/inj/proc.rs @@ -133,7 +133,7 @@ impl InjProc { meter_trans_duration: &Histogram, ) -> Result<(), Box> where - A: Default + Adaptor + InjAdaptor + std::marker::Send + std::marker::Sync, + A: Adaptor + InjAdaptor + std::marker::Send + std::marker::Sync, { match msg { InternalMsg::Request(msg) => panic!( @@ -181,12 +181,11 @@ impl InjProc { #[proc] impl Proc for InjProc where - A: Default + Adaptor + InjAdaptor + std::marker::Send + std::marker::Sync, + A: Adaptor + InjAdaptor + std::marker::Send + std::marker::Sync, { async fn internal_run(&mut self, name: String) -> Result<(), Box> { // Initiate an adaptor for the inj processor - let mut adaptor = A::default(); - adaptor.init(self)?; + let mut adaptor = A::new(self)?; // meter let meter = self.proc.meter(name.clone()); diff --git a/prosa/src/lib.rs b/prosa/src/lib.rs index f4911e4..19a7d51 100644 --- a/prosa/src/lib.rs +++ b/prosa/src/lib.rs @@ -67,7 +67,7 @@ mod tests { } } - #[derive(Default, Adaptor)] + #[derive(Adaptor)] struct TestStubAdaptor { msg_count: u32, } @@ -83,8 +83,8 @@ mod tests { + prosa_utils::msg::tvf::Tvf + std::default::Default, { - fn init(&mut self, _proc: &StubProc) -> Result<(), Box> { - Ok(()) + fn new(_proc: &StubProc) -> Result> { + Ok(Self { msg_count: 0 }) } fn process_request(&mut self, _service_name: &str, request: &M) -> M { diff --git a/prosa/src/stub/adaptor.rs b/prosa/src/stub/adaptor.rs index b68b10d..7c3b0b4 100644 --- a/prosa/src/stub/adaptor.rs +++ b/prosa/src/stub/adaptor.rs @@ -6,8 +6,7 @@ use super::proc::StubProc; extern crate self as prosa; -use opentelemetry::metrics::{Meter, MeterProvider as _}; -use opentelemetry_sdk::metrics::MeterProvider; +use opentelemetry::metrics::Meter; /// Adaptator trait for the stub processor /// @@ -17,7 +16,7 @@ use opentelemetry_sdk::metrics::MeterProvider; /// use prosa::core::adaptor::Adaptor; /// use prosa::stub::adaptor::StubAdaptor; /// -/// #[derive(Default, Adaptor)] +/// #[derive(Adaptor)] /// pub struct MyStubAdaptor { } /// /// impl StubAdaptor for MyStubAdaptor @@ -31,8 +30,8 @@ use opentelemetry_sdk::metrics::MeterProvider; /// + prosa_utils::msg::tvf::Tvf /// + std::default::Default, /// { -/// fn init(&mut self, _proc: &StubProc) -> Result<(), Box> { -/// Ok(()) +/// fn new(_proc: &StubProc) -> Result> { +/// Ok(Self {}) /// } /// fn process_request(&mut self, service_name: &str, request: &M) -> M { /// let mut msg = request.clone(); @@ -54,7 +53,9 @@ where { /// Method called when the processor spawns /// This method is called only once so the processing will be thread safe - fn init(&mut self, proc: &StubProc) -> Result<(), Box>; + fn new(proc: &StubProc) -> Result> + where + Self: Sized; /// Method to process incomming requests fn process_request(&mut self, service_name: &str, request: &M) -> M; } @@ -62,6 +63,7 @@ where /// Parot adaptor for the stub processor. Use to respond to a request with the same message #[derive(Adaptor)] pub struct StubParotAdaptor { + #[allow(unused)] meter: Meter, } @@ -76,20 +78,13 @@ where + prosa_utils::msg::tvf::Tvf + std::default::Default, { - fn init(&mut self, proc: &StubProc) -> Result<(), Box> { - self.meter = proc.get_proc_param().meter("stub_adaptor"); - Ok(()) + fn new(proc: &StubProc) -> Result> { + Ok(Self { + meter: proc.get_proc_param().meter("stub_adaptor"), + }) } fn process_request(&mut self, _service_name: &str, request: &M) -> M { request.clone() } } - -impl Default for StubParotAdaptor { - fn default() -> Self { - Self { - meter: MeterProvider::default().meter("prosa_parot"), - } - } -} diff --git a/prosa/src/stub/proc.rs b/prosa/src/stub/proc.rs index 2ce5670..0c4ed65 100644 --- a/prosa/src/stub/proc.rs +++ b/prosa/src/stub/proc.rs @@ -70,12 +70,11 @@ pub struct StubProc {} #[proc] impl Proc for StubProc where - A: Default + Adaptor + StubAdaptor + std::marker::Send + std::marker::Sync, + A: Adaptor + StubAdaptor + std::marker::Send + std::marker::Sync, { async fn internal_run(&mut self, name: String) -> Result<(), Box> { // Initiate an adaptor for the stub processor - let mut adaptor = A::default(); - adaptor.init(self)?; + let mut adaptor = A::new(self)?; // Declare the processor self.proc.add_proc().await?;