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,
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.
source§impl<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>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
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>(&mut self, value: &Q) -> bool
pub fn remove<Q>(&mut self, value: &Q) -> bool
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,
impl<T, H> BorshDeserialize for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>
source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
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,
impl<T, H> BorshSerialize for LookupSet<T, H>where
T: BorshSerialize,
H: ToKey,
source§impl<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,
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)