Crates.io | triggered |
lib.rs | triggered |
version | 0.1.2 |
source | src |
created_at | 2020-04-20 21:48:37.086017 |
updated_at | 2021-07-19 19:09:42.497917 |
description | Triggers for one time events between tasks and threads |
homepage | |
repository | https://github.com/faern/triggered |
max_upload_size | |
id | 232345 |
size | 26,888 |
Triggers for one time events between tasks and threads.
The mechanism consists of two types, the Trigger
and the Listener
. They come together
as a pair. Much like the sender/receiver pair of a channel. The trigger half has a
Trigger::trigger
method that will make all tasks/threads waiting on
a listener continue executing.
The listener both has a sync Listener::wait
method, and it also implements
Future<Output = ()>
for async support.
Both the Trigger
and Listener
can be cloned. So any number of trigger instances can
trigger any number of waiting listeners. When any one trigger instance belonging to the pair is
triggered, all the waiting listeners will be unblocked. Waiting on a listener whose
trigger already went off will return instantly. So each trigger/listener pair can only be fired
once.
This crate does not use any unsafe
code.
A trivial example showing the basic usage:
#[tokio::main]
async fn main() {
let (trigger, listener) = triggered::trigger();
let task = tokio::spawn(async {
// Blocks until `trigger.trigger()` below
listener.await;
println!("Triggered async task");
});
// This will make any thread blocked in `Listener::wait()` or async task awaiting the
// listener continue execution again.
trigger.trigger();
let _ = task.await;
}
An example showing a trigger/listener pair being used to gracefully shut down some async
server instances on a Ctrl-C event, where only an immutable Fn
closure is accepted:
#[tokio::main]
async fn main() -> Result<(), Error> {
let (shutdown_trigger, shutdown_signal1) = triggered::trigger();
// A sync `Fn` closure will trigger the trigger when the user hits Ctrl-C
ctrlc::set_handler(move || {
shutdown_trigger.trigger();
}).expect("Error setting Ctrl-C handler");
// If the server library has support for something like a shutdown signal:
let shutdown_signal2 = shutdown_signal1.clone();
let server1_task = tokio::spawn(async move {
SomeServer::new().serve_with_shutdown_signal(shutdown_signal1).await;
});
// Or just select between the long running future and the signal to abort it
tokio::select! {
server_result = SomeServer::new().serve() => {
eprintln!("Server error: {:?}", server_result);
}
_ = shutdown_signal2 => {}
}
let _ = server1_task.await;
Ok(())
}
Will work with at least the two latest stable Rust releases. This gives users at least six weeks to upgrade their Rust toolchain after a new stable is released.
The current MSRV can be seen in travis.yml
. Any change to the MSRV will be considered a
breaking change and listed in the changelog.
The event triggering primitives in this library is somewhat similar to channels. The main difference and why I developed this library is that
The listener is somewhat similar to a futures::channel::oneshot::Receiver<()>
. But it:
Future<Output = ()>
instead of
Future<Output = Result<T, Canceled>>
Clone
- Any number of listeners can wait for the same eventListener::wait
] - Both synchronous threads, and asynchronous tasks can wait
at the same time.The trigger, when compared to a futures::channel::oneshot::Sender<()>
has the differences
that it:
&self
- So can be used
in situations where it is not owned or not mutable. For example in Drop
implementations
or callback closures that are limited to Fn
or FnMut
.futures::future::Abortable
One use case of these triggers is to abort futures when some event happens. See examples above. The differences include:
Abortable
does it.
These libraries sometimes allows creating their futures with a shutdown signal that triggers
a clean abort. Something like serve_with_shutdown(signal: impl Future<Output = ()>)
.