1

I've been trying to implement a simple job scheduler. I've got 2 job types async and sync job. They have a property named context which simply is a function. I'm storing a date property too.

So the issue is when I try to write a scheduler it shows the following error

the size for values of type `dyn Fn()` cannot be known at compilation time
the trait `Sized` is not implemented for `dyn Fn()`rustc(E0277)
sync_job.rs(6, 23): required by this bound in `sync_job::SyncJob`
sync_job.rs(6, 23): you could relax the implicit `Sized` bound on `L` if it were used through indirection like `&L` or `Box<L>`

I've created the SyncJob struct like this:

pub struct SyncJob<T, L>
where
    T: chrono::Datelike,
    L: Fn(),
{
    pub scheduled: Option<T>,
    pub context: L,
    pub repeating: bool,
    pub repeating_times: u32
}

Got scheduler like this:


pub struct Scheduler<'a, T>
where
    T: Schedule + Sized
{
    jobs: Box<Vec<&'a T>>
}

impl<L> Scheduler<'_, SyncJob<NaiveDateTime, L>>
where
    L: Fn() + 'static + Sized
{
    fn new(jobs:&'static Vec<&SyncJob<NaiveDateTime, L>>) -> Self {
        Scheduler {
            jobs: Box::new(jobs.clone())
        }
    }
}

And trying to create it like this:

let scheduler: Scheduler<SyncJob<NaiveDateTime, dyn Fn() + Sized>> = Scheduler::new(vec![]);

What is the problem here? I can create SyncJob and AsyncJob without any hassle but when I try to put them in a vector, it blows ._.

Skai
  • 31
  • 1
  • 10
  • @justinas unfortunately no. I've never seen this compiler tip it seems to be added with [this commit](https://github.com/Manishearth/rust/commit/4910206b4a5b36ce0d82e08f1e33e72875fd28df). And I stored the closure as the second answer of that question suggests. – Skai Nov 10 '20 at 21:02
  • I see now. Thing is, you define `L: Fn() + 'static + Sized`, and try to substitute `L` with `dyn Fn() + Sized`. But `dyn T` is always unsized, unlike `&dyn T` or `Box`. A [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) would greatly help to answer your question. – justinas Nov 10 '20 at 21:58
  • 1
    The short answer is that you are trying to have a `Vec` which is impossible. You should probably use a `Vec>` instead. – justinas Nov 10 '20 at 21:59

0 Answers0