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

feat: Add support for generics in interface #231

Merged
merged 1 commit into from
Sep 27, 2023

Conversation

jawoznia
Copy link
Collaborator

close #224

@codecov
Copy link

codecov bot commented Sep 18, 2023

Codecov Report

Merging #231 (f01f822) into feat/generics_support (e6131e4) will increase coverage by 0.52%.
The diff coverage is 77.92%.

@@                    Coverage Diff                    @@
##           feat/generics_support     #231      +/-   ##
=========================================================
+ Coverage                  87.72%   88.24%   +0.52%     
=========================================================
  Files                         24       25       +1     
  Lines                       1303     1336      +33     
=========================================================
+ Hits                        1143     1179      +36     
+ Misses                       160      157       -3     
Files Changed Coverage Δ
sylvia-derive/src/check_generics.rs 94.44% <ø> (+22.22%) ⬆️
sylvia/src/types.rs 80.00% <ø> (ø)
sylvia/tests/generics.rs 0.00% <0.00%> (ø)
sylvia-derive/src/multitest.rs 92.64% <60.00%> (-0.46%) ⬇️
sylvia-derive/src/utils.rs 64.00% <60.00%> (-2.67%) ⬇️
sylvia-derive/src/message.rs 84.82% <76.74%> (+1.31%) ⬆️
sylvia-derive/src/input.rs 94.62% <100.00%> (+0.43%) ⬆️
sylvia/src/into_response.rs 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jawoznia jawoznia force-pushed the support_generic_interfaces branch from 40c98fd to 5a652ed Compare September 19, 2023 11:18
sylvia-derive/src/input.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@hashedone hashedone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no approval yet - first, I would appreciate comments on my question; second, I need to verify what is getting generated as it seems a complicated piece of work.

It is possibly mergeable in this form, but I need to understand better what happens.

Comment on lines 13 to 18
for<'msg_de> Msg: Serialize
+ Deserialize<'msg_de>
+ std::fmt::Debug
+ PartialEq
+ Clone
+ schemars::JsonSchema,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot expect anyone to list this kind of trait bound. However - isn't it simply cosmwasm_std::CustomMsg?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside of Deserialize yes. It's better to use here CustomMsg with Deserialize to confirm that lifetimes are parsed properly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then DeserializeOwned > Deserialize in the example (simpler, and it is rare you benefit from unowned deserialize).

However - how does CustomMsg not require DeserializeOwned, but is it needed here? O.o Strange, will take a look at that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As agreed I will leave one example with Deserialize<'de> just to keep parsing lifetimes tested.
I will also introduce sylvia::types::CustomMsg that will provide bound on both DeserializeOwned and CustomMsg

@@ -303,7 +303,7 @@ impl<'a> EnumMessage<'a> {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(#sylvia ::serde::Serialize, #sylvia ::serde::Deserialize, Clone, Debug, PartialEq, #sylvia ::schemars::JsonSchema, cosmwasm_schema::QueryResponses)]
#[serde(rename_all="snake_case")]
pub enum #unique_enum_name #generics #where_clause {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You sure we can get rid of this entirely? I Have a feeling we will bring it back at some form, but maybe refactored.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it because of duplicated bound on Deserialize impl.

Without the where clause

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum MyEnum<Msg> {}

We will get no additional bounds here

    impl<'de, Msg> _serde::Deserialize<'de> for MyEnum<Msg>
    where
        Msg: _serde::Deserialize<'de>,
    {

But if user will provide a Deserialize bound here

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum MyEnum<Msg>
where
    for<'a> Msg: Deserialize<'a>,

Deserialize will expand to

    impl<'de, Msg> _serde::Deserialize<'de> for MyEnum<Msg>
    where
        for<'a> Msg: Deserialize<'a>,
        Msg: _serde::Deserialize<'de>,

This also aligns with what dtolnay said here serde-rs/json#469 (comment). Basically to keep behavior bounds out of the type and move them to impl block.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it makes sense. First time I've seen I didn't yet know you moved it. I will check generated code today.

generics.as_slice(),
&unbonded_generics,
);
let where_clause = if !wheres.is_empty() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen you removed qouting the where clause, the question is - why do you even calculate it then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see, you moved it.

#(#methods_impl)*
}


pub trait Querier {
pub trait #querier #where_clause {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really needed? 90% of cases you don't want to have where clauses on this level. The best is to keep them only on impl level, or on function definition level. Anywhere else is only when there is very good explanation for that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. This is not needed

@jawoznia jawoznia force-pushed the support_generic_interfaces branch 2 times, most recently from 9a158e0 to f01f822 Compare September 20, 2023 13:58
Copy link
Collaborator

@hashedone hashedone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jawoznia jawoznia merged commit b9ba7ca into feat/generics_support Sep 27, 2023
6 of 7 checks passed
@jawoznia jawoznia deleted the support_generic_interfaces branch September 27, 2023 11:16
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

Successfully merging this pull request may close these issues.

2 participants