kaist-cp/cs431

[Homework BOC] How to initialize a behavior

Closed this issue · 1 comments

I am trying to finish the skeleton code where we have to create the constructor for the Behavior class but I am having a hard time getting initializing the thunk.

Currently, I am getting the following error:

error[E0277]: expected a `FnOnce()` closure, found `()`
   --> src\boc.rs:283:20
    |
283 |             thunk: Box::new(f( unsafe{cowns.get_mut()} )),
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce()` closure, found `()`
    |
    = help: the trait `FnOnce<()>` is not implemented for `()`
    = note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`
    = note: required for the cast from `Box<()>` to `Box<(dyn FnOnce() + Send + 'static)>`

The compile suggests that we wrap the function as follows:
Box::new( || f( unsafe{...}...))

However, when I do this I get a different error because the CownsPtr cannot be shared across threads because it does not implement the sync trait.

I am having a similar issue with calling the thunk in the rayon spawn as I cannot move the behavior to another thread.

I have been having a hard time getting around this issue, any help is appreciated.

You need to use the move keyword, and do Box::new(move || f( unsafe{...}...)).