1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <!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="API documentation for the Rust `quote_spanned` macro in crate `quote`."><meta name="keywords" content="rust, rustlang, rust-lang, quote_spanned"><title>quote::quote_spanned - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script></head><body class="rustdoc macro"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><div class="sidebar-elems"><p class='location'><a href='index.html'>quote</a></p><script>window.sidebarCurrent = {name: 'quote_spanned', ty: 'macro', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Macro <a href='index.html'>quote</a>::<wbr><a class="macro" href=''>quote_spanned</a></span><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../src/quote/lib.rs.html#430-439' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class="rust macro">
- <span class="macro">macro_rules</span><span class="macro">!</span> <span class="ident">quote_spanned</span> {
- (<span class="macro-nonterminal">$</span><span class="macro-nonterminal">span</span>:<span class="ident">expr</span><span class="op">=></span> $(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>) <span class="op">=></span> { ... };
- }</pre>
- </div><div class='docblock'><p>Same as <code>quote!</code>, but applies a given span to all tokens originating within
- the macro invocation.</p>
- <h1 id="syntax" class="section-header"><a href="#syntax">Syntax</a></h1>
- <p>A span expression of type <a href="https://docs.rs/proc-macro2/0.4/proc_macro2/struct.Span.html"><code>Span</code></a>, followed by <code>=></code>, followed by the tokens
- to quote. The span expression should be brief -- use a variable for anything
- more than a few characters. There should be no space before the <code>=></code> token.</p>
- <pre class="rust rust-example-rendered">
- <span class="kw">let</span> <span class="ident">span</span> <span class="op">=</span> <span class="comment">/* ... */</span>;
- <span class="comment">// On one line, use parentheses.</span>
- <span class="kw">let</span> <span class="ident">tokens</span> <span class="op">=</span> <span class="macro">quote_spanned</span><span class="macro">!</span>(<span class="ident">span</span><span class="op">=></span> <span class="ident">Box</span>::<span class="ident">into_raw</span>(<span class="ident">Box</span>::<span class="ident">new</span>(#<span class="ident">init</span>)));
- <span class="comment">// On multiple lines, place the span at the top and use braces.</span>
- <span class="kw">let</span> <span class="ident">tokens</span> <span class="op">=</span> <span class="macro">quote_spanned</span><span class="macro">!</span> {<span class="ident">span</span><span class="op">=></span>
- <span class="ident">Box</span>::<span class="ident">into_raw</span>(<span class="ident">Box</span>::<span class="ident">new</span>(#<span class="ident">init</span>))
- };</pre>
- <p>The lack of space before the <code>=></code> should look jarring to Rust programmers
- and this is intentional. The formatting is designed to be visibly
- off-balance and draw the eye a particular way, due to the span expression
- being evaluated in the context of the procedural macro and the remaining
- tokens being evaluated in the generated code.</p>
- <h1 id="hygiene" class="section-header"><a href="#hygiene">Hygiene</a></h1>
- <p>Any interpolated tokens preserve the <code>Span</code> information provided by their
- <code>ToTokens</code> implementation. Tokens that originate within the <code>quote_spanned!</code>
- invocation are spanned with the given span argument.</p>
- <h1 id="example" class="section-header"><a href="#example">Example</a></h1>
- <p>The following procedural macro code uses <code>quote_spanned!</code> to assert that a
- particular Rust type implements the <a href="https://doc.rust-lang.org/std/marker/trait.Sync.html"><code>Sync</code></a> trait so that references can be
- safely shared between threads.</p>
- <pre class="rust rust-example-rendered">
- <span class="kw">let</span> <span class="ident">ty_span</span> <span class="op">=</span> <span class="ident">ty</span>.<span class="ident">span</span>();
- <span class="kw">let</span> <span class="ident">assert_sync</span> <span class="op">=</span> <span class="macro">quote_spanned</span><span class="macro">!</span> {<span class="ident">ty_span</span><span class="op">=></span>
- <span class="kw">struct</span> <span class="ident">_AssertSync</span> <span class="kw">where</span> #<span class="ident">ty</span>: <span class="ident">Sync</span>;
- };</pre>
- <p>If the assertion fails, the user will see an error like the following. The
- input span of their type is hightlighted in the error.</p>
- <pre><code class="language-text">error[E0277]: the trait bound `*const (): std::marker::Sync` is not satisfied
- --> src/main.rs:10:21
- |
- 10 | static ref PTR: *const () = &();
- | ^^^^^^^^^ `*const ()` cannot be shared between threads safely
- </code></pre>
- <p>In this example it is important for the where-clause to be spanned with the
- line/column information of the user's input type so that error messages are
- placed appropriately by the compiler. But it is also incredibly important
- that <code>Sync</code> resolves at the macro definition site and not the macro call
- site. If we resolve <code>Sync</code> at the same span that the user's type is going to
- be resolved, then they could bypass our check by defining their own trait
- named <code>Sync</code> that is implemented for their type.</p>
- </div></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "quote";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>
|