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

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.

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");

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.

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.

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

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes. Read more
Deserialize this instance from a slice of bytes.
Serialize this instance into a vector of bytes.
Formats the value using the given formatter. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬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

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.