0x1::Collection
Deprecated since @v3 please use Collection2 Provide a account based vector for save resource.
Collection
CollectionStore
borrow
pop_back
exists_at
put
take
borrow_collection
return_collection
destroy_empty
use 0x1::Errors;
use 0x1::Option;
use 0x1::Signer;
use 0x1::Vector;
Collection
Collection in memory, can not drop & store.
struct Collection<T>
items: vector<T>
owner: address
CollectionStore
Collection in global store.
struct CollectionStore<T: store> has key
items: Option::Option<vector<T>>
const EDEPRECATED_FUNCTION: u64 = 19;
const ECOLLECTION_NOT_EXIST: u64 = 101;
The operator require the collection owner.
const ECOLLECTION_NOT_OWNER: u64 = 102;
borrow
Acquire an immutable reference to the i
th element of the collection c
.
Aborts if i
is out of bounds.
public fun borrow<T>(c: &Collection::Collection<T>, i: u64): &T
public fun borrow<T>(c: &Collection<T>, i: u64): &T{
Vector::borrow(&c.items, i)
}
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
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)
}
exists_at
check the Collection exists in addr
fun exists_at<T: store>(addr: address): bool
fun exists_at<T: store>(addr: address): bool{
exists<CollectionStore<T>>(addr)
}
aborts_if false;
put
Deprecated since @v3 Put items to account’s Collection last position.
public fun put<T: store>(_account: &signer, _item: T)
public fun put<T: store>(_account: &signer, _item: T) {
abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
aborts_if false;
take
Take last item from account’s Collection of T.
public fun take<T: store>(account: &signer): T
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
}
aborts_if false;
borrow_collection
Borrow collection of T from addr
public fun borrow_collection<T: store>(addr: address): Collection::Collection<T>
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
}
}
aborts_if false;
return_collection
Return the Collection of T
public fun return_collection<T: store>(c: Collection::Collection<T>)
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);
}
}
aborts_if false;
destroy_empty
fun destroy_empty<T: store>(c: Collection::CollectionStore<T>)
fun destroy_empty<T: store>(c: CollectionStore<T>){
let CollectionStore{ items } = c;
Option::destroy_none(items);
}
aborts_if false;
pragma verify = false;
pragma aborts_if_is_strict = false;