pub struct Vector<T>where
T: BorshSerialize,{ /* private fields */ }
Expand description
An iterable implementation of vector that stores its content on the trie. This implementation will load and store values in the underlying storage lazily.
Uses the following map: index -> element. Because the data is sharded to avoid reading/writing large chunks of data, the values cannot be accessed as a contiguous piece of memory.
This implementation will cache all changes and loads and only updates values that are changed
in storage after it’s dropped through it’s Drop
implementation. These changes can be updated
in storage before the variable is dropped by using Vector::flush
. During the lifetime of
this type, storage will only be read a maximum of one time per index and only written once per
index unless specifically flushed.
This type should be a drop in replacement for Vec
in most cases and will provide contracts
a vector structure which scales much better as the contract data grows.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"a");
assert!(vec.is_empty());
vec.push(1);
vec.push(2);
assert_eq!(vec.len(), 2);
assert_eq!(vec[0], 1);
assert_eq!(vec.pop(), Some(2));
assert_eq!(vec.len(), 1);
vec[0] = 7;
assert_eq!(vec[0], 7);
vec.extend([1, 2, 3].iter().copied());
assert!(Iterator::eq(vec.into_iter(), [7, 1, 2, 3].iter()));
Implementations§
Source§impl<T> Vector<T>where
T: BorshSerialize,
impl<T> Vector<T>where
T: BorshSerialize,
Sourcepub fn len(&self) -> u32
pub fn len(&self) -> u32
Returns the number of elements in the vector, also referred to as its size.
This function returns a u32
rather than the Vec
equivalent of usize
to have
consistency between targets.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"a");
vec.push(1);
vec.push(2);
assert_eq!(vec.len(), 2);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the vector contains no elements.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"a");
assert!(vec.is_empty());
vec.push(1);
assert!(!vec.is_empty());
Sourcepub fn new<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
pub fn new<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
Create new vector with zero elements. Prefixes storage access with the prefix provided.
This prefix can be anything that implements IntoStorageKey
. The prefix is used when
storing and looking up values in storage to ensure no collisions with other collections.
§Examples
use near_sdk::store::Vector;
let mut vec: Vector<u8> = Vector::new(b"a");
Sourcepub fn set(&mut self, index: u32, value: T)
pub fn set(&mut self, index: u32, value: T)
Sets a value at a given index to the value provided. This does not shift values after the index to the right.
The reason to use this over modifying with Vector::get_mut
or
IndexMut::index_mut
is to avoid loading the existing
value from storage. This method will just write the new value.
§Panics
Panics if index
is out of bounds.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"v");
vec.push("test".to_string());
vec.set(0,"new_value".to_string());
assert_eq!(vec.get(0),Some(&"new_value".to_string()));
Source§impl<T> Vector<T>where
T: BorshSerialize + BorshDeserialize,
impl<T> Vector<T>where
T: BorshSerialize + BorshDeserialize,
Sourcepub fn get(&self, index: u32) -> Option<&T>
pub fn get(&self, index: u32) -> Option<&T>
Returns the element by index or None
if it is not present.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"v");
vec.push("test".to_string());
assert_eq!(Some(&"test".to_string()), vec.get(0));
assert_eq!(None, vec.get(3));
Sourcepub fn get_mut(&mut self, index: u32) -> Option<&mut T>
pub fn get_mut(&mut self, index: u32) -> Option<&mut T>
Returns a mutable reference to the element at the index
provided.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"v");
let x = vec![0, 1, 2];
vec.extend(x);
if let Some(elem) = vec.get_mut(1) {
*elem = 42;
}
let actual: Vec<_> = vec.iter().cloned().collect();
assert_eq!(actual, &[0, 42, 2]);
Sourcepub fn swap_remove(&mut self, index: u32) -> T
pub fn swap_remove(&mut self, index: u32) -> 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.
§Examples
use near_sdk::store::Vector;
let mut vec: Vector<u8> = Vector::new(b"v");
vec.extend([1, 2, 3, 4]);
assert_eq!(vec.swap_remove(1), 2);
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), &[1, 4, 3]);
assert_eq!(vec.swap_remove(0), 1);
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), &[3, 4]);
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator over the vector. This iterator will lazily load any values iterated over from storage.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"v");
vec.extend([1, 2, 4]);
let mut iterator = vec.iter();
assert_eq!(iterator.next(), Some(&1));
assert_eq!(iterator.next(), Some(&2));
assert_eq!(iterator.next(), Some(&4));
assert_eq!(iterator.next(), None);
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Returns an iterator over the Vector
that allows modifying each value. This iterator
will lazily load any values iterated over from storage.
§Examples
use near_sdk::store::Vector;
let mut vec = Vector::new(b"v");
vec.extend([1u32, 2, 4]);
for elem in vec.iter_mut() {
*elem += 2;
}
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), &[3u32, 4, 6]);
Sourcepub fn drain<R>(&mut self, range: R) -> Drain<'_, T> ⓘwhere
R: RangeBounds<u32>,
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T> ⓘwhere
R: RangeBounds<u32>,
Creates a draining iterator that removes the specified range in the vector and yields the removed items.
When the iterator is dropped, all elements in the range are removed
from the vector, even if the iterator was not fully consumed. If the
iterator is not dropped (with mem::forget
for example),
the collection will be left in an inconsistent state.
This will not panic on invalid ranges (end > length
or end < start
) and instead the
iterator will just be empty.
§Examples
use near_sdk::store::Vector;
let mut vec: Vector<u32> = Vector::new(b"v");
vec.extend(vec![1, 2, 3]);
let u: Vec<_> = vec.drain(1..).collect();
assert_eq!(vec.iter().copied().collect::<Vec<_>>(), &[1]);
assert_eq!(u, &[2, 3]);
// A full range clears the vector, like `clear()` does
vec.drain(..);
assert!(vec.is_empty());
Trait Implementations§
Source§impl<T> BorshDeserialize for Vector<T>where
T: BorshSerialize,
impl<T> BorshDeserialize for Vector<T>where
T: BorshSerialize,
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl<T> BorshSchema for Vector<T>where
T: BorshSerialize,
impl<T> BorshSchema for Vector<T>where
T: BorshSerialize,
Source§fn declaration() -> Declaration
fn declaration() -> Declaration
Source§fn add_definitions_recursively(
definitions: &mut BTreeMap<Declaration, Definition>,
)
fn add_definitions_recursively( definitions: &mut BTreeMap<Declaration, Definition>, )
Source§impl<T> BorshSerialize for Vector<T>where
T: BorshSerialize,
impl<T> BorshSerialize for Vector<T>where
T: BorshSerialize,
Source§impl<T> Drop for Vector<T>where
T: BorshSerialize,
impl<T> Drop for Vector<T>where
T: BorshSerialize,
Source§impl<T> Extend<T> for Vector<T>where
T: BorshSerialize + BorshDeserialize,
impl<T> Extend<T> for Vector<T>where
T: BorshSerialize + BorshDeserialize,
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T> Index<u32> for Vector<T>where
T: BorshSerialize + BorshDeserialize,
impl<T> Index<u32> for Vector<T>where
T: BorshSerialize + BorshDeserialize,
Source§impl<T> IndexMut<u32> for Vector<T>where
T: BorshSerialize + BorshDeserialize,
impl<T> IndexMut<u32> for Vector<T>where
T: BorshSerialize + BorshDeserialize,
Source§impl<'a, T> IntoIterator for &'a Vector<T>where
T: BorshSerialize + BorshDeserialize,
impl<'a, T> IntoIterator for &'a Vector<T>where
T: BorshSerialize + BorshDeserialize,
Source§impl<'a, T> IntoIterator for &'a mut Vector<T>where
T: BorshSerialize + BorshDeserialize,
impl<'a, T> IntoIterator for &'a mut Vector<T>where
T: BorshSerialize + BorshDeserialize,
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 + UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.