Skip to content

Commit

Permalink
feat: Provide native way to cast deps to empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Aug 15, 2023
1 parent d5eaa62 commit 3f96a92
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
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 a `QuerierWrapper` 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 a `QuerierWrapper` 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 3f96a92

Please sign in to comment.