Skip to content

Commit 6614068

Browse files
committed
Expose range top-level on IndexedMap
1 parent 51dc146 commit 6614068

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

contracts/cw721-base/src/contract.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,7 @@ fn query_all_tokens<S: Storage, A: Api, Q: Querier>(
508508
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
509509
let start = OwnedBound::exclusive(start_after.map(Vec::from));
510510

511-
// TODO: make range top-level
512511
let tokens: StdResult<Vec<String>> = tokens::<S>()
513-
.prefix(())
514512
.range(&deps.storage, start.bound(), Bound::None, Order::Ascending)
515513
.take(limit)
516514
.map(|item| item.map(|(k, _)| String::from_utf8_lossy(&k).to_string()))

packages/storage-plus/src/indexed_map.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use serde::de::DeserializeOwned;
66
use serde::Serialize;
77

88
use crate::indexes::{Index, MultiIndex};
9-
use crate::keys::{Prefixer, PrimaryKey};
9+
use crate::keys::{EmptyPrefix, Prefixer, PrimaryKey};
1010
use crate::map::Map;
1111
use crate::prefix::Prefix;
12-
use crate::UniqueIndex;
12+
use crate::{Bound, UniqueIndex};
1313

1414
/// IndexedBucket works like a bucket but has a secondary index
1515
/// This is a WIP.
@@ -235,6 +235,30 @@ where
235235
}
236236
}
237237

238+
// short-cut for simple keys, rather than .prefix(()).range(...)
239+
impl<'a, 'x, K, T, S> IndexedMap<'a, 'x, K, T, S>
240+
where
241+
K: PrimaryKey<'a>,
242+
T: Serialize + DeserializeOwned + Clone + 'x,
243+
S: Storage + 'x,
244+
K::Prefix: EmptyPrefix,
245+
{
246+
// I would prefer not to copy code from Prefix, but no other way
247+
// with lifetimes (create Prefix inside function and return ref = no no)
248+
pub fn range<'c>(
249+
&self,
250+
store: &'c S,
251+
min: Bound<'_>,
252+
max: Bound<'_>,
253+
order: cosmwasm_std::Order,
254+
) -> Box<dyn Iterator<Item = StdResult<cosmwasm_std::KV<T>>> + 'c>
255+
where
256+
T: 'c,
257+
{
258+
self.prefix(K::Prefix::new()).range(store, min, max, order)
259+
}
260+
}
261+
238262
#[cfg(test)]
239263
mod test {
240264
use super::*;

0 commit comments

Comments
 (0)