Skip to main content

Crate selectables

Crate selectables 

Source
Expand description

§selectables

Lock-free channels with a unified recv-arm selection model.

The crate provides multiple channel flavors that can be selected with the same recv-based protocol, including non-blocking and timed fallback behavior.

§Channel modules

  • unbounded_mpmc lock-free unbounded multi-producer, multi-consumer channel.
  • bounded_mpmc lock-free bounded multi-producer, multi-consumer channel.
  • bounded_mpsc lock-free bounded multi-producer, single-consumer channel.
  • unbounded_mpsc lock-free unbounded multi-producer, single-consumer channel.
  • bounded_broadcast bounded multi-producer, multi-receiver broadcast channel with per-receiver lag detection and independent cursors.
  • oneshot single-send/single-delivery channel compatible with recv select arms.
  • watch latest-value broadcast channel with versioned change notifications.
  • rendezvous zero-buffer synchronous handoff channel.

§Selection model

  • select! supports recv(rx) -> msg => { ... } and send(tx, val) -> res => { ... } arms.
  • Non-blocking fallback: default => { ... }.
  • Timed fallback: default(duration) => { ... }.
  • Low-level builder API is provided by Select and SelectedOperation.

§Timers

§Error model

  • RecvError for blocking receive/completion failures. Includes Lagged { skipped } for bounded broadcast channels when a receiver falls behind senders.
  • [error::TryRecvError] for non-blocking receive attempts.
  • SendError for failed sends that return ownership of the message.

§Lag handling (broadcast channels only)

When using bounded_broadcast, receivers may encounter Lagged { skipped } if they fall behind the senders. This is normal and indicates the ring buffer wrapped around before the receiver caught up. Recommended handling:

match rx.recv() {
    Ok(msg) => process(msg),
    Err(RecvError::Lagged { skipped }) => {
        log::warn!("Receiver lagged by {} messages; recovered", skipped);
        // Receiver cursor was automatically advanced; next recv will get oldest available
    }
    Err(RecvError::Disconnected) => {
        log::info!("All senders disconnected");
        break;
    }
}

See bounded_broadcast module docs for detailed lag recovery patterns.

§Quick example

use std::time::Duration;
use selectables::{bounded_mpmc, select};

let (tx, rx) = bounded_mpmc::channel::<i32>(1);
tx.send(7).unwrap();

select! {
    recv(rx) -> msg => assert_eq!(msg, Ok(7)),
    default(Duration::from_millis(10)) => panic!("unexpected timeout"),
}

Modules§

bounded_broadcast
Bounded multi-producer, multi-receiver broadcast channel with per-receiver lag detection.
bounded_mpmc
Bounded multi-producer, multi-consumer channel with lock-free send and recv.
bounded_mpsc
Bounded multi-producer, single-consumer channel with lock-free send and recv.
interval
Repeating timer channel, selectable via recv arms.
oneshot
Single-send, single-delivery channel for one-time messages.
rendezvous
Rendezvous channel: synchronous, zero-buffer handoff between sender and receiver.
unbounded_mpmc
Unbounded lock-free multi-producer, multi-consumer channel.
unbounded_mpsc
Unbounded lock-free multi-producer, single-consumer channel.
watch
Latest-value broadcast channel with versioned change notifications.

Macros§

select
Dispatch to the first ready channel operation.

Structs§

Select
SelectedOperation
Returned by Select::select(). Carries the winning arm’s index.
SendError

Enums§

RecvError
Error returned by blocking recv operations.

Traits§

SelectableReceiver
Trait for receivers that can participate in recv arms of select!.
SelectableSender
Trait for senders that can participate in send arms of select!.