#events #event-stream #duplicate #debouncing #structures #sensor #event-debouncer

debounce

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

5 releases

0.2.2 Jun 29, 2023
0.2.1 Dec 16, 2021
0.2.0 Oct 22, 2021
0.1.1 Oct 19, 2021
0.1.0 Oct 19, 2021

#14 in #duplicate

Download history 346/week @ 2024-06-10 352/week @ 2024-06-17 340/week @ 2024-06-24 347/week @ 2024-07-01 280/week @ 2024-07-08 431/week @ 2024-07-15 370/week @ 2024-07-22 244/week @ 2024-07-29 62/week @ 2024-08-05 168/week @ 2024-08-12 42/week @ 2024-08-19 94/week @ 2024-08-26 96/week @ 2024-09-02 210/week @ 2024-09-09 93/week @ 2024-09-16 247/week @ 2024-09-23

670 downloads per month
Used in 7 crates (3 directly)

BSD-3-Clause

15KB
270 lines

debounce

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

https://docs.rs/debounce


lib.rs:

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);
});

debouncer.put(String::from("foo"));
debouncer.put(String::from("foo"));
debouncer.put(String::from("bar"));
sleep(delay);

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

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

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

No runtime deps