Skip to content

Commit

Permalink
added calculation for closure type size
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerStarkware committed Dec 22, 2024
1 parent 63dc720 commit f0c68f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 23 additions & 3 deletions corelib/src/test/language_features/closure_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,35 @@ fn option_map_test() {
assert_eq!(option_map(Option::Some(2), |x| Option::Some(x)), Option::Some(Option::Some(2)));
}

fn array_map<T, F, impl Fn: core::ops::Fn<F, (T,)>, +Drop<T>, +Drop<F>, +Drop<Fn::Output>>(
fn fix_sized_array_map<
T, F, impl Fn: core::ops::Fn<F, (T,)>, +Drop<T>, +Drop<F>, +Drop<Fn::Output>,
>(
arr: [T; 2], f: F,
) -> [core::ops::Fn::<F, (T,)>::Output; 2] {
let [a, b] = arr;
[f(a), f(b)]
}

#[test]
fn array_map_test() {
assert_eq!(array_map([2, 3], |x| x + 3), [5, 6]);
fn fix_sized_array_map_test() {
assert_eq!(fix_sized_array_map([2, 3], |x| x + 3), [5, 6]);
}

#[generate_trait]
impl ArrayExt of ArrayExtTrait {
fn map<T, +Drop<T>, F, +Drop<F>, impl func: core::ops::Fn<F, (T,)>, +Drop<func::Output>>(
self: Array<T>, f: F,
) -> Array<func::Output> {
let mut output: Array<func::Output> = array![];
for elem in self {
output.append(f(elem));
};
output
}
}
#[test]
fn array_map_test() {
let arr = array![1, 2, 3];
let result = arr.map(|x| x + 1);
assert_eq!(result, array![2, 3, 4]);
}
4 changes: 3 additions & 1 deletion crates/cairo-lang-lowering/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,9 @@ fn type_size(db: &dyn LoweringGroup, ty: TypeId) -> usize {
.to_usize()
.unwrap()
}
TypeLongId::Closure(_) => unimplemented!(),
TypeLongId::Closure(closure_ty) => {
closure_ty.captured_types.iter().map(|ty| db.type_size(*ty)).sum()
}
TypeLongId::Coupon(_) => 0,
TypeLongId::GenericParameter(_)
| TypeLongId::Var(_)
Expand Down

0 comments on commit f0c68f4

Please sign in to comment.