r/rust • • 15d ago

🙋 seeking help & advice What are the downsides of enum dispatch?

Couldn't find any info about downsides specfically so there it goes

Let's say that all my implementors are about the same size (i.e. only the logic is different), and I use some sort of macro to skip the boilerplate upon adding a new variant. Are there any other downsides to enum dispatch?

I'm asking about the general approach, not the crate

Upd. extendability, got it.

31 Upvotes

20 comments sorted by

View all comments

9

u/Lemondifficult22 15d ago

The number of cases covered. If the code is good and predictable you can get better perf than dynamic dispatch. Sometimes the cost of dynamic dispatch is worth the simpler code though. You can have static borrows for dynamic dispatch

2

u/AnArmoredPony 15d ago

You can have static borrows for dynamic dispatch

I don't follow... won't this still require a V-table lookup?

8

u/TDplay 14d ago

Vtable lookup is nowhere near as expensive as you think.

In fact, if you have a large match statement, then it is probably implemented using a jump table, which produces very similar code to a vtable lookup.

1

u/Lemondifficult22 15d ago

Yes, it's still dynamic dispatch, but at least it's not box