From b83f9dffd14447067ff1f05c7b61d8bbf032ffaa Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 2 Aug 2023 10:29:07 -0500 Subject: [PATCH 1/2] Rename Option::value to Option::_value --- noir_stdlib/src/option.nr | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/noir_stdlib/src/option.nr b/noir_stdlib/src/option.nr index 5cc4dfae887..6949108eb32 100644 --- a/noir_stdlib/src/option.nr +++ b/noir_stdlib/src/option.nr @@ -1,17 +1,17 @@ struct Option { _is_some: bool, - value: T, + _value: T, } impl Option { /// Constructs a None value fn none() -> Self { - Self { _is_some: false, value: crate::unsafe::zeroed() } + Self { _is_some: false, _value: crate::unsafe::zeroed() } } /// Constructs a Some wrapper around the given value - fn some(value: T) -> Self { - Self { _is_some: true, value } + fn some(_value: T) -> Self { + Self { _is_some: true, _value } } /// True if this Option is None @@ -27,13 +27,13 @@ impl Option { /// Asserts `self.is_some()` and returns the wrapped value. fn unwrap(self) -> T { assert(self._is_some); - self.value + self._value } /// Returns the wrapped value if `self.is_some()`. Otherwise, returns the given default value. fn unwrap_or(self, default: T) -> T { if self._is_some { - self.value + self._value } else { default } @@ -43,7 +43,7 @@ impl Option { /// a default value. fn unwrap_or_else(self, default: fn() -> T) -> T { if self._is_some { - self.value + self._value } else { default() } @@ -52,7 +52,7 @@ impl Option { /// If self is `Some(x)`, this returns `Some(f(x))`. Otherwise, this returns `None`. fn map(self, f: fn(T) -> U) -> Option { if self._is_some { - Option::some(f(self.value)) + Option::some(f(self._value)) } else { Option::none() } @@ -61,7 +61,7 @@ impl Option { /// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns the given default value. fn map_or(self, default: U, f: fn(T) -> U) -> U { if self._is_some { - f(self.value) + f(self._value) } else { default } @@ -70,7 +70,7 @@ impl Option { /// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns `default()`. fn map_or_else(self, default: fn() -> U, f: fn(T) -> U) -> U { if self._is_some { - f(self.value) + f(self._value) } else { default() } @@ -91,7 +91,7 @@ impl Option { /// In some languages this function is called `flat_map` or `bind`. fn and_then(self, f: fn(T) -> Option) -> Option { if self._is_some { - f(self.value) + f(self._value) } else { Option::none() } @@ -135,7 +135,7 @@ impl Option { /// Otherwise, this returns `None` fn filter(self, predicate: fn(T) -> bool) -> Self { if self._is_some { - if predicate(self.value) { + if predicate(self._value) { self } else { Option::none() @@ -149,7 +149,7 @@ impl Option { /// This returns None if the outer Option is None. Otherwise, this returns the inner Option. fn flatten(option: Option>) -> Option { if option._is_some { - option.value + option._value } else { Option::none() } From 345ac27ebd1154c09cc36c0d3c6b1e237e810555 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 2 Aug 2023 10:33:20 -0500 Subject: [PATCH 2/2] Add unwrap_unchecked method --- noir_stdlib/src/option.nr | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/noir_stdlib/src/option.nr b/noir_stdlib/src/option.nr index 6949108eb32..919c40fd9e0 100644 --- a/noir_stdlib/src/option.nr +++ b/noir_stdlib/src/option.nr @@ -30,6 +30,13 @@ impl Option { self._value } + /// Returns the inner value without asserting `self.is_some()` + /// Note that if `self` is `None`, there is no guarantee what value will be returned, + /// only that it will be of type `T`. + fn unwrap_unchecked(self) -> T { + self._value + } + /// Returns the wrapped value if `self.is_some()`. Otherwise, returns the given default value. fn unwrap_or(self, default: T) -> T { if self._is_some {