I'm trying to have a function shared between threads, that can be called with another function on it:
fn main() {
let on_virtual_tun_write = std::sync::Arc::new(|f: &dyn FnOnce(&mut [u8]), size: usize|-> std::result::Result<(),()>{
let mut buffer = vec![0; size];
f(buffer.as_mut_slice());
Ok(())
});
}
Once it's called, it uses the f
passed as an argument, to retrieve a buffer. As you can see, I do not intend to copy the function, I just use it as soon as someone calls the closure, and discards it. However, Rust thinks that I want to copy it. What can I do to tell Rust that I just want to use a reference to the function? I thought &dyn
was sufficient.
Error:
error[E0161]: cannot move a value of type dyn for<'r> FnOnce(&'r mut [u8]): the size of dyn for<'r> FnOnce(&'r mut [u8]) cannot be statically determined
--> src/main.rs:4:9
|
4 | f(buffer.as_mut_slice());
| ^
error[E0507]: cannot move out of `*f` which is behind a shared reference
--> src/main.rs:4:9
|
4 | f(buffer.as_mut_slice());
| ^ move occurs because `*f` has type `dyn for<'r> FnOnce(&'r mut [u8])`, which does not implement the `Copy` trait
error: aborting due to 2 previous errors; 1 warning emitted