Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add more impls on Option #4549

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions noir_stdlib/src/option.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::hash::{Hash, Hasher};
use crate::cmp::{Ordering, Ord, Eq};
use crate::default::Default;

struct Option<T> {
_is_some: bool,
_value: T,
Expand Down Expand Up @@ -152,3 +156,51 @@ impl<T> Option<T> {
}
}
}

impl<T> Default for Option<T> {
fn default() -> Self {
Option::none()
}
}

impl<T> Eq for Option<T> where T: Eq {
fn eq(self, other: Self) -> bool {
if self._is_some == other._is_some {
if self._is_some {
self._value == other._value
} else {
true
}
} else {
false
}
}
}

impl<T> Hash for Option<T> where T: Hash {
fn hash<H>(self, state: &mut H) where H: Hasher {
self._is_some.hash(state);
if self._is_some {
self._value.hash(state);
}
}
}

// For this impl we're declaring Option::none < Option::some
impl<T> Ord for Option<T> where T: Ord {
fn cmp(self, other: Self) -> Ordering {
if self._is_some {
if other._is_some {
self._value.cmp(other._value)
} else {
Ordering::greater()
}
} else {
if other._is_some {
Ordering::less()
} else {
Ordering::equal()
}
}
}
}
Loading