From e3bc443e58a75414edcfac5461bf6902e3363aac Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 20 Jan 2025 15:20:24 +0000 Subject: [PATCH] fix: apply proper types to function arguments --- src/lib.nr | 10 +++++++--- src/quicksort/quicksort_explicit.nr | 9 ++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib.nr b/src/lib.nr index 48b1e2d..28c7f1b 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -61,7 +61,7 @@ where **/ pub fn sort_extended( input: [T; N], - sortfn: fn(T, T) -> bool, + sortfn: unconstrained fn(T, T) -> bool, sortfn_assert: fn(T, T) -> (), ) -> [T; N] where @@ -82,7 +82,7 @@ pub struct SortResult { } pub fn sort_advanced( input: [T; N], - sortfn: fn(T, T) -> bool, + sortfn: unconstrained fn(T, T) -> bool, sortfn_assert: fn(T, T) -> (), ) -> SortResult where @@ -107,6 +107,10 @@ mod test { a <= b } + unconstrained fn __sort_u32(a: u32, b: u32) -> bool { + a <= b + } + // unconditional_lt will cost fewer constraints than the `<=` operator // as we do not need to constrain the case where `a > b`, and assign a boolean variable to the result fn unconditional_lt(_a: u32, _b: u32) { @@ -141,7 +145,7 @@ mod test { fn test_sort_extended() { let mut arr: [u32; 7] = [3, 6, 8, 10, 1, 2, 1]; - let sorted = sort_extended(arr, sort_u32, unconditional_lt); + let sorted = sort_extended(arr, __sort_u32, unconditional_lt); let expected: [u32; 7] = [1, 1, 2, 3, 6, 8, 10]; assert(sorted == expected); diff --git a/src/quicksort/quicksort_explicit.nr b/src/quicksort/quicksort_explicit.nr index 85dbe12..6001c8c 100644 --- a/src/quicksort/quicksort_explicit.nr +++ b/src/quicksort/quicksort_explicit.nr @@ -14,7 +14,7 @@ unconstrained fn partition( arr: &mut [T; N], low: u32, high: u32, - sortfn: fn(T, T) -> bool, + sortfn: unconstrained fn(T, T) -> bool, ) -> u32 { let pivot = high; let mut i = low; @@ -32,7 +32,7 @@ unconstrained fn quicksort_recursive( arr: &mut [T; N], low: u32, high: u32, - sortfn: fn(T, T) -> bool, + sortfn: unconstrained fn(T, T) -> bool, ) { if low < high { let pivot_index = partition(arr, low, high, sortfn); @@ -43,7 +43,10 @@ unconstrained fn quicksort_recursive( } } -pub unconstrained fn quicksort(_arr: [T; N], sortfn: fn(T, T) -> bool) -> [T; N] { +pub unconstrained fn quicksort( + _arr: [T; N], + sortfn: unconstrained fn(T, T) -> bool, +) -> [T; N] { let mut arr: [T; N] = _arr; if arr.len() <= 1 {} else { quicksort_recursive(&mut arr, 0, arr.len() - 1, sortfn);