I work with a big array of &dyn Trait. The trait has a relatively small number of methods. The problem is that on each method call we first go to the v-table and only then jump to the method's code. Ideally I would like to remove the indirection and store function pointers together with data pointer.
In other words, today &dyn Trait is represented as (data_ptr, vtable_ptr), but I would like to work with (data_ptr, method1_ptr, method2_ptr, ...).
Is it possible to do it in Rust? Even with unsafe code and manual construction of the type-erased tuple, I am not sure it can be done in a sound manner.
So I can assume that ABIs for fn(&T, u32) and fn(*const (), u32) match? Because I thought that in theory compiler has right to transform fn(&T, u32) to fn(u32, &T) (ABI-wise) or use stack for passing arguments instead of registers, while fn(*const (), u32) will be processed differently.
I cited the conversion from Arc<W: Wake> because it has to deal with a similar problem. It does so by not blindly transmuting the function pointer but instead it creates a function which takes a raw pointer and calls the concrete T's method. With your example this would become: