logo
Expand description

Trait and implementations for hook handlers.

You can implement the trait yourself, or use any of the provided implementations:

  • for closures,
  • for std and tokio channels,
  • for printing to writers, in Debug and Display (where supported) modes (generally used for debugging and testing, as they don’t allow any other output customisation),
  • for (), as placeholder.

The implementation for FnMut only supports fns that return a Future. Unfortunately it’s not possible to provide an implementation for fns that don’t return a Future as well, so to call sync code you must either provide an async handler, or use the SyncFnHandler wrapper.

Examples

In each example on_data is the following function:

fn on_data<T: Handler<Vec<u8>>>(_: T) {}

Async closure:

use tokio::io::{AsyncWriteExt, stdout};
on_data(|data: Vec<u8>| async move {
    stdout().write_all(&data).await
});

Sync code in async closure:

use std::io::{Write, stdout};
on_data(|data: Vec<u8>| async move {
    stdout().write_all(&data)
});

Sync closure with wrapper:

use std::io::{Write, stdout};
on_data(SyncFnHandler::from(|data: Vec<u8>| {
    stdout().write_all(&data)
}));

Std channel:

use std::sync::mpsc;
let (s, r) = mpsc::channel();
on_data(s);

Tokio channel:

use tokio::sync::mpsc;
let (s, r) = mpsc::channel(123);
on_data(s);

Printing to console:

use std::io::{Write, stderr, stdout};
on_data(PrintDebug(stdout()));
on_data(PrintDisplay(stderr()));

Structs

A shareable wrapper for a Handler.

A handler implementation to print to any Writer (e.g. stdout) in Debug format.

A handler implementation to print to any Writer (e.g. stdout) in Display format.

Wrapper for Handlers that are non-future FnMuts.

Traits

A callable that can be used to hook into watchexec.