Crate dialectic[][src]

Rust license: MIT documentation

dialectic (noun): The process of arriving at the truth by stating a thesis, developing a contradictory antithesis, and combining them into a coherent synthesis.

dialectic (crate): Transport-polymorphic session types for asynchronous Rust.

When two concurrent processes communicate, it’s good to give their messages types, which ensure every message is of an expected form.

  • Conventional types merely describe what is valid to communicate.
  • Session types describe when it is valid to communicate, and in what manner.

This crate provides a generic wrapper around almost any type of asynchronous channel that adds compile-time guarantees that a specified session protocol will not be violated by any code using the channel. Such a wrapped channel:

  • has almost no runtime cost in time or memory;
  • is built on async/.await to allow integration with Rust’s powerful async ecosystem;
  • gracefully handles runtime protocol violations, introducing no panics;
  • allows for full duplex concurrent communication, if specified in its type, while preserving all the same session-type safety guarantees; and
  • can even implement context free sessions, a more general form of session type than supported by most other session typing libraries.

Together, these make Dialectic ideal for writing networked services that need to ensure high levels of availability and complex protocol correctness properties in the real world, where protocols might be violated and connections might be dropped.

Dialectic supports a number of async runtimes and backends out-of-the-box, if you don’t want to or don’t need to write your own:

These crates also serve as good references for writing your own backends.

What now?

  • If you are new to session types you might consider starting with the tutorial-style tour of the crate.
  • If you’re familiar with session types, you might jump to the quick reference, then read more about the Session! macro for specifying session types, and continue on to look at the types module and the documentation for Chan.
  • You may also find helpful the full self-contained examples, which show how all the features of the crate come together to build session-typed network programs.
  • If you want to integrate your own channel type with Dialectic, you need to implement the Transmit and Receive traits from the backend module.
  • Or, you can dive into the reference documentation

Quick reference

The prelude module exports most of the relevant constructs for writing programs with Dialectic. Most programs using Dialectic should use dialectic::prelude::*;.

The tutorial covers all the constructs necessary to write session-typed programs with Dialectic. A quick summary:

  • To specify a session type, use the Session! macro, which defines a domain-specific language for session types. It compiles to session types defined in the types module, which most users will never need to use directly.
  • To construct a session-typed Chan, use the methods on the Session trait, such as Session::channel or Session::wrap, depending on whether you need to create both sides of a channel or just one side, respectively.
  • Backend transports suitable for being wrapped in a Chan are provided in backend, along with the Transmit and Receive traits necessary to implement your own.
  • When writing functions which are generic over their backend type, you will need to specify Transmit and Receive bounds on your backend. If you have a lot of these, the Transmitter and Receiver attribute macros can help eliminate them by letting you write them efficiently. All of the examples are written to be backend-agnostic, so taking a look at them may help if you get stuck.

Once you’ve got a channel, here’s what you can do:

Session! Macro InvocationSession Type (S)Channel Operation(s)
(on a channel c: Chan<S, _, _>)
Dual Type (S::Dual)
send T; ...Send<T, P>Given some t: T, returns a new c:
let c = c.send(t).await?;
Recv<T, P::Dual>
recv T; ...Recv<T, P>Returns some t: T and a new c:
let (t, c) = c.recv().await?;
Send<T, P::Dual>
choose { 0 => ..., 1 => ..., ... }; ...Choose<Choices>Given some N < the length of Choices, returns a new c:
let c = c.choose::<N>().await?;
offer { 0 => ..., 1 => ..., ... }; ...Offer<Choices>Given a set of labeled branches N => ... in ascending order, exactly one for each option in the tuple Choices, returns a new c whose type each branch must match:
let c = offer!(c => { 0 => ..., 1 => ..., ... });
loop { ... }; ...
'label: loop { ... }; ...
Loop<P>Whatever operations are available for PLoop<P::Dual>
continue 'label;
Continue<const N: usize>Whatever operations are available for the start of the referred-to loopContinue<N>
break 'label;
Whatever follows the loopWhatever operations are available after the end of the loop statementThe dual of whatever follows the loop
call { ... }; ...
call S; ...
Call<P, Q>Given a closure evaluating the session type P to Done, returns a result and a channel for the type Q:
let (t, c) =|c| async move { … }).await?;
Call<P::Dual, Q::Dual>
split { -> ..., <- ... }; ...Split<P, Q, R>Given a closure evaluating the session types P (send-only) and Q (receive-only) each to Done (potentially concurrently), returns a result and a channel for R:
let (t, c) = c.split(|c| async move { … }).await?;
Split<Q::Dual, P::Dual, R::Dual>
(empty macro invocation)DoneCloses the channel, dropping its receive/transmit ends: c.close();Done



The interface implemented by all transport backends for a Chan.


The prelude module for quickly getting started with Dialectic.


Conversions back and forth between flat tuples and their corresponding inductive list structures.


The introductory tutorial for Dialectic (nothing is exported from this module).


The structs in this module enumerate the shapes of all expressible sessions.


The unary numbers, represented by zero Z and successor S.



The Session! macro compiles a small domain-specific language for describing session types into the types themselves. It should be invoked in type position.


The offer! macro offers a set of different protocols, allowing the other side of the channel to choose with which one to proceed.



The result of offer: an N-ary enumeration of the possible Chans that could result from the what the other party chooses.


A bidirectional communications channel using the session type P over the connections Tx and Rx.


The future returned by Session::over (see its documentation for details).


A placeholder for a missing Transmit or Receive end of a connection.



A representation of what has gone wrong when a connection half Tx or Rx is incomplete.


The error returned when a closure which is expected to complete a channel’s session fails to finish the session of the channel it is given.



The Session extension trait gives methods to create session-typed channels from session types. These are implemented as static methods on the session type itself.

Attribute Macros


In situations where the transmitting backend for a channel is generic, explicitly writing down all the trait bounds necessary to implement a protocol for that parameterized backend can be a lot of boilerplate. The Receiver attribute macro abbreviates these bounds by modifying the where clause of the item to which it is attached.


In situations where the transmitting backend for a channel is generic, explicitly writing down all the trait bounds necessary to implement a protocol for that parameterized backend can be a lot of boilerplate. The Transmitter attribute macro abbreviates these bounds by modifying the where clause of the item to which it is attached.