对象数据结构:对象袋、对象表

ObjectBag 和 ObjectTable 是与 Bag 和 Table 类似的数据结构,分别用于存储不同类型的元素。不同之处在于 ObjectBag 和 ObjectTable 旨在存储对象,并且对象存储在存储系统中。这样,外部工具可以访问这些对象。这一差异在 Move 中是不可见的。

创建 ObjectBag 或 ObjectTable:

let bag = object_bag::new::<T>(ctx);
let table = object_table::new::<K, V>(ctx);

向袋子或表中添加对象:

object_bag::add(&mut bag, obj);
object_table::add(&mut table, key, obj);

从袋子或表中借用对象:

let obj = object_bag::borrow(&bag, id);
let obj = object_table::borrow(&table, key);

从袋子或表中修改对象:

let obj = object_bag::borrow_mut(&mut bag, id);
obj.property = value;
let obj = object_table::borrow_mut(&mut table, key);
obj.property = value;

从袋子或表中移除对象:

let obj = object_bag::remove(&mut bag, id);
let obj = object_table::remove(&mut table, key);

检查袋子或表中是否存在对象:

let exists = object_bag::contains(&bag, id);
let exists = object_table::contains(&table, key);

获取袋子或表中的对象数量:

let len = object_bag::length(&bag);
let len = object_table::length(&table);