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"
version = "0.0.0"
dependencies = [
"crossbeam-channel",
"fst",
"indexmap",
"nohash-hasher",

View file

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

View file

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

View file

@ -119,18 +119,19 @@ impl NotifyActor {
self.watched_dir_entries.clear();
self.watched_file_entries.clear();
let send = |msg| (self.sender)(msg);
send(loader::Message::Progress {
n_total,
n_done: LoadingProgress::Started,
config_version,
dir: None,
});
self.sender
.send(loader::Message::Progress {
n_total,
n_done: LoadingProgress::Started,
config_version,
dir: None,
})
.unwrap();
let (entry_tx, entry_rx) = unbounded();
let (watch_tx, watch_rx) = unbounded();
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);
if do_watch {
_ = entry_tx.send(entry.clone());
@ -140,25 +141,31 @@ impl NotifyActor {
entry,
do_watch,
|file| {
send(loader::Message::Progress {
n_total,
n_done: LoadingProgress::Progress(
processed.load(std::sync::atomic::Ordering::Relaxed),
),
dir: Some(file),
config_version,
})
self.sender
.send(loader::Message::Progress {
n_total,
n_done: LoadingProgress::Progress(
processed
.load(std::sync::atomic::Ordering::Relaxed),
),
dir: Some(file),
config_version,
})
.unwrap()
},
);
send(loader::Message::Loaded { files });
send(loader::Message::Progress {
n_total,
n_done: LoadingProgress::Progress(
processed.fetch_add(1, std::sync::atomic::Ordering::AcqRel) + 1,
),
config_version,
dir: None,
});
self.sender.send(loader::Message::Loaded { files }).unwrap();
self.sender
.send(loader::Message::Progress {
n_total,
n_done: LoadingProgress::Progress(
processed.fetch_add(1, std::sync::atomic::Ordering::AcqRel)
+ 1,
),
config_version,
dir: None,
})
.unwrap();
});
for path in watch_rx {
self.watch(&path);
@ -317,7 +324,7 @@ impl NotifyActor {
}
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"
indexmap.workspace = true
nohash-hasher.workspace = true
crossbeam-channel.workspace = true
paths.workspace = true
stdx.workspace = true

View file

@ -72,7 +72,7 @@ pub enum Message {
}
/// 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.
pub trait Handle: fmt::Debug {