I have an struct that implements the trait A
which has the function fn consume
. I want to pass a callback to this struct, to be called by fn consume
. Something like this:
pub type OnVirtualTunWrite = Arc<dyn Fn(?, usize) -> Result<(), VirtualTunWriteError> + Send + Sync>;
It's on an Arc
because it's shared between threads.
struct A {
on_virtual_tun_write: OnVirtualTunWrite
}
impl S for A {
fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> smoltcp::Result<R>
where
F: FnOnce(&mut [u8]) -> smoltcp::Result<R>,
{
let mut lower = self.lower.as_ref().borrow_mut();
//I should send this f to self.on_virtual_tun_write
(self.on_virtual_tun_write)(f, len);
//return the appropriate result here
OnVirtualTunWrite
is a closure that should receive the f,len
from fn consume
and then use it like this:
let my_on_virtual_tun_write = Arc::new(|?, len| -> ?{
let mut buffer = Vec::new(len);
buffer.resize(len);
//fills buffer with data
f(buffer);
})
How can I make my OnVirtualTunWrite
?
I tried Arc<dyn Fn(dyn FnOnce(&mut [u8]), usize) -> Result<(), ()> + Send + Sync>
but it won't work because dyn Fn
must have arguments with size know at compile time.
Also, there's still a small problem: how do I return -> smoltcp::Result<R>
in OnVirtualTunWrite
if OnVirtualTunWrite
can't possibly know R
?