diff --git a/noir_stdlib/src/option.nr b/noir_stdlib/src/option.nr index 5cc4dfae887..919c40fd9e0 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,20 @@ 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 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 { - self.value + self._value } else { default } @@ -43,7 +50,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 +59,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 +68,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 +77,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 +98,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 +142,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 +156,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() }