47
47
//! (possibly several receivers to handle message priorities among inputs),
48
48
//! and has to give clones of the associated Sender (or Senders) to its peers.
49
49
//! - 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 .
51
51
//! The sender is owned by the builder to be cloned and given to any peer that needs to send data
52
52
//! 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
54
54
//! under construction needs to send messages to. This is the mirror of the previous responsibility:
55
55
//! each builder gives to the others clones of its senders and collects senders from others.
56
56
//! - This is why all the actor building traits
57
57
//! ([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,
61
61
//! using an `impl From<SourceMessage> for SinkMessage`. This flexibility allows an actor to receive
62
62
//! messages from several independent sources (see the [fan_in_message_type](crate::fan_in_message_type) macro).
63
63
use crate :: mpsc;
@@ -95,8 +95,8 @@ pub struct NoConfig;
95
95
/// The [Builder] of an [Actor](crate::Actor) must implement this trait
96
96
/// for every message type that actor can receive from its peers.
97
97
///
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.
100
100
pub trait MessageSink < M : Message > {
101
101
/// Return the sender that can be used by peers to send messages to this actor
102
102
fn get_sender ( & self ) -> DynSender < M > ;
@@ -105,12 +105,12 @@ pub trait MessageSink<M: Message> {
105
105
///
106
106
/// A sink might be interested only in a subset of the messages emitted by the source.
107
107
/// 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 > )
109
109
where
110
110
N : Message ,
111
111
M : From < N > ,
112
112
{
113
- source. register_peer ( config, self . get_sender ( ) . sender_clone ( ) )
113
+ source. connect_sink ( config, & self . get_sender ( ) )
114
114
}
115
115
116
116
/// 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> {
139
139
/// let mut sender_builder = SimpleMessageBoxBuilder::new("Send", 16);
140
140
///
141
141
/// // 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() );
143
143
///
144
144
/// let mut sender: SimpleMessageBox<NoMessage, &'static str>= sender_builder.build();
145
145
/// let receiver: SimpleMessageBox<char, NoMessage> = receiver_builder.build();
@@ -158,8 +158,8 @@ pub trait MessageSink<M: Message> {
158
158
/// # Ok(())
159
159
/// # }
160
160
/// ```
161
- fn add_mapped_input < N , C , MS , MessageMapper > (
162
- & mut self ,
161
+ fn connect_mapped_source < N , C , MS , MessageMapper > (
162
+ & self ,
163
163
config : C ,
164
164
source : & mut impl MessageSource < N , C > ,
165
165
cast : MessageMapper ,
@@ -169,23 +169,30 @@ pub trait MessageSink<M: Message> {
169
169
MessageMapper : Fn ( N ) -> MS ,
170
170
MessageMapper : ' static + Send + Sync ,
171
171
{
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 ( )
174
182
}
175
183
}
176
184
177
185
/// The [Builder] of an [Actor](crate::Actor) must implement this trait
178
186
/// for every message type that actor can send to its peers.
179
187
///
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> >`.
181
189
pub 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 > ) ;
189
196
}
190
197
191
198
/// The [Builder] of an [Actor](crate::Actor) must implement this trait
@@ -290,8 +297,8 @@ pub trait RuntimeRequestSink {
290
297
/// # }
291
298
/// # }
292
299
/// # 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 )
295
302
/// # }
296
303
/// # }
297
304
/// # impl MessageSink<MyActorInput> for MyActorBuilder {
@@ -330,8 +337,8 @@ pub trait RuntimeRequestSink {
330
337
/// // Connect a test box to an actor under test
331
338
/// let mut my_actor_builder = MyActorBuilder::new(MyActorConfig::default());
332
339
/// 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 );
335
342
///
336
343
/// // Build the test box and run the actor
337
344
/// let mut test_box = test_box_builder.build();
@@ -379,8 +386,8 @@ impl<I: Message, O: Message> SimpleMessageBoxBuilder<I, O> {
379
386
config : Config ,
380
387
service : & mut ( impl MessageSink < O > + MessageSource < I , Config > ) ,
381
388
) {
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) ;
384
391
}
385
392
386
393
/// Connect this client message box to the service message box
@@ -399,8 +406,8 @@ impl<I: Message, O: Message> SimpleMessageBoxBuilder<I, O> {
399
406
400
407
/// A `SimpleMessageBoxBuilder<Input,Output>` is a [MessageSource] of `Output` messages ignoring the config.
401
408
impl < 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 ( ) ;
404
411
}
405
412
}
406
413
0 commit comments