Implementing a large number of distinct but similar structs
โ Rust ๐ 2025-07-31 ๐ค surdeus ๐๏ธ 10Suppose I have a family of structs which are identical data structures, but each of which implements a large number (letโs say dozens) of traits many of which they have in common. I would nevertheless like for these structs to be distinct, in other words, no two structs have exactly the same set of traits (obviously, otherwise they'd be entirely equivalent).
For the sake of making this a little more concrete, what I have is something like
struct FourFloatVec1 {
data: [f64; 4],
}
What I need is lots of inequivalent versions of this with different mathematical properties (e.g. some are true vectors and can be added and subtracted, some are merely coordinates, some transform under different representations of different inequivalent mathematical groups, et cetera), but which have many properties in common.
There's nothing stopping me from just doing this explicitly, but it seems like it will require a staggering amount of boilerplate.
What is the recommended way to deal with this? I think you are going to tell me that I should make a custom derive macro, which currently seems like the only plausible option to me, but I thought it was worth checking what experienced people have to say about it.
7 posts - 5 participants
๐ท๏ธ Rust_feed