Skip to content

Commit

Permalink
Sway Ref: Assertions assert_eq (FuelLabs#4688)
Browse files Browse the repository at this point in the history
  • Loading branch information
Braqzen authored Jun 21, 2023
1 parent 7f5bac5 commit 91ce0f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/reference/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
- [assert](./documentation/operations/assertions/assert.md)
- [require](./documentation/operations/assertions/require.md)
- [revert](./documentation/operations/assertions/revert.md)
- [assert_eq](./documentation/operations/assertions/assert-eq.md)
- [Address Namespace](./documentation/operations/namespace/index.md)
- [Address](./documentation/operations/namespace/address.md)
- [ContractId](./documentation/operations/namespace/contract-id.md)
Expand Down
11 changes: 11 additions & 0 deletions docs/reference/src/code/operations/assertions/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ library;

mod req;

#[allow(dead_code)]
// ANCHOR: assert
fn subtract(a: u64, b: u64) -> u64 {
assert(b <= a);
a - b
}
// ANCHOR_END: assert

#[allow(dead_code)]
fn reverts() {
// ANCHOR: revert
revert(42);
// ANCHOR_END: revert
}

#[allow(dead_code)]
// ANCHOR: assert_eq
fn compare(a: u64, b: u64) {
assert_eq(a, b);
// code
}
// ANCHOR_END: assert_eq
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# assert_eq

The `assert_eq` function is automatically imported into every program from the [prelude](../../misc/prelude.md). It takes two expressions which are compared and the result is a [Boolean](../../language/built-ins/boolean.md). If the value is `false` then the virtual machine will revert.

## Example

Here is a function which asserts that `a` and `b` must be equal.

```sway
{{#include ../../../code/operations/assertions/src/lib.sw:assert_eq}}
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Handling exceptions may be done through [if expressions](../../language/control-
- [`assert`](assert.md): Checks if a `condition` is `true` otherwise reverts
- [`require`](require.md): Checks if a `condition` is `true` otherwise logs a `value` and reverts
- [`revert`](revert.md): Reverts the virtual machine with the provided exit code
- [`assert_eq`](assert-eq.md): Checks if `a` and `b` are equal otherwise reverts

0 comments on commit 91ce0f2

Please sign in to comment.