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
sourceimpl<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) -> Self where
S: IntoStorageKey,
pub fn new<S>(prefix: S) -> Self where
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 iter_raw(&self) -> RawIter<'_, T>ⓘNotable traits for RawIter<'a, T>impl<'a, T> Iterator for RawIter<'a, T> type Item = Vec<u8>;
pub fn iter_raw(&self) -> RawIter<'_, T>ⓘNotable traits for RawIter<'a, T>impl<'a, T> Iterator for RawIter<'a, T> type Item = Vec<u8>;
Iterate over raw serialized elements.
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.
sourceimpl<T> Vector<T> where
T: BorshSerialize,
impl<T> Vector<T> where
T: BorshSerialize,
sourceimpl<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.
sourcepub fn iter(&self) -> Iter<'_, T>ⓘNotable traits for Iter<'a, T>impl<'a, T> Iterator for Iter<'a, T> where
T: BorshDeserialize, type Item = T;
pub fn iter(&self) -> Iter<'_, T>ⓘNotable traits for Iter<'a, T>impl<'a, T> Iterator for Iter<'a, T> where
T: BorshDeserialize, type Item = T;
T: BorshDeserialize, type Item = T;
Iterate over deserialized elements.
pub fn to_vec(&self) -> Vec<T>
sourceimpl<T> Vector<T> where
T: BorshSerialize + BorshDeserialize,
impl<T> Vector<T> where
T: BorshSerialize + BorshDeserialize,
Trait Implementations
sourceimpl<T> BorshDeserialize for Vector<T> where
u64: BorshDeserialize,
Vec<u8>: BorshDeserialize,
impl<T> BorshDeserialize for Vector<T> where
u64: BorshDeserialize,
Vec<u8>: BorshDeserialize,
sourceimpl<T> BorshSerialize for Vector<T> where
u64: BorshSerialize,
Vec<u8>: BorshSerialize,
impl<T> BorshSerialize for Vector<T> where
u64: BorshSerialize,
Vec<u8>: BorshSerialize,
Auto Trait Implementations
impl<T> RefUnwindSafe for Vector<T> where
T: RefUnwindSafe,
impl<T> Send for Vector<T> where
T: Send,
impl<T> Sync for Vector<T> where
T: Sync,
impl<T> Unpin for Vector<T> where
T: Unpin,
impl<T> UnwindSafe for Vector<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more