I often found myself wanting to specialize a function with a generic argument.
The way I found to do that with the trait specialization feature goes as in the example below. [Rust playground link]
After removing the useless struct : Version simplified by @vitalyd
This pattern works ok but is quite a mouthful.
Is there some other way to get the same result?
Is there an RFC for function specialization?
Any reason not to implement
&mut u8 as well as a blanket impl for
T (still using specialization) and skip the struct?
@vitalyd Good question...
I simplified the example according to your advise.
So in my real life case, tribulify is part of another trait on which I have no control.
I could still create a TribulifyOnly trait, call the method "fn specialized_tribulify" or something like that, and do what you describe I think.
I wonder if it would be more prone to generate misleading error message talking about the TribulifyOnly trait when the user is mistyping/misusing the original trait.
You could call your trait and/or the method in there something entirely different so users aren't confused . Alternatively, don't make your special trait public and just use it internally to get the specialization effect.
I think specializing methods via trait impls is the only way right now. I'm not sure if there's an RFC around making function specialization (or overloading, which is what this can also be viewed as) a standalone thing.