I have a closure, which I construct:
let r = receiver.clone();
let is_channel_empty = move || -> bool { r.is_empty() };
and then inject via:
Adapter::new(is_channel_empty)
The Adapter looks like this:
type ChannelEmptyFn = dyn Fn() -> bool + Send + Sync;
// for Box: type ChannelEmptyFn = Box<dyn Fn() -> bool + Send + Sync + 'static>;
pub struct Adapter {
is_channel_empty: ChannelEmptyFn,
}
impl Adapter {
pub fn new(is_channel_empty: ChannelEmptyFn) -> Self
{
Self { is_channel_empty }
}
}
I'm getting the obvious, size not known at runtime error.
If I pass a reference, it needs a lifetime and 'static isn't going to work.
If I pass a Box, it gets heap allocated, and while I don't like heap allocations, is an easy solution.
Is there another alternative? I can't pass an fn
(function pointer) as the closure has a captured environment. Is there a way to specify a field type as a closure? other than Fn
, FnMut
, or FnOnce
? I'm just wondering if I'm missing something in my limited knowledge of Rust.
One last thing, the Adapter eventually is wrapped in an Arc, ya there's some irony here, which is the other reason I'd like to have it all allocated in a single block.