cw_storage_plus/indexes/
mod.rs

1// this module requires iterator to be useful at all
2#![cfg(feature = "iterator")]
3mod multi;
4mod prefix;
5mod unique;
6
7pub use multi::MultiIndex;
8pub use prefix::IndexPrefix;
9pub use unique::UniqueIndex;
10
11use serde::de::DeserializeOwned;
12use serde::Serialize;
13
14use cosmwasm_std::{StdResult, Storage};
15
16// Note: we cannot store traits with generic functions inside `Box<dyn Index>`,
17// so I pull S: Storage to a top-level
18pub trait Index<T>
19where
20    T: Serialize + DeserializeOwned + Clone,
21{
22    fn save(&self, store: &mut dyn Storage, pk: &[u8], data: &T) -> StdResult<()>;
23    fn remove(&self, store: &mut dyn Storage, pk: &[u8], old_data: &T) -> StdResult<()>;
24}
25
26#[cfg(test)]
27pub mod test {
28
29    pub fn index_string(data: &str) -> Vec<u8> {
30        data.as_bytes().to_vec()
31    }
32
33    pub fn index_tuple(name: &str, age: u32) -> (Vec<u8>, u32) {
34        (index_string(name), age)
35    }
36
37    pub fn index_string_tuple(data1: &str, data2: &str) -> (Vec<u8>, Vec<u8>) {
38        (index_string(data1), index_string(data2))
39    }
40}