From fe87e1225649ec6dd4b77addede590951512d328 Mon Sep 17 00:00:00 2001 From: pantonshire Date: Sat, 19 Aug 2023 10:16:18 +0100 Subject: [PATCH] add examples dir --- examples/vector.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/vector.rs diff --git a/examples/vector.rs b/examples/vector.rs new file mode 100644 index 0000000..7fb1874 --- /dev/null +++ b/examples/vector.rs @@ -0,0 +1,33 @@ +use std::{error::Error, panic, thread, time::Duration}; + +use treacle::{fold, Debouncer}; + +fn main() -> Result<(), Box> { + // Create a debouncer which combines `u32` events which occur within the same 500ms window by + // pushing them to a `Vec`. + let (debouncer, rx) = + Debouncer::new(Duration::from_millis(500), fold::fold_vec_push::)?; + + // Spawn a thread which receives the debounced events and prints them. + let t = thread::spawn(move || { + while let Ok(event) = rx.recv() { + println!("{:?}", event); + } + }); + + // Create 100 raw events for the debouncer to debounce. + for i in 0..100 { + debouncer.debounce(i); + thread::sleep(Duration::from_millis(50)); + } + + // Dropping the debouncer will gracefully close the mpsc channel, stopping the loop in the + // other thread. + drop(debouncer); + + if let Err(err) = t.join() { + panic::resume_unwind(err); + } + + Ok(()) +}