pub struct Vector<T>where
T: Storable,{ /* private fields */ }Expand description
Vector is a contract-level data structure to provide abstraction by utilizing Get and Set operations associated with Contract Storage. It supports lazy read/write on elements that can be iterated.
§Vector
Vector can be a Contract Field defined in the contract struct. E.g.
#[contract]
struct MyContract {
vector: Vector<u64> // this vector declares to store u64 integers to world state
}The value can be obtained by indexing operations. E.g.
let data = self.vector[0]; // can be either a read from cached value or a read from world state
self.vector[0] = data; // No actual write to world state at this line§Iteration
Iteration may involve read from world state. E.g.
// Iterate over immutable reference to the data
self.vector.iter().for_each(|item|{
...
});
// Iterate over mutable reference to the data
self.vector.iter_mut().for_each(|item|{
//...
});§Storage Model
Account Storage State Key Format:
| Component | Key | Value (Data type) |
|---|---|---|
| Length | P, 0 | u32 |
| Element | P, 1, I | user defined data (borsh-serialized) |
- P: parent key
- I: little endian bytes of index (u32)
§Lazy Write
Trait Storage implements the Vector so that data can be saved to world state
- after execution of action method with receiver
&mut self; or - explicitly calling the setter
Self::set().
Implementations§
Source§impl<'a, T> Vector<T>
impl<'a, T> Vector<T>
pub fn new() -> Self
Sourcepub fn push(&mut self, value: &T)
pub fn push(&mut self, value: &T)
push adds item to the last of vector, which does not immediately take effect in Contract Storage.
Sourcepub fn pop(&mut self)
pub fn pop(&mut self)
pop removes the last item in the vector, which does not immediately take effect in Contract Storage.
Pop doest not return the poped item for saving reading cost.
Sourcepub fn iter(&'a self) -> VectorIter<'a, T> ⓘ
pub fn iter(&'a self) -> VectorIter<'a, T> ⓘ
iter returns VectorIter which implements Iterator
Sourcepub fn iter_mut(&'a mut self) -> VectorIterMut<'a, T> ⓘ
pub fn iter_mut(&'a mut self) -> VectorIterMut<'a, T> ⓘ
iter_mut returns VectorIterMut which implements Iterator