I'm working on an implementation of dynamic dispatch for arbitrary receiver types (not just newtyped pointers). For part of this, I'm adding a query that returns the indexes of the type parameter and field of the struct that change during the unsize coercion, which I can get from the DispatchFromDyn impl. The way this works for coerce_unsized_info, which is very similar, is the query takes the impl's DefId as the key, and then when you call the query, you have to find the right impl first. Ideally though, I would like to have the struct's DefId as the query key, so that the compiler doesn't have to iterate through possible impls every time you want to get the info for that struct (since it will be cached for each struct DefId). Would that be possible, would it be a good idea? I'm not absolutely sure of the reason coerce_unsized_info is implemented the way it is, but maybe since the data comes from the impl (and not the struct definition), the query key has to be the impl. Right now I have a dispatch_from_dyn_info field on ImplData, right beside coerce_unsized_info, and if the query key was the struct's DefId then I'm not sure where I would that field.
Man, that's a dense question :)
For now I'm keeping the impl DefId as the query key, and have another method,
dispatch_from_dyn_info_for_adt_def, that iterates through all the DispatchFromDyn impls, finds the appropriate one, and calls
dispatch_from_dyn_info on it. It seems inefficient, but it's fine during development.