Skip to content

Commit

Permalink
Require destruct instead of drop for unwrapping. (#5902)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalesokhin-starkware authored Jun 27, 2024
1 parent 7b8c7b8 commit 80fadf8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
8 changes: 4 additions & 4 deletions corelib/src/option.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ pub trait OptionTrait<T> {
fn unwrap(self: Option<T>) -> T;
/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Option::Some(v)` to
/// `Result::Ok(v)` and `Option::None` to `Result::Err(err)`.
fn ok_or<E, +Drop<E>>(self: Option<T>, err: E) -> Result<T, E>;
fn ok_or<E, +Destruct<E>>(self: Option<T>, err: E) -> Result<T, E>;
/// Returns `true` if the `Option` is `Option::Some`.
#[must_use]
fn is_some(self: @Option<T>) -> bool;
/// Returns `true` if the `Option` is `Option::None`.
#[must_use]
fn is_none(self: @Option<T>) -> bool;
/// If `self` is `Option::Some(x)`, returns `x`. Otherwise, returns the provided default.
fn unwrap_or<+Drop<T>>(self: Option<T>, default: T) -> T;
fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T;
/// If `self` is `Option::Some(x)`, returns `x`. Otherwise, returns `Default::<T>::default()`.
fn unwrap_or_default<+Default<T>>(self: Option<T>) -> T;
}
Expand All @@ -50,7 +50,7 @@ pub impl OptionTraitImpl<T> of OptionTrait<T> {
}

#[inline]
fn ok_or<E, +Drop<E>>(self: Option<T>, err: E) -> Result<T, E> {
fn ok_or<E, +Destruct<E>>(self: Option<T>, err: E) -> Result<T, E> {
match self {
Option::Some(v) => Result::Ok(v),
Option::None => Result::Err(err),
Expand All @@ -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<+PanicDestruct<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<+PanicDestruct<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<+PanicDestruct<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
17 changes: 7 additions & 10 deletions corelib/src/test/circuit_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ fn test_circuit_success() {
let mul = circuit_mul(inv, sub);

let modulus = TryInto::<_, CircuitModulus>::try_into([7, 0, 0, 0]).unwrap();
let outputs =
match (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") }
};
let outputs = (mul, add, inv)
.new_inputs()
.next([3, 0, 0, 0])
.next([6, 0, 0, 0])
.done()
.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
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ Inlined at:
In function: lib.cairo::foo
enum_init<core::panics::PanicResult::<(core::integer::u8,)>, 0>([32]) -> ([33])
Originating location:
fn expect<+Drop<E>>(self: Result<T, E>, err: felt252) -> T {
^
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
^
In function: core::result::ResultTraitImpl::expect
Inlined at:
u8_overflowing_add(lhs, rhs).expect('u8_add Overflow')
Expand All @@ -631,8 +631,8 @@ Inlined at:
In function: lib.cairo::foo
store_temp<RangeCheck>([28]) -> ([28])
Originating location:
fn expect<+Drop<E>>(self: Result<T, E>, err: felt252) -> T {
^
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
^
In function: core::result::ResultTraitImpl::expect
Inlined at:
u8_overflowing_add(lhs, rhs).expect('u8_add Overflow')
Expand All @@ -644,8 +644,8 @@ Inlined at:
In function: lib.cairo::foo
store_temp<core::panics::PanicResult::<(core::integer::u8,)>>([33]) -> ([33])
Originating location:
fn expect<+Drop<E>>(self: Result<T, E>, err: felt252) -> T {
^
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
^
In function: core::result::ResultTraitImpl::expect
Inlined at:
u8_overflowing_add(lhs, rhs).expect('u8_add Overflow')
Expand All @@ -657,8 +657,8 @@ Inlined at:
In function: lib.cairo::foo
return([28], [33])
Originating location:
fn expect<+Drop<E>>(self: Result<T, E>, err: felt252) -> T {
^
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
^
In function: core::result::ResultTraitImpl::expect
Inlined at:
u8_overflowing_add(lhs, rhs).expect('u8_add Overflow')
Expand Down

0 comments on commit 80fadf8

Please sign in to comment.