Skip to content

Commit

Permalink
Add an assert_eq function to std::assert (FuelLabs#3912)
Browse files Browse the repository at this point in the history
closes FuelLabs#3242

---------

Co-authored-by: Braqzen <[email protected]>
  • Loading branch information
nfurfaro and Braqzen authored Jan 27, 2023
1 parent d558727 commit 9719086
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 3 deletions.
31 changes: 31 additions & 0 deletions sway-lib-std/src/assert.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
library assert;

use ::logging::log;
use ::revert::revert;
use ::error_signals::FAILED_ASSERT_EQ_SIGNAL;


/// 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.
Expand Down Expand Up @@ -28,3 +31,31 @@ pub fn assert(condition: bool) {
revert(0);
}
}

/// Asserts that the given values `v1` & `v2` will always be equal during runtime.
///
/// ### Arguments
///
/// * `v1` - The first value to compare.
/// * `v2` - The second value to compare.
///
/// ### Reverts
///
/// Reverts when `v1` != `v2`.
///
/// ### Examples
///
/// ```sway
/// fn foo(a: u64, b: u64) {
/// assert_eq(a, b);
/// // if code execution continues, that means `a` is equal to `b`
/// log("a is equal to b");
/// }
/// ```
pub fn assert_eq<T>(v1: T, v2: T) where T: Eq {
if (v1 != v2) {
log(v1);
log(v2);
revert(FAILED_ASSERT_EQ_SIGNAL);
}
}
2 changes: 1 addition & 1 deletion sway-lib-std/src/auth.sw
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn msg_sender() -> Result<Identity, AuthError> {
/// `TransactionScript` if they all share the same owner.
fn inputs_owner() -> Result<Identity, AuthError> {
let inputs = input_count();
let mut candidate = Option::None::<Address>;
let mut candidate = Option::None;
let mut i = 0u8;

// Note: `inputs_count` is guaranteed to be at least 1 for any valid tx.
Expand Down
7 changes: 7 additions & 0 deletions sway-lib-std/src/error_signals.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
library error_signals;

/// Revert with this value for a failing call to `std::revert::require`.
/// 18446744073709486080
pub const FAILED_REQUIRE_SIGNAL = 0xffff_ffff_ffff_0000;

/// Revert with this value for a failing call to `std::token::transfer_to_address`.
/// 18446744073709486081
pub const FAILED_TRANSFER_TO_ADDRESS_SIGNAL = 0xffff_ffff_ffff_0001;

/// Revert with this value for a failing call to `std::message::send_message`.
/// 18446744073709486082
pub const FAILED_SEND_MESSAGE_SIGNAL = 0xffff_ffff_ffff_0002;

/// Revert with this value for a failing call to `std::assert::assert_eq`.
/// 18446744073709486083
pub const FAILED_ASSERT_EQ_SIGNAL = 0xffff_ffff_ffff_0003;
2 changes: 1 addition & 1 deletion sway-lib-std/src/vec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<T> Vec<T> {
pub fn get(self, index: u64) -> Option<T> {
// First check that index is within bounds.
if self.len <= index {
return Option::None::<T>;
return Option::None;
};

// Get a pointer to the desired element using `index`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage,0xdb8b58fba65ad50cba7e30e3340d55b1e8b6a327262810eda211293f72c2029c);
let addr = abi(BasicStorage,0xdfeb8129c8f6ecc455be98952149b3448c1b3cb2307f88a3795976c738685691);
let key = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'assert_eq'
source = 'member'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-DFF03713D0FC77C9'

[[package]]
name = 'std'
source = 'path+from-root-DFF03713D0FC77C9'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "assert_eq"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Loading

0 comments on commit 9719086

Please sign in to comment.