Macro apis::def_session
source · [−]macro_rules! def_session {
( context $context:ident {
PROCESSES where
let $process_self:ident = self,
let $message_in:ident = message_in
[
$(process $process:ident (
$($field_name:ident : $field_type:ty $(= $field_default:expr)*),*
) $(-> ($presult_type:ty $(= $presult_default:expr)*))* {
kind { $process_kind:expr }
sourcepoints [ $($sourcepoint:ident),* ]
endpoints [ $($endpoint:ident),* ]
$(initialize $initialize:block)*
$(terminate $terminate:block)*
handle_message $handle_message:block
update $update:block
})+
]
CHANNELS [
$(channel $channel:ident <$local_type:ident> ($kind:ident) {
producers [ $($producer:ident),+ ]
consumers [ $($consumer:ident),+ ]
})*
]
MESSAGES [
$(message $message_type:ident $message_variants:tt)*
]
$(main: $main_process:ident)*
}
) => { ... };
( @expr_option $expr:expr ) => { ... };
( @expr_option ) => { ... };
( @expr_default $default:expr ) => { ... };
( @expr_default ) => { ... };
}
Expand description
Macro to define all parts of a session.
Defines an instance of session:Context
with the given name and the
following associated types:
type MID = MessageId
type CID = ChannelId
type PID = ProcessId
type GMSG = GlobalMessage
type GPROC = GlobalProcess
type GPRES = GlobalPresult
Process and message types with the given names and specifications are defined with implementations of relevant traits.
Process handle_message
and update
behavior is provided as a block of
code which is to be run inside of the actual trait methods where self
is
bound to the provided identifier in both cases, and the message_in
(global message) argument of handle_message
is bound to the provided
identifier.
Examples
From examples/simplex.rs
– defines two processes (Chargen
and Upcase
)
connected by a channel sending Charstreammessage
s:
extern crate apis;
use apis::{channel, message, process, session};
apis::def_session! {
context Mycontext {
PROCESSES where
let process = self,
let message_in = message_in
[
process Chargen (update_count : u64) {
kind { process::Kind::Isochronous {
tick_ms: 20,
ticks_per_update: 1 } }
sourcepoints [Charstream]
endpoints []
handle_message { apis::process::ControlFlow::Break }
update { apis::process::ControlFlow::Break }
}
process Upcase (history : String) {
kind { process::Kind::asynchronous_default() }
sourcepoints []
endpoints [Charstream]
handle_message { apis::process::ControlFlow::Break }
update { apis::process::ControlFlow::Break }
}
]
CHANNELS [
channel Charstream <Charstreammessage> (Simplex) {
producers [Chargen]
consumers [Upcase]
}
]
MESSAGES [
message Charstreammessage {
Achar (char),
Quit
}
]
}
}
The handle_message
and update
definitions have been ommitted for
brevity, but in general any block of code can be substituted that
references the self
and message_in
bindings.