Module sui::bag
A bag is a heterogeneous map-like collection. The collection is similar to
sui::tableBagBagBag==, 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);
At it's core,
sui::bagUID that allows for access to
sui::dynamic_fieldUID can be
deleted, even if it has dynamic fields associated with it, but a bag, on the other hand, must be
empty to be destroyed.
- Struct Bag
- Constants
- Function new
- Function add
- Function borrow
- Function borrow_mut
- Function remove
- Function contains
- Function contains_with_type
- Function length
- Function is_empty
- Function destroy_empty
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::dynamic_field;
use sui::hex;
use sui::object;
use sui::tx_context;
Struct Bag
public struct Bag has key, store
Click to open
Fields
- 
id: sui::object::UID
- the ID of this bag
- 
size: u64
- the number of key-value pairs in the bag
Constants
const EBagNotEmpty: u64 = 0;
Function new
Creates a new, empty bag
public fun new(ctx: &mut sui::tx_context::TxContext): sui::bag::Bag
Click to open
Implementation
public fun new(ctx: &mut TxContext): Bag {
    Bag {
        id: object::new(ctx),
        size: 0,
    }
}
Function add
Adds a key-value pair to the bag
bag: &mut Bagsui::dynamic_field::EFieldAlreadyExistsk: K.
public fun add<K: copy, drop, store, V: store>(bag: &mut sui::bag::Bag, k: K, v: V)
Click to open
Function borrow
Immutable borrows the value associated with the key in the bag
bag: &Bagsui::dynamic_field::EFieldDoesNotExistk: K.
Aborts with sui::dynamic_field::EFieldTypeMismatchpublic fun borrow<K: copy, drop, store, V: store>(bag: &sui::bag::Bag, k: K): &V
Click to open
Function borrow_mut
Mutably borrows the value associated with the key in the bag
bag: &mut Bagsui::dynamic_field::EFieldDoesNotExistk: K.
Aborts with sui::dynamic_field::EFieldTypeMismatchpublic fun borrow_mut<K: copy, drop, store, V: store>(bag: &mut sui::bag::Bag, k: K): &mut V
Click to open
Implementation
public fun borrow_mut<K: copy + drop + store, V: store>(bag: &mut Bag, k: K): &mut V {
    field::borrow_mut(&mut bag.id, k)
}
Function remove
Mutably borrows the key-value pair in the bag
bag: &mut Bagsui::dynamic_field::EFieldDoesNotExistk: K.
Aborts with sui::dynamic_field::EFieldTypeMismatchpublic fun remove<K: copy, drop, store, V: store>(bag: &mut sui::bag::Bag, k: K): V
Click to open
Function contains
Returns true iff there is an value associated with the key k: K in the bag 
bag: &Bagpublic fun contains<K: copy, drop, store>(bag: &sui::bag::Bag, k: K): bool
Click to open
Function contains_with_type
Returns true iff there is an value associated with the key k: K in the bag 
bag: &BagV
public fun contains_with_type<K: copy, drop, store, V: store>(bag: &sui::bag::Bag, k: K): bool
Click to open
Implementation
public fun contains_with_type<K: copy + drop + store, V: store>(bag: &Bag, k: K): bool {
    field::exists_with_type<K, V>(&bag.id, k)
}
Function length
Returns the size of the bag, the number of key-value pairs
public fun length(bag: &sui::bag::Bag): u64
Function is_empty
Returns true iff the bag is empty (if
length0)
public fun is_empty(bag: &sui::bag::Bag): bool
Function destroy_empty
Destroys an empty bag Aborts with
EBagNotEmptypublic fun destroy_empty(bag: sui::bag::Bag)
Click to open
Implementation
public fun destroy_empty(bag: Bag) {
    let Bag { id, size } = bag;
    assert!(size == 0, EBagNotEmpty);
    id.delete()
}