Move `handle_messages` into an extension method for `Arc<Peer>`

Antonio Scandurra created

Change summary

zed/src/lib.rs       |  1 
zed/src/rpc.rs       | 58 ++++++++++++++++++++++++++++++++++++++++++++++
zed/src/util.rs      | 50 --------------------------------------
zed/src/workspace.rs |  7 +++--
4 files changed, 64 insertions(+), 52 deletions(-)

Detailed changes

zed/src/lib.rs 🔗

@@ -6,6 +6,7 @@ pub mod file_finder;
 pub mod language;
 pub mod menus;
 mod operation_queue;
+mod rpc;
 pub mod settings;
 mod sum_tree;
 #[cfg(test)]

zed/src/rpc.rs 🔗

@@ -0,0 +1,58 @@
+use postage::prelude::Stream;
+use std::{future::Future, sync::Arc};
+use zed_rpc::{proto, Peer, TypedEnvelope};
+
+pub trait MessageHandler<'a, M: proto::EnvelopedMessage> {
+    type Output: 'a + Future<Output = anyhow::Result<()>>;
+
+    fn handle(
+        &self,
+        message: TypedEnvelope<M>,
+        rpc: &'a Arc<Peer>,
+        cx: &'a mut gpui::AsyncAppContext,
+    ) -> Self::Output;
+}
+
+impl<'a, M, F, Fut> MessageHandler<'a, M> for F
+where
+    M: proto::EnvelopedMessage,
+    F: Fn(TypedEnvelope<M>, &'a Arc<Peer>, &'a mut gpui::AsyncAppContext) -> Fut,
+    Fut: 'a + Future<Output = anyhow::Result<()>>,
+{
+    type Output = Fut;
+
+    fn handle(
+        &self,
+        message: TypedEnvelope<M>,
+        rpc: &'a Arc<Peer>,
+        cx: &'a mut gpui::AsyncAppContext,
+    ) -> Self::Output {
+        (self)(message, rpc, cx)
+    }
+}
+
+pub trait PeerExt {
+    fn handle_messages<H, M>(&self, handler: H, cx: &mut gpui::MutableAppContext)
+    where
+        H: 'static + for<'a> MessageHandler<'a, M>,
+        M: proto::EnvelopedMessage;
+}
+
+impl PeerExt for Arc<Peer> {
+    fn handle_messages<H, M>(&self, handler: H, cx: &mut gpui::MutableAppContext)
+    where
+        H: 'static + for<'a> MessageHandler<'a, M>,
+        M: proto::EnvelopedMessage,
+    {
+        let rpc = self.clone();
+        let mut messages = smol::block_on(self.add_message_handler::<M>());
+        cx.spawn(|mut cx| async move {
+            while let Some(message) = messages.recv().await {
+                if let Err(err) = handler.handle(message, &rpc, &mut cx).await {
+                    log::error!("error handling message: {:?}", err);
+                }
+            }
+        })
+        .detach();
+    }
+}

zed/src/util.rs 🔗

@@ -1,7 +1,5 @@
-use postage::prelude::Stream;
 use rand::prelude::*;
-use std::{cmp::Ordering, future::Future, sync::Arc};
-use zed_rpc::{proto, Peer, TypedEnvelope};
+use std::cmp::Ordering;
 
 #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
 pub enum Bias {
@@ -55,52 +53,6 @@ where
     }
 }
 
-pub trait MessageHandler<'a, M: proto::EnvelopedMessage> {
-    type Output: 'a + Future<Output = anyhow::Result<()>>;
-
-    fn handle(
-        &self,
-        message: TypedEnvelope<M>,
-        rpc: Arc<Peer>,
-        cx: &'a mut gpui::AsyncAppContext,
-    ) -> Self::Output;
-}
-
-impl<'a, M, F, Fut> MessageHandler<'a, M> for F
-where
-    M: proto::EnvelopedMessage,
-    F: Fn(TypedEnvelope<M>, Arc<Peer>, &'a mut gpui::AsyncAppContext) -> Fut,
-    Fut: 'a + Future<Output = anyhow::Result<()>>,
-{
-    type Output = Fut;
-
-    fn handle(
-        &self,
-        message: TypedEnvelope<M>,
-        rpc: Arc<Peer>,
-        cx: &'a mut gpui::AsyncAppContext,
-    ) -> Self::Output {
-        (self)(message, rpc, cx)
-    }
-}
-
-pub fn handle_messages<H, M>(handler: H, rpc: &Arc<Peer>, cx: &mut gpui::MutableAppContext)
-where
-    H: 'static + for<'a> MessageHandler<'a, M>,
-    M: proto::EnvelopedMessage,
-{
-    let rpc = rpc.clone();
-    let mut messages = smol::block_on(rpc.add_message_handler::<M>());
-    cx.spawn(|mut cx| async move {
-        while let Some(message) = messages.recv().await {
-            if let Err(err) = handler.handle(message, rpc.clone(), &mut cx).await {
-                log::error!("error handling message: {:?}", err);
-            }
-        }
-    })
-    .detach();
-}
-
 pub struct RandomCharIter<T: Rng>(T);
 
 impl<T: Rng> RandomCharIter<T> {

zed/src/workspace.rs 🔗

@@ -4,9 +4,10 @@ pub mod pane_group;
 use crate::{
     editor::{Buffer, Editor},
     language::LanguageRegistry,
+    rpc::PeerExt as _,
     settings::Settings,
     time::ReplicaId,
-    util::{self, SurfResultExt as _},
+    util::SurfResultExt as _,
     worktree::{FileHandle, Worktree, WorktreeHandle},
     AppState,
 };
@@ -45,7 +46,7 @@ pub fn init(cx: &mut MutableAppContext, rpc: Arc<Peer>) {
     ]);
     pane::init(cx);
 
-    util::handle_messages(handle_open_buffer, &rpc, cx);
+    rpc.handle_messages(handle_open_buffer, cx);
 }
 
 pub struct OpenParams {
@@ -109,7 +110,7 @@ fn open_paths(params: &OpenParams, cx: &mut MutableAppContext) {
 
 async fn handle_open_buffer(
     request: TypedEnvelope<proto::OpenBuffer>,
-    rpc: Arc<Peer>,
+    rpc: &Arc<Peer>,
     cx: &mut AsyncAppContext,
 ) -> anyhow::Result<()> {
     let payload = request.payload();