I'm trying to use delay_queue with a trait object
use futures_delay_queue::{delay_queue, DelayQueue};
use futures_intrusive::buffer::GrowingHeapBuf;
use once_cell::sync::{Lazy, OnceCell};
trait FooTrait: Send + Sync {
//
}
static TX: OnceCell<DelayQueue<Box<dyn FooTrait>, GrowingHeapBuf<Box<dynFooTrait>>>> =
OnceCell::new();
fn start() {
tokio::spawn(async { some_task().await });
^^^^^^^^^^^^
//The error is highlighted here
}
async fn some_task() {
let (tx, rx) = delay_queue::<Box<dyn FooTrait>>();
if CLEANER.set(tx).is_err() {
panic!("couldn't setup TX");
}
//The error seems to be related to the rx.receive()
//Commenting out this while loop removes the error
while let Some(val) = rx.receive().await {
//Do something with val
}
}
When I do this I get a strange error message
| tokio::spawn(async { some_task().await });
| ^^^^^^^^^^^^ one type is more general than the other
|
= note: expected type `std::boxed::Box<dyn FooTrait>`
found struct `std::boxed::Box<dyn FooTrait>`
As far as I can see they are both the same
Moreover I don't understand exactly why tokio::spawn
is showing the error
futures_delay_queue Crate
futures_intrusive Crate
edit: the trait FooTrait: Send + Sync
if that is somehow relevant
Can I add some lifetime bounds to work around the issue?