pub trait ProvideChannelType<Runtime>where
Runtime: Async,{
type Sender<T>: Async
where T: Async;
type Receiver<T>: Async
where T: Async;
}Expand description
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,
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 or async-std’s async_std::channel.
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,
which defines abstract one-shot channel types that allow at most one message
to be sent over.
Required Associated Types§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
Source§impl<Component, Runtime> ProvideChannelType<Runtime> for Componentwhere
Runtime: Async,
Component: DelegateComponent<ChannelTypeComponent>,
Component::Delegate: ProvideChannelType<Runtime>,
Provides the abstract Sender and Receiver types for messsage-passing.
impl<Component, Runtime> ProvideChannelType<Runtime> for Componentwhere
Runtime: Async,
Component: DelegateComponent<ChannelTypeComponent>,
Component::Delegate: ProvideChannelType<Runtime>,
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,
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 or async-std’s async_std::channel.
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,
which defines abstract one-shot channel types that allow at most one message
to be sent over.