Struct HashSetZeroCopy

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

Source

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.

Source

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 of HashSet 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>§

Source

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.

Source

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.

Source

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.

Source

pub fn get_capacity(&self) -> usize

Source

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.

Source

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.

Source

pub fn find_element( &self, value: &BigUint, current_sequence_number: Option<usize>, ) -> Result<Option<(&HashSetCell, usize)>, HashSetError>

Source

pub fn find_element_mut( &mut self, value: &BigUint, current_sequence_number: Option<usize>, ) -> Result<Option<(&mut HashSetCell, usize)>, HashSetError>

Source

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.

Source

pub fn first( &self, current_sequence_number: usize, ) -> Result<Option<&HashSetCell>, HashSetError>

Returns a first available element.

Source

pub fn first_no_seq(&self) -> Result<Option<(HashSetCell, u16)>, HashSetError>

Returns a first available element that does not have a sequence number.

Source

pub fn contains( &self, value: &BigUint, sequence_number: Option<usize>, ) -> Result<bool, HashSetError>

Checks if the hash set contains a value.

Source

pub fn mark_with_sequence_number( &mut self, index: usize, sequence_number: usize, ) -> Result<(), HashSetError>

Marks the given element with a given sequence number.

Source

pub fn iter(&self) -> HashSetIterator<'_>

Returns an iterator over elements.

Trait Implementations§

Source§

impl<'a> Debug for HashSetZeroCopy<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Deref for HashSetZeroCopy<'_>

Source§

type Target = HashSet

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for HashSetZeroCopy<'_>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl Drop for HashSetZeroCopy<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V