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>

source

pub fn len(&self) -> u64

Returns the number of elements in the vector, also referred to as its size.

source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

source

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");
source

pub fn to_v2(&self) -> Vector<T>
where T: BorshSerialize,

Helper utility to be able to easily migrate to the new Vector implementation.

This new Vector’s API matches the Rust Vec API more closely and has a caching layer to avoid reading/writing redundant times to storage.

source

pub fn get_raw(&self, index: u64) -> Option<Vec<u8>>

Returns the serialized element by index or None if it is not present.

source

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.

source

pub fn push_raw(&mut self, raw_element: &[u8])

Appends a serialized element to the back of the collection.

source

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.

source

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.

source

pub fn iter_raw(&self) -> RawIter<'_, T>

Iterate over raw serialized elements.

source

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>

source

pub fn clear(&mut self)

Removes all elements from the collection.

source§

impl<T> Vector<T>
where T: BorshSerialize,

source

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

Appends an element to the back of the collection.

source

pub fn extend<IT: IntoIterator<Item = T>>(&mut self, iter: IT)

Extends vector from the given collection.

source§

impl<T> Vector<T>

source

pub fn get(&self, index: u64) -> Option<T>

Returns the element by index or None if it is not present.

source

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.

source

pub fn pop(&mut self) -> Option<T>

Removes the last element from a vector and returns it, or None if it is empty.

source

pub fn iter(&self) -> Iter<'_, T>

Iterate over deserialized elements.

source

pub fn to_vec(&self) -> Vec<T>

source§

impl<T> Vector<T>

source

pub fn replace(&mut self, index: u64, element: &T) -> T

Inserts a element at index, returns an evicted element.

§Panics

If index is out of bounds.

Trait Implementations§

source§

impl<T> BorshDeserialize for Vector<T>

source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

source§

impl<T> BorshSerialize for Vector<T>

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

source§

impl<T: Debug + BorshDeserialize> Debug for Vector<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Vector<T>

§

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§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.