Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[book] Better links / code patches #94

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion book/src/move-basics/assert-and-abort.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ The code above will, of course, abort with abort code `1`.
The `assert!` macro is a built-in macro that can be used to assert a condition. If the condition is
false, the transaction will abort with the given abort code. The `assert!` macro is a convenient way
to abort a transaction if a condition is not met. The macro shortens the code otherwise written with
an `if` expression + `abort`. The `code` argument is required and has to be a `u64` value.
an `if` expression + `abort`. The `code` argument is optional, but has to be a `u64` value or an
`#[error]` (see below for more information).

```move
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:assert}}
Expand All @@ -63,6 +64,16 @@ code and make it easier to understand the abort scenarios.
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:error_const}}
```

## Error messages

Move 2024 introduces a special type of error constant, marked with the `#[error]` attribute. This
attribute allows the error constant to be of type `vector<u8>` and can be used to store an error
message.

```move
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:error_attribute}}
```

## Further reading

- [Abort and Assert](/reference/abort-and-assert.html) in the Move Reference.
Expand Down
2 changes: 0 additions & 2 deletions book/src/move-basics/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Option is a type that represents an optional value which may or may not exist. T
in Move is borrowed from Rust, and it is a very useful primitive in Move. `Option` is defined in the
[Standard Library](./standard-library.md), and is defined as follows:

File: move-stdlib/source/option.move

```move
// File: move-stdlib/source/option.move
/// Abstraction of a value that may or may not be present.
Expand Down
23 changes: 23 additions & 0 deletions book/src/move-basics/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ and which module implements it.

</div>

## Integer Modules

The Move Standard Library provides a set of functions associated with integer types. These functions
are split into multiple modules, each associated with a specific integer type. The modules should
not be imported directly, but their functions are available on every integer value.

> All of the modules provide the same set of functions. Namely, `max`, `diff`,
> `divide_and_round_up`, `sqrt` and `pow`.

<!-- Custom CSS addition in the theme/custom.css -->
<div class="modules-table">

| Module | Description |
| ---------------------------------------------------------------------- | ----------------------------- |
| [std::u8](https://docs.sui.io/references/framework/move-stdlib/u8) | Functions for the `u8` type |
| [std::16](https://docs.sui.io/references/framework/move-stdlib/u64) | Functions for the `u16` type |
| [std::u32](https://docs.sui.io/references/framework/move-stdlib/u64) | Functions for the `u32` type |
| [std::u64](https://docs.sui.io/references/framework/move-stdlib/u64) | Functions for the `u64` type |
| [std::u128](https://docs.sui.io/references/framework/move-stdlib/u128) | Functions for the `u128` type |
| [std::u256](https://docs.sui.io/references/framework/move-stdlib/u256) | Functions for the `u256` type |

</div>

## Exported Addresses

Standard Library exports one named address - `std = 0x1`.
Expand Down
13 changes: 7 additions & 6 deletions book/src/programmability/hot-potato-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ will cover in the next section.

The pattern is used in various forms in the Sui Framework. Here are some examples:

- `sui::borrow` - uses hot potato to ensure that the borrowed value is returned to the correct
container.
- `sui::transfer_policy` - defines a `TransferRequest` - a hot potato which can only be consumed if
all conditions are met.
- `sui::token` - in the Closed Loop Token system, an `ActionRequest` carries the information about
the performed action and collects approvals similarly to `TransferRequest`.
- [sui::borrow](https://docs.sui.io/references/framework/sui-framework/borrow) - uses hot potato to
ensure that the borrowed value is returned to the correct container.
- [sui::transfer_policy](https://docs.sui.io/references/framework/sui-framework/transfer_policy) -
defines a `TransferRequest` - a hot potato which can only be consumed if all conditions are met.
- [sui::token](https://docs.sui.io/references/framework/sui-framework/token) - in the Closed Loop
Token system, an `ActionRequest` carries the information about the performed action and collects
approvals similarly to `TransferRequest`.

## Summary

Expand Down
20 changes: 12 additions & 8 deletions book/src/programmability/sui-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ still part of the same framework._

<div class="modules-table">

| Module | Description | Chapter |
| ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ----------------------------------------------- |
| [sui::bcs](https://docs.sui.io/references/framework/sui-framework/bcs) | Implements the BCS encoding and decoding functions | [Binary Canonical Serialization](./bcs.md) |
| Module | Description | Chapter |
| ---------------------------------------------------------------------------- | ---------------------------------------------------------- | ------------------------------------------ |
| [sui::bcs](https://docs.sui.io/references/framework/sui-framework/bcs) | Implements the BCS encoding and decoding functions | [Binary Canonical Serialization](./bcs.md) |
| [sui::borrow](https://docs.sui.io/references/framework/sui-framework/borrow) | Implements the borrowing mechanic for borrowing by _value_ | [Hot Potato](./hot-potato-pattern.md) |
| [sui::hex](https://docs.sui.io/references/framework/sui-framework/hex) | Implements the hex encoding and decoding functions | - |
| [sui::types](https://docs.sui.io/references/framework/sui-framework/types) | Provides a way to check if the type is a One-Time-Witness | [One Time Witness](./one-time-witness.md) |

## Exported Addresses

Expand Down Expand Up @@ -100,11 +103,12 @@ Commerce:
- sui::transfer_policy


- sui::bcs
- sui::hex
- sui::math
- sui::types
- sui::borrow
Utilities:
+ sui::bcs
+ sui::hex
- sui::math (deprecated)
+ sui::types
+ sui::borrow


- sui::authenticator
Expand Down
19 changes: 19 additions & 0 deletions packages/samples/sources/move-basics/assert-and-abort.move
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,22 @@ public fun update_record(/* ... , */ user_has_access: bool, field_exists: bool)
/* ... */
}
// ANCHOR_END: error_const

public struct User { is_authorized: bool, value: u64 }

// ANCHOR: error_attribute
#[error]
const ENotAuthorized: vector<u8> = b"The user is not authorized to perform this action";

#[error]
const EValueTooLow: vector<u8> = b"The value is too low, it should be at least 10";

/// Performs an action on behalf of the user.
public fun update_value(user: &mut User, value: u64) {
assert!(user.is_authorized, ENotAuthorized);
assert!(value >= 10, EValueTooLow);

user.value = value;
}
// ANCHOR_END: error_attribute
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct Item {}

/// Purchase an item from the shop.
public fun purchase(coin: Coin<SUI>): Item {
assert!(coin.value() == ITEM_PRICE, 0);
assert!(coin.value() == ITEM_PRICE);

transfer::public_transfer(coin, SHOP_OWNER);

Expand Down
22 changes: 11 additions & 11 deletions packages/samples/sources/move-basics/control-flow.move
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun test_if_else() {
0
};

assert!(y == 1, 0);
assert!(y == 1);
}
// ANCHOR_END: if_else
// ANCHOR: while_loop
Expand All @@ -50,9 +50,9 @@ fun while_loop(mut x: u8): u8 {

#[test]
fun test_while() {
assert!(while_loop(0) == 10, 0); // 10 times
assert!(while_loop(5) == 5, 0); // 5 times
assert!(while_loop(10) == 0, 0); // loop never executed
assert!(while_loop(0) == 10); // 10 times
assert!(while_loop(5) == 5); // 5 times
assert!(while_loop(10) == 0); // loop never executed
}
// ANCHOR_END: while_loop
// ANCHOR: infinite_while
Expand All @@ -66,7 +66,7 @@ fun test_infinite_while() {
};

// This line will never be executed.
assert!(x == 5, 0);
assert!(x == 5);
}
// ANCHOR_END: infinite_while
#[allow(dead_code)]
Expand All @@ -81,7 +81,7 @@ fun test_infinite_loop() {
};

// This line will never be executed.
assert!(x == 5, 0);
assert!(x == 5);
}
// ANCHOR_END: infinite_loop
// ANCHOR: break_loop
Expand All @@ -99,7 +99,7 @@ fun test_break_loop() {
}
};

assert!(x == 5, 0);
assert!(x == 5);
}
// ANCHOR_END: break_loop
// ANCHOR: continue_loop
Expand All @@ -124,7 +124,7 @@ fun test_continue_loop() {
}
};

assert!(x == 10, 0); // 10
assert!(x == 10); // 10
}
// ANCHOR_END: continue_loop
// ANCHOR: return_statement
Expand All @@ -144,8 +144,8 @@ fun is_positive(x: u8): bool {

#[test]
fun test_return() {
assert!(is_positive(5) == false, 0);
assert!(is_positive(0) == false, 0);
assert!(is_positive(1) == true, 0);
assert!(is_positive(5) == false);
assert!(is_positive(0) == false);
assert!(is_positive(1) == true);
}
// ANCHOR_END: return_statement
5 changes: 1 addition & 4 deletions packages/samples/sources/move-basics/expression.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#[allow(unused_variable)]
module book::expression {
module book::expression;

#[test]
fun expression_examples() {
Expand Down Expand Up @@ -81,7 +81,4 @@ while (bool_expr) { expr; };
// loop is an expression, but returns `()` as well.
loop { expr; break };
// ANCHOR_END: control_flow


}
}
6 changes: 3 additions & 3 deletions packages/samples/sources/move-basics/function.move
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public fun add(a: u64, b: u64): u64 {
#[test]
fun test_add() {
let sum = add(1, 2);
assert!(sum == 3, 0);
assert!(sum == 3);
}
// ANCHOR_END: math

Expand All @@ -36,8 +36,8 @@ fun get_name_and_age(): (vector<u8>, u8) {
// Tuple must be destructured to access its elements.
// Name and age are declared as immutable variables.
let (name, age) = get_name_and_age();
assert!(name == b"John", 0);
assert!(age == 25, 0);
assert!(name == b"John");
assert!(age == 25);
// ANCHOR_END: tuple_return_imm

// ANCHOR: tuple_return_mut
Expand Down
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/generics.move
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ fun test_swap_type_params() {
let Pair { first: pf1, second: ps1 } = pair1; // first1: u8, second1: bool
let Pair { first: pf2, second: ps2 } = pair2; // first2: bool, second2: u8

assert!(pf1 == ps2, 0x0); // 10 == 10
assert!(ps1 == pf2, 0x0); // true == true
assert!(pf1 == ps2); // 10 == 10
assert!(ps1 == pf2); // true == true
}
// ANCHOR_END: test_pair_swap

Expand Down
6 changes: 3 additions & 3 deletions packages/samples/sources/move-basics/option.move
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public fun register(
let mut opt = option::some(b"Alice");

// `option.is_some()` returns true if option contains a value.
assert!(opt.is_some(), 1);
assert!(opt.is_some());

// internal value can be `borrow`ed and `borrow_mut`ed.
assert!(opt.borrow() == &b"Alice", 0);
assert!(opt.borrow() == &b"Alice");

// `option.extract` takes the value out of the option, leaving the option empty.
let inner = opt.extract();

// `option.is_none()` returns true if option is None.
assert!(opt.is_none(), 2);
assert!(opt.is_none());
// ANCHOR_END: usage
}
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/references.move
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun test_card() {

enter_metro(&mut card);

assert!(is_valid(&card), 0); // read the card!
assert!(is_valid(&card)); // read the card!

enter_metro(&mut card); // modify the card but don't move it
enter_metro(&mut card); // modify the card but don't move it
Expand All @@ -67,7 +67,7 @@ fun test_card_2024() {
let mut card = purchase();

card.enter_metro(); // modify the card but don't move it
assert!(card.is_valid(), 0); // read the card!
assert!(card.is_valid()); // read the card!

card.enter_metro(); // modify the card but don't move it
card.enter_metro(); // modify the card but don't move it
Expand Down
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/string.move
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ let hello = b"Hello".to_string();
// this is a valid UTF-8 string
let hello = b"Hello".try_to_string();

assert!(hello.is_some(), 0); // abort if the value is not valid UTF-8
assert!(hello.is_some()); // abort if the value is not valid UTF-8

// this is not a valid UTF-8 string
let invalid = b"\xFF".try_to_string();

assert!(invalid.is_none(), 0); // abort if the value is valid UTF-8
assert!(invalid.is_none()); // abort if the value is valid UTF-8
// ANCHOR_END: safe_utf8
}
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/struct-methods-2.move
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public fun villain_health(villain: &Villain): u8 { villain.health }
// Test the methods of the `Hero` and `Villain` structs.
fun test_associated_methods() {
let hero = new_hero();
assert!(hero.health() == 100, 1);
assert!(hero.health() == 100);

let villain = new_villain();
assert!(villain.health() == 100, 3);
assert!(villain.health() == 100);
}
// ANCHOR_END: hero_and_villain
4 changes: 2 additions & 2 deletions packages/samples/sources/move-basics/struct-methods.move
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun test_methods() {
let mut hero = new();
hero.heal_spell();

assert!(hero.health() == 110, 1);
assert!(hero.mana() == 90, 2);
assert!(hero.health() == 110);
assert!(hero.mana() == 90);
}
// ANCHOR_END: hero
6 changes: 3 additions & 3 deletions packages/samples/sources/move-basics/struct.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ let mut artist = Artist {
let artist_name = artist.name;

// Access a field of the `Artist` struct.
assert!(artist.name == string::utf8(b"The Beatles"), 0);
assert!(artist.name == b"The Beatles".to_string());

// Mutate the `name` field of the `Artist` struct.
artist.name = string::utf8(b"Led Zeppelin");
artist.name = b"Led Zeppelin".to_string();

// Check that the `name` field has been mutated.
assert!(artist.name == string::utf8(b"Led Zeppelin"), 1);
assert!(artist.name == b"Led Zeppelin".to_string());
// ANCHOR_END: access

// ANCHOR: unpack
Expand Down
2 changes: 1 addition & 1 deletion packages/samples/sources/move-basics/type-reflection.move
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ fun test_type_reflection() {
let (type_name, module_name, _address_str) = do_i_know_you<MyType>();

//
assert!(module_name == b"type_reflection".to_ascii_string(), 1);
assert!(module_name == b"type_reflection".to_ascii_string());
}
// ANCHOR_END: main
9 changes: 3 additions & 6 deletions packages/samples/sources/move-basics/vector.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#[allow(unused_variable)]
module book::vector_syntax {
#[test] fun test_vector() {

// ANCHOR: literal
// An empty vector of bool elements.
let empty: vector<bool> = vector[];
Expand All @@ -21,20 +20,18 @@ let vv: vector<vector<u8>> = vector[
}

#[test] fun vector_methods() {

// ANCHOR: methods
let mut v = vector[10u8, 20, 30];

assert!(v.length() == 3, 0);
assert!(!v.is_empty(), 1);
assert!(v.length() == 3);
assert!(!v.is_empty());

v.push_back(40);
let last_value = v.pop_back();

assert!(last_value == 40, 2);
assert!(last_value == 40);
// ANCHOR_END: methods
}

}


Expand Down
Loading
Loading