0x1::TableType of large-scale storage tables.
TableBoxnewdestroy_emptyaddborrowborrow_with_defaultborrow_mutlengthemptyborrow_mut_with_defaultupsertremovecontainsnew_table_handleadd_boxborrow_boxborrow_box_mutcontains_boxremove_boxdestroy_empty_boxdrop_unchecked_boxuse 0x1::Errors;
TableType of tables
struct Table<K: copy, drop, V> has store
handle: address
length: u64
pragma intrinsic = map,
map_new = new,
map_destroy_empty = destroy_empty,
map_len = length,
map_is_empty = empty,
map_has_key = contains,
map_add_no_override = add,
map_del_must_exist = remove,
map_borrow = borrow,
map_borrow_mut = borrow_mut,
map_spec_get = spec_get,
map_spec_set = spec_set,
map_spec_del = spec_remove,
map_spec_len = spec_len,
map_spec_has_key = spec_contains;
BoxWrapper for values. Required for making values appear as resources in the implementation.
struct Box<V> has drop, store, key
val: V
const EALREADY_EXISTS: u64 = 100;
const ENOT_EMPTY: u64 = 102;
const ENOT_FOUND: u64 = 101;
newCreate a new Table.
public fun new<K: copy, drop, V: store>(): Table::Table<K, V>
public fun new<K: copy + drop, V: store>(): Table<K, V> {
Table{
handle: new_table_handle<K, V>(),
length: 0,
}
}
pragma intrinsic;
destroy_emptyDestroy a table. The table must be empty to succeed.
public fun destroy_empty<K: copy, drop, V>(table: Table::Table<K, V>)
public fun destroy_empty<K: copy + drop, V>(table: Table<K, V>) {
assert!(table.length == 0, Errors::invalid_state(ENOT_EMPTY));
destroy_empty_box<K, V, Box<V>>(&table);
drop_unchecked_box<K, V, Box<V>>(table)
}
pragma intrinsic;
addAdd a new entry to the table. Aborts if an entry for this key already exists. The entry itself is not stored in the table, and cannot be discovered from it.
public fun add<K: copy, drop, V>(table: &mut Table::Table<K, V>, key: K, val: V)
public fun add<K: copy + drop, V>(table: &mut Table<K, V>, key: K, val: V) {
add_box<K, V, Box<V>>(table, key, Box{val});
table.length = table.length + 1
}
pragma intrinsic;
borrowAcquire an immutable reference to the value which key maps to.
Aborts if there is no entry for key.
public fun borrow<K: copy, drop, V>(table: &Table::Table<K, V>, key: K): &V
public fun borrow<K: copy + drop, V>(table: &Table<K, V>, key: K): &V {
&borrow_box<K, V, Box<V>>(table, key).val
}
pragma intrinsic;
borrow_with_defaultAcquire an immutable reference to the value which key maps to.
Returns specified default value if there is no entry for key.
public fun borrow_with_default<K: copy, drop, V>(table: &Table::Table<K, V>, key: K, default: &V): &V
public fun borrow_with_default<K: copy + drop, V>(table: &Table<K, V>, key: K, default: &V): &V {
if (!contains(table, copy key)) {
default
} else {
borrow(table, copy key)
}
}
borrow_mutAcquire a mutable reference to the value which key maps to.
Aborts if there is no entry for key.
public fun borrow_mut<K: copy, drop, V>(table: &mut Table::Table<K, V>, key: K): &mut V
public fun borrow_mut<K: copy + drop, V>(table: &mut Table<K, V>, key: K): &mut V {
&mut borrow_box_mut<K, V, Box<V>>(table, key).val
}
pragma intrinsic;
lengthReturns the length of the table, i.e. the number of entries.
public fun length<K: copy, drop, V>(table: &Table::Table<K, V>): u64
pragma intrinsic;
emptyReturns true if this table is empty.
public fun empty<K: copy, drop, V>(table: &Table::Table<K, V>): bool
pragma intrinsic;
borrow_mut_with_defaultAcquire a mutable reference to the value which key maps to.
Insert the pair (key, default) first if there is no entry for key.
public fun borrow_mut_with_default<K: copy, drop, V: drop>(table: &mut Table::Table<K, V>, key: K, default: V): &mut V
public fun borrow_mut_with_default<K: copy + drop, V: drop>(table: &mut Table<K, V>, key: K, default: V): &mut V {
if (!contains(table, copy key)) {
add(table, copy key, default)
};
borrow_mut(table, key)
}
pragma opaque, verify=false;
aborts_if false;
upsertInsert the pair (key, value) if there is no entry for key.
update the value of the entry for key to value otherwise
public fun upsert<K: copy, drop, V: drop>(table: &mut Table::Table<K, V>, key: K, value: V)
public fun upsert<K: copy + drop, V: drop>(table: &mut Table<K, V>, key: K, value: V) {
if (!contains(table, copy key)) {
add(table, copy key, value)
} else {
let ref = borrow_mut(table, key);
*ref = value;
};
}
removeRemove from table and return the value which key maps to.
Aborts if there is no entry for key.
public fun remove<K: copy, drop, V>(table: &mut Table::Table<K, V>, key: K): V
public fun remove<K: copy + drop, V>(table: &mut Table<K, V>, key: K): V {
let Box{val} = remove_box<K, V, Box<V>>(table, key);
table.length = table.length - 1;
val
}
pragma intrinsic;
containsReturns true iff table contains an entry for key.
public fun contains<K: copy, drop, V>(table: &Table::Table<K, V>, key: K): bool
public fun contains<K: copy + drop, V>(table: &Table<K, V>, key: K): bool {
contains_box<K, V, Box<V>>(table, key)
}
pragma intrinsic;
new_table_handlefun new_table_handle<K, V>(): address
native fun new_table_handle<K, V>(): address;
add_boxfun add_box<K: copy, drop, V, B>(table: &mut Table::Table<K, V>, key: K, val: Table::Box<V>)
native fun add_box<K: copy + drop, V, B>(table: &mut Table<K, V>, key: K, val: Box<V>);
borrow_boxfun borrow_box<K: copy, drop, V, B>(table: &Table::Table<K, V>, key: K): &Table::Box<V>
native fun borrow_box<K: copy + drop, V, B>(table: &Table<K, V>, key: K): &Box<V>;
borrow_box_mutfun borrow_box_mut<K: copy, drop, V, B>(table: &mut Table::Table<K, V>, key: K): &mut Table::Box<V>
native fun borrow_box_mut<K: copy + drop, V, B>(table: &mut Table<K, V>, key: K): &mut Box<V>;
contains_boxfun contains_box<K: copy, drop, V, B>(table: &Table::Table<K, V>, key: K): bool
native fun contains_box<K: copy + drop, V, B>(table: &Table<K, V>, key: K): bool;
remove_boxfun remove_box<K: copy, drop, V, B>(table: &mut Table::Table<K, V>, key: K): Table::Box<V>
native fun remove_box<K: copy + drop, V, B>(table: &mut Table<K, V>, key: K): Box<V>;
destroy_empty_boxfun destroy_empty_box<K: copy, drop, V, B>(table: &Table::Table<K, V>)
native fun destroy_empty_box<K: copy + drop, V, B>(table: &Table<K, V>);
drop_unchecked_boxfun drop_unchecked_box<K: copy, drop, V, B>(table: Table::Table<K, V>)
native fun drop_unchecked_box<K: copy + drop, V, B>(table: Table<K, V>);
native fun spec_len<K, V>(t: Table<K, V>): num;
native fun spec_contains<K, V>(t: Table<K, V>, k: K): bool;
native fun spec_set<K, V>(t: Table<K, V>, k: K, v: V): Table<K, V>;
native fun spec_remove<K, V>(t: Table<K, V>, k: K): Table<K, V>;
native fun spec_get<K, V>(t: Table<K, V>, k: K): V;