From a57faf07f68a2aca806af7f0984ff4d8a550ebaf Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Thu, 19 Dec 2019 10:06:02 +0900 Subject: [PATCH] Added a constructor for `WatchHandle` (#734) Closes #731 --- src/directory/directory.rs | 6 +++--- src/directory/mod.rs | 4 +--- src/directory/watch_event_router.rs | 9 ++++++++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/directory/directory.rs b/src/directory/directory.rs index 6b5092542..78642a00d 100644 --- a/src/directory/directory.rs +++ b/src/directory/directory.rs @@ -119,7 +119,7 @@ pub trait Directory: DirectoryClone + fmt::Debug + Send + Sync + 'static { /// Specifically, subsequent writes or flushes should /// have no effect on the returned `ReadOnlySource` object. /// - /// You should only use this to read files create with [`open_write`] + /// You should only use this to read files create with [Directory::open_write]. fn open_read(&self, path: &Path) -> result::Result; /// Removes a file @@ -160,7 +160,7 @@ pub trait Directory: DirectoryClone + fmt::Debug + Send + Sync + 'static { /// /// This should only be used for small files. /// - /// You should only use this to read files create with [`atomic_write`] + /// You should only use this to read files create with [Directory::atomic_write]. fn atomic_read(&self, path: &Path) -> Result, OpenReadError>; /// Atomically replace the content of a file with data. @@ -197,7 +197,7 @@ pub trait Directory: DirectoryClone + fmt::Debug + Send + Sync + 'static { /// Registers a callback that will be called whenever a change on the `meta.json` /// using the `atomic_write` API is detected. /// - /// The behavior when using `.watch()` on a file using `.open_write(...)` is, on the other + /// The behavior when using `.watch()` on a file using [Directory::open_write] is, on the other /// hand, undefined. /// /// The file will be watched for the lifetime of the returned `WatchHandle`. The caller is diff --git a/src/directory/mod.rs b/src/directory/mod.rs index c7a836909..df5e55d81 100644 --- a/src/directory/mod.rs +++ b/src/directory/mod.rs @@ -23,11 +23,9 @@ pub use self::directory::{Directory, DirectoryClone}; pub use self::directory_lock::{Lock, INDEX_WRITER_LOCK, META_LOCK}; pub use self::ram_directory::RAMDirectory; pub use self::read_only_source::ReadOnlySource; -pub(crate) use self::watch_event_router::WatchCallbackList; -pub use self::watch_event_router::{WatchCallback, WatchHandle}; +pub use self::watch_event_router::{WatchCallback, WatchCallbackList, WatchHandle}; use std::io::{self, BufWriter, Write}; use std::path::PathBuf; - /// Outcome of the Garbage collection pub struct GarbageCollectionResult { /// List of files that were deleted in this cycle diff --git a/src/directory/watch_event_router.rs b/src/directory/watch_event_router.rs index 8b6acf1ce..947b1feac 100644 --- a/src/directory/watch_event_router.rs +++ b/src/directory/watch_event_router.rs @@ -24,13 +24,20 @@ pub struct WatchCallbackList { #[derive(Clone)] pub struct WatchHandle(Arc); +impl WatchHandle { + /// Create a WatchHandle handle. + pub fn new(watch_callback: Arc) -> WatchHandle { + WatchHandle(watch_callback) + } +} + impl WatchCallbackList { /// Suscribes a new callback and returns a handle that controls the lifetime of the callback. pub fn subscribe(&self, watch_callback: WatchCallback) -> WatchHandle { let watch_callback_arc = Arc::new(watch_callback); let watch_callback_weak = Arc::downgrade(&watch_callback_arc); self.router.write().unwrap().push(watch_callback_weak); - WatchHandle(watch_callback_arc) + WatchHandle::new(watch_callback_arc) } fn list_callback(&self) -> Vec> {