-
Notifications
You must be signed in to change notification settings - Fork 172
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
Allow trait bounds to be overridden in macro #808
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
598f439
Parse user defined client_bounds and server_bounds
lexnv ae251b5
Use custom user defined bounds if provided
lexnv 7aa178f
Add provided where clause to the custom bounds
lexnv 77a6911
Add proc_macro with bounds example
lexnv 721a18e
Check against client_bounds wihtout client implementation
lexnv a6e8404
tests: Add ui test for empty bounds
lexnv fdb1515
tests: Add ui test to check bounds without implementation
lexnv 28ea2f0
Add bounds documentation
lexnv 018332b
rpc_macro: Remove `WherePredicate` from parsing
lexnv 019c189
ui: Add test that compiles
lexnv 5801e23
Rename rendered `T` to avoid collision with user provided generic
lexnv 0bcf738
tests: Modify UI correct rpc_bounds test to call server's methods
lexnv 02ed1d5
Merge remote-tracking branch 'origin/master' into 696_trait_bounds
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2019-2021 Parity Technologies (UK) Ltd. | ||
// | ||
// Permission is hereby granted, free of charge, to any | ||
// person obtaining a copy of this software and associated | ||
// documentation files (the "Software"), to deal in the | ||
// Software without restriction, including without | ||
// limitation the rights to use, copy, modify, merge, | ||
// publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software | ||
// is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice | ||
// shall be included in all copies or substantial portions | ||
// of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
use std::net::SocketAddr; | ||
|
||
use jsonrpsee::core::{async_trait, Error}; | ||
use jsonrpsee::proc_macros::rpc; | ||
use jsonrpsee::ws_client::WsClientBuilder; | ||
use jsonrpsee::ws_server::{WsServerBuilder, WsServerHandle}; | ||
|
||
type ExampleHash = [u8; 32]; | ||
|
||
pub trait Config { | ||
type Hash: Send + Sync + 'static; | ||
} | ||
|
||
impl Config for ExampleHash { | ||
type Hash = Self; | ||
} | ||
|
||
/// The RPC macro requires `DeserializeOwned` for output types for the client implementation, while the | ||
/// server implementation requires output types to be bounded by `Serialize`. | ||
/// | ||
/// In this example, we don't want the `Conf` to be bounded by default to | ||
/// `Conf : Send + Sync + 'static + jsonrpsee::core::DeserializeOwned` for client implementation and | ||
/// `Conf : Send + Sync + 'static + jsonrpsee::core::Serialize` for server implementation. | ||
/// | ||
/// Explicitly, specify client and server bounds to handle the `Serialize` and `DeserializeOwned` cases | ||
/// just for the `Conf::hash` part. | ||
#[rpc(server, client, namespace = "foo", client_bounds(T::Hash: jsonrpsee::core::DeserializeOwned), server_bounds(T::Hash: jsonrpsee::core::Serialize))] | ||
pub trait Rpc<T: Config> { | ||
#[method(name = "bar")] | ||
fn method(&self) -> Result<T::Hash, Error>; | ||
} | ||
|
||
pub struct RpcServerImpl; | ||
|
||
#[async_trait] | ||
impl RpcServer<ExampleHash> for RpcServerImpl { | ||
fn method(&self) -> Result<<ExampleHash as Config>::Hash, Error> { | ||
Ok([0u8; 32]) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::FmtSubscriber::builder() | ||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
.try_init() | ||
.expect("setting default subscriber failed"); | ||
|
||
let (server_addr, _handle) = run_server().await?; | ||
let url = format!("ws://{}", server_addr); | ||
|
||
let client = WsClientBuilder::default().build(&url).await?; | ||
assert_eq!(RpcClient::<ExampleHash>::method(&client).await.unwrap(), [0u8; 32]); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn run_server() -> anyhow::Result<(SocketAddr, WsServerHandle)> { | ||
let server = WsServerBuilder::default().build("127.0.0.1:0").await?; | ||
|
||
let addr = server.local_addr()?; | ||
let handle = server.start(RpcServerImpl.into_rpc())?; | ||
Ok((addr, handle)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ impl RpcDescription { | |
let sub_tys: Vec<syn::Type> = self.subscriptions.clone().into_iter().map(|s| s.item).collect(); | ||
|
||
let trait_name = quote::format_ident!("{}Client", &self.trait_def.ident); | ||
let where_clause = generate_where_clause(&self.trait_def, &sub_tys, true); | ||
let where_clause = generate_where_clause(&self.trait_def, &sub_tys, true, self.client_bounds.as_ref()); | ||
let type_idents = self.trait_def.generics.type_params().collect::<Vec<&TypeParam>>(); | ||
let (impl_generics, type_generics, _) = self.trait_def.generics.split_for_impl(); | ||
|
||
|
@@ -63,7 +63,7 @@ impl RpcDescription { | |
#(#sub_impls)* | ||
} | ||
|
||
impl<T #(,#type_idents)*> #trait_name #type_generics for T where T: #super_trait #(,#where_clause)* {} | ||
impl<TypeJsonRpseeInteral #(,#type_idents)*> #trait_name #type_generics for TypeJsonRpseeInteral where TypeJsonRpseeInteral: #super_trait #(,#where_clause)* {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}; | ||
|
||
Ok(trait_impl) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use jsonrpsee::proc_macros::rpc; | ||
use jsonrpsee::core::RpcResult; | ||
|
||
pub trait Config { | ||
type Hash: Send + Sync + 'static; | ||
type NotUsed; | ||
} | ||
|
||
#[rpc(client, namespace = "foo", client_bounds(Conf::Hash: jsonrpsee::core::DeserializeOwned))] | ||
pub trait MyRpcClient<Conf: Config> { | ||
#[method(name = "bar")] | ||
fn method(&self) -> RpcResult<Conf::Hash>; | ||
} | ||
|
||
#[rpc(server, namespace = "foo", server_bounds(Conf::Hash: jsonrpsee::core::Serialize))] | ||
pub trait MyRpcServer<Conf: Config> { | ||
#[method(name = "bar")] | ||
fn method(&self) -> RpcResult<Conf::Hash>; | ||
} | ||
|
||
#[rpc(server, client, namespace = "foo", client_bounds(Conf::Hash: jsonrpsee::core::DeserializeOwned), server_bounds(Conf::Hash: jsonrpsee::core::Serialize))] | ||
pub trait MyRpcServerClient<Conf: Config> { | ||
#[method(name = "bar")] | ||
fn method(&self) -> RpcResult<Conf::Hash>; | ||
} | ||
|
||
fn main() {} | ||
lexnv marked this conversation as resolved.
Show resolved
Hide resolved
|
19 changes: 19 additions & 0 deletions
19
proc-macros/tests/ui/incorrect/rpc/rpc_bounds_without_impl.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use jsonrpsee::proc_macros::rpc; | ||
|
||
pub trait Config { | ||
type Hash: Send + Sync + 'static; | ||
} | ||
|
||
#[rpc(server, client_bounds(), server_bounds(Conf::Hash: jsonrpsee::core::Serialize))] | ||
pub trait ClientBoundsForbidden<Conf: Config> { | ||
#[method(name = "bar")] | ||
fn method(&self) -> Result<Conf::Hash, Error>; | ||
} | ||
|
||
#[rpc(client, server_bounds(), client_bounds(Conf::Hash: jsonrpsee::core::DeserializeOwned))] | ||
pub trait ServerBoundsForbidden<Conf: Config> { | ||
#[method(name = "bar")] | ||
fn method(&self) -> Result<Conf::Hash, Error>; | ||
} | ||
|
||
fn main() {} |
11 changes: 11 additions & 0 deletions
11
proc-macros/tests/ui/incorrect/rpc/rpc_bounds_without_impl.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: Attribute 'client' must be specified with 'client_bounds' | ||
--> tests/ui/incorrect/rpc/rpc_bounds_without_impl.rs:8:11 | ||
| | ||
8 | pub trait ClientBoundsForbidden<Conf: Config> { | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: Attribute 'server' must be specified with 'server_bounds' | ||
--> tests/ui/incorrect/rpc/rpc_bounds_without_impl.rs:14:11 | ||
| | ||
14 | pub trait ServerBoundsForbidden<Conf: Config> { | ||
| ^^^^^^^^^^^^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use jsonrpsee::proc_macros::rpc; | ||
use jsonrpsee::core::Error; | ||
|
||
pub trait Config { | ||
type Hash: Send + Sync + 'static; | ||
} | ||
|
||
/// Client bound must be `Conf::Hash: jsonrpsee::core::DeserializeOwned` | ||
/// Server bound must be `Conf::Hash: jsonrpsee::core::Serialize` | ||
#[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())] | ||
pub trait EmptyBounds<Conf: Config> { | ||
#[method(name = "bar")] | ||
fn method(&self) -> Result<Conf::Hash, Error>; | ||
} | ||
|
||
fn main() {} |
26 changes: 26 additions & 0 deletions
26
proc-macros/tests/ui/incorrect/rpc/rpc_empty_bounds.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error[E0277]: the trait bound `<Conf as Config>::Hash: Serialize` is not satisfied | ||
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:10:1 | ||
| | ||
10 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `<Conf as Config>::Hash` | ||
| | ||
note: required by a bound in `RpcModule::<Context>::register_method` | ||
--> $WORKSPACE/core/src/server/rpc_module.rs | ||
| | ||
| R: Serialize, | ||
| ^^^^^^^^^ required by this bound in `RpcModule::<Context>::register_method` | ||
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0277]: the trait bound `for<'de> <Conf as Config>::Hash: Deserialize<'de>` is not satisfied | ||
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:10:1 | ||
| | ||
10 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'de> Deserialize<'de>` is not implemented for `<Conf as Config>::Hash` | ||
| | ||
= note: required because of the requirements on the impl of `DeserializeOwned` for `<Conf as Config>::Hash` | ||
note: required by a bound in `request` | ||
--> $WORKSPACE/core/src/client/mod.rs | ||
| | ||
| R: DeserializeOwned; | ||
| ^^^^^^^^^^^^^^^^ required by this bound in `request` | ||
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC: before/"currently" we add the trait bound the entire trait right?
Can you just verify that we don't add trait bounds on associated types that are not used in the RPC trait?
such as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debugging the proc-macro, it seems that "currently" (without custom
bounds
attribute):The
rpc
macro implies just:Conf : Send + Sync + 'static + jsonrpsee :: core :: Serialize
.Although, this fails to compile due to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's fine I meant with the overrides for the
Hash
Such that it doesn't expand to something like:
But as it works in subxt all fine, IIRC we only inspect the parameters and return types anyway and add bounds on them so all good.