Tokio::spawn error -> one type is more general than the other

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?

1 Like

As far as I understand, this is a compiler bug of some kind. See #64552.

This is a lifetime error that is trying to say that something somewhere doesn't live long enough (or perhaps doesn't implement Send). The message is awful though :frowning:

https://github.com/rust-lang/rust/issues/64650

Yeah I already visited that. I also figured that It's some kind of lifetime error.
Someone even suggested switching to stable rust as that will give me a better error message. But didn't help. kept getting the same message.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.