Skip to content

Commit 8d0807a

Browse files
ndmitchellfacebook-github-bot
authored andcommitted
Make Watcher a struct, not a trait
Summary: There are only two reasonable implementations, and they both live in the same file, so make it know that internally. Important to allow fallback from watchman to notify transparently. Reviewed By: kinto0 Differential Revision: D73776764 fbshipit-source-id: f82aea11d683af1532dff5d606a891d1bb05f17d
1 parent 0aa9162 commit 8d0807a

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

pyrefly/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ path = "bin/main.rs"
2020
anyhow = "1.0.95"
2121
append-only-vec = "0.1.2"
2222
argfile = "0.1.6"
23-
async-trait = "0.1.86"
2423
blake3 = { version = "=1.5.5", features = ["mmap", "rayon", "traits-preview"] }
2524
bstr = { version = "1.10.0", features = ["serde", "std", "unicode"] }
2625
clap = { version = "4.5.30", features = ["derive", "env", "string", "unicode", "wrap_help"] }

pyrefly/bin/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use clap::Subcommand;
1818
use dupe::Dupe;
1919
use library::ArcId;
2020
use library::ConfigFile;
21-
use library::NotifyWatcher;
21+
use library::Watcher;
2222
use library::clap_env;
2323
use library::finder::ConfigFinder;
2424
use library::get_args_expanded;
@@ -109,7 +109,7 @@ async fn run_check(
109109
allow_forget: bool,
110110
) -> anyhow::Result<CommandExitStatus> {
111111
if watch {
112-
let watcher = NotifyWatcher::new(&files_to_check.roots())?;
112+
let watcher = Watcher::notify(&files_to_check.roots())?;
113113
args.run_watch(watcher, files_to_check, config_finder)
114114
.await?;
115115
Ok(CommandExitStatus::Success)

pyrefly/lib/commands/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ struct RequireLevels {
257257
default: Require,
258258
}
259259

260-
async fn get_watcher_events(watcher: &mut impl Watcher) -> anyhow::Result<CategorizedEvents> {
260+
async fn get_watcher_events(watcher: &mut Watcher) -> anyhow::Result<CategorizedEvents> {
261261
loop {
262262
let events = CategorizedEvents::new(watcher.wait().await?);
263263
if !events.is_empty() {
@@ -312,7 +312,7 @@ impl Args {
312312

313313
pub async fn run_watch(
314314
self,
315-
mut watcher: impl Watcher,
315+
mut watcher: Watcher,
316316
files_to_check: FilteredGlobs,
317317
config_finder: ConfigFinder,
318318
) -> anyhow::Result<()> {

pyrefly/lib/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ pub mod library {
8080
pub use crate::util::thread_pool::init_thread_pool;
8181
pub use crate::util::trace::init_tracing;
8282
#[cfg(not(target_arch = "wasm32"))]
83-
pub use crate::util::watcher::NotifyWatcher;
84-
#[cfg(not(target_arch = "wasm32"))]
8583
pub use crate::util::watcher::Watcher;
86-
#[cfg(not(target_arch = "wasm32"))]
87-
pub use crate::util::watcher::Watchman;
8884
}
8985
}
9086
}

pyrefly/lib/util/watcher.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::time::Duration;
1313
use std::time::Instant;
1414

1515
use anyhow::Context as _;
16-
use async_trait::async_trait;
1716
use notify::Event;
1817
use notify::RecursiveMode;
1918
use notify::Watcher as _;
@@ -30,28 +29,44 @@ use watchman_client::pdu::FileType;
3029
use watchman_client::pdu::SubscribeRequest;
3130
use watchman_client::pdu::SyncTimeout;
3231

33-
#[async_trait]
34-
pub trait Watcher {
35-
async fn wait(&mut self) -> anyhow::Result<Vec<Event>>;
32+
pub struct Watcher(WatcherInner);
33+
34+
enum WatcherInner {
35+
Watchman(Watchman),
36+
Notify(NotifyWatcher),
3637
}
3738

38-
pub struct NotifyWatcher {
39+
impl Watcher {
40+
pub async fn wait(&mut self) -> anyhow::Result<Vec<Event>> {
41+
match &mut self.0 {
42+
WatcherInner::Watchman(w) => w.wait().await,
43+
WatcherInner::Notify(w) => w.wait().await,
44+
}
45+
}
46+
47+
pub async fn watchman(path: &Path) -> anyhow::Result<Self> {
48+
Ok(Self(WatcherInner::Watchman(Watchman::new(path).await?)))
49+
}
50+
51+
pub fn notify(paths: &[PathBuf]) -> anyhow::Result<Self> {
52+
Ok(Self(WatcherInner::Notify(NotifyWatcher::new(paths)?)))
53+
}
54+
}
55+
56+
struct NotifyWatcher {
3957
receiver: Receiver<notify::Result<Event>>,
4058
}
4159

4260
impl NotifyWatcher {
43-
pub fn new(paths: &[PathBuf]) -> anyhow::Result<Self> {
61+
fn new(paths: &[PathBuf]) -> anyhow::Result<Self> {
4462
let (sender, receiver) = channel();
4563
let mut watcher = recommended_watcher(sender)?;
4664
for path in paths {
4765
watcher.watch(path, RecursiveMode::Recursive)?;
4866
}
4967
Ok(Self { receiver })
5068
}
51-
}
5269

53-
#[async_trait]
54-
impl Watcher for NotifyWatcher {
5570
async fn wait(&mut self) -> anyhow::Result<Vec<Event>> {
5671
let mut res = Vec::new();
5772
res.push(self.receiver.recv()??);
@@ -122,13 +137,12 @@ mod watchman_query {
122137
}
123138
}
124139

125-
pub struct Watchman {
140+
struct Watchman {
126141
root: PathBuf,
127142
subscription: Subscription<watchman_query::SubscriptionFields>,
128143
}
129144

130-
#[async_trait]
131-
impl Watcher for Watchman {
145+
impl Watchman {
132146
async fn wait(&mut self) -> anyhow::Result<Vec<Event>> {
133147
loop {
134148
match self
@@ -157,10 +171,8 @@ impl Watcher for Watchman {
157171
}
158172
}
159173
}
160-
}
161174

162-
impl Watchman {
163-
pub async fn new(path: &Path) -> anyhow::Result<Self> {
175+
async fn new(path: &Path) -> anyhow::Result<Self> {
164176
let root = CanonicalPath::canonicalize(path)?;
165177
let watchman_client = Connector::new()
166178
.connect()

0 commit comments

Comments
 (0)