-
Notifications
You must be signed in to change notification settings - Fork 249
feat: rework PathsMut to be more consistent #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
aaaac45
7530d19
f3a13c2
79f2d0e
cc1bc73
1a127c0
5997e67
e1a23fd
a357ff3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,8 @@ | |
|
|
||
| #![allow(non_upper_case_globals, dead_code)] | ||
|
|
||
| use crate::event::*; | ||
| use crate::{ | ||
| unbounded, Config, Error, EventHandler, PathsMut, RecursiveMode, Result, Sender, Watcher, | ||
| }; | ||
| use crate::{event::*, PathOp}; | ||
| use crate::{unbounded, Config, Error, EventHandler, RecursiveMode, Result, Sender, Watcher}; | ||
| use fsevent_sys as fs; | ||
| use fsevent_sys::core_foundation as cf; | ||
| use std::collections::HashMap; | ||
|
|
@@ -267,29 +265,6 @@ extern "C" { | |
| fn CFRunLoopIsWaiting(runloop: cf::CFRunLoopRef) -> cf::Boolean; | ||
| } | ||
|
|
||
| struct FsEventPathsMut<'a>(&'a mut FsEventWatcher); | ||
| impl<'a> FsEventPathsMut<'a> { | ||
| fn new(watcher: &'a mut FsEventWatcher) -> Self { | ||
| watcher.stop(); | ||
| Self(watcher) | ||
| } | ||
| } | ||
| impl PathsMut for FsEventPathsMut<'_> { | ||
| fn add(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<()> { | ||
| self.0.append_path(path, recursive_mode) | ||
| } | ||
|
|
||
| fn remove(&mut self, path: &Path) -> Result<()> { | ||
| self.0.remove_path(path) | ||
| } | ||
|
|
||
| fn commit(self: Box<Self>) -> Result<()> { | ||
| // ignore return error: may be empty path list | ||
| let _ = self.0.run(); | ||
| Ok(()) | ||
| } | ||
|
Comment on lines
-286
to
-290
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added How about just replacing impl Drop for FsEventPathsMut<'_> {
fn drop(&mut self) {
// ignore return error: may be empty path list
let _ = self.0.run();
}
}Would this change solve the inconsistent state? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would, but it seems a bit confusing, isn't it? I tried to rework it to be more clear and less verbose. Compare For known number of paths let mut paths = watcher.paths_mut();
paths.add(path1, recursive)?;
paths.add(path2, recursive)?;
paths.add(path3, recursive)?;
patch.commit()?;and watcher.update_paths(vec![
PathOp::watch_recursive(path1),
PathOp::watch_recursive(path2),
PathOp::watch_recursive(path3),
])?;For unknown number of paths let mut paths = watcher.paths_mut();
for path in my_paths {
paths.add(path, recursive)?;
}
paths.commit()?;and watcher.update_paths(my_paths.iter().map(PathOp::watch_recursive).collect());There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that It seems to me the I appreciate your effort to make the API more friendly. But personally, I'd always prefer to expose a lower-level API than a friendly API in a rust library. You can always create a I know the performance is not really relevant here, but as a Rust developer, it just feels weird to see an API requiring a If you still strongly feel cc @dfaust There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Builder api is a good choice there and it can be made without lib-support, you can just build a To be honest,
Yes, but I just try to explain my opinion, that an API that allows user to have notify objects in a state that can't be come in any other ways is not a good choice. The verbosity reduction is just a side effect. If we remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @branchseer I think about it in a descriptive manner: how I explain that thing to a user.
|
||
| } | ||
|
|
||
| impl FsEventWatcher { | ||
| fn from_event_handler(event_handler: Arc<Mutex<dyn EventHandler>>) -> Result<Self> { | ||
| Ok(FsEventWatcher { | ||
|
|
@@ -321,6 +296,23 @@ impl FsEventWatcher { | |
| result | ||
| } | ||
|
|
||
| fn update_paths_inner( | ||
| &mut self, | ||
| ops: Vec<crate::PathOp>, | ||
| ) -> crate::StdResult<(), crate::UpdatePathsError> { | ||
| self.stop(); | ||
|
|
||
| let result = crate::update_paths(ops, |op| match op { | ||
| crate::PathOp::Watch(path, config) => self.append_path(&path, config.recursive_mode()), | ||
| crate::PathOp::Unwatch(path) => self.remove_path(&path), | ||
| }); | ||
|
|
||
| // ignore return error: may be empty path list | ||
| let _ = self.run(); | ||
|
|
||
| result | ||
| } | ||
|
|
||
| #[inline] | ||
| fn is_running(&self) -> bool { | ||
| self.runloop.is_some() | ||
|
|
@@ -586,14 +578,14 @@ impl Watcher for FsEventWatcher { | |
| self.watch_inner(path, recursive_mode) | ||
| } | ||
|
|
||
| fn paths_mut<'me>(&'me mut self) -> Box<dyn PathsMut + 'me> { | ||
| Box::new(FsEventPathsMut::new(self)) | ||
| } | ||
|
|
||
| fn unwatch(&mut self, path: &Path) -> Result<()> { | ||
| self.unwatch_inner(path) | ||
| } | ||
|
|
||
| fn update_paths(&mut self, ops: Vec<PathOp>) -> crate::StdResult<(), crate::UpdatePathsError> { | ||
| self.update_paths_inner(ops) | ||
| } | ||
|
|
||
| fn configure(&mut self, config: Config) -> Result<bool> { | ||
| let (tx, rx) = unbounded(); | ||
| self.configure_raw_mode(config, tx); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.