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 all commits
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_mut("foo").remove();

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

#[test]
Expand Down
18 changes: 18 additions & 0 deletions packages/storey/src/containers/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ where
self.storage.set(&[], &bytes);
Ok(())
}

/// Remove the value of the item.
///
/// # Example
/// ```
/// # use mocks::encoding::TestEncoding;
/// # use mocks::backend::TestStorage;
/// use storey::containers::Item;
///
/// let mut storage = TestStorage::new();
/// let item = Item::<u64, TestEncoding>::new(0);
///
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
///
///
/// item.access(&mut storage).set(&42).unwrap();

Not a must, but this probably makes a better doc test here.

/// item.access(&mut storage).remove();
/// assert_eq!(item.access(&storage).get().unwrap(), None);
/// ```
pub fn remove(&mut self) {
self.storage.remove(&[]);
}
}

#[cfg(test)]
Expand Down
10 changes: 4 additions & 6 deletions packages/storey/src/containers/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,7 @@ 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))
}
Expand Down Expand Up @@ -331,6 +326,9 @@ mod tests {
storage.get(&[0, 3, 102, 111, 111]),
Some(1337u64.to_le_bytes().to_vec())
);
map.access(&mut storage).entry_mut("foo").remove();

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

Expand Down
Loading