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: Provide native way to cast deps to empty #1813

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading