DisplayFn trait for print fns on clean rustdoc items
#148740
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Not sure if I like this change or not, but it has some pros (and cons) so thought I'd put it up here for discussion.
In #148585 (comment), I lamented the fact that there was a lot of boilerplate around our
print_*fns, due to thefmt::from_fnopening a new block (and indenting everything by another tab).Using a macro to change the signature of a function is definitely not a good idea, so I attempted to do something similar with a trait and a blanket impl.
Basically, the
DisplayFntrait is implemented for fns that take a&mut fmt::Formatter, a "clean" item, and a rustdoc ctx, and adds adisplay_fnmethod on such functions that does the wholefmt::from_fnwrapping.Pros:
&mut Formattercan just pass it to the original function, while those that don't can use thedisplay_fntrait methodMajor con:
Fns are not the greatest DX. If you get a function signature wrong, the diagnostics are not going to be very helpful. And it's not really clear where thatdisplay_fnmethod is coming from (though I don't think it should matter much most of the time, since its functionality is pretty easy to infer from the context, IMHO)r? @GuillaumeGomez feel free to just close this if you don't like the idea, not married to it myself :)