Skip to content

Commit

Permalink
feat(corelib): Rewrite get_or_insert_with code
Browse files Browse the repository at this point in the history
  • Loading branch information
hudem1 committed Jan 15, 2025
1 parent 5218066 commit 9a8c410
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions corelib/src/option.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -805,11 +805,14 @@ pub impl OptionTraitImpl<T> of OptionTrait<T> {
fn get_or_insert_with<F, +core::ops::FnOnce<F, ()>[Output: T], +Drop<F>, +Copy<T>, +Drop<T>>(
ref self: Option<T>, f: F,
) -> T {
if self.is_none() {
self = Option::Some(f());
};

self.unwrap()
match self {
Option::Some(value) => value,
Option::None => {
let value = f();
self = Option::Some(value);
value
}
}
}
}

Expand Down

0 comments on commit 9a8c410

Please sign in to comment.