Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Sep 27, 2022
1 parent 84b857f commit f83f2b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/storage-plus/README.md
Expand Up @@ -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:

Expand Down Expand Up @@ -695,6 +696,11 @@ fn demo() -> StdResult<()> {
let all: StdResult<Vec<_>> = 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(())
}
```
10 changes: 8 additions & 2 deletions packages/storage-plus/src/deque.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -552,7 +553,12 @@ mod tests {
DATA.push_front(&mut store, &p2)?;

let all: StdResult<Vec<_>> = 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(())
}
Expand Down

0 comments on commit f83f2b8

Please sign in to comment.