Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remove entry from map #49

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/cw-storey/tests/smoke_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ fn map() {
let map = Map::<String, Item<u32>>::new(0);

map.access(&mut storage).entry_mut("foo").set(&42).unwrap();

assert_eq!(map.access(&storage).entry("foo").get().unwrap(), Some(42));

map.access(&mut storage).entry_remove("foo");

assert_eq!(map.access(&storage).entry("foo").get().unwrap(), None);
}

#[test]
Expand Down
65 changes: 58 additions & 7 deletions packages/storey/src/containers/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{borrow::Borrow, marker::PhantomData};

use crate::storage::IterableStorage;
use crate::storage::Storage;
use crate::storage::StorageBranch;
use crate::storage::StorageMut;

use super::IterableAccessor;
use super::Storable;
Expand Down Expand Up @@ -155,6 +157,7 @@ impl<K, V, S> MapAccess<K, V, S>
where
K: Key,
V: Storable,
S: Storage,
{
/// Returns an immutable accessor for the inner container of this map.
///
Expand Down Expand Up @@ -192,7 +195,14 @@ where

V::access_impl(StorageBranch::new(&self.storage, key))
}
}

impl<K, V, S> MapAccess<K, V, S>
where
K: Key,
V: Storable,
S: StorageMut,
{
/// Returns a mutable accessor for the inner container of this map.
///
/// # Examples
Expand Down Expand Up @@ -227,15 +237,54 @@ where
K: Borrow<Q>,
Q: Key + ?Sized,
{
let len = key.bytes().len();
let bytes = key.bytes();
let mut key = Vec::with_capacity(len + 1);

key.push(len as u8);
key.extend_from_slice(bytes);
let key = length_prefixed_key(key);

V::access_impl(StorageBranch::new(&mut self.storage, key))
}

/// Removes the inner container for a given key.
///
/// # Examples
///
/// ```
/// # use mocks::encoding::TestEncoding;
/// # use mocks::backend::TestStorage;
/// use storey::containers::{Item, Map};
/// pub fn main()
/// let mut storage = TestStorage::new();
/// let map = Map::<String, Item<u64, TestEncoding>>::new(0);
/// let mut access = map.access(&mut storage);
///
/// access.entry_mut("foo").set(&1337).unwrap();
/// assert_eq!(access.entry("foo").get().unwrap(), Some(1337));
///
/// access.entry_remove("foo");
/// assert_eq!(access.entry("foo").get().unwrap(), None);
/// ```
///
/// ```rust
/// # use mocks::encoding::TestEncoding;
/// # use mocks::backend::TestStorage;
/// use storey::containers::{Item, Map};
///
/// let mut storage = TestStorage::new();
/// let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(0);
/// let mut access = map.access(&mut storage);
///
/// access.entry_mut("foo").entry_mut("bar").set(&1337).unwrap();
/// assert_eq!(access.entry("foo").entry("bar").get().unwrap(), Some(1337));
///
/// access.entry_remove("foo");
/// assert_eq!(access.entry("foo").get().unwrap(), None);
/// ```
pub fn entry_remove<Q>(&mut self, key: &Q)
where
Q: Key + ?Sized,
{
let key = length_prefixed_key(key);

self.storage.remove(&key);
}
}

fn length_prefixed_key<K: Key + ?Sized>(key: &K) -> Vec<u8> {
Expand Down Expand Up @@ -313,7 +362,6 @@ mod tests {

use mocks::backend::TestStorage;
use mocks::encoding::TestEncoding;
use storey_storage::Storage as _;

#[test]
fn map() {
Expand All @@ -331,6 +379,9 @@ mod tests {
storage.get(&[0, 3, 102, 111, 111]),
Some(1337u64.to_le_bytes().to_vec())
);
map.access(&mut storage).entry_remove("foo");

assert_eq!(map.access(&storage).entry("foo").get().unwrap(), None);
assert_eq!(map.access(&storage).entry("bar").get().unwrap(), None);
}

Expand Down