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

Macros error #1221

Open
Pierre-Green opened this issue Oct 22, 2023 · 1 comment
Open

Macros error #1221

Pierre-Green opened this issue Oct 22, 2023 · 1 comment

Comments

@Pierre-Green
Copy link

Hello,
I've started to use jsonrpsee recently. I've created a common cargo crate in my workspace with my Rpc traits and this crate is used in a leptos wasm app and in a tokio backend.
I've tried to write a macro_rules! to conditionally include the RpcServer in the decorator macro as follow:

use jsonrpsee::proc_macros::rpc;
use jsonrpsee_types::error::ErrorObjectOwned;

macro_rules! rpc_trait {
    ($visibility:vis trait $name:ident {$($body:tt)*}) =>
    {
        #[cfg(not(feature = "server"))]
        mod trait_definition {
            use super::*;

            #[rpc(client)]
            #[async_trait]
            $visibility trait $name {
                $($body)*
            }
        }

        #[cfg(feature = "server")]
        mod trait_definition {
            use super::*;

            #[rpc(server, client)]
            #[async_trait]
            $visibility trait $name {
                $($body)*
            }
        }

        pub use trait_definition::*;
    };
}

rpc_trait! {
    pub trait BrainRpc {
        #[method(name = "getStatus")]
        async fn get_status(&self) -> Result<(), ErrorObjectOwned>;
    }
}

but I have the following error:

error[E0425]: cannot find value `__self` in this scope
  --> shared/brain-rpc/src/lib.rs:22:13
   |
22 |               #[rpc(server, client)]
   |               ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
...
33 | / rpc_trait! {
34 | |     pub trait BrainRpc {
35 | |         #[method(name = "getStatus")]
36 | |         async fn get_status(&self) -> Result<(), ErrorObjectOwned>;
37 | |     }
38 | | }
   | |_- in this macro invocation
   |
   = note: this error originates in the attribute macro `rpc` which comes from the expansion of the macro `rpc_trait` (in Nightly builds, run with -Z macro-backtrace for more info)

Does someone have any idea of a way to fix it ?

@niklasad1
Copy link
Member

niklasad1 commented Oct 22, 2023

You could just do:

#[cfg_attr(feature = "server", rpc(client, server))]
#[cfg_attr(not(feature = "server"), rpc(client))]
pub trait BrainRpc {
	#[method(name = "getStatus")]
	async fn get_status(&self) -> Result<(), ErrorObjectOwned>;
}

I think the #[async_trait] stuff on the jsonrpsee rpc trait needs to be elided but looks like "let__self = self" doesn't work with your added module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants