pub struct HashSetZeroCopy<'a> {
pub hash_set: ManuallyDrop<HashSet>,
/* private fields */
}
Expand description
A HashSet
wrapper which can be instantiated from Solana account bytes
without copying them.
Fields§
§hash_set: ManuallyDrop<HashSet>
Implementations§
Source§impl<'a> HashSetZeroCopy<'a>
impl<'a> HashSetZeroCopy<'a>
Sourcepub unsafe fn from_bytes_zero_copy_mut(
bytes: &'a mut [u8],
) -> Result<Self, HashSetError>
pub unsafe fn from_bytes_zero_copy_mut( bytes: &'a mut [u8], ) -> Result<Self, HashSetError>
Casts a byte slice into HashSet
.
§Purpose
This method is meant to be used mostly in Solana programs, where memory constraints are tight and we want to make sure no data is copied.
§Safety
This is highly unsafe. Ensuring the alignment and that the slice provides actual data of the hash set is the caller’s responsibility.
Calling it in async context (or anyhwere where the underlying data can be moved in the memory) is certainly going to cause undefined behavior.
Sourcepub unsafe fn from_bytes_zero_copy_init(
bytes: &'a mut [u8],
capacity_values: usize,
sequence_threshold: usize,
) -> Result<Self, HashSetError>
pub unsafe fn from_bytes_zero_copy_init( bytes: &'a mut [u8], capacity_values: usize, sequence_threshold: usize, ) -> Result<Self, HashSetError>
Casts a byte slice into HashSet
and then initializes it.
bytes
is casted into a reference ofHashSet
and used as storage for the struct.capacity_indices
indicates the size of the indices table. It should already include a desired load factor and be greater than the expected number of elements to avoid filling the set too early and avoid creating clusters.capacity_values
indicates the size of the values array. It should be equal to the number of expected elements, without load factor.sequence_threshold
indicates a difference of sequence numbers which make elements of the has set expired. Expiration means that they can be replaced during insertion of new elements with sequence numbers higher by at least a threshold.
§Purpose
This method is meant to be used mostly in Solana programs to initialize a new account which is supposed to store the hash set.
§Safety
This is highly unsafe. Ensuring the alignment and that the slice has a correct size, which is able to fit the hash set, is the caller’s responsibility.
Calling it in async context (or anywhere where the underlying data can be moved in memory) is certainly going to cause undefined behavior.
Methods from Deref<Target = HashSet>§
Sourcepub fn get_bucket(&self, index: usize) -> Option<&Option<HashSetCell>>
pub fn get_bucket(&self, index: usize) -> Option<&Option<HashSetCell>>
Returns a reference to a bucket under the given index
. Does not check
the validity.
Sourcepub fn get_bucket_mut(
&mut self,
index: usize,
) -> Option<&mut Option<HashSetCell>>
pub fn get_bucket_mut( &mut self, index: usize, ) -> Option<&mut Option<HashSetCell>>
Returns a mutable reference to a bucket under the given index
. Does
not check the validity.
Sourcepub fn get_unmarked_bucket(&self, index: usize) -> Option<&Option<HashSetCell>>
pub fn get_unmarked_bucket(&self, index: usize) -> Option<&Option<HashSetCell>>
Returns a reference to an unmarked bucket under the given index. If the
bucket is marked, returns None
.
pub fn get_capacity(&self) -> usize
Sourcepub fn insert(
&mut self,
value: &BigUint,
current_sequence_number: usize,
) -> Result<usize, HashSetError>
pub fn insert( &mut self, value: &BigUint, current_sequence_number: usize, ) -> Result<usize, HashSetError>
Inserts a value into the hash set, with self.capacity_values
attempts.
Every attempt uses quadratic probing to find an empty cell or a cell which can be overwritten.
current sequence_number
is used to check whether existing values can
be overwritten.
Sourcepub fn find_element_index(
&self,
value: &BigUint,
current_sequence_number: Option<usize>,
) -> Result<Option<usize>, HashSetError>
pub fn find_element_index( &self, value: &BigUint, current_sequence_number: Option<usize>, ) -> Result<Option<usize>, HashSetError>
Finds an index of the provided value
inside buckets
.
Uses the optional current_sequence_number
arguments for checking the
validity of the element.
pub fn find_element( &self, value: &BigUint, current_sequence_number: Option<usize>, ) -> Result<Option<(&HashSetCell, usize)>, HashSetError>
pub fn find_element_mut( &mut self, value: &BigUint, current_sequence_number: Option<usize>, ) -> Result<Option<(&mut HashSetCell, usize)>, HashSetError>
Sourcepub fn find_element_iter(
&mut self,
value: &BigUint,
current_sequence_number: usize,
start_iter: usize,
num_iterations: usize,
) -> Result<Option<(usize, bool)>, HashSetError>
pub fn find_element_iter( &mut self, value: &BigUint, current_sequence_number: usize, start_iter: usize, num_iterations: usize, ) -> Result<Option<(usize, bool)>, HashSetError>
find_element_iter iterates over a fixed range of elements in the hash set. We always have to iterate over the whole range to make sure that the value is not in the hash-set. Returns the position of the first free value.
Sourcepub fn first(
&self,
current_sequence_number: usize,
) -> Result<Option<&HashSetCell>, HashSetError>
pub fn first( &self, current_sequence_number: usize, ) -> Result<Option<&HashSetCell>, HashSetError>
Returns a first available element.
Sourcepub fn first_no_seq(&self) -> Result<Option<(HashSetCell, u16)>, HashSetError>
pub fn first_no_seq(&self) -> Result<Option<(HashSetCell, u16)>, HashSetError>
Returns a first available element that does not have a sequence number.
Sourcepub fn contains(
&self,
value: &BigUint,
sequence_number: Option<usize>,
) -> Result<bool, HashSetError>
pub fn contains( &self, value: &BigUint, sequence_number: Option<usize>, ) -> Result<bool, HashSetError>
Checks if the hash set contains a value.
Sourcepub fn mark_with_sequence_number(
&mut self,
index: usize,
sequence_number: usize,
) -> Result<(), HashSetError>
pub fn mark_with_sequence_number( &mut self, index: usize, sequence_number: usize, ) -> Result<(), HashSetError>
Marks the given element with a given sequence number.
Sourcepub fn iter(&self) -> HashSetIterator<'_> ⓘ
pub fn iter(&self) -> HashSetIterator<'_> ⓘ
Returns an iterator over elements.
Trait Implementations§
Source§impl<'a> Debug for HashSetZeroCopy<'a>
impl<'a> Debug for HashSetZeroCopy<'a>
Source§impl Deref for HashSetZeroCopy<'_>
impl Deref for HashSetZeroCopy<'_>
Source§impl DerefMut for HashSetZeroCopy<'_>
impl DerefMut for HashSetZeroCopy<'_>
Auto Trait Implementations§
impl<'a> Freeze for HashSetZeroCopy<'a>
impl<'a> RefUnwindSafe for HashSetZeroCopy<'a>
impl<'a> Send for HashSetZeroCopy<'a>
impl<'a> !Sync for HashSetZeroCopy<'a>
impl<'a> Unpin for HashSetZeroCopy<'a>
impl<'a> UnwindSafe for HashSetZeroCopy<'a>
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> 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 more