Struct near_sdk::store::LookupSet

source ·
pub struct LookupSet<T, H = Identity>
where T: BorshSerialize, H: ToKey,
{ /* private fields */ }
Expand description

A non-iterable implementation of a set that stores its content directly on the storage trie.

This set stores the values under a hash of the set’s prefix and BorshSerialize of the value and transformed using the set’s ToKey implementation.

The default hash function for LookupSet is Identity which just prefixes the serialized key object and uses these bytes as the key. This is to be backwards-compatible with collections::LookupSet and be fast for small keys. To use a custom function, use with_hasher. Alternative builtin hash functions can be found at near_sdk::store::key.

§Examples

use near_sdk::store::LookupSet;

// Initializes a set, the generic types can be inferred to `LookupSet<String, Identity>`
// The `b"a"` parameter is a prefix for the storage keys of this data structure.
let mut books = LookupSet::new(b"a");


// Add some books.
books.insert("A Dance With Dragons".to_string());
books.insert("To Kill a Mockingbird".to_string());
books.insert("The Odyssey".to_string());
books.insert("The Great Gatsby".to_string());

// Check for a specific one.
assert!(!books.contains("The Winds of Winter"));
assert!(books.contains("The Odyssey"));

// Remove a book.
books.remove("The Odyssey");

assert!(!books.contains("The Odyssey"));

Implementations§

source§

impl<T> LookupSet<T, Identity>
where T: BorshSerialize,

source

pub fn new<S>(prefix: S) -> Self
where S: IntoStorageKey,

Initialize new LookupSet 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.

source§

impl<T, H> LookupSet<T, H>
where T: BorshSerialize, H: ToKey,

source

pub fn with_hasher<S>(prefix: S) -> Self
where S: IntoStorageKey,

Initialize a LookupSet with a custom hash function.

§Example
use near_sdk::store::{LookupSet, key::Keccak256};

let map = LookupSet::<String, Keccak256>::with_hasher(b"m");
source

pub fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: BorshSerialize + ?Sized,

Returns true if the set contains the specified value.

The value may be any borrowed form of the set’s value type, but BorshSerialize on the borrowed form must match those for the value type.

source

pub fn insert(&mut self, value: T) -> bool

Adds a value to the set.

If the set did not have this value present, true is returned.

If the set did have this value present, false is returned.

source

pub fn remove<Q>(&mut self, value: &Q) -> bool
where T: Borrow<Q>, Q: BorshSerialize + ?Sized,

Removes a value from the set. Returns whether the value was present in the set.

The value may be any borrowed form of the set’s value type, but BorshSerialize on the borrowed form must match those for the value type.

Trait Implementations§

source§

impl<T, H> BorshDeserialize for LookupSet<T, H>
where T: BorshSerialize, H: ToKey,

source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

source§

impl<T, H> BorshSerialize for LookupSet<T, H>
where T: BorshSerialize, H: ToKey,

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

source§

impl<T, H> Debug for LookupSet<T, H>
where T: BorshSerialize, H: ToKey,

source§

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

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

impl<T, H> Extend<T> for LookupSet<T, H>
where T: BorshSerialize, H: ToKey,

source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = T>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

§

impl<T, H> Freeze for LookupSet<T, H>

§

impl<T, H> RefUnwindSafe for LookupSet<T, H>

§

impl<T, H> Send for LookupSet<T, H>

§

impl<T, H> Sync for LookupSet<T, H>

§

impl<T, H> Unpin for LookupSet<T, H>

§

impl<T, H> UnwindSafe for LookupSet<T, H>

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.