From 0583aba6bb7b901e907de68912480f4898b9aa3b Mon Sep 17 00:00:00 2001 From: Rasmus Thomsen Date: Sat, 18 Jan 2020 11:16:24 +0100 Subject: [PATCH] refactor(*): use futures 0.3 & tokio 0.2 --- Cargo.toml | 7 +- src/client.rs | 173 +++++++++++++++++++------------------- src/core.rs | 12 +-- src/frontend.rs | 77 ++++++++--------- src/protocol/client.rs | 40 +++++---- src/protocol/endpoint.rs | 28 +++--- src/protocol/server.rs | 32 ++++--- src/protocol/transport.rs | 22 ++--- 8 files changed, 195 insertions(+), 196 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 42ebe43..adb0c4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,12 +16,15 @@ edition = "2018" [dependencies] bytes = "0.4.12" -futures = "0.1.27" +futures = "0.3.1" +futures-core = "0.3" +futures-channel = "0.3" +futures-util = "0.3" log = "0.4.6" serde = "1.0.92" serde_derive = "1.0.92" serde_json = "1.0.39" -tokio = "0.1.21" +tokio = { version="0.2.1", features=["rt-core"] } tokio-codec = "0.1.1" tokio-process = "0.2.3" syntect = { version = "3.2.0", default-features = false } diff --git a/src/client.rs b/src/client.rs index 0b44f6b..bd52b4b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -36,7 +36,7 @@ impl Client { &self, method: &str, params: Value, - ) -> impl Future { + ) -> impl Future> { info!(">>> notification: method={}, params={}", method, ¶ms); self.0 .notify(method, params) @@ -50,7 +50,7 @@ impl Client { &self, method: &str, params: Value, - ) -> impl Future { + ) -> impl Future> { info!(">>> request : method={}, params={}", method, ¶ms); self.0 .request(method, params) @@ -66,10 +66,10 @@ impl Client { view_id: ViewId, method: &str, params: Option, - ) -> impl Future { + ) -> impl Future> { match get_edit_params(view_id, method, params) { - Ok(value) => Either::A(self.request("edit", value)), - Err(e) => Either::B(future::err(e)), + Ok(value) => Either::Left(self.request("edit", value)), + Err(e) => Either::Right(future::err(e)), } } @@ -81,10 +81,10 @@ impl Client { view_id: ViewId, method: &str, params: Option, - ) -> impl Future { + ) -> impl Future> { match get_edit_params(view_id, method, params) { - Ok(value) => Either::A(self.notify("edit", value)), - Err(e) => Either::B(future::err(e)), + Ok(value) => Either::Left(self.notify("edit", value)), + Err(e) => Either::Right(future::err(e)), } } @@ -98,7 +98,7 @@ impl Client { view_id: ViewId, first_line: u64, last_line: u64, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "scroll", Some(json!([first_line, last_line]))) } @@ -106,11 +106,11 @@ impl Client { &self, view_id: ViewId, line: u64, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "goto_line", Some(json!({ "line": line }))) } - pub fn copy(&self, view_id: ViewId) -> impl Future { + pub fn copy(&self, view_id: ViewId) -> impl Future> { self.edit_request(view_id, "copy", None as Option) } @@ -118,19 +118,19 @@ impl Client { &self, view_id: ViewId, buffer: &str, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "paste", Some(json!({ "chars": buffer }))) } - pub fn cut(&self, view_id: ViewId) -> impl Future { + pub fn cut(&self, view_id: ViewId) -> impl Future> { self.edit_request(view_id, "cut", None as Option) } - pub fn undo(&self, view_id: ViewId) -> impl Future { + pub fn undo(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "undo", None as Option) } - pub fn redo(&self, view_id: ViewId) -> impl Future { + pub fn redo(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "redo", None as Option) } @@ -141,7 +141,7 @@ impl Client { case_sensitive: bool, regex: bool, whole_words: bool, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "find", @@ -160,7 +160,7 @@ impl Client { wrap_around: bool, allow_same: bool, modify_selection: ModifySelection, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, command, @@ -177,7 +177,7 @@ impl Client { wrap_around: bool, allow_same: bool, modify_selection: ModifySelection, - ) -> impl Future { + ) -> impl Future> { self.find_other( view_id, "find_next", @@ -193,7 +193,7 @@ impl Client { wrap_around: bool, allow_same: bool, modify_selection: ModifySelection, - ) -> impl Future { + ) -> impl Future> { self.find_other( view_id, "find_previous", @@ -203,7 +203,7 @@ impl Client { ) } - pub fn find_all(&self, view_id: ViewId) -> impl Future { + pub fn find_all(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "find_all", None as Option) } @@ -211,7 +211,7 @@ impl Client { &self, view_id: ViewId, visible: bool, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "highlight_find", @@ -219,11 +219,11 @@ impl Client { ) } - pub fn left(&self, view_id: ViewId) -> impl Future { + pub fn left(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_left", None as Option) } - pub fn left_sel(&self, view_id: ViewId) -> impl Future { + pub fn left_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "move_left_and_modify_selection", @@ -231,11 +231,11 @@ impl Client { ) } - pub fn right(&self, view_id: ViewId) -> impl Future { + pub fn right(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_right", None as Option) } - pub fn right_sel(&self, view_id: ViewId) -> impl Future { + pub fn right_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "move_right_and_modify_selection", @@ -243,11 +243,11 @@ impl Client { ) } - pub fn up(&self, view_id: ViewId) -> impl Future { + pub fn up(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_up", None as Option) } - pub fn up_sel(&self, view_id: ViewId) -> impl Future { + pub fn up_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "move_up_and_modify_selection", @@ -255,11 +255,11 @@ impl Client { ) } - pub fn down(&self, view_id: ViewId) -> impl Future { + pub fn down(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_down", None as Option) } - pub fn down_sel(&self, view_id: ViewId) -> impl Future { + pub fn down_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "move_down_and_modify_selection", @@ -267,30 +267,30 @@ impl Client { ) } - pub fn backspace(&self, view_id: ViewId) -> impl Future { + pub fn backspace(&self, view_id: ViewId) -> impl Future> { self.del(view_id) } - pub fn delete(&self, view_id: ViewId) -> impl Future { + pub fn delete(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "delete_forward", None as Option) } - pub fn del(&self, view_id: ViewId) -> impl Future { + pub fn del(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "delete_backward", None as Option) } pub fn delete_word_backward( &self, view_id: ViewId, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "delete_word_backward", None as Option) } - pub fn page_up(&self, view_id: ViewId) -> impl Future { + pub fn page_up(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "scroll_page_up", None as Option) } - pub fn page_up_sel(&self, view_id: ViewId) -> impl Future { + pub fn page_up_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "page_up_and_modify_selection", @@ -298,11 +298,11 @@ impl Client { ) } - pub fn page_down(&self, view_id: ViewId) -> impl Future { + pub fn page_down(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "scroll_page_down", None as Option) } - pub fn page_down_sel(&self, view_id: ViewId) -> impl Future { + pub fn page_down_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "page_down_and_modify_selection", @@ -310,11 +310,11 @@ impl Client { ) } - pub fn line_start(&self, view_id: ViewId) -> impl Future { + pub fn line_start(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_to_left_end_of_line", None as Option) } - pub fn line_start_sel(&self, view_id: ViewId) -> impl Future { + pub fn line_start_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "move_to_left_end_of_line_and_modify_selection", @@ -322,11 +322,11 @@ impl Client { ) } - pub fn line_end(&self, view_id: ViewId) -> impl Future { + pub fn line_end(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_to_right_end_of_line", None as Option) } - pub fn line_end_sel(&self, view_id: ViewId) -> impl Future { + pub fn line_end_sel(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "move_to_right_end_of_line_and_modify_selection", @@ -334,7 +334,7 @@ impl Client { ) } - pub fn document_begin(&self, view_id: ViewId) -> impl Future { + pub fn document_begin(&self, view_id: ViewId) -> impl Future> { self.edit_notify( view_id, "move_to_beginning_of_document", @@ -345,7 +345,7 @@ impl Client { pub fn document_begin_sel( &self, view_id: ViewId, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "move_to_beginning_of_document_and_modify_selection", @@ -353,11 +353,14 @@ impl Client { ) } - pub fn document_end(&self, view_id: ViewId) -> impl Future { + pub fn document_end(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_to_end_of_document", None as Option) } - pub fn document_end_sel(&self, view_id: ViewId) -> impl Future { + pub fn document_end_sel( + &self, + view_id: ViewId, + ) -> impl Future> { self.edit_notify( view_id, "move_to_end_of_document_and_modify_selection", @@ -365,14 +368,14 @@ impl Client { ) } - pub fn select_all(&self, view_id: ViewId) -> impl Future { + pub fn select_all(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "select_all", None as Option) } pub fn collapse_selections( &self, view_id: ViewId, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "collapse_selections", None as Option) } @@ -380,27 +383,27 @@ impl Client { &self, view_id: ViewId, string: &str, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "insert", Some(json!({ "chars": string }))) } - pub fn insert_newline(&self, view_id: ViewId) -> impl Future { + pub fn insert_newline(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "insert_newline", None as Option) } - pub fn insert_tab(&self, view_id: ViewId) -> impl Future { + pub fn insert_tab(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "insert_tab", None as Option) } - pub fn f1(&self, view_id: ViewId) -> impl Future { + pub fn f1(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "debug_rewrap", None as Option) } - pub fn f2(&self, view_id: ViewId) -> impl Future { + pub fn f2(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "debug_test_fg_spans", None as Option) } - pub fn char(&self, view_id: ViewId, ch: char) -> impl Future { + pub fn char(&self, view_id: ViewId, ch: char) -> impl Future> { self.edit_notify(view_id, "insert", Some(json!({ "chars": ch }))) } @@ -410,7 +413,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "click", Some(json!([line, column, 0, 1]))) } @@ -419,7 +422,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { let ty = "point_select"; self.edit_notify( view_id, @@ -433,7 +436,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { let ty = "toggle_sel"; self.edit_notify( view_id, @@ -447,7 +450,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { let ty = "range_select"; self.edit_notify( view_id, @@ -461,7 +464,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { let ty = "range_select"; self.edit_notify( view_id, @@ -475,7 +478,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { let ty = "word_select"; self.edit_notify( view_id, @@ -489,7 +492,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { let ty = "multi_line_select"; self.edit_notify( view_id, @@ -503,7 +506,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { let ty = "multi_word_select"; self.edit_notify( view_id, @@ -517,7 +520,7 @@ impl Client { view_id: ViewId, line: u64, column: u64, - ) -> impl Future { + ) -> impl Future> { self.edit_notify(view_id, "drag", Some(json!([line, column, 0]))) } @@ -525,10 +528,7 @@ impl Client { /// ```ignore /// {"id":1,"method":"new_view","params":{"file_path":"foo/test.txt"}} /// ``` - pub fn new_view( - &self, - file_path: Option, - ) -> impl Future { + pub fn new_view(&self, file_path: Option) -> impl Future> { let params = if let Some(file_path) = file_path { json!({ "file_path": file_path }) } else { @@ -539,7 +539,7 @@ impl Client { } /// send a `"close_view"` notifycation to the core. - pub fn close_view(&self, view_id: ViewId) -> impl Future { + pub fn close_view(&self, view_id: ViewId) -> impl Future> { self.notify("close_view", json!({ "view_id": view_id })) } @@ -547,12 +547,12 @@ impl Client { &self, view_id: ViewId, file_path: &str, - ) -> impl Future { + ) -> impl Future> { let params = json!({"view_id": view_id, "file_path": file_path}); self.notify("save", params).and_then(|_| Ok(())) } - pub fn set_theme(&self, theme: &str) -> impl Future { + pub fn set_theme(&self, theme: &str) -> impl Future> { let params = json!({ "theme_name": theme }); self.notify("set_theme", params).and_then(|_| Ok(())) } @@ -561,7 +561,7 @@ impl Client { &self, config_dir: Option<&str>, client_extras_dir: Option<&str>, - ) -> impl Future { + ) -> impl Future> { let mut params = Map::new(); if let Some(path) = config_dir { let _ = params.insert("config_dir".into(), json!(path)); @@ -576,7 +576,7 @@ impl Client { &self, view_id: ViewId, name: &str, - ) -> impl Future { + ) -> impl Future> { let params = json!({"view_id": view_id, "plugin_name": name}); self.notify("start", params).and_then(|_| Ok(())) } @@ -585,7 +585,7 @@ impl Client { &self, view_id: ViewId, name: &str, - ) -> impl Future { + ) -> impl Future> { let params = json!({"view_id": view_id, "plugin_name": name}); self.notify("stop", params).and_then(|_| Ok(())) } @@ -596,7 +596,7 @@ impl Client { plugin: &str, method: &str, params: &Value, - ) -> impl Future { + ) -> impl Future> { let params = json!({ "view_id": view_id, "receiver": plugin, @@ -608,22 +608,25 @@ impl Client { self.notify("plugin_rpc", params).and_then(|_| Ok(())) } - pub fn outdent(&self, view_id: ViewId) -> impl Future { + pub fn outdent(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "outdent", None as Option) } - pub fn move_word_left(&self, view_id: ViewId) -> impl Future { + pub fn move_word_left(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "move_word_left", None as Option) } - pub fn move_word_right(&self, view_id: ViewId) -> impl Future { + pub fn move_word_right( + &self, + view_id: ViewId, + ) -> impl Future> { self.edit_notify(view_id, "move_word_right", None as Option) } pub fn move_word_left_sel( &self, view_id: ViewId, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "move_word_left_and_modify_selection", @@ -634,7 +637,7 @@ impl Client { pub fn move_word_right_sel( &self, view_id: ViewId, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "move_word_right_and_modify_selection", @@ -647,7 +650,7 @@ impl Client { view_id: ViewId, width: i32, height: i32, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "resize", @@ -663,7 +666,7 @@ impl Client { view_id: ViewId, chars: &str, preserve_case: bool, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "replace", @@ -674,11 +677,11 @@ impl Client { ) } - pub fn replace_next(&self, view_id: ViewId) -> impl Future { + pub fn replace_next(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "replace_next", None as Option) } - pub fn replace_all(&self, view_id: ViewId) -> impl Future { + pub fn replace_all(&self, view_id: ViewId) -> impl Future> { self.edit_notify(view_id, "replace_all", None as Option) } @@ -686,7 +689,7 @@ impl Client { &self, view_id: ViewId, lang_name: &str, - ) -> impl Future { + ) -> impl Future> { self.notify( "set_language", json!({ "view_id": view_id, "language_id": lang_name }), @@ -698,7 +701,7 @@ impl Client { &self, domain: &str, changes: Value, - ) -> impl Future { + ) -> impl Future> { self.notify( "modify_user_config", json!({ @@ -713,7 +716,7 @@ impl Client { view_id: ViewId, first_line: u64, last_line: u64, - ) -> impl Future { + ) -> impl Future> { self.edit_notify( view_id, "request_lines", diff --git a/src/core.rs b/src/core.rs index cba00e0..b2feed7 100644 --- a/src/core.rs +++ b/src/core.rs @@ -3,7 +3,8 @@ use crate::frontend::{Frontend, FrontendBuilder}; use crate::protocol::Endpoint; use crate::ClientError; use bytes::BytesMut; -use futures::{Future, Poll, Stream}; +use futures::{Future, Stream}; +use futures_core::task::{Context, Poll}; use std::io::{self, Read, Write}; use std::process::Command; use std::process::Stdio; @@ -43,8 +44,8 @@ impl Write for Core { } impl AsyncWrite for Core { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.stdin.shutdown() + fn poll_shutdown(&mut self, cx: &mut Context) -> Poll> { + self.stdin.poll_shutdown() } } @@ -118,10 +119,9 @@ impl CoreStderr { } impl Stream for CoreStderr { - type Item = String; - type Error = io::Error; + type Item = Result; - fn poll(&mut self) -> Poll, Self::Error> { + fn poll_next(&mut self, cx: &mut Context) -> Poll> { self.0.poll() } } diff --git a/src/frontend.rs b/src/frontend.rs index da0350d..8e6650c 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -6,7 +6,7 @@ use crate::structs::{ ThemeChanged, Update, UpdateCmds, }; use futures::{ - future::{self, Either, FutureResult}, + future::{self, Either}, Future, }; use serde_json::{from_value, to_value, Value}; @@ -34,10 +34,10 @@ pub enum XiNotification { /// The `Frontend` trait must be implemented by clients. It defines how the /// client handles notifications and requests coming from `xi-core`. pub trait Frontend { - type NotificationResult: IntoStaticFuture; + type NotificationResult: IntoStaticFuture>; fn handle_notification(&mut self, notification: XiNotification) -> Self::NotificationResult; - type MeasureWidthResult: IntoStaticFuture>, Error = ()>; + type MeasureWidthResult: IntoStaticFuture>, ()>>; fn handle_measure_width(&mut self, request: MeasureWidth) -> Self::MeasureWidthResult; } @@ -62,14 +62,11 @@ where } } -impl Service for F { +impl Service for F { type T = Value; type E = Value; - type RequestFuture = Box + 'static + Send>; - type NotificationFuture = Either< - <::NotificationResult as IntoStaticFuture>::Future, - FutureResult<(), ()>, - >; + type RequestFuture = Box> + 'static + Send>; + type NotificationFuture = <::NotificationResult as IntoStaticFuture>::Future; fn handle_request(&mut self, method: &str, params: Value) -> Self::RequestFuture { info!("<<< request: method={}, params={}", method, ¶ms); @@ -111,158 +108,158 @@ impl Service for F { info!("<<< notification: method={}, params={}", method, ¶ms); match method { "update" => match from_value::(params) { - Ok(update) => Either::A( + Ok(update) => Either::Left( self.handle_notification(XiNotification::Update(update)) .into_static_future(), ), Err(e) => { error!("received invalid update notification: {:?}", e); - Either::B(future::err(())) + Either::Right(future::err(())) } }, "scroll_to" => match from_value::(params) { - Ok(scroll_to) => Either::A( + Ok(scroll_to) => Either::Left( self.handle_notification(XiNotification::ScrollTo(scroll_to)) .into_static_future(), ), Err(e) => { error!("received invalid scroll_to notification: {:?}", e); - Either::B(future::err(())) + Either::Right(future::err(())) } }, "def_style" => match from_value::