Multiparty session types for Rust

Build Status Crate Minimum rustc version

This library implements multiparty session types in Rust for three participants. It relies on sesh. An other library is coming soon to extend to any number of participants.

Usage

Add this to your Cargo.toml:

toml [dependencies] mpstthree = "0.0.1"

Example

Here a way to create a simple protocol involving 3 participants, A, B and C. A sends a payoad to B, then receives an other 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. To implement this example, first, get the right components from the library.

```rust extern crate mpstthree;

use std::boxed::Box; use std::error::Error;

use mpstthree::binary::{End, Recv, Send}; use mpstthree::run_processes; use mpstthree::sessionmpst::SessionMpst;

use mpstthree::functionmpst::close::close_mpst;

use mpstthree::role::atob::RoleAtoB; use mpstthree::role::atoc::RoleAtoC; use mpstthree::role::btoa::RoleBtoA; use mpstthree::role::btoc::RoleBtoC; use mpstthree::role::ctoa::RoleCtoA; use mpstthree::role::ctob::RoleCtoB; use mpstthree::role::end::RoleEnd;

use mpstthree::functionmpst::recv::recvmpstatoc; use mpstthree::functionmpst::recv::recvmpstbtoa; use mpstthree::functionmpst::recv::recvmpstctob;

use mpstthree::functionmpst::send::sendmpstatob; use mpstthree::functionmpst::send::sendmpstbtoc; use mpstthree::functionmpst::send::sendmpstctoa; ```

Then, you have to create the binary session types defining the interactions for each pair of participants.

```rust /// Creating the binary sessions type AtoB = Send; type AtoC = Recv;

type BtoA = Recv; type BtoC = Send;

type CtoA = Send; type CtoB = Recv; ```

Add the queues, which give the correct order of the operations for each participants.

rust /// Queues type QueueA = RoleAtoB<RoleAtoC<RoleEnd>>; type QueueB = RoleBtoA<RoleBtoC<RoleEnd>>; type QueueC = RoleCtoA<RoleCtoB<RoleEnd>>;

You can now encapsulate those binary session types and queues into SessionMpst for each participant.

rust /// Creating the MP sessions type EndpointA<N> = SessionMpst<AtoB<N>, AtoC<N>, QueueA>; type EndpointB<N> = SessionMpst<BtoA<N>, BtoC<N>, QueueB>; type EndpointC<N> = SessionMpst<CtoA<N>, CtoB<N>, QueueC>;

To check to the protocol is correct, it is mandatory to detail the behaviour of the participants with functions which input the Endpoints defined above.

```rust /// Endpoint for A fn simpletripleendpointa(s: EndpointA) -> Result<(), Box> { let s = sendmpstatob(1, s); let (x, s) = recvmpstato_c(s)?;

close_mpst(s)?;

Ok(())

}

/// Endpoint for B fn simpletripleendpointb(s: EndpointB) -> Result<(), Box> { let (x, s) = recvmpstbtoa(s)?; let s = sendmpstbto_c(2, s);

close_mpst(s)?;

Ok(())

}

/// Endpoint for C fn simpletripleendpointc(s: EndpointC) -> Result<(), Box> { let s = sendmpstctoa(3, s); let (x, s) = recvmpstcto_b(s)?;

close_mpst(s)?;

Ok(())

} ```

In the end, you have to link the threads, related to the functions above, together with run_processes(). Do not forget to unwrap() the returned threads.

```rust /// Fork all endpoints fn simpletripleendpoints() { let (threada, threadb, threadc) = runprocesses( simpletripleendpointa, simpletripleendpointb, simpletripleendpoint_c, );

thread_a.unwrap();
thread_b.unwrap();
thread_c.unwrap();

} ```

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 should get cargo installed.

Building

For building the library, run this code.

sh $ cargo build

Running

For running the library, run this code.

sh $ cargo run

Run test

For running the tests, run this code.

sh $ cargo test

Tests are divided in 4 files:

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

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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.