From 52de08330f5052a1f10c5d46e1352ca86b622d71 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Aug 2018 23:56:19 +0300 Subject: dedupe --- crates/server/src/dispatch.rs | 27 ++++++++------------------- crates/server/src/main.rs | 8 +------- crates/server/src/req.rs | 21 ++++++++++++++++++++- 3 files changed, 29 insertions(+), 27 deletions(-) (limited to 'crates/server') diff --git a/crates/server/src/dispatch.rs b/crates/server/src/dispatch.rs index d9681db40..6a609f10e 100644 --- a/crates/server/src/dispatch.rs +++ b/crates/server/src/dispatch.rs @@ -9,20 +9,17 @@ use drop_bomb::DropBomb; use ::{ Result, - req::{Request, Notification}, + req::{ClientRequest, Notification}, io::{Io, RawMsg, RawResponse, RawRequest, RawNotification}, }; -pub struct Responder { +pub struct Responder { id: u64, bomb: DropBomb, ph: PhantomData, } -impl Responder - where - R::Params: DeserializeOwned, - R::Result: Serialize, +impl Responder { pub fn response(self, io: &mut Io, resp: Result) -> Result<()> { match resp { @@ -52,11 +49,8 @@ impl Responder } -fn parse_request_as(raw: RawRequest) -> Result<::std::result::Result<(R::Params, Responder), RawRequest>> - where - R: Request, - R::Params: DeserializeOwned, - R::Result: Serialize, +fn parse_request_as(raw: RawRequest) + -> Result<::std::result::Result<(R::Params, Responder), RawRequest>> { if raw.method != R::METHOD { return Ok(Err(raw)); @@ -73,9 +67,7 @@ fn parse_request_as(raw: RawRequest) -> Result<::std::result::Result<(R::Para pub fn handle_request(req: &mut Option, f: F) -> Result<()> where - R: Request, - R::Params: DeserializeOwned, - R::Result: Serialize, + R: ClientRequest, F: FnOnce(R::Params, Responder) -> Result<()> { match req.take() { @@ -90,11 +82,8 @@ pub fn handle_request(req: &mut Option, f: F) -> Result<()> } } -pub fn expect_request(io: &mut Io, raw: RawRequest) -> Result)>> - where - R: Request, - R::Params: DeserializeOwned, - R::Result: Serialize, +pub fn expect_request(io: &mut Io, raw: RawRequest) + -> Result)>> { let ret = match parse_request_as::(raw)? { Ok(x) => Some(x), diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index f521271fb..dfde8afb1 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -28,12 +28,10 @@ use threadpool::ThreadPool; use crossbeam_channel::{bounded, Sender, Receiver}; use flexi_logger::Logger; use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier}; -use serde::{ser::Serialize, de::DeserializeOwned}; use libanalysis::{WorldState, World}; use ::{ io::{Io, RawMsg, RawRequest}, - req::Request, handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, }; @@ -261,17 +259,13 @@ fn main_loop( } } -fn handle_request_on_threadpool( +fn handle_request_on_threadpool( req: &mut Option, pool: &ThreadPool, world: &WorldState, sender: &Sender, f: fn(World, R::Params) -> Result, ) -> Result<()> - where - R: Request + Send + 'static, - R::Params: DeserializeOwned + Send + 'static, - R::Result: Serialize + Send + 'static, { dispatch::handle_request::(req, |params, resp| { let world = world.snapshot(); diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index 64e7ef4ae..10f0b2919 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs @@ -1,10 +1,29 @@ +use serde::{ser::Serialize, de::DeserializeOwned}; use languageserver_types::{TextDocumentIdentifier, Range}; pub use languageserver_types::{ request::*, notification::*, - InitializeResult, PublishDiagnosticsParams + InitializeResult, PublishDiagnosticsParams, }; + +pub trait ClientRequest: Send + 'static { + type Params: DeserializeOwned + Send + 'static; + type Result: Serialize + Send + 'static; + const METHOD: &'static str; +} + +impl ClientRequest for T + where T: Request + Send + 'static, + T::Params: DeserializeOwned + Send + 'static, + T::Result: Serialize + Send + 'static, +{ + type Params = ::Params; + type Result = ::Result; + const METHOD: &'static str = ::METHOD; +} + + pub enum SyntaxTree {} impl Request for SyntaxTree { -- cgit v1.2.3