Skip to content

Commit 9c8110d

Browse files
authored
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
1 parent 9d4b261 commit 9c8110d

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
lines changed

core/Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ categories = [
2020

2121
[dependencies]
2222
log = "0.4"
23-
futures = "0.3"
23+
# FIXME: Currently a lot of jsonrpc-* crates depend on entire `futures` being
24+
# re-exported but it's not strictly required for this crate. Either adapt the
25+
# remaining crates or settle for this re-export to be a single, common dependency
26+
futures = { version = "0.3", optional = true }
27+
futures-util = { version = "0.3", default-features = false, features = ["std"] }
28+
futures-executor = { version = "0.3", optional = true }
2429
serde = "1.0"
2530
serde_json = "1.0"
2631
serde_derive = "1.0"
2732

2833
[features]
34+
default = ["futures-executor", "futures"]
2935
arbitrary_precision = ["serde_json/arbitrary_precision"]
3036

3137
[badges]

core/examples/async.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use jsonrpc_core::*;
22

33
fn main() {
4-
futures::executor::block_on(async {
4+
futures_executor::block_on(async {
55
let mut io = IoHandler::new();
66

77
io.add_method("say_hello", |_: Params| async {

core/examples/middlewares.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use jsonrpc_core::futures::future::Either;
2-
use jsonrpc_core::futures::{Future, FutureExt};
1+
use jsonrpc_core::futures_util::{future::Either, FutureExt};
32
use jsonrpc_core::*;
3+
use std::future::Future;
44
use std::sync::atomic::{self, AtomicUsize};
55
use std::time::Instant;
66

core/src/calls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::types::{Error, Params, Value};
22
use crate::BoxFuture;
3-
use futures::Future;
43
use std::fmt;
4+
use std::future::Future;
55
use std::sync::Arc;
66

77
/// Metadata trait
@@ -19,7 +19,7 @@ pub trait WrapFuture<T, E> {
1919

2020
impl<T: Send + 'static, E: Send + 'static> WrapFuture<T, E> for Result<T, E> {
2121
fn into_future(self) -> BoxFuture<Result<T, E>> {
22-
Box::pin(futures::future::ready(self))
22+
Box::pin(async { self })
2323
}
2424
}
2525

core/src/delegates.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Delegate rpc calls
22
33
use std::collections::HashMap;
4+
use std::future::Future;
45
use std::sync::Arc;
56

67
use crate::calls::{Metadata, RemoteProcedure, RpcMethod, RpcNotification};
78
use crate::types::{Error, Params, Value};
89
use crate::BoxFuture;
9-
use futures::Future;
1010

1111
struct DelegateAsyncMethod<T, F> {
1212
delegate: Arc<T>,

core/src/io.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use std::collections::{
22
hash_map::{IntoIter, Iter},
33
HashMap,
44
};
5+
use std::future::Future;
56
use std::ops::{Deref, DerefMut};
67
use std::pin::Pin;
78
use std::sync::Arc;
89

9-
use futures::{self, future, Future, FutureExt};
10+
use futures_util::{self, future, FutureExt};
1011
use serde_json;
1112

1213
use crate::calls::{
@@ -197,8 +198,9 @@ impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S> {
197198
/// Handle given request synchronously - will block until response is available.
198199
/// If you have any asynchronous methods in your RPC it is much wiser to use
199200
/// `handle_request` instead and deal with asynchronous requests in a non-blocking fashion.
201+
#[cfg(feature = "futures-executor")]
200202
pub fn handle_request_sync(&self, request: &str, meta: T) -> Option<String> {
201-
futures::executor::block_on(self.handle_request(request, meta))
203+
futures_executor::block_on(self.handle_request(request, meta))
202204
}
203205

204206
/// Handle given request asynchronously.
@@ -441,6 +443,7 @@ impl<M: Metadata + Default> IoHandler<M> {
441443
/// Handle given request synchronously - will block until response is available.
442444
/// If you have any asynchronous methods in your RPC it is much wiser to use
443445
/// `handle_request` instead and deal with asynchronous requests in a non-blocking fashion.
446+
#[cfg(feature = "futures-executor")]
444447
pub fn handle_request_sync(&self, request: &str) -> Option<String> {
445448
self.0.handle_request_sync(request, M::default())
446449
}
@@ -485,7 +488,6 @@ fn write_response(response: Response) -> String {
485488
mod tests {
486489
use super::{Compatibility, IoHandler};
487490
use crate::types::Value;
488-
use futures::future;
489491

490492
#[test]
491493
fn test_io_handler() {
@@ -515,7 +517,7 @@ mod tests {
515517
fn test_async_io_handler() {
516518
let mut io = IoHandler::new();
517519

518-
io.add_method("say_hello", |_| future::ready(Ok(Value::String("hello".to_string()))));
520+
io.add_method("say_hello", |_| async { Ok(Value::String("hello".to_string())) });
519521

520522
let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
521523
let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

core/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ extern crate log;
2727
#[macro_use]
2828
extern crate serde_derive;
2929

30+
#[cfg(feature = "futures")]
3031
pub use futures;
32+
#[cfg(feature = "futures-executor")]
33+
pub use futures_executor;
34+
pub use futures_util;
3135

3236
#[doc(hidden)]
3337
pub extern crate serde;
@@ -45,7 +49,7 @@ pub mod types;
4549
pub type Result<T> = std::result::Result<T, Error>;
4650

4751
/// A `Future` trait object.
48-
pub type BoxFuture<T> = Pin<Box<dyn futures::Future<Output = T> + Send>>;
52+
pub type BoxFuture<T> = Pin<Box<dyn std::future::Future<Output = T> + Send>>;
4953

5054
pub use crate::calls::{
5155
Metadata, RemoteProcedure, RpcMethod, RpcMethodSimple, RpcMethodSync, RpcNotification, RpcNotificationSimple,

core/src/middleware.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use crate::calls::Metadata;
44
use crate::types::{Call, Output, Request, Response};
5-
use futures::{future::Either, Future};
5+
use futures_util::future::Either;
6+
use std::future::Future;
67
use std::pin::Pin;
78

89
/// RPC middleware

0 commit comments

Comments
 (0)