From 9c8110de7b049145f4d0a129bb204160d87af721 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Mon, 8 Mar 2021 19:06:05 +0000 Subject: [PATCH] core: Allow for using only futures-util dependency (#609) * core: Prefer standard Future over futures' variant * core: Make futures::executor convenience support optional * core: Use only futures-util and (optionally) futures-executor * core: Keep futures re-export for backcompat --- core/Cargo.toml | 8 +++++++- core/examples/async.rs | 2 +- core/examples/middlewares.rs | 4 ++-- core/src/calls.rs | 4 ++-- core/src/delegates.rs | 2 +- core/src/io.rs | 10 ++++++---- core/src/lib.rs | 6 +++++- core/src/middleware.rs | 3 ++- 8 files changed, 26 insertions(+), 13 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 48681a436..08536ae8c 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -20,12 +20,18 @@ categories = [ [dependencies] log = "0.4" -futures = "0.3" +# 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" serde_json = "1.0" serde_derive = "1.0" [features] +default = ["futures-executor", "futures"] 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/examples/middlewares.rs b/core/examples/middlewares.rs index 37485c526..6d25352af 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_util::{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..713b47f97 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_util::{self, future, FutureExt}; use serde_json; use crate::calls::{ @@ -197,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. @@ -441,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()) } @@ -485,7 +488,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 +517,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..736363c3d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -27,7 +27,11 @@ 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; #[doc(hidden)] pub extern crate serde; @@ -45,7 +49,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..308a787c4 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_util::future::Either; +use std::future::Future; use std::pin::Pin; /// RPC middleware