diff --git a/CHANGELOG.md b/CHANGELOG.md index f92ce52545..377b65cc2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to ### Added - cosmwasm-std: Implement `Not` for `Uint{64,128,256}` ([#1799]). +- cosmwasm-std: Implement `into_empty` for `QuerierWrapper`, `Deps` and + `DepsMut`. [#1799]: https://github.com/CosmWasm/cosmwasm/pull/1799 diff --git a/packages/std/src/deps.rs b/packages/std/src/deps.rs index 939a5bd5e1..cc7a4681ab 100644 --- a/packages/std/src/deps.rs +++ b/packages/std/src/deps.rs @@ -69,6 +69,28 @@ impl<'a, C: CustomQuery> DepsMut<'a, C> { querier: self.querier, } } + + /// This allows to convert any `DepsMut` into one generic over `Empty` custom + /// query type. + pub fn into_empty(self) -> DepsMut<'a, Empty> { + DepsMut { + storage: self.storage, + api: self.api, + querier: self.querier.into_empty(), + } + } +} + +impl<'a, C: CustomQuery> Deps<'a, C> { + /// This allows to convert any `Deps` into one generic over `Empty` custom + /// query type. + pub fn into_empty(self) -> Deps<'a, Empty> { + Deps { + storage: self.storage, + api: self.api, + querier: self.querier.into_empty(), + } + } } #[cfg(test)] @@ -98,12 +120,13 @@ mod tests { query(deps.as_ref()) } + #[derive(Clone, Serialize, Deserialize)] + struct MyQuery; + impl CustomQuery for MyQuery {} + #[test] fn deps_implements_copy() { impl CustomQuery for u64 {} - #[derive(Clone, Serialize, Deserialize)] - struct MyQuery; - impl CustomQuery for MyQuery {} // With C: Copy let owned = OwnedDeps::<_, _, _, u64> { @@ -127,4 +150,20 @@ mod tests { let _copy1 = deps; let _copy2 = deps; } + + #[test] + fn deps_to_empty() { + let mut owned = OwnedDeps::<_, _, _, MyQuery> { + storage: MockStorage::default(), + api: MockApi::default(), + querier: MockQuerier::::new(&[]), + custom_query_type: PhantomData, + }; + + let deps_mut: DepsMut = owned.as_mut(); + let _: DepsMut = deps_mut.into_empty(); + + let deps: Deps = owned.as_ref(); + let _: Deps = deps.into_empty(); + } } diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 4e77f56873..8aaa853101 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -225,6 +225,15 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { } } + /// This allows to convert any `QuerierWrapper` into a `QuerierWrapper` generic + /// over `Empty` custom query type. + pub fn into_empty(self) -> QuerierWrapper<'a, Empty> { + QuerierWrapper { + querier: self.querier, + custom_query_type: PhantomData, + } + } + /// Makes the query and parses the response. /// /// Any error (System Error, Error or called contract, or Parse Error) are flattened into @@ -441,6 +450,8 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> { #[cfg(test)] mod tests { + use serde::Deserialize; + use super::*; use crate::testing::MockQuerier; use crate::{coins, from_slice, Uint128}; @@ -571,4 +582,16 @@ mod tests { } if msg == "Querier system error: No such contract: foobar" )); } + + #[test] + fn querier_into_empty() { + #[derive(Clone, Serialize, Deserialize)] + struct MyQuery; + impl CustomQuery for MyQuery {} + + let querier: MockQuerier = MockQuerier::new(&[]); + let wrapper = QuerierWrapper::::new(&querier); + + let _: QuerierWrapper = wrapper.into_empty(); + } }