Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Allow for using only futures-util dependency #609

Merged
merged 4 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion core/examples/async.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/examples/middlewares.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions core/src/calls.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,7 +19,7 @@ pub trait WrapFuture<T, E> {

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

Expand Down
2 changes: 1 addition & 1 deletion core/src/delegates.rs
Original file line number Diff line number Diff line change
@@ -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<T, F> {
delegate: Arc<T>,
Expand Down
10 changes: 6 additions & 4 deletions core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -197,8 +198,9 @@ impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S> {
/// 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<String> {
futures::executor::block_on(self.handle_request(request, meta))
futures_executor::block_on(self.handle_request(request, meta))
}

/// Handle given request asynchronously.
Expand Down Expand Up @@ -441,6 +443,7 @@ impl<M: Metadata + Default> IoHandler<M> {
/// 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<String> {
self.0.handle_request_sync(request, M::default())
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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}"#;
Expand Down
6 changes: 5 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,7 +49,7 @@ pub mod types;
pub type Result<T> = std::result::Result<T, Error>;

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

pub use crate::calls::{
Metadata, RemoteProcedure, RpcMethod, RpcMethodSimple, RpcMethodSync, RpcNotification, RpcNotificationSimple,
Expand Down
3 changes: 2 additions & 1 deletion core/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down