0

I want to do the following but without a where clause as the generics get out of hand and I don't even know how to properly fill them out. Is there a way to do this without the use of generics?

pub enum  PreparedMigration<F, Fut> where 
F: FnOnce(&DatabaseConnection) -> Fut,
Fut: Future<Output = bool>{
    Simple(String),
    Statement(Statement),
    Closure(F)
}
A.J
  • 315
  • 4
  • 17
  • 4
    You should usually not put trait bounds on structs/enums: https://stackoverflow.com/q/49229332/6274355 You may also want to create a trait that encompasses these bounds into one, depending on how you use this type. – drewtato Jun 13 '23 at 02:48

1 Answers1

0

Dynamic dispatch could be used here instead to avoid generics, if you're okay with heap allocating the closures and futures:

pub enum PreparedMigration {
    Simple(String),
    Statement(Statement),
    Closure(Pin<Box<dyn FnOnce(&DatabaseConnection) -> Pin<Box<dyn Future<Output = bool>>>>>)
}

You'll probably also need to add Send bounds if you're using a multithreaded scheduler.

apetranzilla
  • 5,331
  • 27
  • 34