blob: c2ff2242db3b5c7913e414e98644ac11a0f22d5a [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 trait used for custom implementations of [`Unpin`]."><meta name="keywords" content="rust, rustlang, rust-lang, UnsafeUnpin"><title>UnsafeUnpin in 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="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 trait"><!--[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="#">UnsafeUnpin</a></h2><div class="sidebar-elems"><section><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In pin_project</a></h2></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">Trait <a href="index.html">pin_project</a>::<wbr><a class="trait" href="#">UnsafeUnpin</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#181">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub unsafe trait UnsafeUnpin { }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A trait used for custom implementations of <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a>.</p>
<p>This trait is used in conjunction with the <code>UnsafeUnpin</code> argument to
the <a href="attr.pin_project.html" title="pin_project"><code>#[pin_project]</code></a> attribute.</p>
<h2 id="safety"><a href="#safety">Safety</a></h2>
<p>The Rust <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> trait is safe to implement - by itself,
implementing it cannot lead to <a href="https://doc.rust-lang.org/reference/behavior-considered-undefined.html">undefined behavior</a>.
Undefined behavior can only occur when other unsafe code is used.</p>
<p>It turns out that using pin projections, which requires unsafe code,
imposes additional requirements on an <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> impl. Normally, all of this
unsafety is contained within this crate, ensuring that it’s impossible for
you to violate any of the guarantees required by pin projection.</p>
<p>However, things change if you want to provide a custom <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> impl
for your <code>#[pin_project]</code> type. As stated in <a href="https://doc.rust-lang.org/nightly/core/pin/index.html#projections-and-structural-pinning">the Rust
documentation</a>, you must be sure to only implement <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a>
when all of your <code>#[pin]</code> fields (i.e. structurally pinned fields) are also
<a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a>.</p>
<p>To help highlight this unsafety, the <code>UnsafeUnpin</code> trait is provided.
Implementing this trait is logically equivalent to implementing <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> -
this crate will generate an <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> impl for your type that ‘forwards’ to
your <code>UnsafeUnpin</code> impl. However, this trait is <code>unsafe</code> - since your type
uses structural pinning (otherwise, you wouldn’t be using this crate!),
you must be sure that your <code>UnsafeUnpin</code> impls follows all of
the requirements for an <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a> impl of a structurally-pinned type.</p>
<p>Note that if you specify <code>#[pin_project(UnsafeUnpin)]</code>, but do <em>not</em>
provide an impl of <code>UnsafeUnpin</code>, your type will never implement <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a>.
This is effectively the same thing as adding a <a href="https://doc.rust-lang.org/nightly/core/marker/struct.PhantomPinned.html"><code>PhantomPinned</code></a> to your
type.</p>
<p>Since this trait is <code>unsafe</code>, impls of it will be detected by the
<code>unsafe_code</code> lint, and by tools like <a href="https://github.com/rust-secure-code/cargo-geiger"><code>cargo geiger</code></a>.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>An <code>UnsafeUnpin</code> impl which, in addition to requiring that structurally
pinned fields be <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="Unpin"><code>Unpin</code></a>, imposes an additional requirement:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pin_project::{pin_project, UnsafeUnpin};
<span class="attribute">#[pin_project(UnsafeUnpin)]
</span><span class="kw">struct </span>Struct&lt;K, V&gt; {
<span class="attribute">#[pin]
</span>field_1: K,
field_2: V,
}
<span class="kw">unsafe impl</span>&lt;K, V&gt; UnsafeUnpin <span class="kw">for </span>Struct&lt;K, V&gt; <span class="kw">where </span>K: Unpin + Clone {}</code></pre></div>
</div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"></div><script src="../implementors/pin_project/trait.UnsafeUnpin.js" async></script></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>