Skip to content

Commit

Permalink
feat: Remove generics from sv::messages in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Apr 22, 2024
1 parent 959c9ae commit 292e12c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 27 deletions.
20 changes: 1 addition & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ where
}
```

### Implement interface
### Implement interface with associated types

```rust
impl<InstantiateParam, ExecParam, FieldType>
Expand Down Expand Up @@ -723,24 +723,6 @@ impl<InstantiateParam, ExecParam, FieldType>
}
```

Now we have to inform Sylvia that the interface implemented for the contract has associated types.
We have to list those types (generics or concrete) next to the interface in the `#[sv::messages]`
attribute:

```rust
#[contract]
#[sv::messages(generic<ExecParam, SvCustomMsg, SvCustomMsg> as Generic)]
impl<InstantiateParam, ExecParam, FieldType>
GenericContract<InstantiateParam, ExecParam, FieldType>
where
for<'msg_de> InstantiateParam: CustomMsg + Deserialize<'msg_de> + 'msg_de,
ExecParam: CustomMsg + DeserializeOwned + 'static,
FieldType: 'static,
{
...
}
```

### Generics in entry_points

Entry points have to be generated with concrete types. Using the `entry_points` macro
Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub struct GenericContract<
#[cfg_attr(not(feature = "library"), entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, String>))]
#[contract]
#[sv::messages(cw1 as Cw1: custom(msg, query))]
#[sv::messages(generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as CustomAndGeneric)]
#[sv::messages(generic as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic as CustomAndGeneric)]
#[sv::custom(msg=SvCustomMsg, query=SvCustomQuery)]
impl<
InstantiateT,
Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/generic_iface_on_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl cosmwasm_std::CustomQuery for SvCustomQuery {}

#[cfg_attr(not(feature = "library"), entry_points)]
#[contract]
#[sv::messages(generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg> as CustomAndGeneric)]
#[sv::messages(generic as Generic: custom(msg, query))]
#[sv::messages(custom_and_generic as CustomAndGeneric)]
#[sv::messages(cw1 as Cw1: custom(msg, query))]
/// Required if interface returns generic `Response`
#[sv::custom(msg=SvCustomMsg, query=SvCustomQuery)]
Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/generics_forwarded/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ pub struct GenericsForwardedContract<
#[cfg_attr(not(feature = "library"), sylvia::entry_points(generics<SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomMsg, SvCustomQuery, String>, custom(msg=SvCustomMsg, query=SvCustomQuery)))]
#[contract]
#[sv::error(ContractError)]
#[sv::messages(generic<Exec1T, Exec2T, Exec3T, Query1T, Query2T, Query3T, Sudo1T, Sudo2T, Sudo3T, SvCustomMsg> as Generic: custom(msg, query))]
#[sv::messages(generic as Generic: custom(msg, query))]
#[sv::messages(cw1 as Cw1: custom(msg, query))]
#[sv::messages(custom_and_generic<Exec1T, Exec2T, Exec3T, Query1T, Query2T, Query3T, Sudo1T, Sudo2T, Sudo3T, SvCustomMsg> as CustomAndGeneric)]
#[sv::messages(custom_and_generic as CustomAndGeneric)]
#[sv::custom(msg=CustomMsgT, query=CustomQueryT)]
impl<
InstantiateT,
Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/parser/attributes/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Parse for ContractMessageAttr {
if !input.is_empty() {
emit_error!(input.span(),
"Unexpected tokens inside `sv::messages` attribtue.";
note = "Maximal supported form of attribute: `#[sv::messages(interface::path<T1, T2> as InterfaceName: custom(msg, query))]`."
note = "Maximal supported form of attribute: `#[sv::messages(interface::path as InterfaceName: custom(msg, query))]`."

Check warning on line 108 in sylvia-derive/src/parser/attributes/messages.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/messages.rs#L108

Added line #L108 was not covered by tests
)
}
Ok(Self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: Unexpected tokens inside `sv::messages` attribtue.

= note: Maximal supported form of attribute: `#[sv::messages(interface::path<T1, T2> as InterfaceName: custom(msg, query))]`.
= note: Maximal supported form of attribute: `#[sv::messages(interface::path as InterfaceName: custom(msg, query))]`.

--> tests/ui/attributes/messages/unexpected_token.rs:24:25
|
Expand Down

0 comments on commit 292e12c

Please sign in to comment.