use core::{ cmp::Ordering, marker::{Destruct, Tuple}, }; use crate::ConstClosure; #[test] fn test1() { const fn func(a: &i32) -> i32 { *a + 2 } let mut f = func; const fn consume(_: F) where F: ~const FnMut(&T, &T) -> Option + ~const Destruct, { } const fn imp( (f,): (&mut F,), (a, b): (&T, &T), ) -> Option where F: ~const Fn(&T) -> K + ~const Destruct, { f(a).partial_cmp(&f(b)) } let cl = ConstClosure::new((&mut f,), imp); consume(cl); } #[test] const fn test2() { struct Func(T); impl + ~const Destruct> const FnOnce for Func { type Output = T::Output; extern "rust-call" fn call_once(self, args: A) -> Self::Output { self.0.call_once(args) } } const fn test(_a: &i32) -> Option { None } let cl = Func(test); const fn consume(_: F) where F: ~const FnOnce(&T) -> Option + ~const Destruct, { } consume(cl); } #[test] const fn test3() { const fn imp( (f,): (&mut F,), (a, b): (&T, &T), ) -> Option where F: ~const FnMut(&T) -> K + ~const Destruct, { f(a).partial_cmp(&f(b)) } const fn trans(_: &T) -> i32 { 3 } let mut tr = trans::; let mut cl = ConstClosure::new((&mut tr,), imp); const fn consume(_: F) where F: ~const FnMut(&T, &T) -> Option + ~const Destruct, { } consume(&mut cl); const fn const_is_sorted_by(_: F) -> bool where F: ~const FnMut(&T, &T) -> Option + ~const Destruct, { true } const_is_sorted_by(&mut cl); const fn imp2( (f,): (&mut F,), (a, b): (&T, &T), ) -> Option where F: ~const Fn(&T) -> K + ~const Destruct, { f(a).partial_cmp(&f(b)) } const fn testx(_: &T) -> i32 { 5 } let mut f = testx::; const_is_sorted_by(ConstClosure::new((&mut f,), imp2)); } #[test] fn test_multiple() { const fn consumer i32>(f: F) -> i32 { f() } const fn user(x: i32, y: i32) -> i32 { const fn imp((x, y): (i32, i32), _: ()) -> i32 { x + y } consumer(ConstClosure::new((x, y), imp)) } const _: () = { let x = 1; let y = 4; assert!(user(x, y) == 5); }; let x = 1; let y = 7; assert_eq!(user(x, y), 8); } #[test] fn test_multiple_mut() { const fn consumer(mut f: F) { f(5); } const fn user(x: &mut i32, y: &mut i32) { const fn imp((x, y): (&mut i32, &mut i32), (val,): (i32,)) { *x += val; *y += val; } consumer(ConstClosure::new((x, y), imp)); } const _: () = { let mut x = 0; let mut y = 4; user(&mut x, &mut y); assert!(x == 5); assert!(y == 9); }; let mut x = 1; let mut y = 7; user(&mut x, &mut y); assert_eq!(x, 6); assert_eq!(y, 12); } #[test] fn test_multiple_ref() { const fn consumer i32 + ~const Destruct>(f: F) -> i32 { f() } const fn user(x: &i32, y: &i32) -> i32 { const fn imp((x, y): (&i32, &i32), _: ()) -> i32 { *x + *y } consumer(ConstClosure::new((x, y), imp)) } const _: () = { let x = 1; let y = 4; assert!(user(&x, &y) == 5); }; let x = 1; let y = 7; assert_eq!(user(&x, &y), 8); }