Skip to content

Commit

Permalink
Move std::assert::require to std::revert::require (#2285)
Browse files Browse the repository at this point in the history
* Move `std::assert::require` to `std::revert`

* Fix comment

* Remove unused use

Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com>
  • Loading branch information
AlicanC and mohammadfawaz committed Jul 23, 2022
1 parent 02b7e94 commit aa5dc0d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
3 changes: 1 addition & 2 deletions examples/identity/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ use errors::MyError;

use std::{
address::Address,
assert::require,
chain::auth::{AuthError, msg_sender},
constants::{BASE_ASSET_ID, ZERO_B256},
contract_id::ContractId,
identity::Identity,
result::Result,
revert::revert,
revert::{require, revert},
token::{force_transfer_to_contract, transfer_to_output}
};

Expand Down
21 changes: 5 additions & 16 deletions sway-lib-std/src/assert.sw
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
library assert;

use ::logging::log;
use ::revert::revert;

const FAILED_REQUIRE_SIGNAL = 42;

/// Assert that a value is true
pub fn assert(a: bool) {
if !a {
/// Asserts that the given `condition` will always be `true` during runtime.
/// To check for conditions that may not be `true`, use `std::revert::require` instead.
/// See: https://en.wikipedia.org/wiki/Assertion_(software_development)#Comparison_with_error_handling
pub fn assert(condition: bool) {
if !condition {
revert(0);
} else {
()
}
}

/// A wrapper for `assert` that allows logging a custom value `v` if condition `c` is not true.
pub fn require<T>(c: bool, v: T) {
if !c {
log(v);
revert(FAILED_REQUIRE_SIGNAL)
} else {
()
}
}
2 changes: 1 addition & 1 deletion sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
library std;

dep intrinsics;
dep revert;
dep logging;
dep revert;
dep assert;
dep option;
dep result;
Expand Down
14 changes: 14 additions & 0 deletions sway-lib-std/src/revert.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
library revert;

use ::logging::log;

const FAILED_REQUIRE_SIGNAL = 42;

/// Context-dependent:
/// will panic if used in a predicate
/// will revert if used in a contract
Expand All @@ -8,3 +12,13 @@ pub fn revert(code: u64) {
rvrt r1;
}
}

/// Checks if the given `condition` is `true` and if not, logs `value` and reverts.
pub fn require<T>(condition: bool, value: T) {
if !condition {
log(value);
revert(FAILED_REQUIRE_SIGNAL)
} else {
()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

use std::assert::require;
use std::revert::require;

const MY_CUSTOM_ERROR_MESSAGE = 100;
const forty_twos = 0x4242424242424242424242424242424242424242424242424242424242424242;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ contract;

use methods_abi::MethodsContract;

use std::result::*;
use std::identity::*;
use std::chain::auth::*;
use std::option::*;
use std::assert::*;
use std::revert::*;

fn bogus() -> Identity {
let sender = msg_sender();
Expand Down

0 comments on commit aa5dc0d

Please sign in to comment.