Render with Display using autoref specialization #359
Merged
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.
This may not be something you're interested in merging. No hard feelings if you close this outright. :-)
I found for my use case that I needed to write
display(...)
in a lot of places, especially in cases where I was using external types (especially the many datetime formatting types fromicu
).This PR adds autoref-based specialization to the
html!
macro such that it will choose a type'sRender
implementation if it is available, and useDisplay
as a fallback.The blanket impl of
Render for T where T: Display
was removed in #271 for good reasons, and I will try to justify this PR in terms of those reasons:Render
orDisplay
.itoa
.Display
is intended to provide a human-readable string. While this isn't necessarily tied to the HTML representation of the thing, it is not unreasonable to want to use the output ofDisplay
in HTML. This solution treats the output ofDisplay
as unescaped content (so it will be escaped before appending, by going throughmaud::display(...)
). If a situation arises where theDisplay
andRender
implementations should be completely different, specialization allows this on a per-type basis.The main drawback of this PR is that compiler errors are slightly more confusing when a type implements neither
Render
norDisplay
. I have tried to name the magic inner types used in the macro with that in mind.Let me know what you think. :-)