Skip to content

Commit 583a37b

Browse files
committed
Implement downcasting for Api trait
1 parent 804e113 commit 583a37b

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/std/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ serde-json-wasm = { version = "1.0.1", default-features = false, features = [
7474
static_assertions = "1.1.0"
7575
thiserror = "1.0.26"
7676
rmp-serde = "1.3.0"
77+
downcast-rs = { version = "2.0.1", default-features = false }
7778

7879
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
7980
bech32 = "0.11.0"

packages/std/src/testing/mock.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,4 +2756,15 @@ mod tests {
27562756
// we are not interested in the exact humanization, just that it works
27572757
mock_api.addr_humanize(&canonical_addr).unwrap();
27582758
}
2759+
2760+
#[test]
2761+
fn mock_api_downcast() {
2762+
let mut deps = mock_dependencies();
2763+
let deps_mut = deps.as_mut();
2764+
2765+
let mock_api = deps_mut.api.downcast_ref::<MockApi>().unwrap();
2766+
2767+
let addr = mock_api.addr_make("input");
2768+
assert_eq!(mock_api.addr_validate(addr.as_str()).unwrap(), addr);
2769+
}
27592770
}

packages/std/src/traits.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::marker::PhantomData;
22
use core::ops::Deref;
3+
use downcast_rs::{impl_downcast, Downcast};
34
use serde::{de::DeserializeOwned, Serialize};
45

56
use crate::coin::Coin;
@@ -130,7 +131,7 @@ pub trait Storage {
130131
///
131132
/// We can use feature flags to opt-in to non-essential methods
132133
/// for backwards compatibility in systems that don't have them all.
133-
pub trait Api {
134+
pub trait Api: Downcast {
134135
/// Takes a human readable address and validates if it is valid.
135136
/// If it the validation succeeds, a `Addr` containing the same data as the input is returned.
136137
///
@@ -329,6 +330,7 @@ pub trait Api {
329330
/// Those messages are not persisted to chain.
330331
fn debug(&self, message: &str);
331332
}
333+
impl_downcast!(Api);
332334

333335
/// A short-hand alias for the two-level query result (1. accessing the contract, 2. executing query in the contract)
334336
pub type QuerierResult = SystemResult<ContractResult<Binary>>;

0 commit comments

Comments
 (0)