0

It's similar to the spirit of SIMD. I have multiple structs that implement a trait, and I want to wrap instances of them in a composite struct, the latter of which implement the trait, too, by calling the methods on each member.

Is there a crate for this?

I know enum_dispatch which generates methods automatically for an enum type. I expect the implementation of my idea to be similar to that, except that we are replacing enum (call on "any") with a struct (call on "every").

Fifnmar
  • 372
  • 1
  • 5
  • 9
  • Not that I know. There are also difficulties (how do you handle return types? how do you handle non-`Copy`, perhaps even non-`Clone` parameters?) – Chayim Friedman Aug 10 '23 at 23:28
  • There does not exist (and it could not exist) a universal solution for your problem like `enum_dispatch`, because there is no universal way to do this for _any_ trait. If you have a _particular_ trait you could write a procedural derive macro, that would do that for you. – Aleksander Krauze Aug 11 '23 at 06:13
  • @AleksanderKrauze Why not? (Barring the problems/questions/limitations I mentioned. `enum_dispatch` is also not universally applicable to all traits). – Chayim Friedman Aug 11 '23 at 09:05
  • @ChayimFriedman Return types are indeed a problem, but does not invalidate the point of this question. In essence I'm trying to implement a multicast messaging pattern, similar to Observer pattern with Composites, and similar to `event`s in C#. I want to do it at compile time. – Fifnmar Aug 11 '23 at 15:16

0 Answers0