debounce/
lib.rs

1//! Data structures and helpers for *debouncing* a stream of events: removing
2//! duplicate events occurring closely in time.
3//!
4//! Examples of such events:
5//!
6//! - File write operations coming from a file system monitor
7//! - Similar log strings from a `tail -f`
8//! - Hardware signals from a sensor
9//!
10//! Debouncers accept a raw stream of events and add a grace period during which
11//! similar events are considered duplicates. The event is delivered after the
12//! timeout expires after the last seen duplicate.
13//!
14//!
15//! ## Data structures vs. debouncers
16//!
17//! The debouncing logic itself is implemented as passive data structures in the
18//! [buffer] module. A buffer only implements `.put` and `.get`, and
19//! is independent of a particular way of organizing asynchronicity, be it
20//! periodic polling, threads, async streams or some other magic.
21//!
22//! The crate also provides [threaded debouncers](thread) built on top of the
23//! buffers, which is what you probably want to use most of the time.
24//!
25//!
26//! ## Example usage
27//!
28//! ```rust
29//! use debounce::EventDebouncer;
30//! use std::thread::sleep;
31//! use std::time::Duration;
32//!
33//! let delay = Duration::from_millis(10);
34//! let debouncer = EventDebouncer::new(delay, move |data: String| {
35//!     println!("{}", data);
36//! });
37//!
38//! debouncer.put(String::from("foo"));
39//! debouncer.put(String::from("foo"));
40//! debouncer.put(String::from("bar"));
41//! sleep(delay);
42//!
43//! // prints one "foo" and one "bar" after the delay
44//! ```
45//!
46//! A working command-line debouncer is implemented in `examples/debounce.rs`
47//! and can be used right away:
48//!
49//! ```sh
50//! inotifywait -m -e close_write . | debounce -d 100
51//! ```
52
53pub mod buffer;
54pub mod thread;
55
56pub use thread::{EventDebouncer, MixedEventDebouncer};