#![cfg_attr(feature = "doc-images",
cfg_attr(
all(),
doc = ::embed_doc_image::embed_image!(
"handling-publisher-diagram",
"src/aggregates/handling_publisher/handling_publisher.svg"
)))]
#![cfg_attr(
not(feature = "doc-images"),
doc = "**Doc images not enabled**. Compile with feature `doc-images` and \
Rust version >= 1.54 to enable."
)]
use crate::prelude::{
handler::lambda::Delegate,
*,
};
use std::{
fmt::Debug,
hash::Hash,
marker::PhantomData,
};
#[cfg(test)]
mod tests;
pub mod prelude
{
pub use super::HandlingPublisher;
}
pub struct HandlingPublisher<P, M>
where
P: Publisher<M>,
{
delegate: P,
_retain_types: PhantomData<M>,
}
impl<P, M> HandlingPublisher<P, M>
where
P: Publisher<M>,
{
pub fn new(delegate: P) -> HandlingPublisher<P, M>
{
HandlingPublisher {
delegate,
_retain_types: PhantomData,
}
}
}
impl<P, M> Publisher<M> for HandlingPublisher<P, M>
where
P: Publisher<M>,
{
fn publish(
&self,
message: &M,
)
{
self.delegate.publish(message);
}
}
impl<P, M> Handler<M> for HandlingPublisher<P, M>
where
P: Publisher<M>,
{
fn handle(
&self,
message: &M,
)
{
self.delegate.publish(message);
}
}
impl<P, M> From<P> for HandlingPublisher<P, M>
where
P: Publisher<M>,
{
fn from(delegate: P) -> Self
{
HandlingPublisher::new(delegate)
}
}
impl<P, M> PartialEq for HandlingPublisher<P, M>
where
P: Publisher<M> + PartialEq,
{
fn eq(
&self,
other: &Self,
) -> bool
{
self.delegate.eq(&other.delegate)
}
}
impl<P, M> Eq for HandlingPublisher<P, M> where P: Publisher<M> + Eq {}
impl<P, M> Debug for HandlingPublisher<P, M>
where
P: Publisher<M> + Debug,
{
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result
{
f.debug_struct("HandlingPublisher")
.field("delegate", &self.delegate)
.finish()
}
}
pub trait IntoHandler<P, M>
where
P: Publisher<M>,
{
fn into_handler(self) -> HandlingPublisher<P, M>;
}
impl<P, M> IntoHandler<P, M> for P
where
P: Publisher<M>,
{
fn into_handler(self) -> HandlingPublisher<P, M>
{
HandlingPublisher::new(self)
}
}