Skip to content

Commit ab9dff9

Browse files
authored
Merge pull request #1941 from CosmWasm/1792-avoid-hashmap
Use IntoIterator for DistributionQuerier::new parameter
2 parents edada3e + 6df3b4e commit ab9dff9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ and this project adheres to
4444
`ValidatorResponse` and `Validator` non-exhaustive. Add `Validator::create`
4545
and `FullDelegation::create` to allow creating them in a stable way. Use
4646
`Addr` type for `ContractInfoResponse::{creator, admin}`. ([#1883])
47+
- cosmwasm-std: Change `DistributionQuerier::new` to take `IntoIterator` instead
48+
of `HashMap`. ([#1941])
4749

4850
[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
4951
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
@@ -54,6 +56,7 @@ and this project adheres to
5456
[#1902]: https://github.com/CosmWasm/cosmwasm/pull/1902
5557
[#1939]: https://github.com/CosmWasm/cosmwasm/pull/1939
5658
[#1940]: https://github.com/CosmWasm/cosmwasm/pull/1940
59+
[#1941]: https://github.com/CosmWasm/cosmwasm/pull/1941
5760

5861
### Removed
5962

packages/std/src/testing/mock.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use alloc::collections::BTreeMap;
22
use bech32::{encode, ToBase32, Variant};
3+
use core::iter::IntoIterator;
34
use core::marker::PhantomData;
45
#[cfg(feature = "cosmwasm_1_3")]
56
use core::ops::Bound;
@@ -989,7 +990,7 @@ impl StakingQuerier {
989990
#[cfg(feature = "cosmwasm_1_3")]
990991
#[derive(Clone, Default)]
991992
pub struct DistributionQuerier {
992-
withdraw_addresses: HashMap<String, String>,
993+
withdraw_addresses: BTreeMap<String, String>,
993994
/// Mock of accumulated rewards, indexed first by delegator and then validator address.
994995
rewards: BTreeMap<String, BTreeMap<String, Vec<DecCoin>>>,
995996
/// Mock of validators that a delegator has bonded to.
@@ -998,9 +999,12 @@ pub struct DistributionQuerier {
998999

9991000
#[cfg(feature = "cosmwasm_1_3")]
10001001
impl DistributionQuerier {
1001-
pub fn new(withdraw_addresses: HashMap<String, String>) -> Self {
1002+
pub fn new<T>(withdraw_addresses: T) -> Self
1003+
where
1004+
T: IntoIterator<Item = (String, String)>,
1005+
{
10021006
DistributionQuerier {
1003-
withdraw_addresses,
1007+
withdraw_addresses: withdraw_addresses.into_iter().collect(),
10041008
..Default::default()
10051009
}
10061010
}
@@ -2359,4 +2363,27 @@ mod tests {
23592363
fn making_an_address_with_empty_prefix_should_panic() {
23602364
MockApi::default().with_prefix("").addr_make("creator");
23612365
}
2366+
2367+
#[test]
2368+
#[cfg(feature = "cosmwasm_1_3")]
2369+
fn distribution_querier_new_works() {
2370+
let addresses = [
2371+
("addr0000".to_string(), "addr0001".to_string()),
2372+
("addr0002".to_string(), "addr0001".to_string()),
2373+
];
2374+
let btree_map = BTreeMap::from(addresses.clone());
2375+
2376+
// should still work with HashMap
2377+
let hashmap = HashMap::from(addresses.clone());
2378+
let querier = DistributionQuerier::new(hashmap);
2379+
assert_eq!(querier.withdraw_addresses, btree_map);
2380+
2381+
// should work with BTreeMap
2382+
let querier = DistributionQuerier::new(btree_map.clone());
2383+
assert_eq!(querier.withdraw_addresses, btree_map);
2384+
2385+
// should work with array
2386+
let querier = DistributionQuerier::new(addresses);
2387+
assert_eq!(querier.withdraw_addresses, btree_map);
2388+
}
23622389
}

0 commit comments

Comments
 (0)