From 3e900966b495f5f55d5dac11ec0f146b70e0167a Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 13 Mar 2024 14:38:43 -0500 Subject: [PATCH 1/3] Add more impls on Option --- noir_stdlib/src/option.nr | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/noir_stdlib/src/option.nr b/noir_stdlib/src/option.nr index 1c32f758af..2e0b1eb9ad 100644 --- a/noir_stdlib/src/option.nr +++ b/noir_stdlib/src/option.nr @@ -1,3 +1,6 @@ +use crate::hash::{ Hash, Hasher }; +use crate::cmp::Ordering; + struct Option { _is_some: bool, _value: T, @@ -152,3 +155,51 @@ impl Option { } } } + +impl Default for Option { + fn default() -> Self { + Option::none() + } +} + +impl Eq for Option 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 Hash for Option where T: Hash { + fn hash(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 Ord for Option 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() + } + } + } +} From 0d7b87c807292707416a61e14c34d7ff2e35636c Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 14 Mar 2024 14:58:31 -0500 Subject: [PATCH 2/3] Add missed imports --- noir_stdlib/src/option.nr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/noir_stdlib/src/option.nr b/noir_stdlib/src/option.nr index 2e0b1eb9ad..44973c0856 100644 --- a/noir_stdlib/src/option.nr +++ b/noir_stdlib/src/option.nr @@ -1,5 +1,6 @@ use crate::hash::{ Hash, Hasher }; -use crate::cmp::Ordering; +use crate::cmp::{ Ordering, Ord, Eq }; +use crate::default::Default; struct Option { _is_some: bool, From c7214e2f49ae845348ef23c526777a0a8da9c33d Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 14 Mar 2024 15:04:38 -0500 Subject: [PATCH 3/3] Questionable fmt enforcement --- noir_stdlib/src/option.nr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/noir_stdlib/src/option.nr b/noir_stdlib/src/option.nr index 44973c0856..c94a1cf836 100644 --- a/noir_stdlib/src/option.nr +++ b/noir_stdlib/src/option.nr @@ -1,5 +1,5 @@ -use crate::hash::{ Hash, Hasher }; -use crate::cmp::{ Ordering, Ord, Eq }; +use crate::hash::{Hash, Hasher}; +use crate::cmp::{Ordering, Ord, Eq}; use crate::default::Default; struct Option {