From 25df909fe66a315e384f92235d2ab727161b71f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wo=C5=BAniak?= Date: Tue, 7 Nov 2023 20:33:22 +0100 Subject: [PATCH] test: Support generic field in contract --- .../generic_contract/src/contract.rs | 28 ++++++++++++++----- .../src/custom_and_generic.rs | 4 ++- .../contracts/generic_contract/src/cw1.rs | 4 ++- .../contracts/generic_contract/src/generic.rs | 4 ++- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/examples/contracts/generic_contract/src/contract.rs b/examples/contracts/generic_contract/src/contract.rs index eef976ea..12f605f9 100644 --- a/examples/contracts/generic_contract/src/contract.rs +++ b/examples/contracts/generic_contract/src/contract.rs @@ -1,4 +1,5 @@ use cosmwasm_std::{Reply, Response, StdResult}; +use cw_storage_plus::Item; use serde::de::DeserializeOwned; use serde::Deserialize; use sylvia::types::{ @@ -9,33 +10,45 @@ use sylvia::{contract, schemars}; #[cfg(not(feature = "library"))] use sylvia::entry_points; -pub struct GenericContract( - std::marker::PhantomData<( +pub struct GenericContract< + InstantiateParam, + ExecParam, + QueryParam, + MigrateParam, + RetType, + FieldType, +> { + _field: Item<'static, FieldType>, + _phantom: std::marker::PhantomData<( InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, )>, -); +} -#[cfg_attr(not(feature = "library"), entry_points(generics))] +#[cfg_attr(not(feature = "library"), entry_points(generics))] #[contract] #[messages(cw1 as Cw1: custom(msg))] #[messages(generic as Generic: custom(msg))] #[messages(custom_and_generic as CustomAndGeneric)] #[sv::custom(msg=SvCustomMsg)] -impl - GenericContract +impl + GenericContract where for<'msg_de> InstantiateParam: CustomMsg + Deserialize<'msg_de> + 'msg_de, ExecParam: CustomMsg + DeserializeOwned + 'static, QueryParam: CustomMsg + DeserializeOwned + 'static, MigrateParam: CustomMsg + DeserializeOwned + 'static, RetType: CustomMsg + DeserializeOwned + 'static, + FieldType: 'static, { pub const fn new() -> Self { - Self(std::marker::PhantomData) + Self { + _field: Item::new("field"), + _phantom: std::marker::PhantomData, + } } #[msg(instantiate)] @@ -96,6 +109,7 @@ mod tests { SvCustomMsg, super::SvCustomMsg, super::SvCustomMsg, + String, _, > = CodeId::store_code(&app); diff --git a/examples/contracts/generic_contract/src/custom_and_generic.rs b/examples/contracts/generic_contract/src/custom_and_generic.rs index d1ce0b12..2dfadcd0 100644 --- a/examples/contracts/generic_contract/src/custom_and_generic.rs +++ b/examples/contracts/generic_contract/src/custom_and_generic.rs @@ -6,7 +6,7 @@ use sylvia::types::{ExecCtx, QueryCtx, SvCustomMsg}; #[contract(module = crate::contract)] #[messages(custom_and_generic as CustomAndGeneric)] #[sv::custom(msg=sylvia::types::SvCustomMsg)] -impl +impl CustomAndGeneric for crate::contract::GenericContract< InstantiateParam, @@ -14,6 +14,7 @@ impl QueryParam, MigrateParam, RetType, + FieldType, > { type Error = StdError; @@ -52,6 +53,7 @@ mod tests { SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, + String, _, >::store_code(&app); diff --git a/examples/contracts/generic_contract/src/cw1.rs b/examples/contracts/generic_contract/src/cw1.rs index 4c5c125c..410bd13d 100644 --- a/examples/contracts/generic_contract/src/cw1.rs +++ b/examples/contracts/generic_contract/src/cw1.rs @@ -6,13 +6,14 @@ use sylvia::types::{ExecCtx, QueryCtx}; #[contract(module = crate::contract)] #[messages(cw1 as Cw1)] #[sv::custom(msg=sylvia::types::SvCustomMsg)] -impl Cw1 +impl Cw1 for crate::contract::GenericContract< InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType, + FieldType, > { type Error = StdError; @@ -49,6 +50,7 @@ mod tests { SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, + String, _, >::store_code(&app); diff --git a/examples/contracts/generic_contract/src/generic.rs b/examples/contracts/generic_contract/src/generic.rs index 8504c8ae..27176d94 100644 --- a/examples/contracts/generic_contract/src/generic.rs +++ b/examples/contracts/generic_contract/src/generic.rs @@ -6,7 +6,7 @@ use sylvia::types::{ExecCtx, QueryCtx, SvCustomMsg}; #[contract(module = crate::contract)] #[messages(generic as Generic)] #[sv::custom(msg=SvCustomMsg)] -impl +impl Generic for crate::contract::GenericContract< InstantiateParam, @@ -14,6 +14,7 @@ impl QueryParam, MigrateParam, RetType, + FieldType, > { type Error = StdError; @@ -58,6 +59,7 @@ mod tests { SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg, + String, _, > = CodeId::store_code(&app);