blob: 71623384249fb959fdce3924b85b4577a4eda287 [file] [log] [blame]
<!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="A crate for safe and ergonomic pin-projection."><meta name="keywords" content="rust, rustlang, rust-lang, pin_project"><title>pin_project - 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" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" 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">&#9776;</button><a class="sidebar-logo" href="../pin_project/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../pin_project/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate pin_project</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 1.1.0</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#traits">Traits</a></li><li><a href="#attributes">Attribute Macros</a></li></ul></section></div></nav><main><div class="width-limiter"><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"><a href="../help.html">?</a></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><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">pin_project</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></h1><span class="out-of-band"><a class="srclink" href="../src/pin_project/lib.rs.html#1-337">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><!-- tidy:crate-doc:start -->
<p>A crate for safe and ergonomic <a href="https://doc.rust-lang.org/std/pin/index.html#projections-and-structural-pinning">pin-projection</a>.</p>
<h3 id="usage"><a href="#usage">Usage</a></h3>
<p>Add this to your <code>Cargo.toml</code>:</p>
<div class="example-wrap"><pre class="language-toml"><code>[dependencies]
pin-project = &quot;1&quot;</code></pre></div>
<p><em>Compiler support: requires rustc 1.56+</em></p>
<h3 id="examples"><a href="#examples">Examples</a></h3>
<p><a href="attr.pin_project.html" title="pin_project"><code>#[pin_project]</code></a> attribute creates projection types
covering all the fields of struct or enum.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::pin::Pin;
<span class="kw">use </span>pin_project::pin_project;
<span class="attribute">#[pin_project]
</span><span class="kw">struct </span>Struct&lt;T, U&gt; {
<span class="attribute">#[pin]
</span>pinned: T,
unpinned: U,
}
<span class="kw">impl</span>&lt;T, U&gt; Struct&lt;T, U&gt; {
<span class="kw">fn </span>method(<span class="self">self</span>: Pin&lt;<span class="kw-2">&amp;mut </span><span class="self">Self</span>&gt;) {
<span class="kw">let </span>this = <span class="self">self</span>.project();
<span class="kw">let _</span>: Pin&lt;<span class="kw-2">&amp;mut </span>T&gt; = this.pinned; <span class="comment">// Pinned reference to the field
</span><span class="kw">let _</span>: <span class="kw-2">&amp;mut </span>U = this.unpinned; <span class="comment">// Normal reference to the field
</span>}
}</code></pre></div>
<p><a href="https://github.com/taiki-e/pin-project/blob/HEAD/examples/struct-default-expanded.rs"><em>code like this will be generated</em></a></p>
<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"><code><span class="kw">use </span>std::pin::Pin;
<span class="kw">use </span>pin_project::pin_project;
<span class="attribute">#[pin_project(project = EnumProj)]
</span><span class="kw">enum </span>Enum&lt;T, U&gt; {
Pinned(<span class="attribute">#[pin] </span>T),
Unpinned(U),
}
<span class="kw">impl</span>&lt;T, U&gt; Enum&lt;T, U&gt; {
<span class="kw">fn </span>method(<span class="self">self</span>: Pin&lt;<span class="kw-2">&amp;mut </span><span class="self">Self</span>&gt;) {
<span class="kw">match </span><span class="self">self</span>.project() {
EnumProj::Pinned(x) =&gt; {
<span class="kw">let _</span>: Pin&lt;<span class="kw-2">&amp;mut </span>T&gt; = x;
}
EnumProj::Unpinned(y) =&gt; {
<span class="kw">let _</span>: <span class="kw-2">&amp;mut </span>U = y;
}
}
}
}</code></pre></div>
<p><a href="https://github.com/taiki-e/pin-project/blob/HEAD/examples/enum-default-expanded.rs"><em>code like this will be generated</em></a></p>
<p>See <a href="attr.pin_project.html" title="pin_project"><code>#[pin_project]</code></a> attribute for more details, and
see <a href="https://github.com/taiki-e/pin-project/blob/HEAD/examples/README.md">examples</a> directory for more examples and generated code.</p>
<h3 id="related-projects"><a href="#related-projects">Related Projects</a></h3>
<ul>
<li><a href="https://github.com/taiki-e/pin-project-lite">pin-project-lite</a>: A lightweight version of pin-project written with declarative macros.</li>
</ul>
<!-- tidy:crate-doc:end --></div></details><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.UnsafeUnpin.html" title="pin_project::UnsafeUnpin trait">UnsafeUnpin</a></div><div class="item-right docblock-short">A trait used for custom implementations of [<code>Unpin</code>].</div></div></div><h2 id="attributes" class="small-section-header"><a href="#attributes">Attribute Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="attr" href="attr.pin_project.html" title="pin_project::pin_project attr">pin_project</a></div><div class="item-right docblock-short">An attribute that creates projection types covering all the fields of
struct or enum.</div></div><div class="item-row"><div class="item-left module-item"><a class="attr" href="attr.pinned_drop.html" title="pin_project::pinned_drop attr">pinned_drop</a></div><div class="item-right docblock-short">An attribute used for custom implementations of [<code>Drop</code>].</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="pin_project" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>