I need a closure that captures by-value and is called at most once, but I cannot have the function using the closure monomorphise on every passed closure, because the closures and functions are mutually recursive and the monomorphisation phase fails. I tried something like:
fn closure_user(closure: Box<FnOnce(usize) -> bool>) -> bool {
closure(3)
}
fn main() {
let big_data = vec![1, 2, 3, 4];
closure_user(Box::new(|x| {
let _ = big_data.into_iter();
false
}));
}
error[E0161]: cannot move a value of type dyn std::ops::FnOnce(usize) -> bool: the size of dyn std::ops::FnOnce(usize) -> bool cannot be statically determined
--> src/main.rs:2:5
|
2 | closure(3)
| ^^^^^^^
The unboxed version is:
fn closure_user<F>(closure: F) -> bool
where
F: FnOnce(usize) -> bool,
{
closure(42)
}
fn main() {
let big_data = vec![1, 2, 3, 4];
closure_user(|x| {
let _ = big_data.into_iter();
false
});
}
It seems that it is impossible to box and unbox the closure as a FnOnce
trait object. Is there any way to have boxed (no type parameter) and by-move (one call only) closures?