session_protocol!() { /* proc-macro */ }Expand description
Generates typestate-encoded session types from a protocol DSL.
The macro takes a protocol specification and generates a module containing
message structs, paired session type aliases (initiator + responder), and
constructor functions. The responder type is the dual of the initiator:
Send↔Recv, Select↔Offer.
§Syntax
ⓘ
session_protocol! {
module_name<T> for ObligationVariant {
msg MessageName;
msg MessageWithFields { field: Type };
send MessageName => select {
send T => end,
send OtherMsg => end,
}
}
}§Body Actions
send Type => body— send a value, then continuerecv Type => body— receive a value, then continueselect { a, b }— local choice (becomesOfferfor responder)offer { a, b }— remote choice (becomesSelectfor responder)loop { body }— recursion point (generatesrenew_loopconstructor)continue— jump back to enclosingloopend— protocol termination
§Generated Items
pub mod <name>containing:- Message structs with
Debug, Clone(+Copyfor unit structs) InitiatorSessiontype aliasResponderSessiontype aliasnew_session(channel_id) -> (Chan<Initiator, ...>, Chan<Responder, ...>)- (if
loopused)InitiatorLoop,ResponderLooptype aliases - (if
loopused)renew_loop(channel_id)constructor
- Message structs with
§Example
ⓘ
session_protocol! {
lease for Lease {
msg AcquireMsg;
msg RenewMsg;
msg ReleaseMsg;
send AcquireMsg => loop {
select {
send RenewMsg => continue,
send ReleaseMsg => end,
}
}
}
}