4747//! (possibly several receivers to handle message priorities among inputs),
4848//! and has to give clones of the associated Sender (or Senders) to its peers.
4949//! - The first responsibility of a builder is to create a channel per receiver of the actor
50- //! under construction. The receiver will be given to the actor on build .
50+ //! under construction. The receiver will be given to the actor when built .
5151//! The sender is owned by the builder to be cloned and given to any peer that needs to send data
5252//! to the actor under construction.
53- //! - The second responsibility of the builder is to collect a Sender for each peer the actor
53+ //! - The second responsibility of the builder is to collect a sender for each peer the actor
5454//! under construction needs to send messages to. This is the mirror of the previous responsibility:
5555//! each builder gives to the others clones of its senders and collects senders from others.
5656//! - This is why all the actor building traits
5757//! ([MessageSource], [MessageSink] and [RuntimeRequestSink])
58- //! are related to exchanges of Sender. A sink gives to a source a sender attached to its receiver.
59- //! - To be precise, the actor builders exchange [DynSender] and not [Sender]. The difference is that
60- //! a [DynSender] can transform the messages sent by the source to adapt them to the sink expectations,
58+ //! are related to exchanges of Sender. A sink gives to a source a sender attached to its own receiver.
59+ //! - To be precise, the actor builders exchange [DynSender] and not [Sender](futures::channel::mpsc::Sender).
60+ //! The difference is that a [DynSender] can transform the messages sent by the source to adapt them to the sink expectations,
6161//! using an `impl From<SourceMessage> for SinkMessage`. This flexibility allows an actor to receive
6262//! messages from several independent sources (see the [fan_in_message_type](crate::fan_in_message_type) macro).
6363use crate :: mpsc;
@@ -95,8 +95,8 @@ pub struct NoConfig;
9595/// The [Builder] of an [Actor](crate::Actor) must implement this trait
9696/// for every message type that actor can receive from its peers.
9797///
98- /// An actor whose builder is a `MessageSink<M, C >` can be connected to any other actor
99- /// whose builder is a `MessageSource<M , C>` so that the sink can receive messages from that source.
98+ /// An actor whose builder is a `MessageSink<M>` can be connected to any other actor
99+ /// whose builder is a `MessageSource<Into<M> , C>` so that the sink can receive messages from that source.
100100pub trait MessageSink < M : Message > {
101101 /// Return the sender that can be used by peers to send messages to this actor
102102 fn get_sender ( & self ) -> DynSender < M > ;
@@ -105,12 +105,12 @@ pub trait MessageSink<M: Message> {
105105 ///
106106 /// A sink might be interested only in a subset of the messages emitted by the source.
107107 /// This subset is defined by the config parameter.
108- fn add_input < N , C > ( & mut self , config : C , source : & mut impl MessageSource < N , C > )
108+ fn connect_source < N , C > ( & self , config : C , source : & mut impl MessageSource < N , C > )
109109 where
110110 N : Message ,
111111 M : From < N > ,
112112 {
113- source. register_peer ( config, self . get_sender ( ) . sender_clone ( ) )
113+ source. connect_sink ( config, & self . get_sender ( ) )
114114 }
115115
116116 /// Add a source of messages to the actor under construction, the messages being translated on the fly.
@@ -139,7 +139,7 @@ pub trait MessageSink<M: Message> {
139139 /// let mut sender_builder = SimpleMessageBoxBuilder::new("Send", 16);
140140 ///
141141 /// // Convert the `&str` sent by the source into an iterator of `char` as expected by the receiver.
142- /// receiver_builder.add_mapped_input (NoConfig, &mut sender_builder, |str: &'static str| str.chars() );
142+ /// receiver_builder.connect_mapped_source (NoConfig, &mut sender_builder, |str: &'static str| str.chars() );
143143 ///
144144 /// let mut sender: SimpleMessageBox<NoMessage, &'static str>= sender_builder.build();
145145 /// let receiver: SimpleMessageBox<char, NoMessage> = receiver_builder.build();
@@ -158,8 +158,8 @@ pub trait MessageSink<M: Message> {
158158 /// # Ok(())
159159 /// # }
160160 /// ```
161- fn add_mapped_input < N , C , MS , MessageMapper > (
162- & mut self ,
161+ fn connect_mapped_source < N , C , MS , MessageMapper > (
162+ & self ,
163163 config : C ,
164164 source : & mut impl MessageSource < N , C > ,
165165 cast : MessageMapper ,
@@ -169,23 +169,30 @@ pub trait MessageSink<M: Message> {
169169 MessageMapper : Fn ( N ) -> MS ,
170170 MessageMapper : ' static + Send + Sync ,
171171 {
172- let sender = MappingSender :: new ( self . get_sender ( ) , cast) ;
173- source. register_peer ( config, sender. into ( ) )
172+ let sender: DynSender < N > = MappingSender :: new ( self . get_sender ( ) , cast) . into ( ) ;
173+ source. connect_sink ( config, & sender)
174+ }
175+ }
176+
177+ /// A [DynSender] can be used as a [MessageSink],
178+ /// provided the messages expected by the sender can be built from those sent to the sink.
179+ impl < N : Message , M : Message + From < N > > MessageSink < N > for DynSender < M > {
180+ fn get_sender ( & self ) -> DynSender < N > {
181+ self . sender_clone ( )
174182 }
175183}
176184
177185/// The [Builder] of an [Actor](crate::Actor) must implement this trait
178186/// for every message type that actor can send to its peers.
179187///
180- /// To receive messages from a `MessageSource<M, C>`, the peer must be a `MessageSink<M >`.
188+ /// To receive messages from a `MessageSource<M, C>`, the peer must be a `MessageSink<From<M> >`.
181189pub trait MessageSource < M : Message , Config > {
182- /// The message will be sent to the peer using the provided `sender`
183- fn register_peer ( & mut self , config : Config , sender : DynSender < M > ) ;
184-
185- /// Connect a peer actor that will consume the message produced by this actor
186- fn add_sink ( & mut self , config : Config , peer : & impl MessageSink < M > ) {
187- self . register_peer ( config, peer. get_sender ( ) ) ;
188- }
190+ /// Connect a peer actor that will consume the messages produced by this actor
191+ ///
192+ /// The messages will be sent to the peer using its sender, the `peer.get_sender()`.
193+ /// A peer can subscribe to a subset of the messages produced by this source.
194+ /// This subset of messages expected by the peer is defined by the `config` parameter.
195+ fn connect_sink ( & mut self , config : Config , peer : & impl MessageSink < M > ) ;
189196}
190197
191198/// The [Builder] of an [Actor](crate::Actor) must implement this trait
@@ -290,8 +297,8 @@ pub trait RuntimeRequestSink {
290297/// # }
291298/// # }
292299/// # impl MessageSource<MyActorOutput, NoConfig> for MyActorBuilder {
293- /// # fn register_peer (&mut self, config: NoConfig, sender: DynSender <MyActorOutput>) {
294- /// # self.messages.register_peer (config, sender )
300+ /// # fn connect_sink (&mut self, config: NoConfig, peer: &impl MessageSink <MyActorOutput>) {
301+ /// # self.messages.connect_sink (config, peer )
295302/// # }
296303/// # }
297304/// # impl MessageSink<MyActorInput> for MyActorBuilder {
@@ -330,8 +337,8 @@ pub trait RuntimeRequestSink {
330337/// // Connect a test box to an actor under test
331338/// let mut my_actor_builder = MyActorBuilder::new(MyActorConfig::default());
332339/// let mut test_box_builder = SimpleMessageBoxBuilder::new("Test box", 16);
333- /// my_actor_builder.register_peer (NoConfig, test_box_builder.get_sender() );
334- /// test_box_builder.register_peer (NoConfig, my_actor_builder.get_sender() );
340+ /// my_actor_builder.connect_sink (NoConfig, & test_box_builder);
341+ /// my_actor_builder.connect_source (NoConfig, &mut test_box_builder );
335342///
336343/// // Build the test box and run the actor
337344/// let mut test_box = test_box_builder.build();
@@ -379,8 +386,8 @@ impl<I: Message, O: Message> SimpleMessageBoxBuilder<I, O> {
379386 config : Config ,
380387 service : & mut ( impl MessageSink < O > + MessageSource < I , Config > ) ,
381388 ) {
382- service. register_peer ( config, self . input_sender . sender_clone ( ) ) ;
383- self . register_peer ( NoConfig , service. get_sender ( ) ) ;
389+ service. connect_sink ( config, self ) ;
390+ self . connect_sink ( NoConfig , service) ;
384391 }
385392
386393 /// Connect this client message box to the service message box
@@ -399,8 +406,8 @@ impl<I: Message, O: Message> SimpleMessageBoxBuilder<I, O> {
399406
400407/// A `SimpleMessageBoxBuilder<Input,Output>` is a [MessageSource] of `Output` messages ignoring the config.
401408impl < I : Message , O : Message , C > MessageSource < O , C > for SimpleMessageBoxBuilder < I , O > {
402- fn register_peer ( & mut self , _config : C , sender : DynSender < O > ) {
403- self . output_sender = sender ;
409+ fn connect_sink ( & mut self , _config : C , peer : & impl MessageSink < O > ) {
410+ self . output_sender = peer . get_sender ( ) ;
404411 }
405412}
406413
0 commit comments