Crate debounce[][src]

Expand description

Data structures and helpers for debouncing a stream of events: removing duplicate events occurring closely in time.

Examples of such events:

  • File write operations coming from a file system monitor
  • Similar log strings from a tail -f
  • Hardware signals from a sensor

Debouncers accept a raw stream of events and add a grace period during which similar events are considered duplicates. The event is delivered after the timeout expires after the last seen duplicate.

Data structures vs. debouncers

The debouncing logic itself is implemented as passive data structures in the buffer module. A buffer only implements .put and .get, and is independent of a particular way of organizing asynchronicity, be it periodic polling, threads, async streams or some other magic.

The crate also provides threaded debouncers built on top of the buffers, which is what you probably want to use most of the time.

Example usage

use debounce::EventDebouncer;
use std::thread::sleep;
use std::time::Duration;

let delay = Duration::from_millis(10);
let debouncer = EventDebouncer::new(delay, move |data: String| {
    println!("{}", data);


// prints one "foo" and one "bar" after the delay

A working command-line debouncer is implemented in examples/ and can be used right away:

inotifywait -m -e close_write . | debounce -d 100


pub use thread::EventDebouncer;
pub use thread::MixedEventDebouncer;