From ff1a64c9d427ffa10101a384ff3d2124722492d6 Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Sat, 18 Sep 2021 21:50:59 +0200 Subject: [PATCH] PollWatcher: unify signature of `new` and `with_delay` * Move `event_handler` logic from `new` to `with_delay` to be able to call: ```rust let (tx, rx) = std::sync::mpsc::channel(); let mut watcher = PollWatcher::with_delay(tx, Duration::from_secs(1))?; ``` instead of: ```rust let (tx, rx) = std::sync::mpsc::channel(); let mut watcher = PollWatcher::with_delay(Arc::new(Mutex::new(tx)), Duration::from_secs(1))?; ``` * Add documentation --- src/poll.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/poll.rs b/src/poll.rs index e7a050ad..4ca721a6 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -43,13 +43,10 @@ fn emit_event(event_handler: &Mutex, res: Result) { } impl PollWatcher { - /// Create a [PollWatcher] which polls every `delay` milliseconds - pub fn with_delay( - event_handler: Arc>, - delay: Duration, - ) -> Result { + /// Create a new [PollWatcher] and set the poll frequency to `delay`. + pub fn with_delay(event_handler: F, delay: Duration) -> Result { let mut p = PollWatcher { - event_handler, + event_handler: Arc::new(Mutex::new(event_handler)), watches: Arc::new(Mutex::new(HashMap::new())), open: Arc::new(AtomicBool::new(true)), delay, @@ -276,8 +273,10 @@ impl PollWatcher { impl Watcher for PollWatcher { /// Create a new [PollWatcher]. + /// + /// The default poll frequency is 30 seconds. + /// Use [with_delay] to manually set the poll frequency. fn new(event_handler: F) -> Result { - let event_handler = Arc::new(Mutex::new(event_handler)); let delay = Duration::from_secs(30); Self::with_delay(event_handler, delay) }