pre-written fold functions for convenience
This commit is contained in:
1 parent
a25eb76de0
commit
70b1b2266d
2 files changed
+63
-18
No files matched your search
+61
@@ -0,0 +1,61 @@
|
||||
//! Pre-written fold functions which can be passed to [`Debouncer::new`](crate::Debouncer::new) to
|
||||
//! specify how raw events should be combined into a single debounced event.
|
||||
|
||||
/// A fold function which combines events of type `T` into a `Vec<T>` by pushing them.
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// # use treacle::{Debouncer, fold::fold_vec_push};
|
||||
/// use std::{time::Duration, thread};
|
||||
///
|
||||
/// let (debouncer, rx) = Debouncer::new(
|
||||
/// Duration::from_millis(100),
|
||||
/// fold_vec_push::<u32>
|
||||
/// )?;
|
||||
///
|
||||
/// debouncer.debounce(4);
|
||||
/// debouncer.debounce(5);
|
||||
///
|
||||
/// thread::sleep(Duration::from_millis(150));
|
||||
/// debouncer.debounce(3);
|
||||
/// debouncer.debounce(2);
|
||||
/// debouncer.debounce(1);
|
||||
///
|
||||
/// assert_eq!(rx.recv().unwrap(), vec![4, 5]);
|
||||
/// assert_eq!(rx.recv().unwrap(), vec![3, 2, 1]);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn fold_vec_push<T>(acc: Option<Vec<T>>, e: T) -> Vec<T> {
|
||||
let mut acc = acc.unwrap_or_default();
|
||||
acc.push(e);
|
||||
acc
|
||||
}
|
||||
|
||||
/// A fold function which discards all raw event data. Useful when you are just interested in the
|
||||
/// existence of events and not any data associated with them.
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// # use treacle::{Debouncer, fold::fold_unit};
|
||||
/// use std::{time::Duration, thread};
|
||||
///
|
||||
/// let (debouncer, rx) = Debouncer::new(
|
||||
/// Duration::from_millis(100),
|
||||
/// fold_unit::<u32>
|
||||
/// )?;
|
||||
///
|
||||
/// debouncer.debounce(4);
|
||||
/// debouncer.debounce(5);
|
||||
///
|
||||
/// thread::sleep(Duration::from_millis(150));
|
||||
/// debouncer.debounce(3);
|
||||
/// debouncer.debounce(2);
|
||||
/// debouncer.debounce(1);
|
||||
///
|
||||
/// assert_eq!(rx.recv().unwrap(), ());
|
||||
/// assert_eq!(rx.recv().unwrap(), ());
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn fold_unit<T>(_acc: Option<()>, _e: T) {}
|
||||
+2
-18
@@ -1,3 +1,5 @@
|
||||
pub mod fold;
|
||||
|
||||
use std::{
|
||||
sync::{Arc, Mutex, Condvar, mpsc},
|
||||
time::Duration,
|
||||
@@ -166,24 +168,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// impl<T> Debouncer<Vec<T>> {
|
||||
// #[inline]
|
||||
// pub fn debounce_push(&self, event_data: T) {
|
||||
// self.debounce(event_data, |acc, event_data| {
|
||||
// let mut acc = acc.unwrap_or_default();
|
||||
// acc.push(event_data);
|
||||
// acc
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl Debouncer<()> {
|
||||
// #[inline]
|
||||
// pub fn debounce_unit(&self) {
|
||||
// self.debounce((), |_acc, _event_data| ());
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<RawEvent, DebouncedEvent, FoldFn> Drop for Debouncer<RawEvent, DebouncedEvent, FoldFn> {
|
||||
fn drop(&mut self) {
|
||||
self.controller.notify_shutdown();
|
||||
|
||||
Reference in new issue
Block a user