Skip to content

Commit

Permalink
Merge pull request #1813 from CosmWasm/cast_deps_to_empty
Browse files Browse the repository at this point in the history
feat: Provide native way to cast deps to empty
  • Loading branch information
jawoznia authored Aug 15, 2023
2 parents 7b9baae + 5741162 commit cb4a6a6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 42 additions & 3 deletions packages/std/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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> {
Expand All @@ -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::<u64>::new(&[]),
custom_query_type: PhantomData,
};

let deps_mut: DepsMut<MyQuery> = owned.as_mut();
let _: DepsMut<Empty> = deps_mut.into_empty();

let deps: Deps<MyQuery> = owned.as_ref();
let _: Deps<Empty> = deps.into_empty();
}
}
23 changes: 23 additions & 0 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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<MyQuery> = MockQuerier::new(&[]);
let wrapper = QuerierWrapper::<MyQuery>::new(&querier);

let _: QuerierWrapper<Empty> = wrapper.into_empty();
}
}

0 comments on commit cb4a6a6

Please sign in to comment.