Skip to content

Latest commit

 

History

History
247 lines (120 loc) · 7.34 KB

bag.md

File metadata and controls

247 lines (120 loc) · 7.34 KB

Module 0x2::bag

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 BagInner has key

Struct Bag

struct Bag has store

Constants

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;

Function new

Creates a new, empty bag

public fun new(): bag::Bag

Function new_dropable

Creates a new, empty bag that can be dropped, so all its values should be dropped

public fun new_dropable(): bag::Bag

Function add

Adds a key-value pair to the bag bag: &mut Bag If the bag is dropable, should call add_dropable instead

public fun add<K: copy, drop, store, V: store>(bag: &mut bag::Bag, k: K, v: V)

Function add_dropable

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)

Function borrow

Immutable borrows the value associated with the key in the bag bag: &Bag.

public fun borrow<K: copy, drop, store, V: store>(bag: &bag::Bag, k: K): &V

Function borrow_mut

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

Function remove

Mutably borrows the key-value pair in the bag bag: &mut Bag and returns the value.

public fun remove<K: copy, drop, store, V: store>(bag: &mut bag::Bag, k: K): V

Function contains

Returns true iff there is an value associated with the key k: K in the bag bag: &Bag

public fun contains<K: copy, drop, store>(bag: &bag::Bag, k: K): bool

Function contains_with_type

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

Function length

Returns the size of the bag, the number of key-value pairs

public fun length(bag: &bag::Bag): u64

Function is_empty

Returns true iff the bag is empty (if length returns 0)

public fun is_empty(bag: &bag::Bag): bool

Function destroy_empty

Destroys an empty bag

public fun destroy_empty(bag: bag::Bag)

Function drop

Drops a bag, the bag should be dropable

public fun drop(bag: bag::Bag)