Sometimes one might need to create an object at run-time that implements a Rust trait. This is one way to do so, provided the trait to be implemented is known statically.
Here's how:
- Define a struct that will hold the implementation of each trait method.
- Define a second "object" struct that holds a "self" value and a reference to an implementation struct.
- Implement the desired trait for the object struct, dispatching to the function pointers in the implementation struct.
An example: