Attribute Macro dialectic::Receiver [−][src]
#[Receiver]
In situations where the transmitting backend for a channel is generic, explicitly writing down
all the trait bounds necessary to implement a protocol for that parameterized backend can be a
lot of boilerplate. The Receiver
attribute macro abbreviates these bounds by modifying the
where
clause of the item to which it is attached.
This macro may be attached to any item capable of supporting a where
-clause: an enum
definition, fn
item (top level or in a trait definition or implementation), impl
block,
struct
definition, trait
definition, type
synonym, or union
definition.
Examples
use dialectic::prelude::*; #[Receiver(Rx for bool, i64)] async fn foo<Tx, Rx>( chan: Chan<Session!{ recv bool; recv i64 }, Tx, Rx> ) -> Result<(), Rx::Error> where Tx: Send + 'static, { let (_, chan) = chan.recv().await?; let (_, chan) = chan.recv().await?; chan.close(); Ok(()) }
Syntax
The Receiver
attribute takes the name of the receiver for which the bounds will be generated,
and, optionally, the keyword for
followed by a comma-separated list of types.
Some example invocations:
#[Receiver(Rx)] #[Receiver(Rx for bool)] #[Receiver(Rx for bool, i64, Vec<String>)]
Expansion
The attribute adds extra bounds to the where
-clause of the item to which it is attached (if
the item does not have a `where-clause, one will be created containing the bounds).
For a transmitter type Rx
, and types T1
, T2
, ...
, the invocation:
#[Receiver(Rx for T1, T2, ...)] fn f<Rx>() {}
…translates to the following bounds:
use dialectic::prelude::*; fn f<Rx>() where Rx: Receiver + Send + 'static, // For each of the types `T1`, `T2`, ... Rx: Receive<T1>, Rx: Receive<T2>, // ... {}