0

I am writing rpc server with rust using jsonrpc-core and jsonrpc-http-server. Here is my code:

let mut io = IoHandler::new();

io.add_method("validate_public_key", |params: Params| {
    let map: HashMap<String, Value> = params.parse().unwrap();
    println!("Params : {:?}", map);

    let public_key = map.get("public_key").unwrap().as_str().unwrap();

    println!("Public Key : {:?}", public_key);

    Ok(Value::String(public_key.to_string()))
});

let server = ServerBuilder::new(io)
    .start_http(&rpc_address.parse().unwrap())
    .unwrap();

How do I remove the anonymous function from add_method? I want to write this method in sperate file and use it here as io.add_method("validate_public_key", |params: Params| validate);

Here is what I tried so far:

pub fn validate(params: Params) -> Result<Value> {
    let map: Map<String, Value> = params.parse().unwrap();
    println!("Params : {:?}", map);

    let public_key = map.get("public_key").unwrap().as_str().unwrap();

    println!("Public Key : {:?}", public_key);

    Ok(Value::String(public_key.to_string()))
}

It give following error:

error[E0277]: the trait bound `fn(jsonrpc_core::types::params::Params)
-> std::result::Result<serde_json::Value, jsonrpc_core::types::error::Error> {app::rpc::validate_public_key}:
futures::future::Future` is not satisfied   --> src/main.rs:37:12    
   |
37 |         io.add_method("validate_public_key", |params: Params| validate_public_key);    
   |            ^^^^^^^^^^ the trait `futures::future::Future` is not implemented for `fn(jsonrpc_core::types::params::Params) -> std::result::Result<serde_json::Value, jsonrpc_core::types::error::Error {app::rpc::validate_public_key}`   
   |    
   = note: required because of the requirements on the impl of `jsonrpc_core::calls::RpcMethodSimple` for `[closure@src/main.rs:37:46: 37:82]`

What am I doing wrong?

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Bopsi
  • 2,090
  • 5
  • 36
  • 58

1 Answers1

0

I just used io.add_method("validate_public_key", validate); instead of io.add_method("validate_public_key", |params: Params| validate);. It's working fine.

Saw the solution in the comment section but it's removed by user now.

Bopsi
  • 2,090
  • 5
  • 36
  • 58
  • 1
    that was me :P, didn't have time to test it and i didn't wanna put something wrong as a comment – sshashank124 Jan 02 '20 at 13:59
  • It is ok to use, please check [this minimal example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f55c2b9eb4dfacf91c19a7aaa524097c), instead of a closure you can pass function pointer for an expected type which implements `Fn` . – Ömer Erden Jan 02 '20 at 14:14
  • Also your first attempt is wrong because API expects you to pass a closure which returns a type that implements `IntoFuture` but you are passing a closure which returns a function pointer. You need to use it like this to make it work : `io.add_method("validate_public_key", |params: Params| validate());`. But i would stick to the passing function pointer solution. – Ömer Erden Jan 02 '20 at 14:17