Skip to content

Commit

Permalink
Require destruct instead of drop for unwrapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalesokhin-starkware committed Jun 26, 2024
1 parent 4d4c2a3 commit 7c59af8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion corelib/src/option.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub impl OptionTraitImpl<T> of OptionTrait<T> {
}

#[inline]
fn unwrap_or<+Drop<T>>(self: Option<T>, default: T) -> T {
fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T {
match self {
Option::Some(x) => x,
Option::None => default,
Expand Down
20 changes: 10 additions & 10 deletions corelib/src/result.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@ pub enum Result<T, E> {
#[generate_trait]
pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// If `val` is `Result::Ok(x)`, returns `x`. Otherwise, panics with `err`.
fn expect<+Drop<E>>(self: Result<T, E>, err: felt252) -> T {
fn expect<+Destruct<E>>(self: Result<T, E>, err: felt252) -> T {
match self {
Result::Ok(x) => x,
Result::Err(_) => core::panic_with_felt252(err),
}
}
/// If `val` is `Result::Ok(x)`, returns `x`. Otherwise, panics.
fn unwrap<+Drop<E>>(self: Result<T, E>) -> T {
fn unwrap<+Destruct<E>>(self: Result<T, E>) -> T {
self.expect('Result::unwrap failed.')
}
/// If `val` is `Result::Ok(x)`, returns `x`. Otherwise, returns `default`.
fn unwrap_or<+Drop<T>, +Drop<E>>(self: Result<T, E>, default: T) -> T {
fn unwrap_or<+Destruct<T>, +Destruct<E>>(self: Result<T, E>, default: T) -> T {
match self {
Result::Ok(x) => x,
Result::Err(_) => default,
}
}
/// If `val` is `Result::Ok(x)`, returns `x`.
/// Otherwise returns `Default::<T>::default()`.
fn unwrap_or_default<+Drop<E>, +Default<T>>(self: Result<T, E>) -> T {
fn unwrap_or_default<+Destruct<E>, +Default<T>>(self: Result<T, E>) -> T {
match self {
Result::Ok(x) => x,
Result::Err(_) => Default::default(),
}
}

/// If `val` is `Result::Err(x)`, returns `x`. Otherwise, panics with `err`.
fn expect_err<+Drop<T>>(self: Result<T, E>, err: felt252) -> E {
fn expect_err<+Destruct<T>>(self: Result<T, E>, err: felt252) -> E {
match self {
Result::Ok(_) => core::panic_with_felt252(err),
Result::Err(x) => x,
}
}
/// If `val` is `Result::Err(x)`, returns `x`. Otherwise, panics.
fn unwrap_err<+Drop<T>>(self: Result<T, E>) -> E {
fn unwrap_err<+Destruct<T>>(self: Result<T, E>) -> E {
self.expect_err('Result::unwrap_err failed.')
}
/// Returns `true` if the `Result` is `Result::Ok`.
Expand All @@ -67,15 +67,15 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
}
/// Returns `true` if the `Result` is `Result::Ok`, and consumes the value.
#[inline]
fn into_is_err<+Drop<T>, +Drop<E>>(self: Result<T, E>) -> bool {
fn into_is_err<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> bool {
match self {
Result::Ok(_) => false,
Result::Err(_) => true,
}
}
/// Returns `true` if the `Result` is `Result::Err`, and consumes the value.
#[inline]
fn into_is_ok<+Drop<T>, +Drop<E>>(self: Result<T, E>) -> bool {
fn into_is_ok<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> bool {
match self {
Result::Ok(_) => true,
Result::Err(_) => false,
Expand All @@ -96,7 +96,7 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// let x: Result<u32, ByteArray> = Result::Err("Nothing here");
/// assert!(x.ok().is_none());
/// ```
fn ok<+Drop<T>, +Drop<E>>(self: Result<T, E>) -> Option<T> {
fn ok<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> Option<T> {
match self {
Result::Ok(x) => Option::Some(x),
Result::Err(_) => Option::None,
Expand All @@ -117,7 +117,7 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// let x: Result<u32, ByteArray> = Result::Ok(2);
/// assert!(x.err().is_none());
/// ```
fn err<+Drop<T>, +Drop<E>>(self: Result<T, E>) -> Option<E> {
fn err<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> Option<E> {
match self {
Result::Ok(_) => Option::None,
Result::Err(x) => Option::Some(x),
Expand Down
7 changes: 2 additions & 5 deletions corelib/src/test/circuit_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ fn test_circuit_success() {

let modulus = TryInto::<_, CircuitModulus>::try_into([7, 0, 0, 0]).unwrap();
let outputs =
match (mul, add, inv)
(mul, add, inv)
.new_inputs()
.next([3, 0, 0, 0])
.next([6, 0, 0, 0])
.done()
.eval(modulus) {
Result::Ok(outputs) => { outputs },
Result::Err(_) => { panic!("Expected success") }
};
.eval(modulus).unwrap();

assert_eq!(outputs.get_output(add), u384 { limb0: 2, limb1: 0, limb2: 0, limb3: 0 });
assert_eq!(outputs.get_output(inv), u384 { limb0: 4, limb1: 0, limb2: 0, limb3: 0 });
Expand Down

0 comments on commit 7c59af8

Please sign in to comment.