Struct ink_storage::Vec [−][src]
pub struct Vec<T> where
T: PackedLayout, { /* fields omitted */ }
Expand description
A contiguous growable array type, written Vec<T>
but pronounced “vector”.
Note
Despite the similarity to Rust’s Vec
type this storage Vec
has many
differences in its internal data layout. While it stores its data in contiguous
storage slots this does not mean that the data is actually densely stored
in memory.
Also its technical performance characteristics may be different from Rust’s
Vec
due to the differences stated above.
Allows to store up to 2^32
elements and is guaranteed to not reallocate
upon pushing new elements to it.
Implementations
Returns an iterator yielding shared references to all elements of the vector.
Note
Avoid unbounded iteration over big storage vectors.
Prefer using methods like Iterator::take
in order to limit the number
of yielded elements.
Returns an iterator yielding exclusive references to all elements of the vector.
Note
Avoid unbounded iteration over big storage vectors.
Prefer using methods like Iterator::take
in order to limit the number
of yielded elements.
Binary searches this sorted vector for a given element.
If the value is found then Result::Ok
is returned, containing the
index of the matching element. If there are multiple matches, then any
one of the matches could be returned. If the value is not found then
Result::Err
is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
See also binary_search_by
, binary_search_by_key
.
Examples
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1, 4]
.
use ink_storage::Vec as StorageVec;
let s: StorageVec<i32> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
.into_iter()
.copied()
.collect();
assert_eq!(s.binary_search(&13), Ok(9));
assert_eq!(s.binary_search(&4), Err(7));
assert_eq!(s.binary_search(&100), Err(13));
let r = s.binary_search(&1);
assert!(match r { Ok(1..=4) => true, _ => false, });
Binary searches this sorted vector with a comparator function.
The comparator function should implement an order consistent
with the sort order of the underlying vector, returning an
order code that indicates whether its argument is Less
,
Equal
or Greater
the desired target.
If the value is found then Result::Ok
is returned, containing the
index of the matching element. If there are multiple matches, then any
one of the matches could be returned. If the value is not found then
Result::Err
is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
See also binary_search
, binary_search_by_key
.
Examples
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1, 4]
.
use ink_storage::Vec as StorageVec;
let s: StorageVec<i32> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
.into_iter()
.copied()
.collect();
let seek = 13;
assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
let seek = 4;
assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
let seek = 100;
assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
let seek = 1;
let r = s.binary_search_by(|probe| probe.cmp(&seek));
assert!(match r { Ok(1..=4) => true, _ => false, });
Binary searches this sorted vector with a key extraction function.
If the value is found then Result::Ok
is returned, containing the
index of the matching element. If there are multiple matches, then any
one of the matches could be returned. If the value is not found then
Result::Err
is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
See also binary_search
, binary_search_by
.
Examples
Looks up a series of four elements in a vector of pairs sorted by
their second elements. The first is found, with a uniquely
determined position; the second and third are not found; the
fourth could match any position in [1, 4]
.
use ink_storage::Vec as StorageVec;
let s: StorageVec<(i32, i32)> = [
(0, 0),
(2, 1),
(4, 1),
(5, 1),
(3, 1),
(1, 2),
(2, 3),
(4, 5),
(5, 8),
(3, 13),
(1, 21),
(2, 34),
(4, 55),
]
.into_iter()
.copied()
.collect();
assert_eq!(s.binary_search_by_key(&13, |&(a, b)| b), Ok(9));
assert_eq!(s.binary_search_by_key(&4, |&(a, b)| b), Err(7));
assert_eq!(s.binary_search_by_key(&100, |&(a, b)| b), Err(13));
let r = s.binary_search_by_key(&1, |&(a, b)| b);
assert!(match r { Ok(1..=4) => true, _ => false, });
Pops the last element from the vector and returns it.
Returns None
if the vector is empty.
Returns an exclusive reference to the first element if any.
Returns an exclusive reference to the last element if any.
Returns an exclusive reference to the indexed element.
Returns None
if index
is out of bounds.
Removes the indexed element from the vector and returns it.
The last element of the vector is put into the indexed slot.
Returns None
and does not mutate the vector if the index is out of bounds.
Note
This operation does not preserve ordering but is constant time.
Removes the indexed element from the vector.
The last element of the vector is put into the indexed slot.
Returns Some(())
if an element has been removed and None
otherwise.
Note
This operation should be preferred over Vec::swap_remove
if there is
no need to return the removed element since it avoids a contract storage
read for some use cases.
Sets the elements at the given index to the new value.
Won’t return the old element back to the caller. Prefer this operation over other method of overriding an element in the storage vector since this is more efficient.
Trait Implementations
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
Pulls an instance of Self
from the contract storage. Read more
Pushes an instance of Self
to the contract storage. Read more
Clears an instance of Self
from the contract storage. Read more
Indicates whether a type requires deep clean-up of its state meaning that
a clean-up routine has to decode an entity into an instance in order to
eventually recurse upon its tear-down.
This is not required for the majority of primitive data types such as i32
,
however types such as storage::Box
that might want to forward the clean-up
procedure to their inner T
require a deep clean-up. Read more
Auto Trait Implementations
impl<T> !RefUnwindSafe for Vec<T>
impl<T> !UnwindSafe for Vec<T>
Blanket Implementations
Mutably borrows from an owned value. Read more
fn saturated_from<T>(t: T) -> Self where
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Self where
Self: UniqueSaturatedFrom<T>,
Convert from a value of T
into an equivalent instance of Self
. Read more
fn saturated_into<T>(self) -> T where
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> T where
Self: UniqueSaturatedInto<T>,
Consume self to return an equivalent value of T
. Read more
pub fn unique_saturated_into(self) -> T
pub fn unique_saturated_into(self) -> T
Consume self to return an equivalent value of T
.