Skip to content

Commit

Permalink
Add Eq for Result (#6140)
Browse files Browse the repository at this point in the history
## Description
Closes #6077

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
  • Loading branch information
SwayStar123 and K1-R1 authored Sep 13, 2024
1 parent 0fa1b9a commit 7b07216
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sway-lib-std/src/result.sw
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,17 @@ impl<T, E> Result<T, E> {
// - `ok(self) -> Option<T>`
// - `err(self) -> Option<E>`
}

impl<T, E> Eq for Result<T, E>
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,
}
}
}
114 changes: 114 additions & 0 deletions test/src/in_language_tests/test_programs/result_inline_tests/src/eq.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
library;

#[test]
fn test_eq_u64_u64() {
let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(0);
assert_eq(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Ok(1);
assert_eq(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Ok(42);
assert_eq(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Ok(u64::max());
assert_eq(a, b);
}

#[test]
fn test_neq_u64_u64() {
// Ok
let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(1);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(42);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Ok(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Ok(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Ok(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Ok(0);
assert_ne(a, b);

// Err
let a: Result<u64, u64> = Err(0);
let b: Result<u64, u64> = Err(1);
assert_ne(a, b);

let a: Result<u64, u64> = Err(0);
let b: Result<u64, u64> = Err(42);
assert_ne(a, b);

let a: Result<u64, u64> = Err(0);
let b: Result<u64, u64> = Err(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Err(1);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Err(42);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Err(u64::max());
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

// Ok-Err
let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Err(1);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Err(42);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Err(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(1);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(42);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(0);
let b: Result<u64, u64> = Err(u64::max());
assert_ne(a, b);

let a: Result<u64, u64> = Ok(1);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(42);
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);

let a: Result<u64, u64> = Ok(u64::max());
let b: Result<u64, u64> = Err(0);
assert_ne(a, b);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library;

mod eq;

#[test]
fn result_is_ok() {
use std::bytes::Bytes;
Expand Down

0 comments on commit 7b07216

Please sign in to comment.