From afe96a63374e63a107c3e5c5b21d005119453e3b Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 20 Jan 2021 11:23:48 +0100 Subject: [PATCH 1/4] core: Prefer standard Future over futures' variant --- core/examples/middlewares.rs | 4 ++-- core/src/calls.rs | 4 ++-- core/src/delegates.rs | 2 +- core/src/io.rs | 6 +++--- core/src/lib.rs | 2 +- core/src/middleware.rs | 3 ++- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/core/examples/middlewares.rs b/core/examples/middlewares.rs index 37485c526..69c541d9c 100644 --- a/core/examples/middlewares.rs +++ b/core/examples/middlewares.rs @@ -1,6 +1,6 @@ -use jsonrpc_core::futures::future::Either; -use jsonrpc_core::futures::{Future, FutureExt}; +use jsonrpc_core::futures::{future::Either, FutureExt}; use jsonrpc_core::*; +use std::future::Future; use std::sync::atomic::{self, AtomicUsize}; use std::time::Instant; diff --git a/core/src/calls.rs b/core/src/calls.rs index 36718c94f..dfc8dcb16 100644 --- a/core/src/calls.rs +++ b/core/src/calls.rs @@ -1,7 +1,7 @@ use crate::types::{Error, Params, Value}; use crate::BoxFuture; -use futures::Future; use std::fmt; +use std::future::Future; use std::sync::Arc; /// Metadata trait @@ -19,7 +19,7 @@ pub trait WrapFuture { impl WrapFuture for Result { fn into_future(self) -> BoxFuture> { - Box::pin(futures::future::ready(self)) + Box::pin(async { self }) } } diff --git a/core/src/delegates.rs b/core/src/delegates.rs index 4d43152b3..15b60e65a 100644 --- a/core/src/delegates.rs +++ b/core/src/delegates.rs @@ -1,12 +1,12 @@ //! Delegate rpc calls use std::collections::HashMap; +use std::future::Future; use std::sync::Arc; use crate::calls::{Metadata, RemoteProcedure, RpcMethod, RpcNotification}; use crate::types::{Error, Params, Value}; use crate::BoxFuture; -use futures::Future; struct DelegateAsyncMethod { delegate: Arc, diff --git a/core/src/io.rs b/core/src/io.rs index 153455be1..1d6f3429e 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -2,11 +2,12 @@ use std::collections::{ hash_map::{IntoIter, Iter}, HashMap, }; +use std::future::Future; use std::ops::{Deref, DerefMut}; use std::pin::Pin; use std::sync::Arc; -use futures::{self, future, Future, FutureExt}; +use futures::{self, future, FutureExt}; use serde_json; use crate::calls::{ @@ -485,7 +486,6 @@ fn write_response(response: Response) -> String { mod tests { use super::{Compatibility, IoHandler}; use crate::types::Value; - use futures::future; #[test] fn test_io_handler() { @@ -515,7 +515,7 @@ mod tests { fn test_async_io_handler() { let mut io = IoHandler::new(); - io.add_method("say_hello", |_| future::ready(Ok(Value::String("hello".to_string())))); + io.add_method("say_hello", |_| async { Ok(Value::String("hello".to_string())) }); let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#; let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#; diff --git a/core/src/lib.rs b/core/src/lib.rs index dc1f4059f..456f28c2a 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -45,7 +45,7 @@ pub mod types; pub type Result = std::result::Result; /// A `Future` trait object. -pub type BoxFuture = Pin + Send>>; +pub type BoxFuture = Pin + Send>>; pub use crate::calls::{ Metadata, RemoteProcedure, RpcMethod, RpcMethodSimple, RpcMethodSync, RpcNotification, RpcNotificationSimple, diff --git a/core/src/middleware.rs b/core/src/middleware.rs index 71dd725ee..eb85bee9a 100644 --- a/core/src/middleware.rs +++ b/core/src/middleware.rs @@ -2,7 +2,8 @@ use crate::calls::Metadata; use crate::types::{Call, Output, Request, Response}; -use futures::{future::Either, Future}; +use futures::future::Either; +use std::future::Future; use std::pin::Pin; /// RPC middleware From b5709eaf8cf89981e19221fb45e965e76aa8ae7b Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 20 Jan 2021 12:23:36 +0100 Subject: [PATCH 2/4] core: Make futures::executor convenience support optional --- core/Cargo.toml | 4 +++- core/examples/async.rs | 2 +- core/src/io.rs | 4 +++- core/src/lib.rs | 2 ++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 48681a436..bc406dc6f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -20,12 +20,14 @@ categories = [ [dependencies] log = "0.4" -futures = "0.3" +futures = { version = "0.3", default-features = false, features = ["std"] } +futures-executor = { version = "0.3", optional = true } serde = "1.0" serde_json = "1.0" serde_derive = "1.0" [features] +default = ["futures-executor"] arbitrary_precision = ["serde_json/arbitrary_precision"] [badges] diff --git a/core/examples/async.rs b/core/examples/async.rs index 0e65b5b19..b6397b240 100644 --- a/core/examples/async.rs +++ b/core/examples/async.rs @@ -1,7 +1,7 @@ use jsonrpc_core::*; fn main() { - futures::executor::block_on(async { + futures_executor::block_on(async { let mut io = IoHandler::new(); io.add_method("say_hello", |_: Params| async { diff --git a/core/src/io.rs b/core/src/io.rs index 1d6f3429e..0fa3367cc 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -198,8 +198,9 @@ impl> MetaIoHandler { /// Handle given request synchronously - will block until response is available. /// If you have any asynchronous methods in your RPC it is much wiser to use /// `handle_request` instead and deal with asynchronous requests in a non-blocking fashion. + #[cfg(feature = "futures-executor")] pub fn handle_request_sync(&self, request: &str, meta: T) -> Option { - futures::executor::block_on(self.handle_request(request, meta)) + futures_executor::block_on(self.handle_request(request, meta)) } /// Handle given request asynchronously. @@ -442,6 +443,7 @@ impl IoHandler { /// Handle given request synchronously - will block until response is available. /// If you have any asynchronous methods in your RPC it is much wiser to use /// `handle_request` instead and deal with asynchronous requests in a non-blocking fashion. + #[cfg(feature = "futures-executor")] pub fn handle_request_sync(&self, request: &str) -> Option { self.0.handle_request_sync(request, M::default()) } diff --git a/core/src/lib.rs b/core/src/lib.rs index 456f28c2a..491583865 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -27,6 +27,8 @@ extern crate log; #[macro_use] extern crate serde_derive; +#[cfg(feature = "futures-executor")] +pub use futures_executor; pub use futures; #[doc(hidden)] From c32813c7ea8a14448fbdcb54ef5e99b3a0b770bc Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 20 Jan 2021 12:41:36 +0100 Subject: [PATCH 3/4] core: Use only futures-util and (optionally) futures-executor --- core/Cargo.toml | 2 +- core/examples/middlewares.rs | 2 +- core/src/io.rs | 2 +- core/src/lib.rs | 2 +- core/src/middleware.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index bc406dc6f..852b28daf 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -20,7 +20,7 @@ categories = [ [dependencies] log = "0.4" -futures = { version = "0.3", default-features = false, features = ["std"] } +futures-util = { version = "0.3", default-features = false, features = ["std"] } futures-executor = { version = "0.3", optional = true } serde = "1.0" serde_json = "1.0" diff --git a/core/examples/middlewares.rs b/core/examples/middlewares.rs index 69c541d9c..6d25352af 100644 --- a/core/examples/middlewares.rs +++ b/core/examples/middlewares.rs @@ -1,4 +1,4 @@ -use jsonrpc_core::futures::{future::Either, FutureExt}; +use jsonrpc_core::futures_util::{future::Either, FutureExt}; use jsonrpc_core::*; use std::future::Future; use std::sync::atomic::{self, AtomicUsize}; diff --git a/core/src/io.rs b/core/src/io.rs index 0fa3367cc..713b47f97 100644 --- a/core/src/io.rs +++ b/core/src/io.rs @@ -7,7 +7,7 @@ use std::ops::{Deref, DerefMut}; use std::pin::Pin; use std::sync::Arc; -use futures::{self, future, FutureExt}; +use futures_util::{self, future, FutureExt}; use serde_json; use crate::calls::{ diff --git a/core/src/lib.rs b/core/src/lib.rs index 491583865..9e269bd3a 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -29,7 +29,7 @@ extern crate serde_derive; #[cfg(feature = "futures-executor")] pub use futures_executor; -pub use futures; +pub use futures_util; #[doc(hidden)] pub extern crate serde; diff --git a/core/src/middleware.rs b/core/src/middleware.rs index eb85bee9a..308a787c4 100644 --- a/core/src/middleware.rs +++ b/core/src/middleware.rs @@ -2,7 +2,7 @@ use crate::calls::Metadata; use crate::types::{Call, Output, Request, Response}; -use futures::future::Either; +use futures_util::future::Either; use std::future::Future; use std::pin::Pin; From 9a0d2889a6160e7221f5c56fae8d34bf35cf3573 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 20 Jan 2021 13:19:44 +0100 Subject: [PATCH 4/4] core: Keep futures re-export for backcompat --- core/Cargo.toml | 6 +++++- core/src/lib.rs | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 852b28daf..08536ae8c 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -20,6 +20,10 @@ categories = [ [dependencies] log = "0.4" +# FIXME: Currently a lot of jsonrpc-* crates depend on entire `futures` being +# re-exported but it's not strictly required for this crate. Either adapt the +# remaining crates or settle for this re-export to be a single, common dependency +futures = { version = "0.3", optional = true } futures-util = { version = "0.3", default-features = false, features = ["std"] } futures-executor = { version = "0.3", optional = true } serde = "1.0" @@ -27,7 +31,7 @@ serde_json = "1.0" serde_derive = "1.0" [features] -default = ["futures-executor"] +default = ["futures-executor", "futures"] arbitrary_precision = ["serde_json/arbitrary_precision"] [badges] diff --git a/core/src/lib.rs b/core/src/lib.rs index 9e269bd3a..736363c3d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -27,6 +27,8 @@ extern crate log; #[macro_use] extern crate serde_derive; +#[cfg(feature = "futures")] +pub use futures; #[cfg(feature = "futures-executor")] pub use futures_executor; pub use futures_util;