Skip to content

Commit

Permalink
Added Eq for identity (FuelLabs#2028)
Browse files Browse the repository at this point in the history
* Added Eq for identity
  • Loading branch information
Braqzen authored Jun 17, 2022
1 parent 7fb883c commit 45d9e52
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sway-lib-std/src/identity.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ pub enum Identity {
Address: Address,
ContractId: ContractId,
}

impl core::ops::Eq for Identity {
fn eq(self, other: Self) -> bool {
match (self, other) {
(Identity::Address(address1), Identity::Address(address2)) => { address1 == address2 },
(Identity::ContractId(asset1), Identity::ContractId(asset2)) => { asset1 == asset2 },
_ => { false },
}
}
}
1 change: 1 addition & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub fn run(locked: bool, filter_regex: Option<regex::Regex>) {
ProgramState::Return(1),
), // true
("should_pass/stdlib/ge_test", ProgramState::Return(1)), // true
("should_pass/stdlib/identity_eq", ProgramState::Return(1)), // true
("should_pass/stdlib/intrinsics", ProgramState::Return(1)), // true
("should_pass/stdlib/option", ProgramState::Return(1)), // true
("should_pass/stdlib/require", ProgramState::Return(1)), // true
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,14 @@
[[package]]
name = 'core'
source = 'path+from-root-E7BBED1900347958'
dependencies = []

[[package]]
name = 'identity_eq'
source = 'root'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-E7BBED1900347958'
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 = "identity_eq"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "bool"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
script;

use std::{address::Address, assert::assert, contract_id::ContractId, identity::*};

fn main() -> bool {
let b1 = 0x0000000000000000000000000000000000000000000000000000000000000001;
let b2 = 0x0000000000000000000000000000000000000000000000000000000000000002;

let address1 = Identity::Address(~Address::from(b1));
let address2 = Identity::Address(~Address::from(b2));
let contract1 = Identity::ContractId(~ContractId::from(b1));
let contract2 = Identity::ContractId(~ContractId::from(b2));

// Eq is True
assert(address1 == address1);
assert(contract1 == contract1);

// Eq is False
assert(!(address1 == address2));
assert(!(address2 == address1));

assert(!(contract1 == contract2));
assert(!(contract2 == contract1));

assert(!(address1 == contract1));
assert(!(contract1 == address1));

// Neq is True
assert(address1 != address2);
assert(address2 != address1);

assert(contract1 != contract2);
assert(contract2 != contract1);

assert(address1 != contract1);
assert(contract1 != address1);

// Neq is False
assert(!(address1 != address1));
assert(!(contract1 != contract1));

true
}

0 comments on commit 45d9e52

Please sign in to comment.