Skip to content

Commit

Permalink
rename to store_by_key and make it a store of a one length array
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Pringle <richard.pringle@avalabs.org>
Signed-off-by: Franfran <51274081+iFrostizz@users.noreply.github.com>
  • Loading branch information
iFrostizz and richardpringle committed Jul 5, 2024
1 parent 7dfad96 commit a08c10e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions x/programs/rust/wasmlanche-sdk/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ impl<'a, K: Key> State<'a, K> {
pairs: Pairs,
) -> Result<(), Error> {
let cache = &mut self.cache.borrow_mut();
for (key, value) in pairs {
Self::store_internal(cache, key, value)?;
}

pairs
.into_iter()
.map(|(k, v)| borsh::to_vec(&v).map(|bytes| (k, Some(bytes))))
.try_for_each(|result| {
result.map(|(k, v)| {
cache.insert(k, v);
})
})
.map_err(|_| StateError::Serialization)?;

Ok(())
}
Expand All @@ -63,9 +70,8 @@ impl<'a, K: Key> State<'a, K> {
/// # Errors
/// Returns an [`Error`] if the key or value cannot be
/// serialized or if the host fails to handle the operation.
pub fn store_single<V: BorshSerialize>(self, key: K, value: &V) -> Result<(), Error> {
let cache = &mut self.cache.borrow_mut();
Self::store_internal(cache, key, value)
pub fn store_by_key<V: BorshSerialize>(self, key: K, value: &V) -> Result<(), Error> {
self.store([(key, value)])
}

fn store_internal<V: BorshSerialize>(
Expand Down

0 comments on commit a08c10e

Please sign in to comment.