mediator_sys/mediator/asynchronous/basic/builder.rs
1use async_std::sync::Mutex;
2
3use crate::mediator::{
4 asynchronous::basic::basic::BasicAsyncMediator,
5 builder::{BuilderFlow, BuilderInternal},
6 listener::Listener,
7 synchronous::basic::{basic::BasicMediator, interface::BasicMediatorBuilderInterface},
8};
9use std::{fmt::Debug, sync::mpsc::channel};
10
11/// The [`BasicAsyncBuilder`] helps you to create a [`BasicAsyncMediator`].
12///
13/// The [`BasicAsyncBuilder`] is part of the builder pattern.
14/// It has only two functionalities. The first one is adding a [`Listener`] via
15/// [`BasicAsyncBuilder::add_listener()`].
16/// The second one is the mandatory [`BuilderFlow::build()`], which returns
17/// a [`BasicAsyncMediator`].
18///
19pub struct BasicAsyncBuilder<Ev>
20where
21 Ev: Debug,
22{
23 mediator: BasicMediator<Ev>,
24}
25
26impl<Ev> BuilderInternal<BasicAsyncMediator<Ev>, BasicAsyncBuilder<Ev>> for BasicAsyncMediator<Ev>
27where
28 Ev: Debug,
29{
30 /// Creates a [`BasicAsyncBuilder`] with the goal of producing a [`BasicAsyncMediator`].
31 ///
32 fn builder() -> BasicAsyncBuilder<Ev> {
33 BasicAsyncBuilder::<Ev> {
34 mediator: BasicMediator::<Ev> {
35 channel: channel(),
36 listener: vec![],
37 },
38 }
39 }
40}
41
42impl<M, Ev> BasicMediatorBuilderInterface<M, Ev> for BasicAsyncBuilder<Ev>
43where
44 Ev: Debug,
45{
46 /// Adds a user-defined listener to the [`BasicAsyncBuilder`].
47 ///
48 /// To be able to supply a closure that implements [`Listener`],
49 /// it must satisfy [`Send`] and `'static` bounds.
50 ///
51 /// Also it must be a `Fn(Ev)` with a return type of `()`
52 /// where `Ev` is the user-defined event type
53 /// that must be [`Clone`] and [`Debug`].
54 ///
55 fn add_listener<F>(mut self, f: F) -> Self
56 where
57 F: Listener<Ev>,
58 {
59 self.mediator.listener.push(Box::new(f));
60 self
61 }
62}
63
64impl<Ev> BasicAsyncBuilder<Ev>
65where
66 Ev: Debug,
67{
68 /// Adds a user-defined listener to the [`BasicAsyncBuilder`].
69 ///
70 /// The supplied type must be a [`Listener`].
71 /// As such, it must implement [`Send`] and `Fn(Ev)`,
72 /// besides being `'static`.
73 ///
74 /// As a side note, here, `Ev` is the user-defined event type
75 /// that must be [`Clone`] and [`Debug`].
76 ///
77 /// # Examples
78 ///
79 /// Basic usage:
80 ///
81 /// ```
82 /// use mediator_sys::asynchronous::basic::*;
83 ///
84 /// #[derive(Debug, Clone)]
85 /// enum MyEvent {
86 /// One,
87 /// Two
88 /// }
89 ///
90 /// let mediator = BasicAsyncMediator::<MyEvent>::builder()
91 /// .add_listener(|ev| {
92 /// /* Your listening logic */
93 /// })
94 /// .build();
95 ///
96 pub fn add_listener<F>(self, f: F) -> Self
97 where
98 F: Listener<Ev>,
99 {
100 <Self as BasicMediatorBuilderInterface<BasicMediator<Ev>, Ev>>::add_listener(self, f)
101 }
102}
103
104impl<Ev> BuilderFlow<BasicAsyncMediator<Ev>> for BasicAsyncBuilder<Ev>
105where
106 Ev: Debug,
107{
108 /// Builds the [`BasicAsyncMediator`] and returns it.
109 ///
110 /// Because [`BasicAsyncMediator`] implements [`BuilderInternal`],
111 /// which in turn means, that the [`BasicAsyncBuilder`] implements [`BuilderFlow`]
112 /// and not [`crate::builder::TryBuilderFlow`], this method will
113 /// always return a [`BasicAsyncMediator`] as stated by the return type.
114 ///
115 fn build(self) -> BasicAsyncMediator<Ev> {
116 BasicAsyncMediator {
117 basic: Mutex::new(self.mediator),
118 }
119 }
120}