Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7b0b5ca
design draft
xermicus Nov 2, 2023
e05f98a
basic tests
xermicus Nov 2, 2023
be69ee7
cache the length
xermicus Nov 6, 2023
65d9ea6
get and set
xermicus Nov 6, 2023
a5e04d6
Merge branch 'master' into storage-vec
xermicus Nov 15, 2023
ac73b3f
add clear
xermicus Nov 15, 2023
ab05008
add basic integration test
xermicus Nov 15, 2023
3616b6f
add fallible methods
xermicus Nov 15, 2023
3013112
tests for fallible ops
xermicus Nov 15, 2023
e29edd0
improve clear tests
xermicus Nov 15, 2023
bd03135
fmt
xermicus Nov 15, 2023
ec4e745
spellcheck
xermicus Nov 15, 2023
63f6166
fmt
xermicus Nov 15, 2023
b3a564c
do not alias
xermicus Nov 15, 2023
439f31e
spellcheck
xermicus Nov 16, 2023
45a5776
peek
xermicus Nov 16, 2023
e49b846
make example more meaningful
xermicus Nov 17, 2023
d7abe1f
Merge branch 'master' into storage-vec
xermicus Nov 17, 2023
b0980af
add to mother
xermicus Nov 17, 2023
c87ac98
rm stale toolchain files
xermicus Nov 17, 2023
afe08f0
Merge branch 'master' into storage-vec
xermicus Nov 29, 2023
f01d430
code review
xermicus Nov 29, 2023
a0257bd
impl FromIter for StorageVec
xermicus Nov 29, 2023
634f77c
clippy
xermicus Nov 29, 2023
703430a
remove unneeded impl from slice and fix UI tests
xermicus Nov 29, 2023
50271bb
bump scale dep
xermicus Nov 29, 2023
5c2cb4e
ui
xermicus Nov 29, 2023
dc24fb9
put the cached len into a cell so we do not rely on writing operation…
xermicus Nov 29, 2023
3348363
keep None length if the vec does not exist
xermicus Nov 29, 2023
2b15422
Merge branch 'master' into storage-vec
xermicus Nov 29, 2023
07e0e2b
changelog
xermicus Nov 29, 2023
b36ae42
improve peek test
xermicus Nov 29, 2023
f53eb73
add dev note regarding cached len
xermicus Nov 29, 2023
7e161ff
Merge branch 'master' into storage-vec
xermicus Nov 29, 2023
b3dc029
Merge branch 'master' into storage-vec
xermicus Nov 30, 2023
7a12c1e
Update crates/storage/src/lazy/vec.rs
xermicus Nov 30, 2023
1d9b7d2
fmt
xermicus Nov 30, 2023
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
Prev Previous commit
Next Next commit
add fallible methods
Signed-off-by: Cyrill Leutwiler <[email protected]>
  • Loading branch information
xermicus committed Nov 15, 2023
commit 3616b6f9a3075cdfa15f62545df839bbf06c74d1
106 changes: 84 additions & 22 deletions crates/storage/src/lazy/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@
//! Instead it is just a simple wrapper around the contract storage facilities.

use ink_primitives::Key;
use ink_storage_traits::{
AutoKey,
Packed,
Storable,
StorableHint,
StorageKey,
};
use ink_storage_traits::{AutoKey, Packed, Storable, StorableHint, StorageKey};
use scale::EncodeLike;

use crate::{
Lazy,
Mapping,
};
use crate::{Lazy, Mapping};

/// A vector of values (elements) directly on contract storage.
///
Expand Down Expand Up @@ -156,11 +147,7 @@ where
#[cfg(feature = "std")]
const _: () = {
use crate::traits::StorageLayout;
use ink_metadata::layout::{
Layout,
LayoutKey,
RootLayout,
};
use ink_metadata::layout::{Layout, LayoutKey, RootLayout};

impl<V, KeyType> StorageLayout for StorageVec<V, KeyType>
where
Expand Down Expand Up @@ -229,6 +216,24 @@ where
assert!(self.elements.insert(slot, value).is_none());
}

/// Try to append an element to the back of the vector.
///
///Returns:
///
/// * Ok(()) if the value was inserted successfully
/// * Err(_) if the encoded value exceeds the static buffer size.
pub fn try_push<T>(&mut self, value: &T) -> Result<(), ink_env::Error>
where
T: Storable + scale::EncodeLike<V>,
{
let slot = self.len();
self.set_len(slot.checked_add(1).unwrap());

assert!(self.elements.try_insert(slot, value)?.is_none());

Ok(())
}

/// Pops the last element from the vector and returns it.
//
/// Returns `None` if the vector is empty.
Expand All @@ -240,10 +245,21 @@ where
let slot = self.len().checked_sub(1)?;
self.set_len(slot);

self.elements
.take(slot)
.expect("we can only grow via push, which adjusts the length")
.into()
self.elements.take(slot)
}

/// Try to pop the last element from the vector and returns it.
//
/// Returns `None` if the vector is empty.
///
/// # Panics
///
/// * If the value overgrows the static buffer size.
pub fn try_pop(&mut self) -> Option<Result<V, ink_env::Error>> {
let slot = self.len().checked_sub(1)?;
self.set_len(slot);

self.elements.try_take(slot)
}

/// Access an element at given `index`.
Expand All @@ -255,19 +271,54 @@ where
self.elements.get(index)
}

/// Try to access an element at given `index`.
///
/// Returns:
///
/// * Some(Ok(_)) containing the value if it existed and was decoded successfully.
/// * Some(Err(_)) if the value existed but its length exceeds the static buffer size.
/// * None if there was no value under this mapping key.
pub fn try_get(&self, index: u32) -> Option<ink_env::Result<V>> {
self.elements.try_get(index)
}

/// Set the `value` at given `index`.
///
/// # Panics
///
/// * If the index is out of bounds.
/// * If decoding the element exceeds the static buffer size.
pub fn set<T>(&mut self, index: u32, value: &T)
pub fn set<T>(&mut self, index: u32, value: &T) -> Option<u32>
where
T: Storable + EncodeLike<V>,
{
assert!(index < self.len());

let _ = self.elements.insert(index, value);
self.elements.insert(index, value)
}

/// Try to set the `value` at given `index`.
///
/// Returns:
///
/// Ok(Some(_)) if the value was inserted successfully, containing the size in bytes of the pre-existing value at the specified key if any.
/// Ok(None) if the insert was successful but there was no pre-existing value.
/// Err(_) if the encoded value exceeds the static buffer size.
///
/// # Panics
///
/// Panics if `index` exceeds the length of the vector.
pub fn try_set<T>(
&mut self,
index: u32,
value: &T,
) -> Result<Option<u32>, ink_env::Error>
where
T: Storable + EncodeLike<V>,
{
assert!(index < self.len());

self.elements.try_insert(index, value)
}

/// Delete all elements from storage.
Expand All @@ -282,6 +333,17 @@ where
}
self.set_len(0);
}

/// Remove the element at `index`.
///
/// # Panics
///
/// Panics if `index` exceeds the length of the vector.
pub fn clear_at(&mut self, index: u32) {
assert!(index < self.len());

ink_env::clear_contract_storage(&index);
}
}

impl<V, KeyType> ::core::fmt::Debug for StorageVec<V, KeyType>
Expand Down