Struct near_sdk::collections::vector::Vector
source · pub struct Vector<T> { /* private fields */ }
Expand description
An iterable implementation of vector that stores its content on the trie. Uses the following map: index -> element.
Implementations§
source§impl<T> Vector<T>
impl<T> Vector<T>
sourcepub fn len(&self) -> u64
pub fn len(&self) -> u64
Returns the number of elements in the vector, also referred to as its size.
sourcepub fn new<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
pub fn new<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
Create new vector with zero elements. Use id
as a unique identifier on the trie.
§Examples
use near_sdk::collections::Vector;
let mut set: Vector<u32> = Vector::new(b"m");
sourcepub fn to_v2(&self) -> Vector<T>where
T: BorshSerialize,
pub fn to_v2(&self) -> Vector<T>where
T: BorshSerialize,
sourcepub fn get_raw(&self, index: u64) -> Option<Vec<u8>>
pub fn get_raw(&self, index: u64) -> Option<Vec<u8>>
Returns the serialized element by index or None
if it is not present.
sourcepub fn swap_remove_raw(&mut self, index: u64) -> Vec<u8> ⓘ
pub fn swap_remove_raw(&mut self, index: u64) -> Vec<u8> ⓘ
Removes an element from the vector and returns it in serialized form.
The removed element is replaced by the last element of the vector.
Does not preserve ordering, but is O(1)
.
§Panics
Panics if index
is out of bounds.
sourcepub fn push_raw(&mut self, raw_element: &[u8])
pub fn push_raw(&mut self, raw_element: &[u8])
Appends a serialized element to the back of the collection.
sourcepub fn pop_raw(&mut self) -> Option<Vec<u8>>
pub fn pop_raw(&mut self) -> Option<Vec<u8>>
Removes the last element from a vector and returns it without deserializing, or None
if it is empty.
sourcepub fn replace_raw(&mut self, index: u64, raw_element: &[u8]) -> Vec<u8> ⓘ
pub fn replace_raw(&mut self, index: u64, raw_element: &[u8]) -> Vec<u8> ⓘ
Inserts a serialized element at index
, returns a serialized evicted element.
§Panics
If index
is out of bounds.
sourcepub fn extend_raw<IT: IntoIterator<Item = Vec<u8>>>(&mut self, iter: IT)
pub fn extend_raw<IT: IntoIterator<Item = Vec<u8>>>(&mut self, iter: IT)
Extends vector from the given collection of serialized elements.
source§impl<T> Vector<T>where
T: BorshSerialize,
impl<T> Vector<T>where
T: BorshSerialize,
source§impl<T> Vector<T>where
T: BorshDeserialize,
impl<T> Vector<T>where
T: BorshDeserialize,
sourcepub fn get(&self, index: u64) -> Option<T>
pub fn get(&self, index: u64) -> Option<T>
Returns the element by index or None
if it is not present.
sourcepub fn swap_remove(&mut self, index: u64) -> T
pub fn swap_remove(&mut self, index: u64) -> T
Removes an element from the vector and returns it.
The removed element is replaced by the last element of the vector.
Does not preserve ordering, but is O(1)
.
§Panics
Panics if index
is out of bounds.
sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from a vector and returns it, or None
if it is empty.