Use Sender instead of boxed closure in vfs

This commit is contained in:
mo8it 2024-08-09 23:40:32 +02:00
parent cb6b2ab5ba
commit 3b560a550a
6 changed files with 38 additions and 31 deletions

1
Cargo.lock generated
View file

@ -2317,6 +2317,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
name = "vfs" name = "vfs"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"crossbeam-channel",
"fst", "fst",
"indexmap", "indexmap",
"nohash-hasher", "nohash-hasher",

View file

@ -64,8 +64,7 @@ pub fn load_workspace(
let (sender, receiver) = unbounded(); let (sender, receiver) = unbounded();
let mut vfs = vfs::Vfs::default(); let mut vfs = vfs::Vfs::default();
let mut loader = { let mut loader = {
let loader = let loader = vfs_notify::NotifyHandle::spawn(sender);
vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
Box::new(loader) Box::new(loader)
}; };

View file

@ -185,8 +185,7 @@ impl GlobalState {
pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState { pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
let loader = { let loader = {
let (sender, receiver) = unbounded::<vfs::loader::Message>(); let (sender, receiver) = unbounded::<vfs::loader::Message>();
let handle: vfs_notify::NotifyHandle = let handle: vfs_notify::NotifyHandle = vfs::loader::Handle::spawn(sender);
vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
let handle = Box::new(handle) as Box<dyn vfs::loader::Handle>; let handle = Box::new(handle) as Box<dyn vfs::loader::Handle>;
Handle { handle, receiver } Handle { handle, receiver }
}; };

View file

@ -119,18 +119,19 @@ impl NotifyActor {
self.watched_dir_entries.clear(); self.watched_dir_entries.clear();
self.watched_file_entries.clear(); self.watched_file_entries.clear();
let send = |msg| (self.sender)(msg); self.sender
send(loader::Message::Progress { .send(loader::Message::Progress {
n_total, n_total,
n_done: LoadingProgress::Started, n_done: LoadingProgress::Started,
config_version, config_version,
dir: None, dir: None,
}); })
.unwrap();
let (entry_tx, entry_rx) = unbounded(); let (entry_tx, entry_rx) = unbounded();
let (watch_tx, watch_rx) = unbounded(); let (watch_tx, watch_rx) = unbounded();
let processed = AtomicUsize::new(0); let processed = AtomicUsize::new(0);
config.load.into_par_iter().enumerate().for_each(move |(i, entry)| { config.load.into_par_iter().enumerate().for_each(|(i, entry)| {
let do_watch = config.watch.contains(&i); let do_watch = config.watch.contains(&i);
if do_watch { if do_watch {
_ = entry_tx.send(entry.clone()); _ = entry_tx.send(entry.clone());
@ -140,25 +141,31 @@ impl NotifyActor {
entry, entry,
do_watch, do_watch,
|file| { |file| {
send(loader::Message::Progress { self.sender
n_total, .send(loader::Message::Progress {
n_done: LoadingProgress::Progress( n_total,
processed.load(std::sync::atomic::Ordering::Relaxed), n_done: LoadingProgress::Progress(
), processed
dir: Some(file), .load(std::sync::atomic::Ordering::Relaxed),
config_version, ),
}) dir: Some(file),
config_version,
})
.unwrap()
}, },
); );
send(loader::Message::Loaded { files }); self.sender.send(loader::Message::Loaded { files }).unwrap();
send(loader::Message::Progress { self.sender
n_total, .send(loader::Message::Progress {
n_done: LoadingProgress::Progress( n_total,
processed.fetch_add(1, std::sync::atomic::Ordering::AcqRel) + 1, n_done: LoadingProgress::Progress(
), processed.fetch_add(1, std::sync::atomic::Ordering::AcqRel)
config_version, + 1,
dir: None, ),
}); config_version,
dir: None,
})
.unwrap();
}); });
for path in watch_rx { for path in watch_rx {
self.watch(&path); self.watch(&path);
@ -317,7 +324,7 @@ impl NotifyActor {
} }
fn send(&self, msg: loader::Message) { fn send(&self, msg: loader::Message) {
(self.sender)(msg); self.sender.send(msg).unwrap();
} }
} }

View file

@ -18,6 +18,7 @@ tracing.workspace = true
fst = "0.4.7" fst = "0.4.7"
indexmap.workspace = true indexmap.workspace = true
nohash-hasher.workspace = true nohash-hasher.workspace = true
crossbeam-channel.workspace = true
paths.workspace = true paths.workspace = true
stdx.workspace = true stdx.workspace = true

View file

@ -72,7 +72,7 @@ pub enum Message {
} }
/// Type that will receive [`Messages`](Message) from a [`Handle`]. /// Type that will receive [`Messages`](Message) from a [`Handle`].
pub type Sender = Box<dyn Fn(Message) + Send + Sync>; pub type Sender = crossbeam_channel::Sender<Message>;
/// Interface for reading and watching files. /// Interface for reading and watching files.
pub trait Handle: fmt::Debug { pub trait Handle: fmt::Debug {