Specialized drop implementation

We have a structure A with a type parameter for which we use two selector types T and U, i.e., A and A have different implementations of a trait (which trait is not fundamental for the problem).

Our problem is that we need to specialize the Drop implementation to T vs U and Drop cannot be specialized.

Currently we are using a DropHelper trait containing the method depending on T vs U, and T and U implement it. By bounding the type parameter of S to DropHelper we can then call the method from the Drop implementation: playground.

However, this approach has the downside of adding a trait bound to the type parameter, which is inconvenient as we have to propagate it around in some cases.

Any alternatives?

1 Like

Maybe TypeId? When I set out to build the example I forgot that Any and TypeId don't work well with lifetimes, so unfortunately it still requires a S: 'static bound on A's generic parameter. But maybe this fits your use-case better than a DropHelper trait?

3 Likes

Fantastic, solved! The type is just an empty selector type, 'static is not a problem.

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.