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.
Add this to your Cargo.toml
:
toml
[dependencies]
mpstthree = "0.0.13"
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.
```rust // Used for the functions that will process the protocol use std::boxed::Box; use std::error::Error;
// Used for creating the types use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send}; use mpstthree::meshedchannels::MeshedChannels;
// Used for creating the stack and the name of each role use mpstthree::role::a::RoleA; use mpstthree::role::b::RoleB; use mpstthree::role::c::RoleC; use mpstthree::role::end::RoleEnd;
// Used inside the functions which process the protocol for receiving one payload use mpstthree::functionmpst::recv::recvmpstafromc; use mpstthree::functionmpst::recv::recvmpstbfroma; use mpstthree::functionmpst::recv::recvmpstcfromb;
// Used inside the functions which process the protocol for sending one payload use mpstthree::functionmpst::send::sendmpstatob; use mpstthree::functionmpst::send::sendmpstbtoc; use mpstthree::functionmpst::send::sendmpstctoa;
// Used inside the functions which process the protocol for closing the connexion use mpstthree::functionmpst::close::close_mpst;
// Used for connecting all the roles, represented as MeshedChannels, together use mpstthree::functionmpst::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.
```rust
// Creating the binary sessions
// for A
type AtoB
// for B
type BtoA
// for C
type CtoA
Add the stacks which give the correct order of the operations for each participant.
rust
// Stacks
// for A
type StackA = RoleB<RoleC<RoleEnd>>;
// for B
type StackB = RoleA<RoleC<RoleEnd>>;
// for C
type StackC = RoleA<RoleB<RoleEnd>>;
You can now encapsulate those binary session types and stacks into MeshedChannels for each participant. We also add the names of the related roles.
rust
// Creating the MP sessions
// for A
type EndpointA<N> = MeshedChannels<AtoB<N>, AtoC<N>, StackA, RoleA<RoleEnd>>;
// for B
type EndpointB<N> = MeshedChannels<BtoA<N>, BtoC<N>, StackB, RoleB<RoleEnd>>;
// for C
type EndpointC<N> = MeshedChannels<CtoA<N>, CtoB<N>, StackC, RoleC<RoleEnd>>;
To run the protocol, we need to detail the behaviour of the participants with functions that input the Endpoints defined above.
```rust
// Function to process Endpoint of A
fn simpletripleendpointa(s: EndpointA
close_mpst(s)
}
// Function to process Endpoint of B
fn simpletripleendpointb(s: EndpointB
close_mpst(s)
}
// Function to process Endpoint of C
fn simpletripleendpointc(s: EndpointC
close_mpst(s)
} ```
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.
```rust // Fork all endpoints fn simpletripleendpoints() { let (threada, threadb, threadc) = forkmpst( endpointa, endpointb, endpoint_c, );
thread_a.join().unwrap();
thread_b.join().unwrap();
thread_c.join().unwrap();
} ```
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
You need to have Rust.
You will get cargo
installed.
For building the library, run this code.
sh
cargo build
For running the library, run this code.
sh
cargo run
For running the tests, run this code.
sh
cargo test
Tests are divided into 8 folders:
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.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning.
See also the list of contributors who participated in this project.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
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.