I have two traits: A
and B
, I want them all to implement a trait C
automatically, but a type implements A
may also implements B
, I want them to be mutex.
I wrote this to do it: Rust Playground.
I don't know whether this way is sound or it has overhead.
Rust doesn't support mutually exclusive traits so far.
See for example
- Mutually exclusive traits by withoutboats · Pull Request #1148 · rust-lang/rfcs · GitHub
- Disjointness based on associated types. by withoutboats · Pull Request #1672 · rust-lang/rfcs · GitHub
The latter proposal would let your code compile, but as noted in that PR, it would also allow mutually exclusive traits more generally, and thus it was postponed until a more comprehensive approach is decided on.
There has been some related experimentation recently (not stabilized):
1 Like
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.