I rant for a bit
S
Composite
CompositeMut
A
B
C
TA
TB
TC
TAB
TAC
TBC
T0
T1
T2
R
T
These are all the names of types and traits you have used in your examples. Not a single one is a concrete noun that describes what the thing does or is.
This is not real code. This is an exploration of the language, which is all right in its own way, but you can't solve architectural problems by throwing toy examples at them because the architecture of the solution depends on the nature of the problem by definition.
Given any piece of code stripped of context, you can keep saying "but what if X
, Y
and Z
?" until there are no possible solutions left. But that's not a productive line of questioning. I see the same thing happening with people who think Rust needs inheritance: they construct a toy problem such that the only possible solution is exactly inheritance, then say "How do you solve this in Rust?" And people in threads like this give some helpful suggestions for reframing the problem, or transform the code in such a way that it technically works but isn't very pretty, and the asker gets frustrated and says "Why can't Rust do this one simple thing?" when the reality is that the problem they're imagining doesn't exist in real life. People solve vastly complicated problems in Rust without needing inheritance. (Serde is a great example.)
I'm not saying you've completely imagined your problem. What I am saying is that even if you have a real problem, it's not evidenced in the alphabet soup of toy examples in the thread so far. If you have real world code that you think isn't well designed because you can't abstract over mutability, post that and we'll see if there's a way to organize it better.
I will agree that the inability to abstract over mutability is occasionally a pain point. However, in all the cases I am currently thinking of, the commonality between the mut
and non-mut
versions is purely on the syntactic level. Abstracting common syntax, when the types are not held in common, is just what declarative macros are great for. Maybe this is the kind of problem that actually should just be solved with a macro.