Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cannot move a value of type FnOnce" when moving a boxed function

Tags:

rust

I'm trying to do some higher order programming in Rust, but I'm having some difficulty dealing with closures. Here's a code snippet that illustrates one of the problems I'm having:

pub enum Foo {
    Bar(Box<FnOnce(i32)>),
}

pub fn app(i: i32, arg: Foo) {
    match arg {
        Foo::Bar(f) => f(i),
    }
}

When I compile this piece of code I get the following error message:

error[E0161]: cannot move a value of type std::ops::FnOnce(i32) + 'static: the size of std::ops::FnOnce(i32) + 'static cannot be statically determined
 --> src/main.rs:7:24
  |
7 |         Foo::Bar(f) => f(i),
  |                        ^

Since I put the function in a Box, I would have thought that that would deal with the problem of the compiler not knowing the size. How can I make the above program compile?

like image 850
svenningsson Avatar asked May 23 '15 10:05

svenningsson


1 Answers

Here's the FnOnce trait's definition (simplified a little):

pub trait FnOnce<Args> {
    type Output;

    fn call_once(self, args: Args) -> Self::Output;
}

To call a FnOnce closure, you need to be able to move the closure value itself into the invocation. Note that self has to be the actual closure type; a Box<dyn FnOnce> is a different type altogether.

Rust 1.35

Box<dyn FnOnce> is now able to be called; your original code works as-is.

Prior versions

There is a type in the standard library for working around this situation: FnBox. Unfortunately, it's unstable.

Your alternate choices are:

  • Refactor the code so that instead of Box<FnOnce>, you preserve the actual closure type.
  • Use Box<FnMut> instead.
  • Wait for FnBox to stabilise.
  • Switch to a nightly compiler.
like image 177
DK. Avatar answered Oct 04 '22 12:10

DK.