pub struct SVec<T: StableType + AsFixedSizeBytes> { /* private fields */ }Expand description
Stable analog of Vec
May reallocate on inserts. In this case will copy the underlying data to a new location.
This is a “finite” data structure, it can only holp up to u32::MAX / T::SIZE elements. Putting
more elements inside will panic.
T has to implement both StableType and AsFixedSizeBytes. SVec itself implements these
traits and can be nested inside other stable data structures.
When SVec is stable-dropped, its elements are also stable-dropped but in reverse order.
Implementations§
Source§impl<T: StableType + AsFixedSizeBytes> SVec<T>
impl<T: StableType + AsFixedSizeBytes> SVec<T>
Sourcepub fn new_with_capacity(capacity: usize) -> Result<Self, OutOfMemory>
pub fn new_with_capacity(capacity: usize) -> Result<Self, OutOfMemory>
Creates a SVec of requested capacity.
Does allocate stable memory, returning OutOfMemory if there is not enough of it.
If this function returns Ok, you are guaranteed to have enough stable memory to store at
least capacity elements in it.
§Example
let mut at_least_10_numbers = SVec::<u64>::new_with_capacity(10)
.expect("Out of memory");Sourcepub const fn max_capacity() -> usize
pub const fn max_capacity() -> usize
Returns the maximum possible capacity of this SVec
Sourcepub fn get(&self, idx: usize) -> Option<SRef<'_, T>>
pub fn get(&self, idx: usize) -> Option<SRef<'_, T>>
Returns a SRef pointing to the element at requested index
See also SVec::get_mut.
If out of bounds, returns None
§Example
let mut vec = SVec::<u64>::new();
vec.push(20).expect("Out of memory");
let elem = vec.get(0).unwrap();
assert_eq!(*elem, 20);Sourcepub fn remove(&mut self, idx: usize) -> T
pub fn remove(&mut self, idx: usize) -> T
Removes element at the requested index, back-shifting all elements after it
§Panics
Panics if out of bounds.
Sourcepub fn swap(&mut self, idx1: usize, idx2: usize)
pub fn swap(&mut self, idx1: usize, idx2: usize)
Swaps elements at requested indices with each other
§Panics
Panics if idx1 == idx2 or if any of indices are out of bounds.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the SVec from elements
Does not reallocate or shrink the underlying memory block.
Sourcepub fn binary_search_by<FN>(&self, f: FN) -> Result<usize, usize>
pub fn binary_search_by<FN>(&self, f: FN) -> Result<usize, usize>
Sourcepub fn iter(&self) -> SVecIter<'_, T>
pub fn iter(&self) -> SVecIter<'_, T>
Returns an immutable iterator over this collection
§Example
let mut vec = SVec::new_with_capacity(100).expect("Out of memory");
for i in 0..100 {
vec.push(i);
}
for elem in vec.iter() {
println!("{}", *elem); // will print '0, 1, 2, 3, 4, ...'
}Sourcepub fn debug_print(&self)
pub fn debug_print(&self)
Prints byte representation of this collection
Useful for tests
Trait Implementations§
Source§impl<T: StableType + AsFixedSizeBytes> AsFixedSizeBytes for SVec<T>
impl<T: StableType + AsFixedSizeBytes> AsFixedSizeBytes for SVec<T>
Source§fn as_fixed_size_bytes(&self, buf: &mut [u8])
fn as_fixed_size_bytes(&self, buf: &mut [u8])
Source§fn from_fixed_size_bytes(arr: &[u8]) -> Self
fn from_fixed_size_bytes(arr: &[u8]) -> Self
Source§fn as_new_fixed_size_bytes(&self) -> Self::Buf
fn as_new_fixed_size_bytes(&self) -> Self::Buf
Source§impl<T: StableType + AsFixedSizeBytes + Debug> Debug for SVec<T>
impl<T: StableType + AsFixedSizeBytes + Debug> Debug for SVec<T>
Source§impl<T: StableType + AsFixedSizeBytes> Default for SVec<T>
impl<T: StableType + AsFixedSizeBytes> Default for SVec<T>
Source§impl<T: StableType + AsFixedSizeBytes> Drop for SVec<T>
impl<T: StableType + AsFixedSizeBytes> Drop for SVec<T>
Source§impl<T: StableType + AsFixedSizeBytes> StableType for SVec<T>
impl<T: StableType + AsFixedSizeBytes> StableType for SVec<T>
Source§unsafe fn stable_drop_flag_off(&mut self)
unsafe fn stable_drop_flag_off(&mut self)
off position, if it is applicable Read moreSource§unsafe fn stable_drop_flag_on(&mut self)
unsafe fn stable_drop_flag_on(&mut self)
on position, if it is applicable Read more