lambda-fairy / maud

:pencil: Compile-time HTML templates for Rust
https://maud.lambda.xyz
Apache License 2.0
1.98k stars 132 forks source link

Render with Display using autoref specialization #359

Closed simonask closed 1 year ago

simonask commented 1 year ago

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 from icu).

This PR adds autoref-based specialization to the html! macro such that it will choose a type's Render implementation if it is available, and use Display 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:

The main drawback of this PR is that compiler errors are slightly more confusing when a type implements neither Render nor Display. I have tried to name the magic inner types used in the macro with that in mind.

Let me know what you think. :-)

lambda-fairy commented 1 year ago

Thanks and sorry for the delay!

I didn't expect the solution to be so clean – seeing your take validates the approach for me.

Let's merge this for now and see what people think in the next release.