Skip to content

Commit

Permalink
Expose range top-level on IndexedMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Oct 13, 2020
1 parent 51dc146 commit 6614068
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 0 additions & 2 deletions contracts/cw721-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,7 @@ fn query_all_tokens<S: Storage, A: Api, Q: Querier>(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = OwnedBound::exclusive(start_after.map(Vec::from));

// TODO: make range top-level
let tokens: StdResult<Vec<String>> = tokens::<S>()
.prefix(())
.range(&deps.storage, start.bound(), Bound::None, Order::Ascending)
.take(limit)
.map(|item| item.map(|(k, _)| String::from_utf8_lossy(&k).to_string()))
Expand Down
28 changes: 26 additions & 2 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::indexes::{Index, MultiIndex};
use crate::keys::{Prefixer, PrimaryKey};
use crate::keys::{EmptyPrefix, Prefixer, PrimaryKey};
use crate::map::Map;
use crate::prefix::Prefix;
use crate::UniqueIndex;
use crate::{Bound, UniqueIndex};

/// IndexedBucket works like a bucket but has a secondary index
/// This is a WIP.
Expand Down Expand Up @@ -235,6 +235,30 @@ where
}
}

// short-cut for simple keys, rather than .prefix(()).range(...)
impl<'a, 'x, K, T, S> IndexedMap<'a, 'x, K, T, S>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone + 'x,
S: Storage + 'x,
K::Prefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
pub fn range<'c>(
&self,
store: &'c S,
min: Bound<'_>,
max: Bound<'_>,
order: cosmwasm_std::Order,
) -> Box<dyn Iterator<Item = StdResult<cosmwasm_std::KV<T>>> + 'c>
where
T: 'c,
{
self.prefix(K::Prefix::new()).range(store, min, max, order)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 6614068

Please sign in to comment.