51 lines
7.4 KiB
HTML
51 lines
7.4 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Custom `#[derive(...)]` macro for implementing `fmt::Display` via doc comment attributes."><title>Display in displaydoc - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="displaydoc" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc derive"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Display</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../displaydoc/index.html">displaydoc</a><span class="version">0.2.5</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Display</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#generic-type-parameters" title="Generic Type Parameters">Generic Type Parameters</a></li><li><a href="#using-debug-implementations-with-type-parameters" title="Using `Debug` Implementations with Type Parameters">Using <code>Debug</code> Implementations with Type Parameters</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate displaydoc</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">displaydoc</a></div><h1>Derive Macro <span class="derive">Display</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/displaydoc/lib.rs.html#181-186">Source</a> </span></div><pre class="rust item-decl"><code>#[derive(Display)]
|
||
{
|
||
<span class="comment">// Attributes available to this derive:</span>
|
||
#[ignore_extra_doc_attributes]
|
||
#[prefix_enum_doc_attributes]
|
||
#[displaydoc]
|
||
}
|
||
</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><a href="https://doc.rust-lang.org/edition-guide/rust-2018/macros/custom-derive.html">Custom <code>#[derive(...)]</code> macro</a>
|
||
for implementing <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display"><code>fmt::Display</code></a> via doc comment attributes.</p>
|
||
<h4 id="generic-type-parameters"><a class="doc-anchor" href="#generic-type-parameters">§</a>Generic Type Parameters</h4>
|
||
<p>Type parameters to an enum or struct using this macro should <em>not</em> need to
|
||
have an explicit <code>Display</code> constraint at the struct or enum definition
|
||
site. A <code>Display</code> implementation for the <code>derive</code>d struct or enum is
|
||
generated assuming each type parameter implements <code>Display</code>, but that should
|
||
be possible without adding the constraint to the struct definition itself:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>displaydoc::Display;
|
||
|
||
<span class="doccomment">/// oh no, an error: {0}
|
||
</span><span class="attr">#[derive(Display)]
|
||
</span><span class="kw">pub struct </span>Error<E>(<span class="kw">pub </span>E);
|
||
|
||
<span class="comment">// No need to require `E: Display`, since `displaydoc::Display` adds that implicitly.
|
||
</span><span class="kw">fn </span>generate_error<E>(e: E) -> Error<E> { Error(e) }
|
||
|
||
<span class="macro">assert!</span>(<span class="string">"oh no, an error: muahaha" </span>== <span class="kw-2">&</span><span class="macro">format!</span>(<span class="string">"{}"</span>, generate_error(<span class="string">"muahaha"</span>)));</code></pre></div><h4 id="using-debug-implementations-with-type-parameters"><a class="doc-anchor" href="#using-debug-implementations-with-type-parameters">§</a>Using <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug"><code>Debug</code></a> Implementations with Type Parameters</h4>
|
||
<p>However, if a type parameter must instead be constrained with the
|
||
<a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug"><code>Debug</code></a> trait so that some field may be printed with
|
||
<code>{:?}</code>, that constraint must currently still also be specified redundantly
|
||
at the struct or enum definition site. If a struct or enum field is being
|
||
formatted with <code>{:?}</code> via <a href="index.html" title="mod displaydoc"><code>displaydoc</code></a>, and a generic type
|
||
parameter must implement <code>Debug</code> to do that, then that struct or enum
|
||
definition will need to propagate the <code>Debug</code> constraint to every type
|
||
parameter it’s instantiated with:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>core::fmt::Debug;
|
||
<span class="kw">use </span>displaydoc::Display;
|
||
|
||
<span class="doccomment">/// oh no, an error: {0:?}
|
||
</span><span class="attr">#[derive(Display)]
|
||
</span><span class="kw">pub struct </span>Error<E: Debug>(<span class="kw">pub </span>E);
|
||
|
||
<span class="comment">// `E: Debug` now has to propagate to callers.
|
||
</span><span class="kw">fn </span>generate_error<E: Debug>(e: E) -> Error<E> { Error(e) }
|
||
|
||
<span class="macro">assert!</span>(<span class="string">"oh no, an error: \"cool\"" </span>== <span class="kw-2">&</span><span class="macro">format!</span>(<span class="string">"{}"</span>, generate_error(<span class="string">"cool"</span>)));
|
||
|
||
<span class="comment">// Try this with a struct that doesn't impl `Display` at all, unlike `str`.
|
||
</span><span class="attr">#[derive(Debug)]
|
||
</span><span class="kw">pub struct </span>Oh;
|
||
<span class="macro">assert!</span>(<span class="string">"oh no, an error: Oh" </span>== <span class="kw-2">&</span><span class="macro">format!</span>(<span class="string">"{}"</span>, generate_error(Oh)));</code></pre></div></div></details></section></div></main></body></html> |