mcquery-rs/doc/pin_project_lite/macro.pin_project.html

171 lines
20 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 `pin_project` macro in crate `pin_project_lite`."><meta name="keywords" content="rust, rustlang, rust-lang, pin_project"><title>pin_project_lite::pin_project - 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 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">&#9776;</div><a href='../pin_project_lite/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><div class="sidebar-elems"><p class="location"><a href="index.html">pin_project_lite</a></p><script>window.sidebarCurrent = {name: "pin_project", 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!" 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/pin_project_lite/lib.rs.html#294-338" title="goto source code">[src]</a></span><span class="in-band">Macro <a href="index.html">pin_project_lite</a>::<wbr><a class="macro" href="">pin_project</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><div class="example-wrap"><pre class="rust macro">
<span class="macro">macro_rules</span><span class="macro">!</span> <span class="ident">pin_project</span> {
(
$(<span class="attribute">#[<span class="ident">doc</span> $(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">doc</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>]</span>)<span class="op">*</span>
<span class="attribute">#[<span class="ident">project</span> <span class="op">=</span> <span class="macro-nonterminal">$</span><span class="macro-nonterminal">proj_mut_ident</span>:<span class="ident">ident</span>]</span>
<span class="attribute">#[<span class="ident">project_ref</span> <span class="op">=</span> <span class="macro-nonterminal">$</span><span class="macro-nonterminal">proj_ref_ident</span>:<span class="ident">ident</span>]</span>
$(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="op">*</span>
) <span class="op">=</span><span class="op">&gt;</span> { ... };
(
$(<span class="attribute">#[<span class="ident">doc</span> $(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">doc</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>]</span>)<span class="op">*</span>
<span class="attribute">#[<span class="ident">project</span> <span class="op">=</span> <span class="macro-nonterminal">$</span><span class="macro-nonterminal">proj_mut_ident</span>:<span class="ident">ident</span>]</span>
$(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="op">*</span>
) <span class="op">=</span><span class="op">&gt;</span> { ... };
(
$(<span class="attribute">#[<span class="ident">doc</span> $(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">doc</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>]</span>)<span class="op">*</span>
<span class="attribute">#[<span class="ident">project_ref</span> <span class="op">=</span> <span class="macro-nonterminal">$</span><span class="macro-nonterminal">proj_ref_ident</span>:<span class="ident">ident</span>]</span>
$(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="op">*</span>
) <span class="op">=</span><span class="op">&gt;</span> { ... };
(
$(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="op">*</span>
) <span class="op">=</span><span class="op">&gt;</span> { ... };
}</pre></div>
</div><div class="docblock"><p>A macro that creates a projection type covering all the fields of struct.</p>
<p>This macro creates a projection type according to the following rules:</p>
<ul>
<li>For the field that uses <code>#[pin]</code> attribute, makes the pinned reference to the field.</li>
<li>For the other fields, makes the unpinned reference to the field.</li>
</ul>
<p>And the following methods are implemented on the original type:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">fn</span> <span class="ident">project</span>(<span class="self">self</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">Self</span><span class="op">&gt;</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Projection</span><span class="op">&lt;</span><span class="lifetime">&#39;_</span><span class="op">&gt;</span>;
<span class="kw">fn</span> <span class="ident">project_ref</span>(<span class="self">self</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="self">Self</span><span class="op">&gt;</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">ProjectionRef</span><span class="op">&lt;</span><span class="lifetime">&#39;_</span><span class="op">&gt;</span>;</pre></div>
<p>By passing an attribute with the same name as the method to the macro,
you can name the projection type returned from the method. This allows you
to use pattern matching on the projected types.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="macro">pin_project</span><span class="macro">!</span> {
<span class="attribute">#[<span class="ident">project</span> <span class="op">=</span> <span class="ident">EnumProj</span>]</span>
<span class="kw">enum</span> <span class="ident">Enum</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">Variant</span> { <span class="attribute">#[<span class="ident">pin</span>]</span> <span class="ident">field</span>: <span class="ident">T</span> },
}
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Enum</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">method</span>(<span class="self">self</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">Self</span><span class="op">&gt;</span>) {
<span class="kw">let</span> <span class="ident">this</span>: <span class="ident">EnumProj</span><span class="op">&lt;</span><span class="lifetime">&#39;_</span>, <span class="ident">T</span><span class="op">&gt;</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">project</span>();
<span class="kw">match</span> <span class="ident">this</span> {
<span class="ident">EnumProj</span>::<span class="ident">Variant</span> { <span class="ident">field</span> } <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="kw">_</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">T</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">field</span>;
}
}
}
}</pre></div>
<p>The <code>#[project]</code> (and <code>#[project_ref]</code>) attribute must precede the other
attributes except for <code>#[doc]</code>. For example, the following code will not be compiled:</p>
<div class='information'><div class='tooltip compile_fail'><span class='tooltiptext'>This example deliberately fails to compile</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail">
<span class="macro">pin_project</span><span class="macro">!</span> {
<span class="doccomment">/// documents (`#[doc]`) can be placed before `#[project]`.</span>
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Clone</span>)]</span> <span class="comment">// &lt;--- ERROR</span>
<span class="attribute">#[<span class="ident">project</span> <span class="op">=</span> <span class="ident">EnumProj</span>]</span>
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>)]</span> <span class="comment">// &lt;--- Ok</span>
<span class="kw">enum</span> <span class="ident">Enum</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">Variant</span> { <span class="attribute">#[<span class="ident">pin</span>]</span> <span class="ident">field</span>: <span class="ident">T</span> },
}
}</pre></div>
<p>Also, note that the projection types returned by <code>project</code> and <code>project_ref</code> have
an additional lifetime at the beginning of generics.</p>
<pre><code class="language-text">let this: EnumProj&lt;'_, T&gt; = self.project();
^^
</code></pre>
<p>The visibility of the projected types and projection methods is based on the
original type. However, if the visibility of the original type is <code>pub</code>, the
visibility of the projected types and the projection methods is downgraded
to <code>pub(crate)</code>.</p>
<h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1>
<p><code>pin_project!</code> macro guarantees safety in much the same way as <a href="https://github.com/taiki-e/pin-project">pin-project</a> crate.
Both are completely safe unless you write other unsafe code.</p>
<p>See <a href="https://github.com/taiki-e/pin-project">pin-project</a> crate for more details.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">pin_project_lite</span>::<span class="ident">pin_project</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">pin</span>::<span class="ident">Pin</span>;
<span class="macro">pin_project</span><span class="macro">!</span> {
<span class="kw">struct</span> <span class="ident">Struct</span><span class="op">&lt;</span><span class="ident">T</span>, <span class="ident">U</span><span class="op">&gt;</span> {
<span class="attribute">#[<span class="ident">pin</span>]</span>
<span class="ident">pinned</span>: <span class="ident">T</span>,
<span class="ident">unpinned</span>: <span class="ident">U</span>,
}
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span>, <span class="ident">U</span><span class="op">&gt;</span> <span class="ident">Struct</span><span class="op">&lt;</span><span class="ident">T</span>, <span class="ident">U</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">method</span>(<span class="self">self</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">Self</span><span class="op">&gt;</span>) {
<span class="kw">let</span> <span class="ident">this</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">project</span>();
<span class="kw">let</span> <span class="kw">_</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">T</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">this</span>.<span class="ident">pinned</span>; <span class="comment">// Pinned reference to the field</span>
<span class="kw">let</span> <span class="kw">_</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">U</span> <span class="op">=</span> <span class="ident">this</span>.<span class="ident">unpinned</span>; <span class="comment">// Normal reference to the field</span>
}
}</pre></div>
<p>To use <code>pin_project!</code> on enums, you need to name the projection type
returned from the method.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">pin_project_lite</span>::<span class="ident">pin_project</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">pin</span>::<span class="ident">Pin</span>;
<span class="macro">pin_project</span><span class="macro">!</span> {
<span class="attribute">#[<span class="ident">project</span> <span class="op">=</span> <span class="ident">EnumProj</span>]</span>
<span class="kw">enum</span> <span class="ident">Enum</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">Struct</span> {
<span class="attribute">#[<span class="ident">pin</span>]</span>
<span class="ident">field</span>: <span class="ident">T</span>,
},
<span class="ident">Unit</span>,
}
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Enum</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">method</span>(<span class="self">self</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">Self</span><span class="op">&gt;</span>) {
<span class="kw">match</span> <span class="self">self</span>.<span class="ident">project</span>() {
<span class="ident">EnumProj</span>::<span class="ident">Struct</span> { <span class="ident">field</span> } <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="kw">_</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">T</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">field</span>;
}
<span class="ident">EnumProj</span>::<span class="ident">Unit</span> <span class="op">=</span><span class="op">&gt;</span> {}
}
}
}</pre></div>
<p>If you want to call the <code>project()</code> method multiple times or later use the
original <a href="https://doc.rust-lang.org/nightly/core/pin/struct.Pin.html"><code>Pin</code></a> type, it needs to use <a href="https://doc.rust-lang.org/nightly/core/pin/struct.Pin.html#method.as_mut"><code>.as_mut()</code></a> to avoid
consuming the <a href="https://doc.rust-lang.org/nightly/core/pin/struct.Pin.html"><code>Pin</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">pin_project_lite</span>::<span class="ident">pin_project</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">pin</span>::<span class="ident">Pin</span>;
<span class="macro">pin_project</span><span class="macro">!</span> {
<span class="kw">struct</span> <span class="ident">Struct</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="attribute">#[<span class="ident">pin</span>]</span>
<span class="ident">field</span>: <span class="ident">T</span>,
}
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Struct</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">call_project_twice</span>(<span class="kw-2">mut</span> <span class="self">self</span>: <span class="ident">Pin</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">Self</span><span class="op">&gt;</span>) {
<span class="comment">// `project` consumes `self`, so reborrow the `Pin&lt;&amp;mut Self&gt;` via `as_mut`.</span>
<span class="self">self</span>.<span class="ident">as_mut</span>().<span class="ident">project</span>();
<span class="self">self</span>.<span class="ident">as_mut</span>().<span class="ident">project</span>();
}
}</pre></div>
<h1 id="unpin" class="section-header"><a href="#unpin"><code>!Unpin</code></a></h1>
<p>If you want to ensure that <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> is not implemented, use <code>#[pin]</code>
attribute for a <a href="https://doc.rust-lang.org/nightly/core/marker/struct.PhantomPinned.html"><code>PhantomPinned</code></a> field.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">pin_project_lite</span>::<span class="ident">pin_project</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">marker</span>::<span class="ident">PhantomPinned</span>;
<span class="macro">pin_project</span><span class="macro">!</span> {
<span class="kw">struct</span> <span class="ident">Struct</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">field</span>: <span class="ident">T</span>,
<span class="attribute">#[<span class="ident">pin</span>]</span> <span class="comment">// &lt;------ This `#[pin]` is required to make `Struct` to `!Unpin`.</span>
<span class="ident">_pin</span>: <span class="ident">PhantomPinned</span>,
}
}</pre></div>
<p>Note that using <a href="https://doc.rust-lang.org/nightly/core/marker/struct.PhantomPinned.html"><code>PhantomPinned</code></a> without <code>#[pin]</code> attribute has no effect.</p>
</div></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "pin_project_lite";</script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>