Multiparty session types for Rust
This library implements multiparty session types in Rust for at least two participants. It relies on sesh.
A short video presentation of the library can be found here: https://youtu.be/ej1FetN31HE.
Usage
Add this to your Cargo.toml
:
[]
= "0.1.14"
Example
Assume a simple protocol involving 3 participants, A, B and C. A sends a payload to B, then receives another payload from C. Upon receiving the payload from A, B sends a payload to C. This protocol can be written as A!B.A?C.B!C.0. To implement this example, first, get the right components from the library.
// Used for the functions that will process the protocol
use Box;
use Error;
// Used for creating the types
use ;
use MeshedChannels;
// Used for creating the stack and the name of each role
use RoleA;
use RoleB;
use RoleC;
use RoleEnd;
// Used inside the functions which process the protocol for receiving one payload
use recv_mpst_a_from_c;
use recv_mpst_b_from_a;
use recv_mpst_c_from_b;
// Used inside the functions which process the protocol for sending one payload
use send_mpst_a_to_b;
use send_mpst_b_to_c;
use send_mpst_c_to_a;
// Used inside the functions which process the protocol for closing the connexion
use close_mpst;
// Used for connecting all the roles, represented as MeshedChannels, together
use fork_mpst;
Then, you have to create the binary session types defining the interactions for each pair of participants. Note that each created type can be reused as many time as needed. For our example, we create several times the same binary session type for clarity, but we could use only two of those types for the whole protocol instead.
// Creating the binary sessions
// for A
type AtoB<N> = ;
type AtoC<N> = ;
// for B
type BtoA<N> = ;
type BtoC<N> = ;
// for C
type CtoA<N> = ;
type CtoB<N> = ;
Add the stacks which give the correct order of the operations for each participant.
// Stacks
// for A
type StackA = ;
// for B
type StackB = ;
// for C
type StackC = ;
You can now encapsulate those binary session types and stacks into MeshedChannels for each participant. We also add the names of the related roles.
// Creating the MP sessions
// for A
type EndpointA<N> = ;
// for B
type EndpointB<N> = ;
// for C
type EndpointC<N> = ;
To run the protocol, we need to detail the behaviour of the participants with functions that input the Endpoints defined above.
// Function to process Endpoint of A
// Function to process Endpoint of B
// Function to process Endpoint of C
In the end, you have to link/fork the threads, related to the functions above, together with fork_mpst(). Do not forget to unwrap() the returned threads.
// Fork all endpoints
Getting started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
You need to have Rust.
You will get cargo
installed.
Building
For building the library, run this code.
Run test
For running the tests, run this code.
Running
For running an example XXX of the library, run this code.
Going further
With this library, one can write any protocol with at least two participants and using methods to shorten the writing and checking. You can check the tests and examples to have a larger overview of the different possibilities provided by this library.
Available features
The different features available are:
default
: default features, for implementing the basic example above.macros_simple
: feature for implementing protocols with three participants, whatever are their name.macros_multiple
: feature for implementing protocols with any number of participants. Containsmacros_simple
.baking
: feature for implementing protocols with any number of participants and using associated functions instead of functions.Containsmacros_multiple
.transport_tcp
: feature containing primitives for communicating with TCP. Requiresopenssl
,pkg-config
andlibssl-dev
installed on your machine.transport_udp
: feature containing primitives for communicating with UDP. Requiresopenssl
,pkg-config
andlibssl-dev
installed on your machine.transport_http
: feature containing primitives for communicating with HTTP/HTTPS. Requiresopenssl
,pkg-config
andlibssl-dev
installed on your machine.transport
: feature containingtransport_tcp
,transport_udp
andtransport_http
.checking
: feature for the top-down approach. Needs the [KMC
] tool.full
: feature containingchecking
,baking
andtransport
.
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Versioning
We use SemVer for versioning.
Authors
- Nicolas Lagaillardie - Initial work - NicolasLagaillardie
- Rumyana Neykova - Initial work - RumyanaNeykova
- Nobuko Yoshida - Initial work - NobukoYoshida
See also the list of contributors who participated in this project.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Acknowledgment
This project is part of my current PhD under the supervision of Nobuko Yoshida, that I would like to thank. I was also helped by my colleagues from Imperial College London.