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

Confusing behaviour with super traits with the rpc macro #1453

Open
benluelo opened this issue Sep 11, 2024 · 0 comments
Open

Confusing behaviour with super traits with the rpc macro #1453

benluelo opened this issue Sep 11, 2024 · 0 comments

Comments

@benluelo
Copy link

benluelo commented Sep 11, 2024

The following code:

use jsonrpsee::{async_client::Client, proc_macros::rpc};

pub trait Super {}

#[rpc(client, server)]
trait Rpc: Super {
    #[method(name = "method")]
    fn method(&self) {}
}

fn takes_super<T: Super>() {}

fn main() {
    takes_super::<Client>()
}

Results in the following error:

error[E0277]: the trait bound `Client: Super` is not satisfied
  --> src/main.rs:14:19
   |
14 |     takes_super::<Client>()
   |                   ^^^^^^ the trait `Super` is not implemented for `Client`
   |

Although this makes sense (Client does not impl Super), this behaviour is very confusing since the Super bound on Rpc is not rejected. I also attempted to use the client_bounds and server_bounds attributes, which does indeed require Super to be implemented:

#[rpc(
    client,
    server,
    client_bounds(Self: Super),
    server_bounds(Self: Super)
)]
trait Rpc: Super {
    #[method(name = "method")]
    fn method(&self) {}
}

fn takes_super<T: Super>() {}

fn takes_rpc_client<T: RpcClient>() {}

fn main() {
    takes_super::<Client>();
    takes_rpc_client::<Client>();
}
error[E0277]: the trait bound `Client: Super` is not satisfied
  --> src/main.rs:21:19
   |
21 |     takes_super::<Client>();
   |                   ^^^^^^ the trait `Super` is not implemented for `Client`
   |

error[E0277]: the trait bound `Client: RpcClient` is not satisfied
  --> src/main.rs:22:24
   |
22 |     takes_rpc_client::<Client>();
   |                        ^^^^^^ the trait `Super` is not implemented for `Client`, which is required by `Client: RpcClient`
   |

However, any attempt to use Super in the #[rpc] definition causes issues:

use jsonrpsee::{
    async_client::Client,
    core::{DeserializeOwned, Serialize},
    proc_macros::rpc,
};

pub trait Super {
    type Assoc: Send + Sync + Serialize + DeserializeOwned;
}

#[rpc(
    client,
    server,
    client_bounds(Self: Super),
    server_bounds(Self: Super)
)]
trait Rpc: Super {
    #[method(name = "method")]
    fn method(&self, assoc: <Self as Super>::Assoc) {}
}

fn takes_super<T: Super>() {}

fn takes_rpc_client<T: RpcClient>() {}

fn main() {
    takes_super::<Client>();
    takes_rpc_client::<Client>();
}
error[E0277]: the trait bound `Self: Super` is not satisfied
  --> src/main.rs:19:29
   |
19 |     fn method(&self, assoc: <Self as Super>::Assoc) {}
   |                             ^^^^^^^^^^^^^^^^^^^^^^ the trait `Super` is not implemented for `Self`
   |
help: consider further restricting `Self`
   |
19 |     fn method(&self, assoc: <Self as Super>::Assoc) where Self: Super {}
   |                                                     +++++++++++++++++

error[E0277]: the trait bound `Self: Super` is not satisfied
  --> src/main.rs:19:53
   |
19 |     fn method(&self, assoc: <Self as Super>::Assoc) {}
   |                                                     ^^ the trait `Super` is not implemented for `Self`
   |
help: consider further restricting `Self`
   |
19 |     fn method(&self, assoc: <Self as Super>::Assoc) where Self: Super {}
   |                                                     +++++++++++++++++

error[E0277]: the trait bound `Client: Super` is not satisfied
  --> src/main.rs:27:19
   |
27 |     takes_super::<Client>();
   |                   ^^^^^^ the trait `Super` is not implemented for `Client`
   |
note: required by a bound in `takes_super`
  --> src/main.rs:22:19
   |
22 | fn takes_super<T: Super>() {}
   |                   ^^^^^ required by this bound in `takes_super`

error[E0277]: the trait bound `Client: RpcClient` is not satisfied
  --> src/main.rs:28:24
   |
28 |     takes_rpc_client::<Client>();
   |                        ^^^^^^ the trait `Super` is not implemented for `Client`, which is required by `Client: RpcClient`
   |
note: required for `Client` to implement `RpcClient`
  --> src/main.rs:11:1
   |
11 | / #[rpc(
12 | |     client,
13 | |     server,
14 | |     client_bounds(Self: Super),
15 | |     server_bounds(Self: Super)
16 | | )]
   | |__^
17 |   trait Rpc: Super {
   |         ^^^
note: required by a bound in `takes_rpc_client`
  --> src/main.rs:24:24
   |
24 | fn takes_rpc_client<T: RpcClient>() {}
   |                        ^^^^^^^^^ required by this bound in `takes_rpc_client`
   = note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)

While I'm not sure what the best behaviour is here, I think it would probably be best to reject super traits on the trait definition entirely (since they're just outright ignored currently), and clarify in the documentation that the server_bounds only applies to the generated into_rpc method, not the entire *Server trait (whereas it applies to the entire *Client trait):

#[rpc(
    client,
    server,
    client_bounds(Self: ClientBound),
    server_bounds(Self: ServerBound)
)]
trait Rpc: Super {
    #[method(name = "method")]
    fn method(&self, assoc: <Self as Super>::Assoc) {}
}
///Server trait implementation for the `Rpc` RPC API.
pub trait RpcServer: Sized + Send + Sync + 'static { // < no `Super` or `ServerBound` bound
    fn method(&self, assoc: <Self as Super>::Assoc) {}
    ///Collects all the methods and subscriptions defined in the trait and adds them into a single `RpcModule`.
    fn into_rpc(self) -> jsonrpsee::RpcModule<Self>
    where
        Self: ServerBound, // < `ServerBound` bound is only on the `into_rpc` method
    {
        // snip
    }
}
///Client implementation for the `Rpc` RPC API.
pub trait RpcClient: jsonrpsee::core::client::ClientT
where
    Self: ClientBound,
{
    fn method<'life0, 'async_trait>(
        &'life0 self,
        assoc: <Self as Super>::Assoc,
    ) -> ::core::pin::Pin<
        Box<
            dyn ::core::future::Future<
                Output = Result<(), jsonrpsee::core::client::Error>,
            > + ::core::marker::Send + 'async_trait,
        >,
    >
    where
        'life0: 'async_trait,
        Self: ::core::marker::Sync + 'async_trait,
    {
        // snip
    }
}
impl<TypeJsonRpseeInteral> RpcClient for TypeJsonRpseeInteral
where
    TypeJsonRpseeInteral: jsonrpsee::core::client::ClientT,
    Self: ClientBound,
{}
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

1 participant