Skip to content

Commit

Permalink
[framework] Formatting: collections and dynamic fields 4/N (#19497)
Browse files Browse the repository at this point in the history
## Description 

Another PR in the formatting series, covers collection modules and
dynamic fields.

See also:

- #19496
- #19463  
- #19461 

## Test plan 

Tests must pass, framework must produce the same bytecode.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
damirka authored Sep 23, 2024
1 parent 8d369a0 commit a3c1b54
Show file tree
Hide file tree
Showing 16 changed files with 1,378 additions and 1,411 deletions.
27 changes: 12 additions & 15 deletions crates/sui-framework/docs/sui-framework/dynamic_field.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,7 @@ type.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_borrow">borrow</a>&lt;Name: <b>copy</b> + drop + store, Value: store&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &UID,
name: Name,
): &Value {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_borrow">borrow</a>&lt;Name: <b>copy</b> + drop + store, Value: store&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>: &UID, name: Name): &Value {
<b>let</b> object_addr = <a href="../sui-framework/object.md#0x2_object">object</a>.to_address();
<b>let</b> hash = <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> field = <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_borrow_child_object">borrow_child_object</a>&lt;<a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>, hash);
Expand Down Expand Up @@ -260,10 +257,7 @@ type.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_remove">remove</a>&lt;Name: <b>copy</b> + drop + store, Value: store&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &<b>mut</b> UID,
name: Name,
): Value {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_remove">remove</a>&lt;Name: <b>copy</b> + drop + store, Value: store&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>: &<b>mut</b> UID, name: Name): Value {
<b>let</b> object_addr = <a href="../sui-framework/object.md#0x2_object">object</a>.to_address();
<b>let</b> hash = <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_Field">Field</a> { id, name: _, value } = <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_remove_child_object">remove_child_object</a>&lt;<a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(object_addr, hash);
Expand Down Expand Up @@ -293,10 +287,7 @@ Returns true if and only if the <code><a href="../sui-framework/object.md#0x2_ob
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_exists_">exists_</a>&lt;Name: <b>copy</b> + drop + store&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &UID,
name: Name,
): bool {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_exists_">exists_</a>&lt;Name: <b>copy</b> + drop + store&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>: &UID, name: Name): bool {
<b>let</b> object_addr = <a href="../sui-framework/object.md#0x2_object">object</a>.to_address();
<b>let</b> hash = <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_has_child_object">has_child_object</a>(object_addr, hash)
Expand Down Expand Up @@ -325,7 +316,7 @@ Removes the dynamic field if it exists. Returns the <code>some(Value)</code> if

<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_remove_if_exists">remove_if_exists</a>&lt;Name: <b>copy</b> + drop + store, Value: store&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &<b>mut</b> UID,
name: Name
name: Name,
): Option&lt;Value&gt; {
<b>if</b> (<a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_exists_">exists_</a>&lt;Name&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>, name)) {
<a href="../move-stdlib/option.md#0x1_option_some">option::some</a>(<a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_remove">remove</a>(<a href="../sui-framework/object.md#0x2_object">object</a>, name))
Expand Down Expand Up @@ -446,7 +437,10 @@ May abort with <code><a href="../sui-framework/dynamic_field.md#0x2_dynamic_fiel
<summary>Implementation</summary>


<pre><code><b>public</b>(package) <b>native</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>&lt;K: <b>copy</b> + drop + store&gt;(parent: <b>address</b>, k: K): <b>address</b>;
<pre><code><b>public</b>(package) <b>native</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>&lt;K: <b>copy</b> + drop + store&gt;(
parent: <b>address</b>,
k: K,
): <b>address</b>;
</code></pre>


Expand Down Expand Up @@ -516,7 +510,10 @@ we need two versions to return a reference or a mutable reference
<summary>Implementation</summary>


<pre><code><b>public</b>(package) <b>native</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_borrow_child_object_mut">borrow_child_object_mut</a>&lt;Child: key&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>: &<b>mut</b> UID, id: <b>address</b>): &<b>mut</b> Child;
<pre><code><b>public</b>(package) <b>native</b> <b>fun</b> <a href="../sui-framework/dynamic_field.md#0x2_dynamic_field_borrow_child_object_mut">borrow_child_object_mut</a>&lt;Child: key&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &<b>mut</b> UID,
id: <b>address</b>,
): &<b>mut</b> Child;
</code></pre>


Expand Down
15 changes: 3 additions & 12 deletions crates/sui-framework/docs/sui-framework/dynamic_object_field.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ specified type.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_borrow">borrow</a>&lt;Name: <b>copy</b> + drop + store, Value: key + store&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &UID,
name: Name,
): &Value {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_borrow">borrow</a>&lt;Name: <b>copy</b> + drop + store, Value: key + store&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>: &UID, name: Name): &Value {
borrow_impl!(<a href="../sui-framework/object.md#0x2_object">object</a>, name)
}
</code></pre>
Expand Down Expand Up @@ -199,10 +196,7 @@ Returns true if and only if the <code><a href="../sui-framework/object.md#0x2_ob
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_exists_">exists_</a>&lt;Name: <b>copy</b> + drop + store&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &UID,
name: Name,
): bool {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_exists_">exists_</a>&lt;Name: <b>copy</b> + drop + store&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>: &UID, name: Name): bool {
<b>let</b> key = <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_Wrapper">Wrapper</a> { name };
field::exists_with_type&lt;<a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_Wrapper">Wrapper</a>&lt;Name&gt;, ID&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>, key)
}
Expand Down Expand Up @@ -258,10 +252,7 @@ Returns none otherwise
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_id">id</a>&lt;Name: <b>copy</b> + drop + store&gt;(
<a href="../sui-framework/object.md#0x2_object">object</a>: &UID,
name: Name,
): Option&lt;ID&gt; {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_id">id</a>&lt;Name: <b>copy</b> + drop + store&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>: &UID, name: Name): Option&lt;ID&gt; {
<b>let</b> key = <a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_Wrapper">Wrapper</a> { name };
<b>if</b> (!field::exists_with_type&lt;<a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_Wrapper">Wrapper</a>&lt;Name&gt;, ID&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>, key)) <b>return</b> <a href="../move-stdlib/option.md#0x1_option_none">option::none</a>();
<b>let</b> (_field, value_addr) = field::field_info&lt;<a href="../sui-framework/dynamic_object_field.md#0x2_dynamic_object_field_Wrapper">Wrapper</a>&lt;Name&gt;&gt;(<a href="../sui-framework/object.md#0x2_object">object</a>, key);
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-framework/docs/sui-framework/priority_queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Create a new priority queue from the input entry vectors.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_new">new</a>&lt;T: drop&gt;(<b>mut</b> entries: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="../sui-framework/priority_queue.md#0x2_priority_queue_Entry">Entry</a>&lt;T&gt;&gt;) : <a href="../sui-framework/priority_queue.md#0x2_priority_queue_PriorityQueue">PriorityQueue</a>&lt;T&gt; {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_new">new</a>&lt;T: drop&gt;(<b>mut</b> entries: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="../sui-framework/priority_queue.md#0x2_priority_queue_Entry">Entry</a>&lt;T&gt;&gt;): <a href="../sui-framework/priority_queue.md#0x2_priority_queue_PriorityQueue">PriorityQueue</a>&lt;T&gt; {
<b>let</b> len = entries.length();
<b>let</b> <b>mut</b> i = len / 2;
// Max heapify from the first node that is a parent (node at len / 2).
Expand Down Expand Up @@ -151,7 +151,7 @@ Pop the entry with the highest priority value.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_pop_max">pop_max</a>&lt;T: drop&gt;(pq: &<b>mut</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_PriorityQueue">PriorityQueue</a>&lt;T&gt;) : (<a href="../move-stdlib/u64.md#0x1_u64">u64</a>, T) {
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_pop_max">pop_max</a>&lt;T: drop&gt;(pq: &<b>mut</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_PriorityQueue">PriorityQueue</a>&lt;T&gt;): (<a href="../move-stdlib/u64.md#0x1_u64">u64</a>, T) {
<b>let</b> len = pq.entries.length();
<b>assert</b>!(len &gt; 0, <a href="../sui-framework/priority_queue.md#0x2_priority_queue_EPopFromEmptyHeap">EPopFromEmptyHeap</a>);
// Swap the max element <b>with</b> the last element in the entries and remove the max element.
Expand Down Expand Up @@ -184,7 +184,7 @@ Insert a new entry into the queue.


<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_insert">insert</a>&lt;T: drop&gt;(pq: &<b>mut</b> <a href="../sui-framework/priority_queue.md#0x2_priority_queue_PriorityQueue">PriorityQueue</a>&lt;T&gt;, priority: <a href="../move-stdlib/u64.md#0x1_u64">u64</a>, value: T) {
pq.entries.push_back(<a href="../sui-framework/priority_queue.md#0x2_priority_queue_Entry">Entry</a> { priority, value});
pq.entries.push_back(<a href="../sui-framework/priority_queue.md#0x2_priority_queue_Entry">Entry</a> { priority, value });
<b>let</b> index = pq.entries.length() - 1;
<a href="../sui-framework/priority_queue.md#0x2_priority_queue_restore_heap_recursive">restore_heap_recursive</a>(&<b>mut</b> pq.entries, index);
}
Expand Down
6 changes: 4 additions & 2 deletions crates/sui-framework/docs/sui-framework/table_vec.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Create an empty TableVec.

<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/table_vec.md#0x2_table_vec_empty">empty</a>&lt;Element: store&gt;(ctx: &<b>mut</b> TxContext): <a href="../sui-framework/table_vec.md#0x2_table_vec_TableVec">TableVec</a>&lt;Element&gt; {
<a href="../sui-framework/table_vec.md#0x2_table_vec_TableVec">TableVec</a> {
contents: <a href="../sui-framework/table.md#0x2_table_new">table::new</a>(ctx)
contents: <a href="../sui-framework/table.md#0x2_table_new">table::new</a>(ctx),
}
}
</code></pre>
Expand Down Expand Up @@ -364,7 +364,9 @@ Aborts if <code>i</code> or <code>j</code> is out of bounds.
<pre><code><b>public</b> <b>fun</b> <a href="../sui-framework/table_vec.md#0x2_table_vec_swap">swap</a>&lt;Element: store&gt;(t: &<b>mut</b> <a href="../sui-framework/table_vec.md#0x2_table_vec_TableVec">TableVec</a>&lt;Element&gt;, i: <a href="../move-stdlib/u64.md#0x1_u64">u64</a>, j: <a href="../move-stdlib/u64.md#0x1_u64">u64</a>) {
<b>assert</b>!(t.<a href="../sui-framework/table_vec.md#0x2_table_vec_length">length</a>() &gt; i, <a href="../sui-framework/table_vec.md#0x2_table_vec_EIndexOutOfBound">EIndexOutOfBound</a>);
<b>assert</b>!(t.<a href="../sui-framework/table_vec.md#0x2_table_vec_length">length</a>() &gt; j, <a href="../sui-framework/table_vec.md#0x2_table_vec_EIndexOutOfBound">EIndexOutOfBound</a>);
<b>if</b> (i == j) { <b>return</b> };
<b>if</b> (i == j) {
<b>return</b>
};
<b>let</b> element_i = t.contents.remove(i);
<b>let</b> element_j = t.contents.remove(j);
t.contents.add(j, element_i);
Expand Down
Loading

0 comments on commit a3c1b54

Please sign in to comment.