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 Option<T> to noir stdlib #1781

Merged
merged 8 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
jfecher marked this conversation as resolved.
Show resolved Hide resolved
authors = [""]
compiler_version = "0.7.0"

[dependencies]
53 changes: 53 additions & 0 deletions crates/nargo_cli/tests/test_data_ssa_refactor/option/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use dep::std::option::Option;

fn main() {
let none = Option::none();
let some = Option::some(3);

assert(none.is_none());
assert(some.is_some());

assert(some.unwrap() == 3);

assert(none.unwrap_or(2) == 2);
assert(some.unwrap_or(2) == 3);

assert(none.unwrap_or_else(|| 5) == 5);
assert(some.unwrap_or_else(|| 5) == 3);

assert(none.map(|x| x * 2).is_none());
assert(some.map(|x| x * 2).unwrap() == 6);

assert(none.map_or(0, |x| x * 2) == 0);
assert(some.map_or(0, |x| x * 2) == 6);

assert(none.map_or_else(|| 0, |x| x * 2) == 0);
assert(some.map_or_else(|| 0, |x| x * 2) == 6);

assert(none.and(none).is_none());
assert(none.and(some).is_none());
assert(some.and(none).is_none());
assert(some.and(some).is_some());

let add1_u64 = |value: Field| Option::some(value as u64 + 1);

assert(none.and_then(|_value| Option::none()).is_none());
assert(none.and_then(add1_u64).is_none());
assert(some.and_then(|_value| Option::none()).is_none());
assert(some.and_then(add1_u64).unwrap() == 4);

assert(none.or(none).is_none());
assert(none.or(some).is_some());
assert(some.or(none).is_some());
assert(some.or(some).is_some());

assert(none.or_else(|| Option::none()).is_none());
assert(none.or_else(|| Option::some(5)).is_some());
assert(some.or_else(|| Option::none()).is_some());
assert(some.or_else(|| Option::some(5)).is_some());

assert(none.xor(none).is_none());
assert(none.xor(some).is_some());
assert(some.xor(none).is_some());
assert(some.xor(some).is_none());
}
1 change: 1 addition & 0 deletions noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod ec;
mod unsafe;
mod collections;
mod compat;
mod option;

#[builtin(println)]
fn println<T>(_input : T) {}
Expand Down
113 changes: 113 additions & 0 deletions noir_stdlib/src/option.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
struct Option<T> {
_is_some: bool,
value: T,
}

impl<T> Option<T> {
fn none() -> Self {
Self { _is_some: false, value: crate::unsafe::zeroed() }
}

fn some(value: T) -> Self {
Self { _is_some: true, value }
}

fn is_none(self) -> bool {
!self._is_some
}

fn is_some(self) -> bool {
self._is_some
}

fn unwrap(self) -> T {
assert(self._is_some);
self.value
}

fn unwrap_or(self, default: T) -> T {
if self._is_some {
self.value
} else {
default
}
}

fn unwrap_or_else(self, default: fn() -> T) -> T {
if self._is_some {
self.value
} else {
default()
}
}

fn map<U>(self, f: fn(T) -> U) -> Option<U> {
if self._is_some {
Option::some(f(self.value))
} else {
Option::none()
}
}

fn map_or<U>(self, default: U, f: fn(T) -> U) -> U {
if self._is_some {
f(self.value)
} else {
default
}
}

fn map_or_else<U>(self, default: fn() -> U, f: fn(T) -> U) -> U {
if self._is_some {
f(self.value)
} else {
default()
}
}

fn and(self, other: Self) -> Self {
jfecher marked this conversation as resolved.
Show resolved Hide resolved
if self.is_none() {
Option::none()
} else {
other
}
}

fn and_then<U>(self, f: fn(T) -> Option<U>) -> Option<U> {
if self._is_some {
f(self.value)
} else {
Option::none()
}
}

fn or(self, other: Self) -> Self {
if self._is_some {
self
} else {
other
}
}

fn or_else<U>(self, default: fn() -> Option<T>) -> Option<T> {
if self._is_some {
self
} else {
default()
}
}

fn xor(self, other: Self) -> Self {
if self._is_some {
if other._is_some {
Option::none()
} else {
self
}
} else if other._is_some {
other
} else {
Option::none()
}
}
}