mcquery-rs/doc/syn/spanned/index.html

60 lines
7.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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 `spanned` mod in crate `syn`."><meta name="keywords" content="rust, rustlang, rust-lang, spanned"><title>syn::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="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings"></script><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg">
<link rel="alternate icon" type="image/png" href="../../favicon-16x16.png">
<link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[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">&#9776;</div><a href='../../syn/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a><p class="location">Module spanned</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#traits">Traits</a></li></ul></div><p class="location"><a href="../index.html">syn</a></p><script>window.sidebarCurrent = {name: "spanned", ty: "mod", relpath: "../"};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><span class="help-button">?</span>
<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="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span><a class="srclink" href="../../src/syn/spanned.rs.html#1-114" title="goto source code">[src]</a></span><span class="in-band">Module <a href="../index.html">syn</a>::<wbr><a class="mod" href="">spanned</a></span></h1><div class="docblock"><p>A trait that can provide the <code>Span</code> of the complete contents of a syntax
tree node.</p>
<p><em>This module is available only if Syn is built with both the <code>&quot;parsing&quot;</code> and
<code>&quot;printing&quot;</code> features.</em></p>
<br>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<p>Suppose in a procedural macro we have a <a href="../../syn/enum.Type.html"><code>Type</code></a> that we want to assert
implements the <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html"><code>Sync</code></a> trait. Maybe this is the type of one of the fields
of a struct for which we are deriving a trait implementation, and we need to
be able to pass a reference to one of those fields across threads.</p>
<p>If the field type does <em>not</em> implement <code>Sync</code> as required, we want the
compiler to report an error pointing out exactly which type it was.</p>
<p>The following macro code takes a variable <code>ty</code> of type <code>Type</code> and produces a
static assertion that <code>Sync</code> is implemented for that type.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">proc_macro</span>::<span class="ident">TokenStream</span>;
<span class="kw">use</span> <span class="ident">proc_macro2</span>::<span class="ident">Span</span>;
<span class="kw">use</span> <span class="ident">quote</span>::<span class="ident">quote_spanned</span>;
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">Type</span>;
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">spanned</span>::<span class="ident">Spanned</span>;
<span class="attribute">#[<span class="ident">proc_macro_derive</span>(<span class="ident">MyMacro</span>)]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">my_macro</span>(<span class="ident">input</span>: <span class="ident">TokenStream</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">TokenStream</span> {
<span class="comment">/* ... */</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 class="ident">span</span>()<span class="op">=</span><span class="op">&gt;</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>;
};
<span class="comment">/* ... */</span>
}</pre></div>
<p>By inserting this <code>assert_sync</code> fragment into the output code generated by
our macro, the user's code will fail to compile if <code>ty</code> does not implement
<code>Sync</code>. The errors they would see look like the following.</p>
<pre><code class="language-text">error[E0277]: the trait bound `*const i32: std::marker::Sync` is not satisfied
--&gt; src/main.rs:10:21
|
10 | bad_field: *const i32,
| ^^^^^^^^^^ `*const i32` cannot be shared between threads safely
</code></pre>
<p>In this technique, using the <code>Type</code>'s span for the error message makes the
error appear in the correct place underlining the right type.</p>
<br>
<h1 id="limitations" class="section-header"><a href="#limitations">Limitations</a></h1>
<p>The underlying <a href="https://doc.rust-lang.org/nightly/proc_macro/struct.Span.html#method.join" title="proc_macro::Span::join"><code>proc_macro::Span::join</code></a> method is nightly-only. When
called from within a procedural macro in a nightly compiler, <code>Spanned</code> will
use <code>join</code> to produce the intended span. When not using a nightly compiler,
only the span of the <em>first token</em> of the syntax tree node is returned.</p>
<p>In the common case of wanting to use the joined span as the span of a
<code>syn::Error</code>, consider instead using <a href="../../syn/struct.Error.html#method.new_spanned"><code>syn::Error::new_spanned</code></a> which is
able to span the error correctly under the complete syntax tree node without
needing the unstable <code>join</code>.</p>
</div><h2 id="traits" class="section-header"><a href="#traits">Traits</a></h2>
<table><tr class="module-item"><td><a class="trait" href="trait.Spanned.html" title="syn::spanned::Spanned trait">Spanned</a></td><td class="docblock-short"><p>A trait that can provide the <code>Span</code> of the complete contents of a syntax
tree node.</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../";window.currentCrate = "syn";</script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>