Module mz_ore::channel::trigger

source ·
Available on crate feature async only.
Expand description

Trigger channels.

Trigger channels are a very simple form of channel that communicate only one bit of information: whether the sending half of the channel is open or closed.

Here’s an example of using a trigger channel to trigger work on a background task.

use mz_ore::channel::trigger;

// Create a new trigger channel.
let (trigger, trigger_rx) = trigger::channel();

// Spawn a task to do some background work, but only once triggered.
tokio::spawn(async {
    // Wait for trigger to get dropped.
    trigger_rx.await;

    // Do background work.
});

// Do some prep work.

// Fire `trigger` by dropping it.
drop(trigger);

A trigger channel never errors. It is not an error to drop the receiver before dropping the corresponding trigger.

Trigger channels can be easily simulated with tokio::sync::oneshot channels (and in fact the implementation uses oneshot channels under the hood). However, using trigger channels can result in clearer code when the additional features of oneshot channels are not required, as trigger channels have a simpler API.

Structs§

  • The receiving half of a trigger channel.
  • The sending half of a trigger channel.

Functions§

  • Creates a new trigger channel.