Skip to content

Commit

Permalink
feat: Removed Default trait from the example implementations of Adaptor
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Schyns <[email protected]>
  • Loading branch information
oschijns authored and Olivier Schyns committed Sep 30, 2024
1 parent bc6598e commit 601b7f8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 42 deletions.
2 changes: 1 addition & 1 deletion prosa/src/core/adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use prosa_macros::Adaptor;
/// ```
/// use prosa::core::adaptor::Adaptor;
///
/// #[derive(Default, Adaptor)]
/// #[derive(Adaptor)]
/// struct MyAdaptor {}
/// ```
pub trait Adaptor {
Expand Down
13 changes: 6 additions & 7 deletions prosa/src/core/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M>) -> Result<(), Box<dyn Error>>;
//! fn new(proc: &MyProc<M>) -> Result<Self, Box<dyn Error>> 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
//! }
Expand All @@ -48,9 +48,9 @@
//! + prosa_utils::msg::tvf::Tvf
//! + std::default::Default,
//! {
//! fn init(&mut self, proc: &MyProc<M>) -> Result<(), Box<dyn Error>> {
//! fn new(proc: &MyProc<M>) -> Result<Self, Box<dyn Error>> {
//! // Init your adaptor from processor parameters
//! Ok(())
//! Ok(Self {})
//! }
//!
//! fn process_request(&self, service_name: &str, request: &M) -> M {
Expand Down Expand Up @@ -98,12 +98,11 @@
//! #[proc]
//! impl<A> Proc<A> for MyProc
//! where
//! A: Default + Adaptor + MyAdaptorTrait<M> + std::marker::Send + std::marker::Sync,
//! A: Adaptor + MyAdaptorTrait<M> + std::marker::Send + std::marker::Sync,
//! {
//! async fn internal_run(&mut self, name: String) -> Result<(), Box<dyn std::error::Error>> {
//! // 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?;
Expand Down
16 changes: 9 additions & 7 deletions prosa/src/inj/adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M> InjAdaptor<M> for MyInjAdaptor
Expand All @@ -28,8 +28,8 @@ extern crate self as prosa;
/// + prosa_utils::msg::tvf::Tvf
/// + std::default::Default,
/// {
/// fn init(&mut self, _proc: &InjProc<M>) -> Result<(), Box<dyn std::error::Error>> {
/// Ok(())
/// fn new(_proc: &InjProc<M>) -> Result<Self, Box<dyn std::error::Error>> {
/// Ok(Self {})
/// }
/// fn build_transaction(&mut self) -> M {
/// let mut msg = M::default();
Expand All @@ -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<M>) -> Result<(), Box<dyn Error>>;
fn new(proc: &InjProc<M>) -> Result<Self, Box<dyn Error>>
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)
Expand All @@ -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<M> InjAdaptor<M> for InjDummyAdaptor
Expand All @@ -81,8 +83,8 @@ where
+ prosa_utils::msg::tvf::Tvf
+ std::default::Default,
{
fn init(&mut self, _proc: &InjProc<M>) -> Result<(), Box<dyn Error>> {
Ok(())
fn new(_proc: &InjProc<M>) -> Result<Self, Box<dyn Error>> {
Ok(Self {})
}

fn build_transaction(&mut self) -> M {
Expand Down
7 changes: 3 additions & 4 deletions prosa/src/inj/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl InjProc {
meter_trans_duration: &Histogram<f64>,
) -> Result<(), Box<dyn std::error::Error>>
where
A: Default + Adaptor + InjAdaptor<M> + std::marker::Send + std::marker::Sync,
A: Adaptor + InjAdaptor<M> + std::marker::Send + std::marker::Sync,
{
match msg {
InternalMsg::Request(msg) => panic!(
Expand Down Expand Up @@ -181,12 +181,11 @@ impl InjProc {
#[proc]
impl<A> Proc<A> for InjProc
where
A: Default + Adaptor + InjAdaptor<M> + std::marker::Send + std::marker::Sync,
A: Adaptor + InjAdaptor<M> + std::marker::Send + std::marker::Sync,
{
async fn internal_run(&mut self, name: String) -> Result<(), Box<dyn std::error::Error>> {
// 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());
Expand Down
6 changes: 3 additions & 3 deletions prosa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mod tests {
}
}

#[derive(Default, Adaptor)]
#[derive(Adaptor)]
struct TestStubAdaptor {
msg_count: u32,
}
Expand All @@ -83,8 +83,8 @@ mod tests {
+ prosa_utils::msg::tvf::Tvf
+ std::default::Default,
{
fn init(&mut self, _proc: &StubProc<M>) -> Result<(), Box<dyn Error>> {
Ok(())
fn new(_proc: &StubProc<M>) -> Result<Self, Box<dyn Error>> {
Ok(Self { msg_count: 0 })
}

fn process_request(&mut self, _service_name: &str, request: &M) -> M {
Expand Down
29 changes: 12 additions & 17 deletions prosa/src/stub/adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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<M> StubAdaptor<M> for MyStubAdaptor
Expand All @@ -31,8 +30,8 @@ use opentelemetry_sdk::metrics::MeterProvider;
/// + prosa_utils::msg::tvf::Tvf
/// + std::default::Default,
/// {
/// fn init(&mut self, _proc: &StubProc<M>) -> Result<(), Box<dyn std::error::Error>> {
/// Ok(())
/// fn new(_proc: &StubProc<M>) -> Result<Self, Box<dyn std::error::Error>> {
/// Ok(Self {})
/// }
/// fn process_request(&mut self, service_name: &str, request: &M) -> M {
/// let mut msg = request.clone();
Expand All @@ -54,14 +53,17 @@ 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<M>) -> Result<(), Box<dyn Error>>;
fn new(proc: &StubProc<M>) -> Result<Self, Box<dyn Error>>
where
Self: Sized;
/// Method to process incomming requests
fn process_request(&mut self, service_name: &str, request: &M) -> M;
}

/// 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,
}

Expand All @@ -76,20 +78,13 @@ where
+ prosa_utils::msg::tvf::Tvf
+ std::default::Default,
{
fn init(&mut self, proc: &StubProc<M>) -> Result<(), Box<dyn Error>> {
self.meter = proc.get_proc_param().meter("stub_adaptor");
Ok(())
fn new(proc: &StubProc<M>) -> Result<Self, Box<dyn Error>> {
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"),
}
}
}
5 changes: 2 additions & 3 deletions prosa/src/stub/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ pub struct StubProc {}
#[proc]
impl<A> Proc<A> for StubProc
where
A: Default + Adaptor + StubAdaptor<M> + std::marker::Send + std::marker::Sync,
A: Adaptor + StubAdaptor<M> + std::marker::Send + std::marker::Sync,
{
async fn internal_run(&mut self, name: String) -> Result<(), Box<dyn std::error::Error>> {
// 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?;
Expand Down

0 comments on commit 601b7f8

Please sign in to comment.