Skip to main content

Module session

Module session 

Source
Expand description

Session types for protocol-safe communication.

Session types encode communication protocols at the type level, ensuring protocol compliance at compile time. This module provides:

  • Core protocol building blocks: Send, Recv, Choose, Offer, End
  • Duality: Every session type has a dual — if one endpoint sends, the other receives
  • Typed endpoints: Communication channels that advance through protocol states

§Protocol Example

A simple request-response protocol:

// Client side: send a request, receive a response
type ClientProtocol = Send<Request, Recv<Response, End>>;

// Server side is the dual: receive a request, send a response
type ServerProtocol = Dual<ClientProtocol>;
// = Recv<Request, Send<Response, End>>

§Design

Session types are zero-sized marker types that exist only at compile time. They encode the protocol as a type-level state machine. Each communication operation consumes the current endpoint and returns one at the next state, ensuring protocol steps are followed in order and exactly once (affine use).

§Compile-Time Protocol Compliance

Protocol violations are type errors. The following shows correct usage:

use asupersync::session::{Send, Recv, End, Session, Dual};

// Dual of Send<T, End> is Recv<T, End>
fn _check_duality() {
    fn _assert_same<A, B>() where A: Session<Dual = B>, B: Session<Dual = A> {}
    _assert_same::<Send<u32, End>, Recv<u32, End>>();
}

Attempting to call send on a Recv endpoint is a type error:

use asupersync::session::{Recv, End, channel};

// ERROR: Endpoint<Recv<u32, End>> does not have a send() method
async fn wrong_direction() {
    let cx = asupersync::cx::Cx::for_testing();
    type P = Recv<u32, End>;
    let (ep, _peer) = channel::<P>();
    ep.send(&cx, 42).await.unwrap();
}

Attempting to close an endpoint before the protocol completes is a type error:

use asupersync::session::{Send, End, channel};

// ERROR: Endpoint<Send<u32, End>> does not have close()
fn premature_close() {
    type P = Send<u32, End>;
    let (ep, _peer) = channel::<P>();
    ep.close(); // Only Endpoint<End> has close()
}

Calling recv on a Send endpoint is a type error:

use asupersync::session::{Send, End, channel};

// ERROR: Endpoint<Send<u32, End>> does not have recv()
async fn recv_on_send() {
    let cx = asupersync::cx::Cx::for_testing();
    type P = Send<u32, End>;
    let (ep, _peer) = channel::<P>();
    ep.recv(&cx).await.unwrap();
}

Structs§

Choose
A protocol step where this endpoint chooses between two continuations.
End
Protocol termination — no further communication.
Endpoint
A typed endpoint at session state S.
Offer
A protocol step where this endpoint offers two continuations for the peer to choose.
Recv
A protocol step that receives a value of type T, then continues with Next.
Send
A protocol step that sends a value of type T, then continues with Next.

Enums§

Branch
Direction chosen by Choose — left or right branch.
Offered
Result of an offer operation — the peer’s chosen branch.
SessionError
Error returned when a session operation fails.

Traits§

Session
Marker trait for valid session types.

Functions§

channel
Creates a pair of dual session-typed endpoints.

Type Aliases§

Dual
Computes the dual of a session type.