Skip to content

Commit

Permalink
[move-2024] Method syntax for move-stdlib (#16466)
Browse files Browse the repository at this point in the history
## Description 

Does what it says on the tin (read: makes the code all shiny!)

Public use funs:

```
ascii.move
10:    public use fun std::string::from_ascii as String.to_string;

vector.move
10:    public use fun std::string::utf8 as vector.to_string;
14:    public use fun std::string::try_utf8 as vector.try_to_string;
17:    public use fun std::ascii::string as vector.to_ascii_string;
21:    public use fun std::ascii::try_string as vector.try_to_ascii_string;
```

## Test Plan 

All the tests still work as expected, plus hand-analysis of decompiled bytecode matches 1:1.

---
If your changes are not user-facing and do not break anything, you can
skip the following section. Otherwise, please briefly describe what has
changed under the Release Notes section.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
cgswords authored Mar 21, 2024
1 parent d5ef4bc commit 5ec36c6
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 215 deletions.
21 changes: 9 additions & 12 deletions crates/sui-framework/docs/move-stdlib/ascii.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,8 @@ Convert a vector of bytes <code>bytes</code> into an <code><a href="../move-stdl

<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/string.md#0x1_string">string</a>(bytes: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../move-stdlib/ascii.md#0x1_ascii_String">String</a> {
<b>let</b> x = <a href="../move-stdlib/ascii.md#0x1_ascii_try_string">try_string</a>(bytes);
<b>assert</b>!(
<a href="../move-stdlib/option.md#0x1_option_is_some">option::is_some</a>(&x),
<a href="../move-stdlib/ascii.md#0x1_ascii_EINVALID_ASCII_CHARACTER">EINVALID_ASCII_CHARACTER</a>
);
<a href="../move-stdlib/option.md#0x1_option_destroy_some">option::destroy_some</a>(x)
<b>assert</b>!(x.is_some(), <a href="../move-stdlib/ascii.md#0x1_ascii_EINVALID_ASCII_CHARACTER">EINVALID_ASCII_CHARACTER</a>);
x.destroy_some()
}
</code></pre>

Expand All @@ -180,10 +177,10 @@ characters. Otherwise returns <code>None</code>.


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/ascii.md#0x1_ascii_try_string">try_string</a>(bytes: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;): Option&lt;<a href="../move-stdlib/ascii.md#0x1_ascii_String">String</a>&gt; {
<b>let</b> len = <a href="../move-stdlib/vector.md#0x1_vector_length">vector::length</a>(&bytes);
<b>let</b> len = bytes.<a href="../move-stdlib/ascii.md#0x1_ascii_length">length</a>();
<b>let</b> <b>mut</b> i = 0;
<b>while</b> (i &lt; len) {
<b>let</b> possible_byte = *<a href="../move-stdlib/vector.md#0x1_vector_borrow">vector::borrow</a>(&bytes, i);
<b>let</b> possible_byte = bytes[i];
<b>if</b> (!<a href="../move-stdlib/ascii.md#0x1_ascii_is_valid_char">is_valid_char</a>(possible_byte)) <b>return</b> <a href="../move-stdlib/option.md#0x1_option_none">option::none</a>();
i = i + 1;
};
Expand Down Expand Up @@ -213,10 +210,10 @@ Returns <code><b>false</b></code> otherwise. Not all <code><a href="../move-stdl


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/ascii.md#0x1_ascii_all_characters_printable">all_characters_printable</a>(<a href="../move-stdlib/string.md#0x1_string">string</a>: &<a href="../move-stdlib/ascii.md#0x1_ascii_String">String</a>): bool {
<b>let</b> len = <a href="../move-stdlib/vector.md#0x1_vector_length">vector::length</a>(&<a href="../move-stdlib/string.md#0x1_string">string</a>.bytes);
<b>let</b> len = <a href="../move-stdlib/string.md#0x1_string">string</a>.bytes.<a href="../move-stdlib/ascii.md#0x1_ascii_length">length</a>();
<b>let</b> <b>mut</b> i = 0;
<b>while</b> (i &lt; len) {
<b>let</b> byte = *<a href="../move-stdlib/vector.md#0x1_vector_borrow">vector::borrow</a>(&<a href="../move-stdlib/string.md#0x1_string">string</a>.bytes, i);
<b>let</b> byte = <a href="../move-stdlib/string.md#0x1_string">string</a>.bytes[i];
<b>if</b> (!<a href="../move-stdlib/ascii.md#0x1_ascii_is_printable_char">is_printable_char</a>(byte)) <b>return</b> <b>false</b>;
i = i + 1;
};
Expand Down Expand Up @@ -244,7 +241,7 @@ Returns <code><b>false</b></code> otherwise. Not all <code><a href="../move-stdl


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/ascii.md#0x1_ascii_push_char">push_char</a>(<a href="../move-stdlib/string.md#0x1_string">string</a>: &<b>mut</b> <a href="../move-stdlib/ascii.md#0x1_ascii_String">String</a>, char: <a href="../move-stdlib/ascii.md#0x1_ascii_Char">Char</a>) {
<a href="../move-stdlib/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> <a href="../move-stdlib/string.md#0x1_string">string</a>.bytes, char.byte);
<a href="../move-stdlib/string.md#0x1_string">string</a>.bytes.push_back(char.byte);
}
</code></pre>

Expand All @@ -268,7 +265,7 @@ Returns <code><b>false</b></code> otherwise. Not all <code><a href="../move-stdl


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/ascii.md#0x1_ascii_pop_char">pop_char</a>(<a href="../move-stdlib/string.md#0x1_string">string</a>: &<b>mut</b> <a href="../move-stdlib/ascii.md#0x1_ascii_String">String</a>): <a href="../move-stdlib/ascii.md#0x1_ascii_Char">Char</a> {
<a href="../move-stdlib/ascii.md#0x1_ascii_Char">Char</a> { byte: <a href="../move-stdlib/vector.md#0x1_vector_pop_back">vector::pop_back</a>(&<b>mut</b> <a href="../move-stdlib/string.md#0x1_string">string</a>.bytes) }
<a href="../move-stdlib/ascii.md#0x1_ascii_Char">Char</a> { byte: <a href="../move-stdlib/string.md#0x1_string">string</a>.bytes.pop_back() }
}
</code></pre>

Expand All @@ -292,7 +289,7 @@ Returns <code><b>false</b></code> otherwise. Not all <code><a href="../move-stdl


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/ascii.md#0x1_ascii_length">length</a>(<a href="../move-stdlib/string.md#0x1_string">string</a>: &<a href="../move-stdlib/ascii.md#0x1_ascii_String">String</a>): u64 {
<a href="../move-stdlib/vector.md#0x1_vector_length">vector::length</a>(<a href="../move-stdlib/ascii.md#0x1_ascii_as_bytes">as_bytes</a>(<a href="../move-stdlib/string.md#0x1_string">string</a>))
<a href="../move-stdlib/string.md#0x1_string">string</a>.<a href="../move-stdlib/ascii.md#0x1_ascii_as_bytes">as_bytes</a>().<a href="../move-stdlib/ascii.md#0x1_ascii_length">length</a>()
}
</code></pre>

Expand Down
54 changes: 27 additions & 27 deletions crates/sui-framework/docs/move-stdlib/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Return true if <code>t</code> does not hold a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_is_none">is_none</a>&lt;Element&gt;(t: &<a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;): bool {
<a href="../move-stdlib/vector.md#0x1_vector_is_empty">vector::is_empty</a>(&t.vec)
t.vec.is_empty()
}
</code></pre>

Expand All @@ -180,7 +180,7 @@ Return true if <code>t</code> holds a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>&lt;Element&gt;(t: &<a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;): bool {
!<a href="../move-stdlib/vector.md#0x1_vector_is_empty">vector::is_empty</a>(&t.vec)
!t.vec.is_empty()
}
</code></pre>

Expand All @@ -206,7 +206,7 @@ Always returns <code><b>false</b></code> if <code>t</code> does not hold a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_contains">contains</a>&lt;Element&gt;(t: &<a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;, e_ref: &Element): bool {
<a href="../move-stdlib/vector.md#0x1_vector_contains">vector::contains</a>(&t.vec, e_ref)
t.vec.<a href="../move-stdlib/option.md#0x1_option_contains">contains</a>(e_ref)
}
</code></pre>

Expand All @@ -232,8 +232,8 @@ Aborts if <code>t</code> does not hold a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_borrow">borrow</a>&lt;Element&gt;(t: &<a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;): &Element {
<b>assert</b>!(<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(t), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
<a href="../move-stdlib/vector.md#0x1_vector_borrow">vector::borrow</a>(&t.vec, 0)
<b>assert</b>!(t.<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
&t.vec[0]
}
</code></pre>

Expand All @@ -260,8 +260,8 @@ Return <code>default_ref</code> if <code>t</code> does not hold a value

<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_borrow_with_default">borrow_with_default</a>&lt;Element&gt;(t: &<a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;, default_ref: &Element): &Element {
<b>let</b> vec_ref = &t.vec;
<b>if</b> (<a href="../move-stdlib/vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) default_ref
<b>else</b> <a href="../move-stdlib/vector.md#0x1_vector_borrow">vector::borrow</a>(vec_ref, 0)
<b>if</b> (vec_ref.is_empty()) default_ref
<b>else</b> &vec_ref[0]
}
</code></pre>

Expand Down Expand Up @@ -291,8 +291,8 @@ Return <code>default</code> if <code>t</code> does not hold a value
default: Element,
): Element {
<b>let</b> vec_ref = &t.vec;
<b>if</b> (<a href="../move-stdlib/vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) default
<b>else</b> *<a href="../move-stdlib/vector.md#0x1_vector_borrow">vector::borrow</a>(vec_ref, 0)
<b>if</b> (vec_ref.is_empty()) default
<b>else</b> vec_ref[0]
}
</code></pre>

Expand All @@ -319,7 +319,7 @@ Aborts if <code>t</code> already holds a value

<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_fill">fill</a>&lt;Element&gt;(t: &<b>mut</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;, e: Element) {
<b>let</b> vec_ref = &<b>mut</b> t.vec;
<b>if</b> (<a href="../move-stdlib/vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) <a href="../move-stdlib/vector.md#0x1_vector_push_back">vector::push_back</a>(vec_ref, e)
<b>if</b> (vec_ref.is_empty()) vec_ref.push_back(e)
<b>else</b> <b>abort</b> <a href="../move-stdlib/option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a>
}
</code></pre>
Expand All @@ -346,8 +346,8 @@ Aborts if <code>t</code> does not hold a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_extract">extract</a>&lt;Element&gt;(t: &<b>mut</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;): Element {
<b>assert</b>!(<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(t), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
<a href="../move-stdlib/vector.md#0x1_vector_pop_back">vector::pop_back</a>(&<b>mut</b> t.vec)
<b>assert</b>!(t.<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
t.vec.pop_back()
}
</code></pre>

Expand All @@ -373,8 +373,8 @@ Aborts if <code>t</code> does not hold a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_borrow_mut">borrow_mut</a>&lt;Element&gt;(t: &<b>mut</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;): &<b>mut</b> Element {
<b>assert</b>!(<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(t), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
<a href="../move-stdlib/vector.md#0x1_vector_borrow_mut">vector::borrow_mut</a>(&<b>mut</b> t.vec, 0)
<b>assert</b>!(t.<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
&<b>mut</b> t.vec[0]
}
</code></pre>

Expand All @@ -400,10 +400,10 @@ Aborts if <code>t</code> does not hold a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_swap">swap</a>&lt;Element&gt;(t: &<b>mut</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;, e: Element): Element {
<b>assert</b>!(<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(t), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
<b>assert</b>!(t.<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
<b>let</b> vec_ref = &<b>mut</b> t.vec;
<b>let</b> old_value = <a href="../move-stdlib/vector.md#0x1_vector_pop_back">vector::pop_back</a>(vec_ref);
<a href="../move-stdlib/vector.md#0x1_vector_push_back">vector::push_back</a>(vec_ref, e);
<b>let</b> old_value = vec_ref.pop_back();
vec_ref.push_back(e);
old_value
}
</code></pre>
Expand Down Expand Up @@ -432,9 +432,9 @@ Different from swap(), swap_or_fill() allows for <code>t</code> not holding a va

<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_swap_or_fill">swap_or_fill</a>&lt;Element&gt;(t: &<b>mut</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;, e: Element): <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt; {
<b>let</b> vec_ref = &<b>mut</b> t.vec;
<b>let</b> old_value = <b>if</b> (<a href="../move-stdlib/vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) <a href="../move-stdlib/option.md#0x1_option_none">none</a>()
<b>else</b> <a href="../move-stdlib/option.md#0x1_option_some">some</a>(<a href="../move-stdlib/vector.md#0x1_vector_pop_back">vector::pop_back</a>(vec_ref));
<a href="../move-stdlib/vector.md#0x1_vector_push_back">vector::push_back</a>(vec_ref, e);
<b>let</b> old_value = <b>if</b> (vec_ref.is_empty()) <a href="../move-stdlib/option.md#0x1_option_none">none</a>()
<b>else</b> <a href="../move-stdlib/option.md#0x1_option_some">some</a>(vec_ref.pop_back());
vec_ref.push_back(e);
old_value
}
</code></pre>
Expand All @@ -461,8 +461,8 @@ Destroys <code>t.</code> If <code>t</code> holds a value, return it. Returns <co

<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_destroy_with_default">destroy_with_default</a>&lt;Element: drop&gt;(t: <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;, default: Element): Element {
<b>let</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a> { <b>mut</b> vec } = t;
<b>if</b> (<a href="../move-stdlib/vector.md#0x1_vector_is_empty">vector::is_empty</a>(&vec)) default
<b>else</b> <a href="../move-stdlib/vector.md#0x1_vector_pop_back">vector::pop_back</a>(&<b>mut</b> vec)
<b>if</b> (vec.is_empty()) default
<b>else</b> vec.pop_back()
}
</code></pre>

Expand All @@ -488,10 +488,10 @@ Aborts if <code>t</code> does not hold a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_destroy_some">destroy_some</a>&lt;Element&gt;(t: <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;): Element {
<b>assert</b>!(<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(&t), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
<b>assert</b>!(t.<a href="../move-stdlib/option.md#0x1_option_is_some">is_some</a>(), <a href="../move-stdlib/option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>);
<b>let</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a> { <b>mut</b> vec } = t;
<b>let</b> elem = <a href="../move-stdlib/vector.md#0x1_vector_pop_back">vector::pop_back</a>(&<b>mut</b> vec);
<a href="../move-stdlib/vector.md#0x1_vector_destroy_empty">vector::destroy_empty</a>(vec);
<b>let</b> elem = vec.pop_back();
vec.destroy_empty();
elem
}
</code></pre>
Expand All @@ -518,9 +518,9 @@ Aborts if <code>t</code> holds a value


<pre><code><b>public</b> <b>fun</b> <a href="../move-stdlib/option.md#0x1_option_destroy_none">destroy_none</a>&lt;Element&gt;(t: <a href="../move-stdlib/option.md#0x1_option_Option">Option</a>&lt;Element&gt;) {
<b>assert</b>!(<a href="../move-stdlib/option.md#0x1_option_is_none">is_none</a>(&t), <a href="../move-stdlib/option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a>);
<b>assert</b>!(t.<a href="../move-stdlib/option.md#0x1_option_is_none">is_none</a>(), <a href="../move-stdlib/option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a>);
<b>let</b> <a href="../move-stdlib/option.md#0x1_option_Option">Option</a> { vec } = t;
<a href="../move-stdlib/vector.md#0x1_vector_destroy_empty">vector::destroy_empty</a>(vec)
vec.destroy_empty()
}
</code></pre>

Expand Down
Loading

0 comments on commit 5ec36c6

Please sign in to comment.