#events #stream #duplicate #structures #helper #removing #debouncing

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

#431 in Data structures

Download history 64/week @ 2024-01-08 123/week @ 2024-01-15 88/week @ 2024-01-22 84/week @ 2024-01-29 59/week @ 2024-02-05 235/week @ 2024-02-12 241/week @ 2024-02-19 279/week @ 2024-02-26 210/week @ 2024-03-04 272/week @ 2024-03-11 295/week @ 2024-03-18 189/week @ 2024-03-25 194/week @ 2024-04-01 214/week @ 2024-04-08 227/week @ 2024-04-15 302/week @ 2024-04-22

947 downloads per month
Used in 5 crates (2 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