Now a REAL lib
This commit is contained in:
1
doc/quote/all.html
Normal file
1
doc/quote/all.html
Normal file
@ -0,0 +1 @@
|
||||
<!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="List of all items in this crate"><meta name="keywords" content="rust, rustlang, rust-lang"><title>List of all items in this crate</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><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="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location">Crate quote</h2></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../quote/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">List of all items</span></h1><h3 id="Traits">Traits</h3><ul class="traits docblock"><li><a href="trait.IdentFragment.html">IdentFragment</a></li><li><a href="trait.ToTokens.html">ToTokens</a></li><li><a href="trait.TokenStreamExt.html">TokenStreamExt</a></li></ul><h3 id="Macros">Macros</h3><ul class="macros docblock"><li><a href="macro.format_ident.html">format_ident</a></li><li><a href="macro.quote.html">quote</a></li><li><a href="macro.quote_spanned.html">quote_spanned</a></li></ul></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="quote" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
||||
11
doc/quote/ext/trait.TokenStreamExt.html
Normal file
11
doc/quote/ext/trait.TokenStreamExt.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;URL=../../quote/trait.TokenStreamExt.html">
|
||||
<title>Redirection</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Redirecting to <a href="../../quote/trait.TokenStreamExt.html">../../quote/trait.TokenStreamExt.html</a>...</p>
|
||||
<script>location.replace("../../quote/trait.TokenStreamExt.html" + location.search + location.hash);</script>
|
||||
</body>
|
||||
</html>
|
||||
11
doc/quote/ident_fragment/trait.IdentFragment.html
Normal file
11
doc/quote/ident_fragment/trait.IdentFragment.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;URL=../../quote/trait.IdentFragment.html">
|
||||
<title>Redirection</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Redirecting to <a href="../../quote/trait.IdentFragment.html">../../quote/trait.IdentFragment.html</a>...</p>
|
||||
<script>location.replace("../../quote/trait.IdentFragment.html" + location.search + location.hash);</script>
|
||||
</body>
|
||||
</html>
|
||||
57
doc/quote/index.html
Normal file
57
doc/quote/index.html
Normal file
@ -0,0 +1,57 @@
|
||||
<!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="github crates-io docs-rs"><meta name="keywords" content="rust, rustlang, rust-lang, quote"><title>quote - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><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="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate quote</a></h2><div class="sidebar-elems"><div class="block"><ul><li class="version">Version 1.0.23</li><li><a id="all-types" href="all.html">All Items</a></li></ul></div><section><div class="block"><ul><li><a href="#macros">Macros</a></li><li><a href="#traits">Traits</a></li></ul></div></section></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../quote/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">quote</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/quote/lib.rs.html#1-1434">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><a href="https://github.com/dtolnay/quote"><img src="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github" alt="github" /></a> <a href="https://crates.io/crates/quote"><img src="https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust" alt="crates-io" /></a> <a href="https://docs.rs/quote"><img src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" alt="docs-rs" /></a></p>
|
||||
<br>
|
||||
<p>This crate provides the <a href="macro.quote.html"><code>quote!</code></a> macro for turning Rust syntax tree data
|
||||
structures into tokens of source code.</p>
|
||||
<p>Procedural macros in Rust receive a stream of tokens as input, execute
|
||||
arbitrary Rust code to determine how to manipulate those tokens, and produce
|
||||
a stream of tokens to hand back to the compiler to compile into the caller’s
|
||||
crate. Quasi-quoting is a solution to one piece of that — producing
|
||||
tokens to return to the compiler.</p>
|
||||
<p>The idea of quasi-quoting is that we write <em>code</em> that we treat as <em>data</em>.
|
||||
Within the <code>quote!</code> macro, we can write what looks like code to our text
|
||||
editor or IDE. We get all the benefits of the editor’s brace matching,
|
||||
syntax highlighting, indentation, and maybe autocompletion. But rather than
|
||||
compiling that as code into the current crate, we can treat it as data, pass
|
||||
it around, mutate it, and eventually hand it back to the compiler as tokens
|
||||
to compile into the macro caller’s crate.</p>
|
||||
<p>This crate is motivated by the procedural macro use case, but is a
|
||||
general-purpose Rust quasi-quoting library and is not specific to procedural
|
||||
macros.</p>
|
||||
<div class="example-wrap"><pre class="language-toml"><code>[dependencies]
|
||||
quote = "1.0"</code></pre></div><br>
|
||||
<h2 id="example"><a href="#example">Example</a></h2>
|
||||
<p>The following quasi-quoted block of code is something you might find in <a href="https://serde.rs/">a</a>
|
||||
procedural macro having to do with data structure serialization. The <code>#var</code>
|
||||
syntax performs interpolation of runtime variables into the quoted tokens.
|
||||
Check out the documentation of the <a href="macro.quote.html"><code>quote!</code></a> macro for more detail about
|
||||
the syntax. See also the <a href="macro.quote_spanned.html"><code>quote_spanned!</code></a> macro which is important for
|
||||
implementing hygienic procedural macros.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">tokens</span> <span class="op">=</span> <span class="macro">quote!</span> {
|
||||
<span class="kw">struct</span> <span class="ident">SerializeWith</span> #<span class="ident">generics</span> #<span class="ident">where_clause</span> {
|
||||
<span class="ident">value</span>: <span class="kw-2">&</span><span class="lifetime">'a</span> #<span class="ident">field_ty</span>,
|
||||
<span class="ident">phantom</span>: <span class="ident">core::marker::PhantomData</span><span class="op"><</span>#<span class="ident">item_ty</span><span class="op">></span>,
|
||||
}
|
||||
|
||||
<span class="kw">impl</span> #<span class="ident">generics</span> <span class="ident">serde::Serialize</span> <span class="kw">for</span> <span class="ident">SerializeWith</span> #<span class="ident">generics</span> #<span class="ident">where_clause</span> {
|
||||
<span class="kw">fn</span> <span class="ident">serialize</span><span class="op"><</span><span class="ident">S</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">serializer</span>: <span class="ident">S</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">S::Ok</span>, <span class="ident">S::Error</span><span class="op">></span>
|
||||
<span class="kw">where</span>
|
||||
<span class="ident">S</span>: <span class="ident">serde::Serializer</span>,
|
||||
{
|
||||
#<span class="ident">path</span>(<span class="self">self</span>.<span class="ident">value</span>, <span class="ident">serializer</span>)
|
||||
}
|
||||
}
|
||||
|
||||
<span class="ident">SerializeWith</span> {
|
||||
<span class="ident">value</span>: #<span class="ident">value</span>,
|
||||
<span class="ident">phantom</span>: <span class="ident">core::marker::PhantomData</span>::<span class="op"><</span>#<span class="ident">item_ty</span><span class="op">></span>,
|
||||
}
|
||||
};</code></pre></div>
|
||||
</div></details><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.format_ident.html" title="quote::format_ident macro">format_ident</a></div><div class="item-right docblock-short"><p>Formatting macro for constructing <code>Ident</code>s.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.quote.html" title="quote::quote macro">quote</a></div><div class="item-right docblock-short"><p>The whole point.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.quote_spanned.html" title="quote::quote_spanned macro">quote_spanned</a></div><div class="item-right docblock-short"><p>Same as <code>quote!</code>, but applies a given span to all tokens originating within
|
||||
the macro invocation.</p>
|
||||
</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.IdentFragment.html" title="quote::IdentFragment trait">IdentFragment</a></div><div class="item-right docblock-short"><p>Specialized formatting trait used by <code>format_ident!</code>.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.ToTokens.html" title="quote::ToTokens trait">ToTokens</a></div><div class="item-right docblock-short"><p>Types that can be interpolated inside a <code>quote!</code> invocation.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.TokenStreamExt.html" title="quote::TokenStreamExt trait">TokenStreamExt</a></div><div class="item-right docblock-short"><p>TokenStream extension trait with methods for appending tokens.</p>
|
||||
</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="quote" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
||||
11
doc/quote/macro.format_ident!.html
Normal file
11
doc/quote/macro.format_ident!.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;URL=macro.format_ident.html">
|
||||
<title>Redirection</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Redirecting to <a href="macro.format_ident.html">macro.format_ident.html</a>...</p>
|
||||
<script>location.replace("macro.format_ident.html" + location.search + location.hash);</script>
|
||||
</body>
|
||||
</html>
|
||||
77
doc/quote/macro.format_ident.html
Normal file
77
doc/quote/macro.format_ident.html
Normal file
@ -0,0 +1,77 @@
|
||||
<!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="Formatting macro for constructing `Ident`s."><meta name="keywords" content="rust, rustlang, rust-lang, format_ident"><title>format_ident in quote - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><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="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2 class="location"><a href="index.html">In quote</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../quote/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Macro <a href="index.html">quote</a>::<wbr><a class="macro" href="#">format_ident</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/quote/format.rs.html#111-125">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><div class="example-wrap"><pre class="rust macro"><code><span class="macro">macro_rules!</span> <span class="ident">format_ident</span> {
|
||||
(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">fmt</span>:<span class="ident">expr</span>) => { ... };
|
||||
(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">fmt</span>:<span class="ident">expr</span>, $(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">rest</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>) => { ... };
|
||||
}</code></pre></div>
|
||||
</div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Formatting macro for constructing <code>Ident</code>s.</p>
|
||||
<br>
|
||||
<h2 id="syntax"><a href="#syntax">Syntax</a></h2>
|
||||
<p>Syntax is copied from the <a href="https://doc.rust-lang.org/1.64.0/alloc/macro.format.html" title="format!"><code>format!</code></a> macro, supporting both positional and
|
||||
named arguments.</p>
|
||||
<p>Only a limited set of formatting traits are supported. The current mapping
|
||||
of format types to traits is:</p>
|
||||
<ul>
|
||||
<li><code>{}</code> ⇒ <a href="trait.IdentFragment.html"><code>IdentFragment</code></a></li>
|
||||
<li><code>{:o}</code> ⇒ <a href="https://doc.rust-lang.org/1.64.0/core/fmt/trait.Octal.html"><code>Octal</code></a></li>
|
||||
<li><code>{:x}</code> ⇒ <a href="https://doc.rust-lang.org/1.64.0/core/fmt/trait.LowerHex.html"><code>LowerHex</code></a></li>
|
||||
<li><code>{:X}</code> ⇒ <a href="https://doc.rust-lang.org/1.64.0/core/fmt/trait.UpperHex.html"><code>UpperHex</code></a></li>
|
||||
<li><code>{:b}</code> ⇒ <a href="https://doc.rust-lang.org/1.64.0/core/fmt/trait.Binary.html"><code>Binary</code></a></li>
|
||||
</ul>
|
||||
<p>See <a href="https://doc.rust-lang.org/1.64.0/alloc/fmt/index.html" title="std::fmt"><code>std::fmt</code></a> for more information.</p>
|
||||
<br>
|
||||
<h2 id="identfragment"><a href="#identfragment">IdentFragment</a></h2>
|
||||
<p>Unlike <code>format!</code>, this macro uses the <a href="trait.IdentFragment.html"><code>IdentFragment</code></a> formatting trait by
|
||||
default. This trait is like <code>Display</code>, with a few differences:</p>
|
||||
<ul>
|
||||
<li><code>IdentFragment</code> is only implemented for a limited set of types, such as
|
||||
unsigned integers and strings.</li>
|
||||
<li><a href="../proc_macro2/struct.Ident.html"><code>Ident</code></a> arguments will have their <code>r#</code> prefixes stripped, if present.</li>
|
||||
</ul>
|
||||
<br>
|
||||
<h2 id="hygiene"><a href="#hygiene">Hygiene</a></h2>
|
||||
<p>The <a href="../proc_macro2/struct.Span.html"><code>Span</code></a> of the first <code>Ident</code> argument is used as the span of the final
|
||||
identifier, falling back to <a href="../proc_macro2/struct.Span.html#method.call_site"><code>Span::call_site</code></a> when no identifiers are
|
||||
provided.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// If `ident` is an Ident, the span of `my_ident` will be inherited from it.</span>
|
||||
<span class="kw">let</span> <span class="ident">my_ident</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"My{}{}"</span>, <span class="ident">ident</span>, <span class="string">"IsCool"</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">my_ident</span>, <span class="string">"MyIdentIsCool"</span>);</code></pre></div>
|
||||
<p>Alternatively, the span can be overridden by passing the <code>span</code> named
|
||||
argument.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">my_span</span> <span class="op">=</span> <span class="comment">/* ... */</span>;
|
||||
<span class="macro">format_ident!</span>(<span class="string">"MyIdent"</span>, <span class="ident">span</span> <span class="op">=</span> <span class="ident">my_span</span>);</code></pre></div>
|
||||
<p><br></p>
|
||||
<h2 id="panics"><a href="#panics">Panics</a></h2>
|
||||
<p>This method will panic if the resulting formatted string is not a valid
|
||||
identifier.</p>
|
||||
<br>
|
||||
<h2 id="examples"><a href="#examples">Examples</a></h2>
|
||||
<p>Composing raw and non-raw identifiers:</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">my_ident</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"My{}"</span>, <span class="string">"Ident"</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">my_ident</span>, <span class="string">"MyIdent"</span>);
|
||||
|
||||
<span class="kw">let</span> <span class="ident">raw</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"r#Raw"</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">raw</span>, <span class="string">"r#Raw"</span>);
|
||||
|
||||
<span class="kw">let</span> <span class="ident">my_ident_raw</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"{}Is{}"</span>, <span class="ident">my_ident</span>, <span class="ident">raw</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">my_ident_raw</span>, <span class="string">"MyIdentIsRaw"</span>);</code></pre></div>
|
||||
<p>Integer formatting options:</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">num</span>: <span class="ident">u32</span> <span class="op">=</span> <span class="number">10</span>;
|
||||
|
||||
<span class="kw">let</span> <span class="ident">decimal</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"Id_{}"</span>, <span class="ident">num</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">decimal</span>, <span class="string">"Id_10"</span>);
|
||||
|
||||
<span class="kw">let</span> <span class="ident">octal</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"Id_{:o}"</span>, <span class="ident">num</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">octal</span>, <span class="string">"Id_12"</span>);
|
||||
|
||||
<span class="kw">let</span> <span class="ident">binary</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"Id_{:b}"</span>, <span class="ident">num</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">binary</span>, <span class="string">"Id_1010"</span>);
|
||||
|
||||
<span class="kw">let</span> <span class="ident">lower_hex</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"Id_{:x}"</span>, <span class="ident">num</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">lower_hex</span>, <span class="string">"Id_a"</span>);
|
||||
|
||||
<span class="kw">let</span> <span class="ident">upper_hex</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"Id_{:X}"</span>, <span class="ident">num</span>);
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">upper_hex</span>, <span class="string">"Id_A"</span>);</code></pre></div>
|
||||
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="quote" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
||||
11
doc/quote/macro.quote!.html
Normal file
11
doc/quote/macro.quote!.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;URL=macro.quote.html">
|
||||
<title>Redirection</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Redirecting to <a href="macro.quote.html">macro.quote.html</a>...</p>
|
||||
<script>location.replace("macro.quote.html" + location.search + location.hash);</script>
|
||||
</body>
|
||||
</html>
|
||||
190
doc/quote/macro.quote.html
Normal file
190
doc/quote/macro.quote.html
Normal file
@ -0,0 +1,190 @@
|
||||
<!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="The whole point."><meta name="keywords" content="rust, rustlang, rust-lang, quote"><title>quote in quote - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><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="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2 class="location"><a href="index.html">In quote</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../quote/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Macro <a href="index.html">quote</a>::<wbr><a class="macro" href="#">quote</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/quote/lib.rs.html#473-477">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><div class="example-wrap"><pre class="rust macro"><code><span class="macro">macro_rules!</span> <span class="ident">quote</span> {
|
||||
($(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>) => { ... };
|
||||
}</code></pre></div>
|
||||
</div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>The whole point.</p>
|
||||
<p>Performs variable interpolation against the input and produces it as
|
||||
<a href="../proc_macro2/struct.TokenStream.html" title="proc_macro2::TokenStream"><code>proc_macro2::TokenStream</code></a>.</p>
|
||||
<p>Note: for returning tokens to the compiler in a procedural macro, use
|
||||
<code>.into()</code> on the result to convert to <a href="https://doc.rust-lang.org/1.64.0/proc_macro/struct.TokenStream.html" title="proc_macro::TokenStream"><code>proc_macro::TokenStream</code></a>.</p>
|
||||
<br>
|
||||
<h2 id="interpolation"><a href="#interpolation">Interpolation</a></h2>
|
||||
<p>Variable interpolation is done with <code>#var</code> (similar to <code>$var</code> in
|
||||
<code>macro_rules!</code> macros). This grabs the <code>var</code> variable that is currently in
|
||||
scope and inserts it in that location in the output tokens. Any type
|
||||
implementing the <a href="trait.ToTokens.html"><code>ToTokens</code></a> trait can be interpolated. This includes most
|
||||
Rust primitive types as well as most of the syntax tree types from the <a href="https://github.com/dtolnay/syn">Syn</a>
|
||||
crate.</p>
|
||||
<p>Repetition is done using <code>#(...)*</code> or <code>#(...),*</code> again similar to
|
||||
<code>macro_rules!</code>. This iterates through the elements of any variable
|
||||
interpolated within the repetition and inserts a copy of the repetition body
|
||||
for each one. The variables in an interpolation may be a <code>Vec</code>, slice,
|
||||
<code>BTreeSet</code>, or any <code>Iterator</code>.</p>
|
||||
<ul>
|
||||
<li><code>#(#var)*</code> — no separators</li>
|
||||
<li><code>#(#var),*</code> — the character before the asterisk is used as a separator</li>
|
||||
<li><code>#( struct #var; )*</code> — the repetition can contain other tokens</li>
|
||||
<li><code>#( #k => println!("{}", #v), )*</code> — even multiple interpolations</li>
|
||||
</ul>
|
||||
<br>
|
||||
<h2 id="hygiene"><a href="#hygiene">Hygiene</a></h2>
|
||||
<p>Any interpolated tokens preserve the <code>Span</code> information provided by their
|
||||
<code>ToTokens</code> implementation. Tokens that originate within the <code>quote!</code>
|
||||
invocation are spanned with <a href="https://docs.rs/proc-macro2/1.0/proc_macro2/struct.Span.html#method.call_site"><code>Span::call_site()</code></a>.</p>
|
||||
<p>A different span can be provided through the <a href="macro.quote_spanned.html"><code>quote_spanned!</code></a> macro.</p>
|
||||
<br>
|
||||
<h2 id="return-type"><a href="#return-type">Return type</a></h2>
|
||||
<p>The macro evaluates to an expression of type <code>proc_macro2::TokenStream</code>.
|
||||
Meanwhile Rust procedural macros are expected to return the type
|
||||
<code>proc_macro::TokenStream</code>.</p>
|
||||
<p>The difference between the two types is that <code>proc_macro</code> types are entirely
|
||||
specific to procedural macros and cannot ever exist in code outside of a
|
||||
procedural macro, while <code>proc_macro2</code> types may exist anywhere including
|
||||
tests and non-macro code like main.rs and build.rs. This is why even the
|
||||
procedural macro ecosystem is largely built around <code>proc_macro2</code>, because
|
||||
that ensures the libraries are unit testable and accessible in non-macro
|
||||
contexts.</p>
|
||||
<p>There is a <a href="https://doc.rust-lang.org/std/convert/trait.From.html"><code>From</code></a>-conversion in both directions so returning the output of
|
||||
<code>quote!</code> from a procedural macro usually looks like <code>tokens.into()</code> or
|
||||
<code>proc_macro::TokenStream::from(tokens)</code>.</p>
|
||||
<br>
|
||||
<h2 id="examples"><a href="#examples">Examples</a></h2><h4 id="procedural-macro"><a href="#procedural-macro">Procedural macro</a></h4>
|
||||
<p>The structure of a basic procedural macro is as follows. Refer to the <a href="https://github.com/dtolnay/syn">Syn</a>
|
||||
crate for further useful guidance on using <code>quote!</code> as part of a procedural
|
||||
macro.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">proc_macro</span>;
|
||||
|
||||
<span class="kw">use</span> <span class="ident">proc_macro::TokenStream</span>;
|
||||
<span class="kw">use</span> <span class="ident">quote::quote</span>;
|
||||
|
||||
<span class="attribute">#[<span class="ident">proc_macro_derive</span>(<span class="ident">HeapSize</span>)]</span>
|
||||
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">derive_heap_size</span>(<span class="ident">input</span>: <span class="ident">TokenStream</span>) -> <span class="ident">TokenStream</span> {
|
||||
<span class="comment">// Parse the input and figure out what implementation to generate...</span>
|
||||
<span class="kw">let</span> <span class="ident">name</span> <span class="op">=</span> <span class="comment">/* ... */</span>;
|
||||
<span class="kw">let</span> <span class="ident">expr</span> <span class="op">=</span> <span class="comment">/* ... */</span>;
|
||||
|
||||
<span class="kw">let</span> <span class="ident">expanded</span> <span class="op">=</span> <span class="macro">quote!</span> {
|
||||
<span class="comment">// The generated impl.</span>
|
||||
<span class="kw">impl</span> <span class="ident">heapsize::HeapSize</span> <span class="kw">for</span> #<span class="ident">name</span> {
|
||||
<span class="kw">fn</span> <span class="ident">heap_size_of_children</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">usize</span> {
|
||||
#<span class="ident">expr</span>
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
<span class="comment">// Hand the output tokens back to the compiler.</span>
|
||||
<span class="ident">TokenStream::from</span>(<span class="ident">expanded</span>)
|
||||
}</code></pre></div>
|
||||
<p><br></p>
|
||||
<h4 id="combining-quoted-fragments"><a href="#combining-quoted-fragments">Combining quoted fragments</a></h4>
|
||||
<p>Usually you don’t end up constructing an entire final <code>TokenStream</code> in one
|
||||
piece. Different parts may come from different helper functions. The tokens
|
||||
produced by <code>quote!</code> themselves implement <code>ToTokens</code> and so can be
|
||||
interpolated into later <code>quote!</code> invocations to build up a final result.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">type_definition</span> <span class="op">=</span> <span class="macro">quote!</span> {...};
|
||||
<span class="kw">let</span> <span class="ident">methods</span> <span class="op">=</span> <span class="macro">quote!</span> {...};
|
||||
|
||||
<span class="kw">let</span> <span class="ident">tokens</span> <span class="op">=</span> <span class="macro">quote!</span> {
|
||||
#<span class="ident">type_definition</span>
|
||||
#<span class="ident">methods</span>
|
||||
};</code></pre></div>
|
||||
<p><br></p>
|
||||
<h4 id="constructing-identifiers"><a href="#constructing-identifiers">Constructing identifiers</a></h4>
|
||||
<p>Suppose we have an identifier <code>ident</code> which came from somewhere in a macro
|
||||
input and we need to modify it in some way for the macro output. Let’s
|
||||
consider prepending the identifier with an underscore.</p>
|
||||
<p>Simply interpolating the identifier next to an underscore will not have the
|
||||
behavior of concatenating them. The underscore and the identifier will
|
||||
continue to be two separate tokens as if you had written <code>_ x</code>.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// incorrect</span>
|
||||
<span class="macro">quote!</span> {
|
||||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">_</span>#<span class="ident">ident</span> <span class="op">=</span> <span class="number">0</span>;
|
||||
}</code></pre></div>
|
||||
<p>The solution is to build a new identifier token with the correct value. As
|
||||
this is such a common case, the <a href="macro.format_ident.html" title="format_ident!"><code>format_ident!</code></a> macro provides a
|
||||
convenient utility for doing so correctly.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">varname</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">"_{}"</span>, <span class="ident">ident</span>);
|
||||
<span class="macro">quote!</span> {
|
||||
<span class="kw">let</span> <span class="kw-2">mut</span> #<span class="ident">varname</span> <span class="op">=</span> <span class="number">0</span>;
|
||||
}</code></pre></div>
|
||||
<p>Alternatively, the APIs provided by Syn and proc-macro2 can be used to
|
||||
directly build the identifier. This is roughly equivalent to the above, but
|
||||
will not handle <code>ident</code> being a raw identifier.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">concatenated</span> <span class="op">=</span> <span class="macro">format!</span>(<span class="string">"_{}"</span>, <span class="ident">ident</span>);
|
||||
<span class="kw">let</span> <span class="ident">varname</span> <span class="op">=</span> <span class="ident">syn::Ident::new</span>(<span class="kw-2">&</span><span class="ident">concatenated</span>, <span class="ident">ident</span>.<span class="ident">span</span>());
|
||||
<span class="macro">quote!</span> {
|
||||
<span class="kw">let</span> <span class="kw-2">mut</span> #<span class="ident">varname</span> <span class="op">=</span> <span class="number">0</span>;
|
||||
}</code></pre></div>
|
||||
<p><br></p>
|
||||
<h4 id="making-method-calls"><a href="#making-method-calls">Making method calls</a></h4>
|
||||
<p>Let’s say our macro requires some type specified in the macro input to have
|
||||
a constructor called <code>new</code>. We have the type in a variable called
|
||||
<code>field_type</code> of type <code>syn::Type</code> and want to invoke the constructor.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// incorrect</span>
|
||||
<span class="macro">quote!</span> {
|
||||
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> #<span class="ident">field_type::new</span>();
|
||||
}</code></pre></div>
|
||||
<p>This works only sometimes. If <code>field_type</code> is <code>String</code>, the expanded code
|
||||
contains <code>String::new()</code> which is fine. But if <code>field_type</code> is something
|
||||
like <code>Vec<i32></code> then the expanded code is <code>Vec<i32>::new()</code> which is invalid
|
||||
syntax. Ordinarily in handwritten Rust we would write <code>Vec::<i32>::new()</code>
|
||||
but for macros often the following is more convenient.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">quote!</span> {
|
||||
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="op"><</span>#<span class="ident">field_type</span><span class="op">></span><span class="ident">::new</span>();
|
||||
}</code></pre></div>
|
||||
<p>This expands to <code><Vec<i32>>::new()</code> which behaves correctly.</p>
|
||||
<p>A similar pattern is appropriate for trait methods.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">quote!</span> {
|
||||
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="op"><</span>#<span class="ident">field_type</span> <span class="kw">as</span> <span class="ident">core::default::Default</span><span class="op">></span><span class="ident">::default</span>();
|
||||
}</code></pre></div>
|
||||
<p><br></p>
|
||||
<h4 id="interpolating-text-inside-of-doc-comments"><a href="#interpolating-text-inside-of-doc-comments">Interpolating text inside of doc comments</a></h4>
|
||||
<p>Neither doc comments nor string literals get interpolation behavior in
|
||||
quote:</p>
|
||||
|
||||
<div class='information'><div class='tooltip compile_fail'>ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail"><code><span class="macro">quote!</span> {
|
||||
<span class="doccomment">/// try to interpolate: #ident</span>
|
||||
<span class="doccomment">///</span>
|
||||
<span class="doccomment">/// ...</span>
|
||||
}</code></pre></div>
|
||||
|
||||
<div class='information'><div class='tooltip compile_fail'>ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail"><code><span class="macro">quote!</span> {
|
||||
<span class="attribute">#[<span class="ident">doc</span> <span class="op">=</span> <span class="string">"try to interpolate: #ident"</span>]</span>
|
||||
}</code></pre></div>
|
||||
<p>Instead the best way to build doc comments that involve variables is by
|
||||
formatting the doc string literal outside of quote.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">msg</span> <span class="op">=</span> <span class="macro">format!</span>(...);
|
||||
<span class="macro">quote!</span> {
|
||||
<span class="attribute">#[<span class="ident">doc</span> <span class="op">=</span> #<span class="ident">msg</span>]</span>
|
||||
<span class="doccomment">///</span>
|
||||
<span class="doccomment">/// ...</span>
|
||||
}</code></pre></div>
|
||||
<p><br></p>
|
||||
<h4 id="indexing-into-a-tuple-struct"><a href="#indexing-into-a-tuple-struct">Indexing into a tuple struct</a></h4>
|
||||
<p>When interpolating indices of a tuple or tuple struct, we need them not to
|
||||
appears suffixed as integer literals by interpolating them as <a href="https://docs.rs/syn/1.0/syn/struct.Index.html"><code>syn::Index</code></a>
|
||||
instead.</p>
|
||||
|
||||
<div class='information'><div class='tooltip compile_fail'>ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail"><code><span class="kw">let</span> <span class="ident">i</span> <span class="op">=</span> <span class="number">0usize</span>..<span class="self">self</span>.<span class="ident">fields</span>.<span class="ident">len</span>();
|
||||
|
||||
<span class="comment">// expands to 0 + self.0usize.heap_size() + self.1usize.heap_size() + ...</span>
|
||||
<span class="comment">// which is not valid syntax</span>
|
||||
<span class="macro">quote!</span> {
|
||||
<span class="number">0</span> #( <span class="op">+</span> <span class="self">self</span>.#<span class="ident">i</span>.<span class="ident">heap_size</span>() )<span class="op">*</span>
|
||||
}</code></pre></div>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">i</span> <span class="op">=</span> (<span class="number">0</span>..<span class="self">self</span>.<span class="ident">fields</span>.<span class="ident">len</span>()).<span class="ident">map</span>(<span class="ident">syn::Index::from</span>);
|
||||
|
||||
<span class="comment">// expands to 0 + self.0.heap_size() + self.1.heap_size() + ...</span>
|
||||
<span class="macro">quote!</span> {
|
||||
<span class="number">0</span> #( <span class="op">+</span> <span class="self">self</span>.#<span class="ident">i</span>.<span class="ident">heap_size</span>() )<span class="op">*</span>
|
||||
}</code></pre></div>
|
||||
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="quote" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
||||
11
doc/quote/macro.quote_spanned!.html
Normal file
11
doc/quote/macro.quote_spanned!.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;URL=macro.quote_spanned.html">
|
||||
<title>Redirection</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Redirecting to <a href="macro.quote_spanned.html">macro.quote_spanned.html</a>...</p>
|
||||
<script>location.replace("macro.quote_spanned.html" + location.search + location.hash);</script>
|
||||
</body>
|
||||
</html>
|
||||
52
doc/quote/macro.quote_spanned.html
Normal file
52
doc/quote/macro.quote_spanned.html
Normal file
@ -0,0 +1,52 @@
|
||||
<!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="Same as `quote!`, but applies a given span to all tokens originating within the macro invocation."><meta name="keywords" content="rust, rustlang, rust-lang, quote_spanned"><title>quote_spanned in quote - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><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="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../quote/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2 class="location"><a href="index.html">In quote</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../quote/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Macro <a href="index.html">quote</a>::<wbr><a class="macro" href="#">quote_spanned</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/quote/lib.rs.html#612-616">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><div class="example-wrap"><pre class="rust macro"><code><span class="macro">macro_rules!</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="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>) => { ... };
|
||||
}</code></pre></div>
|
||||
</div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Same as <code>quote!</code>, but applies a given span to all tokens originating within
|
||||
the macro invocation.</p>
|
||||
<br>
|
||||
<h2 id="syntax"><a href="#syntax">Syntax</a></h2>
|
||||
<p>A span expression of type <a href="https://docs.rs/proc-macro2/1.0/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>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><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="ident">span</span>=> <span class="ident">Box::into_raw</span>(<span class="ident">Box::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="ident">span</span>=>
|
||||
<span class="ident">Box::into_raw</span>(<span class="ident">Box::new</span>(#<span class="ident">init</span>))
|
||||
};</code></pre></div>
|
||||
<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>
|
||||
<br>
|
||||
<h2 id="hygiene"><a href="#hygiene">Hygiene</a></h2>
|
||||
<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>
|
||||
<br>
|
||||
<h2 id="example"><a href="#example">Example</a></h2>
|
||||
<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>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><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="ident">ty_span</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>;
|
||||
};</code></pre></div>
|
||||
<p>If the assertion fails, the user will see an error like the following. The
|
||||
input span of their type is highlighted in the error.</p>
|
||||
<div class="example-wrap"><pre class="language-text"><code>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></div>
|
||||
<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.</p>
|
||||
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="quote" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
||||
1
doc/quote/sidebar-items.js
Normal file
1
doc/quote/sidebar-items.js
Normal file
@ -0,0 +1 @@
|
||||
window.SIDEBAR_ITEMS = {"macro":[["format_ident","Formatting macro for constructing `Ident`s."],["quote","The whole point."],["quote_spanned","Same as `quote!`, but applies a given span to all tokens originating within the macro invocation."]],"trait":[["IdentFragment","Specialized formatting trait used by `format_ident!`."],["ToTokens","Types that can be interpolated inside a `quote!` invocation."],["TokenStreamExt","TokenStream extension trait with methods for appending tokens."]]};
|
||||
11
doc/quote/to_tokens/trait.ToTokens.html
Normal file
11
doc/quote/to_tokens/trait.ToTokens.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0;URL=../../quote/trait.ToTokens.html">
|
||||
<title>Redirection</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Redirecting to <a href="../../quote/trait.ToTokens.html">../../quote/trait.ToTokens.html</a>...</p>
|
||||
<script>location.replace("../../quote/trait.ToTokens.html" + location.search + location.hash);</script>
|
||||
</body>
|
||||
</html>
|
||||
12
doc/quote/trait.IdentFragment.html
Normal file
12
doc/quote/trait.IdentFragment.html
Normal file
File diff suppressed because one or more lines are too long
41
doc/quote/trait.ToTokens.html
Normal file
41
doc/quote/trait.ToTokens.html
Normal file
File diff suppressed because one or more lines are too long
28
doc/quote/trait.TokenStreamExt.html
Normal file
28
doc/quote/trait.TokenStreamExt.html
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user