Avoid clones

This commit is contained in:
Aleksey Kladov 2018-09-02 15:18:43 +03:00
parent 80be61ed78
commit 1329dd4e28
4 changed files with 14 additions and 14 deletions

View file

@ -51,7 +51,7 @@ pub fn run_server(
pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<RawRequest> { pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<RawRequest> {
match req.cast::<Shutdown>() { match req.cast::<Shutdown>() {
Ok((id, ())) => { Ok((id, ())) => {
let resp = RawResponse::ok::<Shutdown>(id, ()); let resp = RawResponse::ok::<Shutdown>(id, &());
sender.send(RawMessage::Response(resp)); sender.send(RawMessage::Response(resp));
None None
} }
@ -72,7 +72,7 @@ fn initialize(
msg => msg =>
bail!("expected initialize request, got {:?}", msg), bail!("expected initialize request, got {:?}", msg),
}; };
let resp = RawResponse::ok::<Initialize>(id, InitializeResult { capabilities: caps }); let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps });
sender.send(RawMessage::Response(resp)); sender.send(RawMessage::Response(resp));
match receiver.recv() { match receiver.recv() {
Some(RawMessage::Notification(n)) => { Some(RawMessage::Notification(n)) => {

View file

@ -88,7 +88,7 @@ impl RawMessage {
} }
impl RawRequest { impl RawRequest {
pub fn new<R>(id: u64, params: R::Params) -> RawRequest pub fn new<R>(id: u64, params: &R::Params) -> RawRequest
where where
R: Request, R: Request,
R::Params: Serialize, R::Params: Serialize,
@ -96,7 +96,7 @@ impl RawRequest {
RawRequest { RawRequest {
id: id, id: id,
method: R::METHOD.to_string(), method: R::METHOD.to_string(),
params: to_value(&params).unwrap(), params: to_value(params).unwrap(),
} }
} }
pub fn cast<R>(self) -> ::std::result::Result<(u64, R::Params), RawRequest> pub fn cast<R>(self) -> ::std::result::Result<(u64, R::Params), RawRequest>
@ -114,7 +114,7 @@ impl RawRequest {
} }
impl RawResponse { impl RawResponse {
pub fn ok<R>(id: u64, result: R::Result) -> RawResponse pub fn ok<R>(id: u64, result: &R::Result) -> RawResponse
where R: Request, where R: Request,
R::Result: Serialize, R::Result: Serialize,
{ {
@ -135,14 +135,14 @@ impl RawResponse {
} }
impl RawNotification { impl RawNotification {
pub fn new<N>(params: N::Params) -> RawNotification pub fn new<N>(params: &N::Params) -> RawNotification
where where
N: Notification, N: Notification,
N::Params: Serialize, N::Params: Serialize,
{ {
RawNotification { RawNotification {
method: N::METHOD.to_string(), method: N::METHOD.to_string(),
params: to_value(&params).unwrap(), params: to_value(params).unwrap(),
} }
} }
pub fn cast<N>(self) -> ::std::result::Result<N::Params, RawNotification> pub fn cast<N>(self) -> ::std::result::Result<N::Params, RawNotification>

View file

@ -130,7 +130,7 @@ fn main_loop_inner(
Event::Ws(ws) => { Event::Ws(ws) => {
match ws { match ws {
Ok(ws) => { Ok(ws) => {
let not = RawNotification::new::<req::DidReloadWorkspace>(vec![ws.clone()]); let not = RawNotification::new::<req::DidReloadWorkspace>(&vec![ws.clone()]);
msg_sender.send(RawMessage::Notification(not)); msg_sender.send(RawMessage::Notification(not));
state.set_workspaces(vec![ws]); state.set_workspaces(vec![ws]);
state_changed = true; state_changed = true;
@ -288,7 +288,7 @@ fn on_notification(
let file_id = state.remove_mem_file(path.as_path())?; let file_id = state.remove_mem_file(path.as_path())?;
subs.remove_sub(file_id); subs.remove_sub(file_id);
let params = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() }; let params = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() };
let not = RawNotification::new::<req::PublishDiagnostics>(params); let not = RawNotification::new::<req::PublishDiagnostics>(&params);
msg_sender.send(RawMessage::Notification(not)); msg_sender.send(RawMessage::Notification(not));
return Ok(()) return Ok(())
} }
@ -326,7 +326,7 @@ impl<'a> PoolDispatcher<'a> {
let sender = self.sender.clone(); let sender = self.sender.clone();
self.pool.execute(move || { self.pool.execute(move || {
let resp = match f(world, params, token) { let resp = match f(world, params, token) {
Ok(resp) => RawResponse::ok::<R>(id, resp), Ok(resp) => RawResponse::ok::<R>(id, &resp),
Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()), Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()),
}; };
let task = Task::Respond(resp); let task = Task::Respond(resp);
@ -363,7 +363,7 @@ fn update_file_notifications_on_threadpool(
error!("failed to compute diagnostics: {:?}", e) error!("failed to compute diagnostics: {:?}", e)
} }
Ok(params) => { Ok(params) => {
let not = RawNotification::new::<req::PublishDiagnostics>(params); let not = RawNotification::new::<req::PublishDiagnostics>(&params);
sender.send(Task::Notify(not)); sender.send(Task::Notify(not));
} }
} }
@ -372,7 +372,7 @@ fn update_file_notifications_on_threadpool(
error!("failed to compute decorations: {:?}", e) error!("failed to compute decorations: {:?}", e)
} }
Ok(params) => { Ok(params) => {
let not = RawNotification::new::<req::PublishDecorations>(params); let not = RawNotification::new::<req::PublishDecorations>(&params);
sender.send(Task::Notify(not)) sender.send(Task::Notify(not))
} }
} }

View file

@ -83,7 +83,7 @@ impl Server {
}; };
for (path, text) in files { for (path, text) in files {
res.send_notification(RawNotification::new::<DidOpenTextDocument>( res.send_notification(RawNotification::new::<DidOpenTextDocument>(
DidOpenTextDocumentParams { &DidOpenTextDocumentParams {
text_document: TextDocumentItem { text_document: TextDocumentItem {
uri: Url::from_file_path(path).unwrap(), uri: Url::from_file_path(path).unwrap(),
language_id: "rust".to_string(), language_id: "rust".to_string(),
@ -149,7 +149,7 @@ impl Server {
R: Request, R: Request,
R::Params: Serialize, R::Params: Serialize,
{ {
let r = RawRequest::new::<R>(id, params); let r = RawRequest::new::<R>(id, &params);
self.sender.as_ref() self.sender.as_ref()
.unwrap() .unwrap()
.send(RawMessage::Request(r)); .send(RawMessage::Request(r));