Skip to content

Commit

Permalink
fix: Rename Option::value to Option::_value (#2127)
Browse files Browse the repository at this point in the history
* Rename Option::value to Option::_value

* Add unwrap_unchecked method
  • Loading branch information
jfecher authored Aug 2, 2023
1 parent 435ab35 commit 8a1ace7
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions noir_stdlib/src/option.nr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
struct Option<T> {
_is_some: bool,
value: T,
_value: T,
}

impl<T> Option<T> {
/// 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
Expand All @@ -27,13 +27,20 @@ impl<T> Option<T> {
/// 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
}
Expand All @@ -43,7 +50,7 @@ impl<T> Option<T> {
/// a default value.
fn unwrap_or_else(self, default: fn() -> T) -> T {
if self._is_some {
self.value
self._value
} else {
default()
}
Expand All @@ -52,7 +59,7 @@ impl<T> Option<T> {
/// If self is `Some(x)`, this returns `Some(f(x))`. Otherwise, this returns `None`.
fn map<U>(self, f: fn(T) -> U) -> Option<U> {
if self._is_some {
Option::some(f(self.value))
Option::some(f(self._value))
} else {
Option::none()
}
Expand All @@ -61,7 +68,7 @@ impl<T> Option<T> {
/// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns the given default value.
fn map_or<U>(self, default: U, f: fn(T) -> U) -> U {
if self._is_some {
f(self.value)
f(self._value)
} else {
default
}
Expand All @@ -70,7 +77,7 @@ impl<T> Option<T> {
/// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns `default()`.
fn map_or_else<U>(self, default: fn() -> U, f: fn(T) -> U) -> U {
if self._is_some {
f(self.value)
f(self._value)
} else {
default()
}
Expand All @@ -91,7 +98,7 @@ impl<T> Option<T> {
/// In some languages this function is called `flat_map` or `bind`.
fn and_then<U>(self, f: fn(T) -> Option<U>) -> Option<U> {
if self._is_some {
f(self.value)
f(self._value)
} else {
Option::none()
}
Expand Down Expand Up @@ -135,7 +142,7 @@ impl<T> Option<T> {
/// 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()
Expand All @@ -149,7 +156,7 @@ impl<T> Option<T> {
/// This returns None if the outer Option is None. Otherwise, this returns the inner Option.
fn flatten(option: Option<Option<T>>) -> Option<T> {
if option._is_some {
option.value
option._value
} else {
Option::none()
}
Expand Down

0 comments on commit 8a1ace7

Please sign in to comment.