starcoin-framework

Module 0x1::Collection

Deprecated since @v3 please use Collection2 Provide a account based vector for save resource.

use 0x1::Errors;
use 0x1::Option;
use 0x1::Signer;
use 0x1::Vector;

Struct Collection

Collection in memory, can not drop & store.

struct Collection<T>
Fields
items: vector<T>
owner: address
the owner of Collection.

Resource CollectionStore

Collection in global store.

struct CollectionStore<T: store> has key
Fields
items: Option::Option<vector<T>>
items in the CollectionStore. use Option at here is for temporary take away from store to construct Collection.

Constants

const EDEPRECATED_FUNCTION: u64 = 19;

const ECOLLECTION_NOT_EXIST: u64 = 101;

The operator require the collection owner.

const ECOLLECTION_NOT_OWNER: u64 = 102;

Function borrow

Acquire an immutable reference to the ith element of the collection c. Aborts if i is out of bounds.

public fun borrow<T>(c: &Collection::Collection<T>, i: u64): &T
Implementation
public fun borrow<T>(c: &Collection<T>, i: u64): &T{
    Vector::borrow(&c.items, i)
}

Function pop_back

Pop an element from the end of vector v. Aborts if v is empty.

public fun pop_back<T>(account: &signer, c: &mut Collection::Collection<T>): T
Implementation
public fun pop_back<T>(account: &signer, c: &mut Collection<T>): T {
    assert!(Signer::address_of(account) == c.owner, Errors::requires_address(ECOLLECTION_NOT_OWNER));
    Vector::pop_back<T>(&mut c.items)
}

Function exists_at

check the Collection exists in addr

fun exists_at<T: store>(addr: address): bool
Implementation
fun exists_at<T: store>(addr: address): bool{
    exists<CollectionStore<T>>(addr)
}
Specification
aborts_if false;

Function put

Deprecated since @v3 Put items to account’s Collection last position.

public fun put<T: store>(_account: &signer, _item: T)
Implementation
public fun put<T: store>(_account: &signer, _item: T) {
    abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
Specification
aborts_if false;

Function take

Take last item from account’s Collection of T.

public fun take<T: store>(account: &signer): T
Implementation
public fun take<T: store>(account: &signer): T acquires CollectionStore{
    let addr = Signer::address_of(account);
    let c = borrow_collection<T>(addr);
    let item = pop_back(account, &mut c);
    return_collection(c);
    item
}
Specification
aborts_if false;

Function borrow_collection

Borrow collection of T from addr

public fun borrow_collection<T: store>(addr: address): Collection::Collection<T>
Implementation
public fun borrow_collection<T: store>(addr: address): Collection<T> acquires CollectionStore{
    assert!(exists_at<T>(addr), Errors::invalid_state(ECOLLECTION_NOT_EXIST));
    let c = borrow_global_mut<CollectionStore<T>>(addr);
    let items = Option::extract(&mut c.items);
    Collection{
        items,
        owner: addr
    }
}
Specification
aborts_if false;

Function return_collection

Return the Collection of T

public fun return_collection<T: store>(c: Collection::Collection<T>)
Implementation
public fun return_collection<T: store>(c: Collection<T>) acquires CollectionStore{
    let Collection{ items, owner } = c;
    if (Vector::is_empty(&items)) {
        let c = move_from<CollectionStore<T>>(owner);
        destroy_empty(c);
        Vector::destroy_empty(items);
    }else{
        let c = borrow_global_mut<CollectionStore<T>>(owner);
        Option::fill(&mut c.items, items);
    }
}
Specification
aborts_if false;

Function destroy_empty

fun destroy_empty<T: store>(c: Collection::CollectionStore<T>)
Implementation
fun destroy_empty<T: store>(c: CollectionStore<T>){
    let CollectionStore{ items } = c;
    Option::destroy_none(items);
}
Specification
aborts_if false;

Module Specification

pragma verify = false;
pragma aborts_if_is_strict = false;