Vector

Struct Vector 

Source
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:

ComponentKeyValue (Data type)
LengthP, 0u32
ElementP, 1, Iuser 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

  1. after execution of action method with receiver &mut self; or
  2. explicitly calling the setter Self::set().

Implementations§

Source§

impl<'a, T> Vector<T>
where T: Storable + Clone,

Source

pub fn new() -> Self

Source

pub fn len(&self) -> usize

length of the vector

Source

pub fn is_empty(&self) -> bool

check if it is empty vector

Source

pub fn push(&mut self, value: &T)

push adds item to the last of vector, which does not immediately take effect in Contract Storage.

Source

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.

Source

pub fn iter(&'a self) -> VectorIter<'a, T>

iter returns VectorIter which implements Iterator

Source

pub fn iter_mut(&'a mut self) -> VectorIterMut<'a, T>

iter_mut returns VectorIterMut which implements Iterator

Trait Implementations§

Source§

impl<T> Clone for Vector<T>
where T: Storable + Clone,

Source§

fn clone(&self) -> Vector<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Default for Vector<T>
where T: Storable + Default,

Source§

fn default() -> Vector<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Index<usize> for Vector<T>
where T: Storable + Clone,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for Vector<T>
where T: Storable + Clone,

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> Storable for Vector<T>
where T: Storable + Clone,

Source§

fn __load_storage(field: &StoragePath) -> Self

the implementation should eventually call get() to obtain data from world-state and assign the value to the fields of struct
Source§

fn __save_storage(&mut self, field: &StoragePath)

the implementation should eventually call set() to obtain fields’ value of struct and save it to world-state

Auto Trait Implementations§

§

impl<T> !Freeze for Vector<T>

§

impl<T> !RefUnwindSafe for Vector<T>

§

impl<T> Send for Vector<T>
where T: Send,

§

impl<T> !Sync for Vector<T>

§

impl<T> Unpin for Vector<T>

§

impl<T> UnwindSafe for Vector<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V