diff --git a/packages/storage-plus/README.md b/packages/storage-plus/README.md index e92069d2c..017dba7ba 100644 --- a/packages/storage-plus/README.md +++ b/packages/storage-plus/README.md @@ -636,11 +636,12 @@ the index key (the part that corresponds to the primary key, that is). So, to correctly use type-safe bounds over multi-indexes ranges, it is fundamental for this `PK` type to be correctly defined, so that it matches the primary key type, or its (typically owned) deserialization variant. -## Deque +## VecDeque -The usage of a [`Deque`](./src/deque.rs) is pretty straight-forward. +The usage of a [`VecDeque`](./src/deque.rs) is pretty straight-forward. Conceptually it works like a storage-backed version of Rust std's `VecDeque` and can be used as a queue or stack. It allows you to push and pop elements on both ends and also read the first or last element without mutating the deque. +You can also read a specific index directly. Example Usage: @@ -695,6 +696,11 @@ fn demo() -> StdResult<()> { let all: StdResult> = DATA.iter(&store)?.collect(); assert_eq!(all?, [p2, p1]); + // or access an index directly + assert_eq!(DATA.get(&store, 0)?, Some(p2)); + assert_eq!(DATA.get(&store, 1)?, Some(p1)); + assert_eq!(DATA.get(&store, 3)?, None); + Ok(()) } ``` \ No newline at end of file diff --git a/packages/storage-plus/src/deque.rs b/packages/storage-plus/src/deque.rs index 92ea1ebd6..6c51440cb 100644 --- a/packages/storage-plus/src/deque.rs +++ b/packages/storage-plus/src/deque.rs @@ -9,7 +9,8 @@ use crate::helpers::{may_deserialize, namespaces_with_key}; const TAIL_KEY: &[u8] = b"t"; const HEAD_KEY: &[u8] = b"h"; -/// A deque stores multiple items at the given key. It provides efficient FIFO and LIFO access. +/// A deque stores multiple items at the given key. It provides efficient FIFO and LIFO access, +/// as well as direct index access. /// /// It has a maximum capacity of `u32::MAX - 1`. Make sure to never exceed that number when using this type. /// If you do, the methods won't work as intended anymore. @@ -552,7 +553,12 @@ mod tests { DATA.push_front(&mut store, &p2)?; let all: StdResult> = DATA.iter(&store)?.collect(); - assert_eq!(all?, [p2, p1]); + assert_eq!(all?, [p2.clone(), p1.clone()]); + + // or access an index directly + assert_eq!(DATA.get(&store, 0)?, Some(p2)); + assert_eq!(DATA.get(&store, 1)?, Some(p1)); + assert_eq!(DATA.get(&store, 3)?, None); Ok(()) }