What is the best way to use some features of one struct in another struct?

I created a vector type to operate vectors and some vector operations work exactly with color operations, for example multiplication by a scalar, while others such as normalize do not make sense in a color. How could I use certain vector features in color without having to rewrite the same code manually? vector and color types need to be distinct, the solution of using vector as an alias for color is not suitable here.
Thanks!

1 Like

Usually such code is duplicated. It can be implemented using macro_rules to save you copypasting.

For some things you could use a trait with a default implementation, but traits' default impls can't access struct fields, which requires more trait methods…

2 Likes

You could have private standalone functions implementing the common parts and then forward the public impl struct or impl trait functions to those.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.