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 ._.