1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*!
Support for use of abstract communication channels within a runtime context.
This module define the abstract traits that can be implemented by a runtime
context to support message-passing concurrency. This provides similar
functionalities as the Rust channel types defined in
[`std::sync::mpsc::channel`](https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html).
*/
use *;
use crateHasStreamType;
/**
Provides the abstract `Sender` and `Receiver` types for messsage-passing.
The `Sender` and `Receiver` types are parameterized by an arbitrary payload
type `T` using generic associated types. Given any payload type `T` that
implements [`Async`], a runtime context that implements `HasChannelTypes`
should be able to provide the concrete types `Sender<T>` and `Receiver<T>`,
where messages of type `T` can be sent over to the `Sender<T>` end and
received from the `Receiver<T>` end.
The abstract `Sender` and `Receiver` types need to support the
message-passing passing asynchronously, i.e. inside async functions.
As a result, although it work similar to the Rust channel provided in
the standard library,
[`std::sync::mpsc::channel`](https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html),
the standard Rust channels are not suited for use here, as they could block
the entire thread running the async tasks.
Instead, there are async equivalent of the channel types offered by async
libraries such as Tokio's
[tokio::sync::mpsc::channel](https://docs.rs/tokio/latest/tokio/sync/mpsc/fn.channel.html)
or async-std's [async_std::channel](https://docs.rs/async-std/latest/async_std/channel/index.html).
A main difference between the channel types defined here and the common
MPSC (multiple producer, single consumer) channels in the stated libraries
is that we allow multiple consumers to use the same receiver. This is to
avoid the use of `&mut` references, which would require the entire context
containing a receiver to be mutable. Instead, concrete types can wrap a
single-consumer receiver as `Arc<Mutex<Receiver<T>>>` in the concrete
type definition, so that it can be used as a multi-consumer receiver.
The methods for using the abstract channel types are available in separate
traits, [`CanCreateChannels`] and [`CanUseChannels`]. This is because code
that needs to create new channels do not necessary need to use the channels,
and vice versa. Having separate traits makes it clear which capability a
component needs from the runtime.
There is also a similar trait
[`HasChannelOnceTypes`](super::channel_once::HasChannelOnceTypes),
which defines abstract one-shot channel types that allow at most one message
to be sent over.
*/
/**
Allow the creation of new sender-receiver pairs for the channel types
defined in [`HasChannelTypes`].
*/