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

Adapt to Futures 0.1.15 by not using boxed() #196

Merged
merged 11 commits into from
Sep 1, 2017
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
2 changes: 1 addition & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use jsonrpc_core::futures::Future;
fn main() {
let io = IoHandler::new();
io.add_async_method("say_hello", |_params: Params| {
futures::finished(Value::String("hello".into())).boxed()
Box::new(futures::finished(Value::String("hello".into())))
});

let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
Expand Down
4 changes: 2 additions & 2 deletions core/examples/middlewares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ impl Middleware<Meta> for MyMiddleware {
let request_number = self.0.fetch_add(1, atomic::Ordering::SeqCst);
println!("Processing request {}: {:?}, {:?}", request_number, request, meta);

next(request, meta).map(move |res| {
Box::new(next(request, meta).map(move |res| {
println!("Processing took: {:?}", start.elapsed());
res
}).boxed()
}))
}
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/calls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;
use types::{Params, Value, Error};
use futures::{BoxFuture, Future};
use futures::Future;
use BoxFuture;

/// Metadata trait
pub trait Metadata: Default + Clone + Send + 'static {}
Expand Down Expand Up @@ -60,7 +61,7 @@ impl<F: Send + Sync + 'static, X: Send + 'static> RpcMethodSimple for F where
X: Future<Item=Value, Error=Error>,
{
fn call(&self, params: Params) -> BoxFuture<Value, Error> {
self(params).boxed()
Box::new(self(params))
}
}

Expand All @@ -78,7 +79,7 @@ impl<F: Send + Sync + 'static, X: Send + 'static, T> RpcMethod<T> for F where
X: Future<Item=Value, Error=Error>,
{
fn call(&self, params: Params, meta: T) -> BoxFuture<Value, Error> {
self(params, meta).boxed()
Box::new(self(params, meta))
}
}

Expand Down
11 changes: 6 additions & 5 deletions core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use std::collections::HashMap;
use std::ops::{Deref, DerefMut};

use serde_json;
use futures::{self, future, Future, BoxFuture};
use futures::{self, future, Future};

use calls::{RemoteProcedure, Metadata, RpcMethodSync, RpcMethodSimple, RpcMethod, RpcNotificationSimple, RpcNotification};
use middleware::{self, Middleware};
use types::{Params, Error, ErrorCode, Version};
use types::{Request, Response, Call, Output};
use BoxFuture;

/// A type representing middleware or RPC response before serialization.
pub type FutureResponse = BoxFuture<Option<Response>, ()>;
Expand Down Expand Up @@ -178,11 +179,11 @@ impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S> {
Ok(request) => B(self.handle_rpc_request(request, meta)),
};

result.map(|response| {
Box::new(result.map(|response| {
let res = response.map(write_response);
debug!(target: "rpc", "Response: {:?}.", res);
res
}).boxed()
}))
}

/// Handle deserialized RPC request.
Expand Down Expand Up @@ -234,9 +235,9 @@ impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S> {
};

match result {
Ok(result) => A(result
Ok(result) => A(Box::new(result
.then(move |result| futures::finished(Some(Output::from(result, id, jsonrpc))))
.boxed()),
)),
Err(err) => B(futures::finished(Some(Output::from(Err(err), id, jsonrpc)))),
}
},
Expand Down
3 changes: 3 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ mod io;
mod middleware;
pub mod types;

/// A `Future` trait object.
pub type BoxFuture<T, E> = Box<futures::Future<Item = T, Error = E> + Send>;

pub use calls::{RemoteProcedure, Metadata, RpcMethodSync, RpcMethodSimple, RpcMethod, RpcNotificationSimple, RpcNotification};
pub use io::{Compatibility, IoHandler, MetaIoHandler, FutureResponse};
pub use middleware::{Middleware, Noop as NoopMiddleware};
Expand Down
5 changes: 3 additions & 2 deletions core/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use calls::Metadata;
use types::{Request, Response};
use futures::{Future, BoxFuture};
use futures::Future;
use BoxFuture;

/// RPC middleware
pub trait Middleware<M: Metadata>: Send + Sync + 'static {
Expand All @@ -27,7 +28,7 @@ impl<M: Metadata> Middleware<M> for Noop {
F: FnOnce(Request, M) -> X + Send,
X: Future<Item=Option<Response>, Error=()> + Send + 'static,
{
process(request, meta).boxed()
Box::new(process(request, meta))
}
}

Expand Down
4 changes: 2 additions & 2 deletions http/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use hyper::{self, mime, server, Method};
use hyper::header::{self, Headers};
use unicase::Ascii;

use jsonrpc::{Metadata, Middleware, NoopMiddleware};
use jsonrpc::futures::{Future, Poll, Async, BoxFuture, Stream};
use jsonrpc::{BoxFuture, Metadata, Middleware, NoopMiddleware};
use jsonrpc::futures::{Future, Poll, Async, Stream};
use response::Response;
use server_utils::cors;

Expand Down
6 changes: 3 additions & 3 deletions http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use std::sync::{mpsc, Arc};
use std::net::SocketAddr;

use hyper::server;
use jsonrpc::MetaIoHandler;
use jsonrpc::futures::{self, Future, IntoFuture, BoxFuture, Stream};
use jsonrpc::{BoxFuture, MetaIoHandler};
use jsonrpc::futures::{self, Future, IntoFuture, Stream};
use jsonrpc::futures::sync::oneshot;
use server_utils::reactor::{Remote, UninitializedRemote};

Expand Down Expand Up @@ -132,7 +132,7 @@ impl<T> From<Option<T>> for RequestMiddlewareAction where
},
Some(handler) => RequestMiddlewareAction::Respond {
should_validate_hosts: true,
handler: handler.into_future().boxed(),
handler: Box::new(handler.into_future()),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions http/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ fn serve() -> Server {
let mut io = IoHandler::default();
io.add_method("hello", |_params: Params| Ok(Value::String("world".into())));
io.add_async_method("hello_async", |_params: Params| {
futures::finished(Value::String("world".into())).boxed()
Box::new(futures::finished(Value::String("world".into())))
});
io.add_async_method("hello_async2", |_params: Params| {
let (c, p) = futures::oneshot();
thread::spawn(move || {
thread::sleep(::std::time::Duration::from_millis(10));
c.send(Value::String("world".into())).unwrap();
});
p.map_err(|_| Error::invalid_request()).boxed()
Box::new(p.map_err(|_| Error::invalid_request()))
});

ServerBuilder::new(io)
Expand Down
1 change: 0 additions & 1 deletion ipc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ tokio-service = "0.1"
jsonrpc-core = { version = "7.1", path = "../core" }
jsonrpc-server-utils = { version = "7.1", path = "../server-utils" }
parity-tokio-ipc = { git = "https://github.com/nikvolf/parity-tokio-ipc" }
bytes = "0.4"

[dev-dependencies]
env_logger = "0.4"
Expand Down
1 change: 0 additions & 1 deletion ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extern crate jsonrpc_core as jsonrpc;
extern crate jsonrpc_server_utils as server_utils;
extern crate parity_tokio_ipc;
extern crate tokio_service;
extern crate bytes;

#[macro_use] extern crate log;

Expand Down
14 changes: 7 additions & 7 deletions ipc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::sync::Arc;
use tokio_service::{self, Service as TokioService};
use jsonrpc::futures::{future, Future, Stream, Sink};
use jsonrpc::futures::sync::{mpsc, oneshot};
use jsonrpc::{Metadata, MetaIoHandler, Middleware, NoopMiddleware};
use jsonrpc::futures::BoxFuture;
use jsonrpc::{BoxFuture, Metadata, MetaIoHandler, Middleware, NoopMiddleware};

use server_utils::tokio_core::reactor::Remote;
use server_utils::tokio_io::AsyncRead;
Expand Down Expand Up @@ -118,7 +117,7 @@ impl<M: Metadata, S: Middleware<M>> ServerBuilder<M, S> {
Ok(l) => l,
Err(e) => {
start_signal.send(Err(e)).expect("Cannot fail since receiver never dropped before receiving");
return future::ok(()).boxed();
return Box::new(future::ok(())) as BoxFuture<_, _>;
}
};

Expand Down Expand Up @@ -180,10 +179,11 @@ impl<M: Metadata, S: Middleware<M>> ServerBuilder<M, S> {
});

let stop = stop_receiver.map_err(|_| std::io::ErrorKind::Interrupted.into());
server.select(stop)
.map(|_| ())
.map_err(|_| ())
.boxed()
Box::new(
server.select(stop)
.map(|_| ())
.map_err(|_| ())
)
});

match start_receiver.wait().expect("Message should always be sent") {
Expand Down
7 changes: 3 additions & 4 deletions macros/examples/meta-macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ extern crate jsonrpc_tcp_server;

use std::collections::BTreeMap;

use jsonrpc_core::{MetaIoHandler, Metadata, Error, Value};
use jsonrpc_core::futures::{self, BoxFuture, Future};
use jsonrpc_core::{futures, BoxFuture, MetaIoHandler, Metadata, Error, Value};

#[derive(Clone, Default)]
struct Meta(String);
Expand Down Expand Up @@ -47,11 +46,11 @@ impl Rpc for RpcImpl {
}

fn call(&self, x: u64) -> BoxFuture<String, Error> {
futures::finished(format!("OK: {}", x)).boxed()
Box::new(futures::finished(format!("OK: {}", x)))
}

fn call_meta(&self, meta: Self::Metadata, map: BTreeMap<String, Value>) -> BoxFuture<String, Error> {
futures::finished(format!("From: {}, got: {:?}", meta.0, map)).boxed()
Box::new(futures::finished(format!("From: {}, got: {:?}", meta.0, map)))
}
}

Expand Down
12 changes: 6 additions & 6 deletions macros/examples/pubsub-macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::thread;
use std::sync::{atomic, Arc, RwLock};
use std::collections::HashMap;

use jsonrpc_core::{Metadata, Error, ErrorCode};
use jsonrpc_core::futures::{BoxFuture, Future, future};
use jsonrpc_core::{BoxFuture, Metadata, Error, ErrorCode};
use jsonrpc_core::futures::{future, Future};
use jsonrpc_pubsub::{Session, PubSubMetadata, PubSubHandler, SubscriptionId};

use jsonrpc_macros::pubsub;
Expand Down Expand Up @@ -76,15 +76,15 @@ impl Rpc for RpcImpl {

fn unsubscribe(&self, id: SubscriptionId) -> BoxFuture<bool, Error> {
let removed = self.active.write().unwrap().remove(&id);
if removed.is_some() {
future::ok(true).boxed()
Box::new(if removed.is_some() {
future::ok(true)
} else {
future::err(Error {
code: ErrorCode::InvalidParams,
message: "Invalid subscription.".into(),
data: None,
}).boxed()
}
})
})
}
}

Expand Down
5 changes: 2 additions & 3 deletions macros/examples/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ extern crate jsonrpc_core;
#[macro_use]
extern crate jsonrpc_macros;

use jsonrpc_core::{IoHandler, Error};
use jsonrpc_core::futures::{self, BoxFuture, Future};
use jsonrpc_core::{futures, BoxFuture, IoHandler, Error};

build_rpc_trait! {
pub trait Rpc {
Expand Down Expand Up @@ -33,7 +32,7 @@ impl Rpc for RpcImpl {
}

fn call(&self, _: u64) -> BoxFuture<String, Error> {
futures::finished("OK".to_owned()).boxed()
Box::new(futures::finished("OK".to_owned()))
}
}

Expand Down
Loading