diff --git a/sway-lib-std/src/result.sw b/sway-lib-std/src/result.sw index f4f5f79a2b0..9b0532ee567 100644 --- a/sway-lib-std/src/result.sw +++ b/sway-lib-std/src/result.sw @@ -269,3 +269,17 @@ impl Result { // - `ok(self) -> Option` // - `err(self) -> Option` } + +impl Eq for Result +where + T: Eq, + E: Eq, +{ + fn eq(self, other: Self) -> bool { + match (self, other) { + (Self::Ok(a), Self::Ok(b)) => a == b, + (Self::Err(a), Self::Err(b)) => a == b, + _ => false, + } + } +} diff --git a/test/src/in_language_tests/test_programs/result_inline_tests/src/eq.sw b/test/src/in_language_tests/test_programs/result_inline_tests/src/eq.sw new file mode 100644 index 00000000000..18ce687b26e --- /dev/null +++ b/test/src/in_language_tests/test_programs/result_inline_tests/src/eq.sw @@ -0,0 +1,114 @@ +library; + +#[test] +fn test_eq_u64_u64() { + let a: Result = Ok(0); + let b: Result = Ok(0); + assert_eq(a, b); + + let a: Result = Ok(1); + let b: Result = Ok(1); + assert_eq(a, b); + + let a: Result = Ok(42); + let b: Result = Ok(42); + assert_eq(a, b); + + let a: Result = Ok(u64::max()); + let b: Result = Ok(u64::max()); + assert_eq(a, b); +} + +#[test] +fn test_neq_u64_u64() { + // Ok + let a: Result = Ok(0); + let b: Result = Ok(1); + assert_ne(a, b); + + let a: Result = Ok(0); + let b: Result = Ok(42); + assert_ne(a, b); + + let a: Result = Ok(0); + let b: Result = Ok(u64::max()); + assert_ne(a, b); + + let a: Result = Ok(1); + let b: Result = Ok(0); + assert_ne(a, b); + + let a: Result = Ok(42); + let b: Result = Ok(0); + assert_ne(a, b); + + let a: Result = Ok(u64::max()); + let b: Result = Ok(0); + assert_ne(a, b); + + // Err + let a: Result = Err(0); + let b: Result = Err(1); + assert_ne(a, b); + + let a: Result = Err(0); + let b: Result = Err(42); + assert_ne(a, b); + + let a: Result = Err(0); + let b: Result = Err(u64::max()); + assert_ne(a, b); + + let a: Result = Err(1); + let b: Result = Err(0); + assert_ne(a, b); + + let a: Result = Err(42); + let b: Result = Err(0); + assert_ne(a, b); + + let a: Result = Err(u64::max()); + let b: Result = Err(0); + assert_ne(a, b); + + // Ok-Err + let a: Result = Ok(0); + let b: Result = Err(0); + assert_ne(a, b); + + let a: Result = Ok(1); + let b: Result = Err(1); + assert_ne(a, b); + + let a: Result = Ok(42); + let b: Result = Err(42); + assert_ne(a, b); + + let a: Result = Ok(u64::max()); + let b: Result = Err(u64::max()); + assert_ne(a, b); + + let a: Result = Ok(0); + let b: Result = Err(1); + assert_ne(a, b); + + let a: Result = Ok(0); + let b: Result = Err(42); + assert_ne(a, b); + + let a: Result = Ok(0); + let b: Result = Err(u64::max()); + assert_ne(a, b); + + let a: Result = Ok(1); + let b: Result = Err(0); + assert_ne(a, b); + + let a: Result = Ok(42); + let b: Result = Err(0); + assert_ne(a, b); + + let a: Result = Ok(u64::max()); + let b: Result = Err(0); + assert_ne(a, b); +} diff --git a/test/src/in_language_tests/test_programs/result_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/result_inline_tests/src/main.sw index 2166d4b643b..381c4ec24f5 100644 --- a/test/src/in_language_tests/test_programs/result_inline_tests/src/main.sw +++ b/test/src/in_language_tests/test_programs/result_inline_tests/src/main.sw @@ -1,5 +1,7 @@ library; +mod eq; + #[test] fn result_is_ok() { use std::bytes::Bytes;