79217976

Date: 2024-11-23 14:03:04
Score: 1
Natty:
Report link

The problem comes from the log() method depending on a generic parameter.
When a trait is implemented in order to achieve dynamic polymorphism, (you can call this trait an interface) each implementer must provide an implementation of the expected function with the exact same prototype.
However, using a generic can be seen as defining exactly the prototype only at the call site (with actual types chosen); such a prototype cannot have been determined beforehand by the implementers in order to prepare the virtual-tables.

If your approach strongly relies on dynamic polymorphism (OO), then you should probably also consider the event parameter of log() the same way: event: &dyn Keyed.
Then, you would be able to return a Box<dyn EventPersister>.

You will face the same problem about Serialize since this trait (from serde, I assume) is not object-safe for the same reason (generic parameters).
Maybe should you introduce a serialize() method in Keyed and rely internally on serde::Serialize in the implementers?

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): face the same problem
  • Ends in question mark (2):
  • High reputation (-2):
Posted by: prog-fh