Is it possible to omit or pass some "any" type?
I want to be able to define a single "queue" which can be used by registering processors for the different jobs. Similar to how bull queue in Node works.
The queue shouldn't care about the <P, R>
generics on the processors. Its only job would be to enqueue a job, get the processor for that job (associated with the "name" the processor is registered with) and forward the job to it.
However, the processors
property on the Queue
struct needs some generic type to be passed to it. I tried to get around this by using a trait, but I am kind of back where I started.
I think I am trying to mimic the TypeScript any
type.
I have seen suggestions to use an enum which would specify the different job types; however, this would complicate the processor of creating a reusable queue imo.
If I were to do this, instead of letting the user just register different processors, they would have to register them as well as modifying the enum for the new job type. Additionally, since the jobs are matched based on the associated processor, there wouldn't be the benefit to pattern matching.
The dyn Trait seems to be what I am looking for. However, I don't see how it works when the trait has associated types.
#[derive(Debug)]
struct JobOptions {}
#[allow(unused)]
#[derive(Debug)]
struct Job<P, R> {
processor: String,
payload: Box<P>,
response: Option<Box<R>>,
options: Option<JobOptions>,
status: JobStatus,
}
#[allow(unused)]
struct Processor<P, R> {
name: String,
queue: VecDeque<Rc<Cell<Job<P, R>>>>,
}
trait JobProcessor {
type Payload;
type Response;
}
impl<P, R> JobProcessor for Processor<P, R> {
type Payload = P;
type Response = R;
}
#[derive(Default)]
struct Queue {
processors: HashMap<String, Rc<Cell<JobProcessor<?, ?>>>>,
}