-
Notifications
You must be signed in to change notification settings - Fork 274
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
Switch to std::future::Future #485
Comments
Yeah, I think it would be great to switch at least @tomaka you have some experience with porting to |
Most of the transition should be straight-forward, except that you might have to add One part where it might be difficult is that libraries compatible with new futures have ditched lots of combinators in favour of async-await-friendly methods that borrow objects. For example, calling Also, unless that changed, if you use tokio for networking, you will have to use tokio for executing futures as well. I'd encourage using |
According to Rust Forge, version 1.39 will land on November 7th, which will include stable async/await. In that case, should we start transitioning to |
I think it's worth start the transition, I didn't have time to look into it yet. I'd be really happy to accept a PR though if you are interested in working on this @ebkalderon :) |
@tomusdrw Sure, I suppose I can try! |
I've successfully converted EDIT: See the switch-to-std-future branch for details. One particularly unfortunate change in |
Can we just convert |
@tomusdrw Almost, but not quite. It seems we will also need to make some changes to |
Apparently, there was mention of |
We can force the methods to return a Please also follow https://github.com/paritytech/jsonrpsee/ as that's a new library we are working on that is fully |
Now async/await is stable - do you need any help with migration @ebkalderon? |
@frondeus Stability of async/await wasn't exactly a blocker for starting migration, but rather the removal of the |
Good news, looks like |
Hey @ebkalderon, now that I'm trying to call an async function from within my RPC handler but I find it troublesome to convert the result to |
It is not back yet - it was reverted cause of some strange performance drops. |
Oh I see, thanks for the info @frondeus! Then I assume the only way to use
|
If anyone wants to use async functions in RPC handlers, this is how I resolved it. First, you'll need the futures = { version = "0.3.2", features = ["compat"] } Then you can have an RPC handler like this (see this and this): use jsonrpc_core::BoxFuture;
use futures::future::{FutureExt, TryFutureExt};
fn handler(&self, ...) -> BoxFuture<...> {
// ...
// this block should produce an `RpcResult`
let fut = async move {
// ...
Ok(res)
};
Box::new(fut.boxed().compat())
} Additionally, you need to make sure that all your types are use futures::future::Future;
let fut2: Box<dyn Future<Output=Result<..., ....>> + Send> = Box::new(fut); You also need to make sure that there's no reference to let field = self.field.clone();
let fut = async move {
// ...
let x = field.bar().await;
// ...
}; (Sorry if this is off-topic; it seems useful to me before this crate is updated to use |
@Thegaram Thanks for the tips! Using your instructions, I was able to get I'm still holding out for |
@Thegaram I've found that you can eliminate the double nested future boxing by creating the following type alias to use in place of pub type BoxFuture<T> = Compat<Pin<Box<dyn Future<Output = Result<T, Error>> + Send>>>; Then you would change your RPC handler example to this instead: use futures::future::{FutureExt, TryFutureExt};
type BoxFuture<T> = Compat<Pin<Box<dyn Future<Output = Result<T, Error>> + Send>>>;
fn handler(&self, ...) -> BoxFuture<...> {
// ...
// this block should produce an `RpcResult`
let fut = async move {
// ...
Ok(res)
};
fut.boxed().compat()
} |
Hi! Thanks for all the work involved in making this library! I'm exploring what options for using JSON-RPC in Rust there are for a project I'm working on now and I was wondering what's the status of converting |
Transitioning from
futures
0.1 tostd::future::Future
would greatly improve the user experience in the near future, especially with async/await stabilizing in Rust 1.39 andstd::future::Future
compatible versions oftokio
,hyper
, andtower
on the horizon. It seems thatjsonrpc-core
is currently the only library in my personal JSON-RPC projects that still relies exclusively onfutures
0.1, and it is blocking me from switching over to the new trait completely. I understand if this issue will sit on the backburner for the time being until at leasttokio
0.2.0 is released, though.The text was updated successfully, but these errors were encountered: