A bag is a heterogeneous map-like collection. The collection is similar to moveos_std::table
in that
its keys and values are not stored within the Bag
value, but instead are stored using Sui's
object system. The Bag
struct acts only as a handle into the object system to retrieve those
keys and values.
Note that this means that Bag
values with exactly the same key-value mapping will not be
equal, with ==
, at runtime. For example
let bag1 = bag::new();
let bag2 = bag::new();
bag::add(&mut bag1, 0, false);
bag::add(&mut bag1, 1, true);
bag::add(&mut bag2, 0, false);
bag::add(&mut bag2, 1, true);
// bag1 does not equal bag2, despite having the same entries
assert!(&bag1 != &bag2, 0);
- Resource
BagInner
- Struct
Bag
- Constants
- Function
new
- Function
new_dropable
- Function
add
- Function
add_dropable
- Function
borrow
- Function
borrow_mut
- Function
remove
- Function
contains
- Function
contains_with_type
- Function
length
- Function
is_empty
- Function
destroy_empty
- Function
drop
use 0x2::object;
struct BagInner has key
struct Bag has store
If add a non-dropable value to a dropable bag, will abort with this code
const ErrorBagIsDropable: u64 = 1;
If drop a non-dropable bag, will abort with this code
const ErrorBagIsNotDropable: u64 = 2;
Creates a new, empty bag
Creates a new, empty bag that can be dropped, so all its values should be dropped
public fun new_dropable(): bag::Bag
Adds a key-value pair to the bag bag: &mut Bag
If the bag is dropable, should call add_dropable
instead
Adds a key-value pair to the bag bag: &mut Bag
, the V
should be dropable
public fun add_dropable<K: copy, drop, store, V: drop, store>(bag: &mut bag::Bag, k: K, v: V)
Immutable borrows the value associated with the key in the bag bag: &Bag
.
Mutably borrows the value associated with the key in the bag bag: &mut Bag
.
public fun borrow_mut<K: copy, drop, store, V: store>(bag: &mut bag::Bag, k: K): &mut V
Mutably borrows the key-value pair in the bag bag: &mut Bag
and returns the value.
Returns true iff there is an value associated with the key k: K
in the bag bag: &Bag
Returns true iff there is an value associated with the key k: K
in the bag bag: &Bag
and the value is of type V
public fun contains_with_type<K: copy, drop, store, V: store>(bag: &bag::Bag, k: K): bool
Returns the size of the bag, the number of key-value pairs
Returns true iff the bag is empty (if length
returns 0
)
Destroys an empty bag
public fun destroy_empty(bag: bag::Bag)
Drops a bag, the bag should be dropable