paritytech / jsonrpc

Rust JSON-RPC implementation
MIT License
791 stars 274 forks source link

how can i define rpc trait in another mod? #654

Closed hunjixin closed 2 years ago

hunjixin commented 2 years ago

when define rpc train and impl in main file , work okay but when move rpc train and impl to sub mod, build fail

code

use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

#[rpc(server)]
pub trait ProofRpc {
    /// Adds two numbers and returns a result
    #[rpc(name = "add")]
    fn add(&self, a: u64, b: u64) -> Result<u64>;
}

pub struct ProofImpl{}

impl ProofRpc for ProofImpl {
    fn add(&self, a: u64, b: u64) -> Result<u64> {
        Ok(a + b)
    }
}

log

error[E0599]: no method named `to_delegate` found for struct `ProofImpl` in the current scope
  --> src/main.rs:39:40
   |
39 |             io.extend_with(ProofImpl{}.to_delegate());
   |                                        ^^^^^^^^^^^ method not found in `ProofImpl`
   | 
  ::: src/proof_rpc/proof.rs:4:1
   |
4  | #[rpc(server)]
   | -------------- the method is available for `ProofImpl` here
...
12 | pub struct ProofImpl{}
   | -------------------- method `to_delegate` not found for this
niklasad1 commented 2 years ago

Hey

Can you please provide a complete example of what you are doing (I don't understand)? I guess it should be possible to reproduce if put them in different modules in main.rs

did you import the trait you defined in main i.e, ProofRpc?

hunjixin commented 2 years ago
├── config
│   └── mod.rs
├── main.rs
└── proof_rpc
    ├── mod.rs
    └── proof.rs

impl rpc in proof_rpc package and extend_with in main. but i got idea to solve, just pass iohandler to proof_rpc, and extend method in the package. work fine.