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
sourceimpl<T> LookupSet<T, Identity>where
T: BorshSerialize,
impl<T> LookupSet<T, Identity>where
T: BorshSerialize,
sourcepub fn new<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
pub fn new<S>(prefix: S) -> Selfwhere
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.
sourceimpl<T, H> LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
impl<T, H> LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
sourcepub fn with_hasher<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
pub fn with_hasher<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
sourcepub fn contains<Q: ?Sized>(&self, value: &Q) -> boolwhere
T: Borrow<Q>,
Q: BorshSerialize,
pub fn contains<Q: ?Sized>(&self, value: &Q) -> boolwhere
T: Borrow<Q>,
Q: BorshSerialize,
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.
sourcepub fn insert(&mut self, value: T) -> bool
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.
sourcepub fn remove<Q: ?Sized>(&mut self, value: &Q) -> boolwhere
T: Borrow<Q>,
Q: BorshSerialize,
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> boolwhere
T: Borrow<Q>,
Q: BorshSerialize,
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
sourceimpl<T, H> BorshDeserialize for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
Box<[u8]>: BorshDeserialize,
impl<T, H> BorshDeserialize for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
Box<[u8]>: BorshDeserialize,
sourceimpl<T, H> BorshSerialize for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
Box<[u8]>: BorshSerialize,
impl<T, H> BorshSerialize for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
Box<[u8]>: BorshSerialize,
sourceimpl<T, H> Debug for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
impl<T, H> Debug for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
sourceimpl<T, H> Extend<T> for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
impl<T, H> Extend<T> for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
sourcefn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)