genericsrusttraitsspecialization

Generic function specialization for a concrete type


In this example I'm trying to make some color conversion utils: I'd like to guarantee that every type that implements the ColorSpace trait can be easily converted to any other such type. To achieve this I ask to implement explicitly the functions from_xyz and into_xyz (since XYZ is the standard color space used in colorimetry) so that all other conversions (from_color<T> and into_color<T>) could use XYZ as an intermadiate step. In some cases this is redundant and more specific implementations are desirable: converting between a color space and XYZ or, for example, converting between RGB and HSL that derive directly from each other. Is there a way to specialize a generic function implementation for a concrete type?


Solution

  • You're looking for specialization.

    At the time of writing, it's not stable, only available on nightly, and has multiple issues, so it's inadvisable to use it in its current form. Given the amount of time the issue has been open, it's unlikely to be stabilized anytime soon.