#![cfg_attr(feature = "doc-images",
cfg_attr(
all(),
doc = ::embed_doc_image::embed_image!(
"subscribing-handler-diagram",
"src/aggregates/subscribing_handler/subscribing_handler.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,
publisher_subscriber::heap,
*,
};
use delegate::delegate;
use std::{
hash::Hash,
marker::PhantomData,
};
#[cfg(test)]
mod tests;
pub mod prelude
{
pub use super::SubscribingHandler;
}
pub struct SubscribingHandler<H, M>
where
H: Handler<M>,
{
handler: H,
_retain_types: PhantomData<M>,
}
impl<H, M> SubscribingHandler<H, M>
where
H: Handler<M>,
{
pub fn new(handler: H) -> SubscribingHandler<H, M>
{
SubscribingHandler {
handler,
_retain_types: PhantomData,
}
}
}
impl<H, M> SubscribingHandler<H, M>
where
H: Handler<M> + 'static,
M: 'static,
{
pub fn into_heap(self) -> heap::Subscriber<M>
{
heap::Subscriber::new(Box::new(self))
}
}
impl<H, M> Subscriber<M> for SubscribingHandler<H, M>
where
H: Handler<M>,
{
fn receive(
&self,
message: &M,
)
{
self.handler.handle(message);
}
}
#[allow(clippy::inline_always)]
impl<H, M> Handler<M> for SubscribingHandler<H, M>
where
H: Handler<M>,
{
delegate! {
to self.handler{
fn handle(
&self,
message: &M,
);
}
}
}
impl<H, M> From<H> for SubscribingHandler<H, M>
where
H: Handler<M>,
{
fn from(handler: H) -> Self
{
SubscribingHandler::new(handler)
}
}
impl<H, M> PartialEq for SubscribingHandler<H, M>
where
H: PartialEq + Handler<M>,
{
fn eq(
&self,
other: &Self,
) -> bool
{
self.handler.eq(&other.handler)
}
}
impl<H, M> Eq for SubscribingHandler<H, M> where H: Eq + Handler<M> {}
impl<H, M> Hash for SubscribingHandler<H, M>
where
H: Handler<M> + Hash,
{
fn hash<S: std::hash::Hasher>(
&self,
state: &mut S,
)
{
self.handler.hash(state);
}
}
pub trait IntoSubscriber<H, M>
where
H: Handler<M>,
{
fn into_subscriber(self) -> SubscribingHandler<H, M>;
}
impl<H, M> IntoSubscriber<H, M> for H
where
H: Handler<M>,
{
fn into_subscriber(self) -> SubscribingHandler<H, M>
{
SubscribingHandler::new(self)
}
}