Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 0c2243f8a1696816431e7210e991fa52 > files > 12191

rust-doc-1.35.0-1.mga7.armv7hl.rpm

<!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` mod in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, pin"><title>core::pin - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize1.35.0.css"><link rel="stylesheet" type="text/css" href="../../rustdoc1.35.0.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark1.35.0.css"><link rel="stylesheet" type="text/css" href="../../light1.35.0.css" id="themeStyle"><script src="../../storage1.35.0.js"></script><noscript><link rel="stylesheet" href="../../noscript1.35.0.css"></noscript><link rel="shortcut icon" href="../../favicon1.35.0.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow1.35.0.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='../../core/index.html'><img src='../../rust-logo1.35.0.png' alt='logo' width='100'></a><p class='location'>Module pin</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li></ul></div><p class='location'><a href='../index.html'>core</a></p><script>window.sidebarCurrent = {name: 'pin', 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!"><img src="../../brush1.35.0.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme1.35.0.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel1.35.0.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span class='since' title='Stable since Rust version 1.33.0'>1.33.0</span><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/core/pin.rs.html#1-635' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>core</a>::<wbr><a class="mod" href=''>pin</a></span></h1><div class='docblock'><p>Types that pin data to its location in memory.</p>
<p>It is sometimes useful to have objects that are guaranteed to not move,
in the sense that their placement in memory does not change, and can thus be relied upon.
A prime example of such a scenario would be building self-referential structs,
since moving an object with pointers to itself will invalidate them,
which could cause undefined behavior.</p>
<p>A <a href="struct.Pin.html"><code>Pin&lt;P&gt;</code></a> ensures that the pointee of any pointer type <code>P</code> has a stable location in memory,
meaning it cannot be moved elsewhere and its memory cannot be deallocated
until it gets dropped. We say that the pointee is &quot;pinned&quot;.</p>
<p>By default, all types in Rust are movable. Rust allows passing all types by-value,
and common smart-pointer types such as <code>Box&lt;T&gt;</code> and <code>&amp;mut T</code> allow replacing and
moving the values they contain: you can move out of a <code>Box&lt;T&gt;</code>, or you can use <a href="../../std/mem/fn.swap.html"><code>mem::swap</code></a>.
<a href="struct.Pin.html"><code>Pin&lt;P&gt;</code></a> wraps a pointer type <code>P</code>, so <code>Pin&lt;Box&lt;T&gt;&gt;</code> functions much like a regular <code>Box&lt;T&gt;</code>:
when a <code>Pin&lt;Box&lt;T&gt;&gt;</code> gets dropped, so do its contents, and the memory gets deallocated.
Similarily, <code>Pin&lt;&amp;mut T&gt;</code> is a lot like <code>&amp;mut T</code>. However, <a href="struct.Pin.html"><code>Pin&lt;P&gt;</code></a> does not let clients
actually obtain a <code>Box&lt;T&gt;</code> or <code>&amp;mut T</code> to pinned data, which implies that you cannot use
operations such as <a href="../../std/mem/fn.swap.html"><code>mem::swap</code></a>:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">pin</span>::<span class="ident">Pin</span>;
<span class="kw">fn</span> <span class="ident">swap_pins</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>(<span class="ident">x</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="ident">y</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="comment">// `mem::swap` needs `&amp;mut T`, but we cannot get it.</span>
    <span class="comment">// We are stuck, we cannot swap the contents of these references.</span>
    <span class="comment">// We could use `Pin::get_unchecked_mut`, but that is unsafe for a reason:</span>
    <span class="comment">// we are not allowed to use it for moving things out of the `Pin`.</span>
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Apin%3A%3APin%3B%0Afn%20swap_pins%3CT%3E(x%3A%20Pin%3C%26mut%20T%3E%2C%20y%3A%20Pin%3C%26mut%20T%3E)%20%7B%0A%20%20%20%20%2F%2F%20%60mem%3A%3Aswap%60%20needs%20%60%26mut%20T%60%2C%20but%20we%20cannot%20get%20it.%0A%20%20%20%20%2F%2F%20We%20are%20stuck%2C%20we%20cannot%20swap%20the%20contents%20of%20these%20references.%0A%20%20%20%20%2F%2F%20We%20could%20use%20%60Pin%3A%3Aget_unchecked_mut%60%2C%20but%20that%20is%20unsafe%20for%20a%20reason%3A%0A%20%20%20%20%2F%2F%20we%20are%20not%20allowed%20to%20use%20it%20for%20moving%20things%20out%20of%20the%20%60Pin%60.%0A%7D%0A%7D">Run</a></pre></div>
<p>It is worth reiterating that <a href="struct.Pin.html"><code>Pin&lt;P&gt;</code></a> does <em>not</em> change the fact that a Rust compiler
considers all types movable. <a href="../../std/mem/fn.swap.html"><code>mem::swap</code></a> remains callable for any <code>T</code>. Instead, <code>Pin&lt;P&gt;</code>
prevents certain <em>values</em> (pointed to by pointers wrapped in <code>Pin&lt;P&gt;</code>) from being
moved by making it impossible to call methods that require <code>&amp;mut T</code> on them
(like <a href="../../std/mem/fn.swap.html"><code>mem::swap</code></a>).</p>
<p><a href="struct.Pin.html"><code>Pin&lt;P&gt;</code></a> can be used to wrap any pointer type <code>P</code>, and as such it interacts with
<a href="../../std/ops/trait.Deref.html"><code>Deref</code></a> and <a href="../../std/ops/trait.DerefMut.html"><code>DerefMut</code></a>. A <code>Pin&lt;P&gt;</code> where <code>P: Deref</code> should be considered
as a &quot;<code>P</code>-style pointer&quot; to a pinned <code>P::Target</code> -- so, a <code>Pin&lt;Box&lt;T&gt;&gt;</code> is
an owned pointer to a pinned <code>T</code>, and a <code>Pin&lt;Rc&lt;T&gt;&gt;</code> is a reference-counted
pointer to a pinned <code>T</code>.
For correctness, <a href="struct.Pin.html"><code>Pin&lt;P&gt;</code></a> relies on the <a href="../../std/ops/trait.Deref.html"><code>Deref</code></a> and <a href="../../std/ops/trait.DerefMut.html"><code>DerefMut</code></a> implementations
to not move out of their <code>self</code> parameter, and to only ever return a pointer
to pinned data when they are called on a pinned pointer.</p>
<h1 id="unpin" class="section-header"><a href="#unpin"><code>Unpin</code></a></h1>
<p>However, these restrictions are usually not necessary. Many types are always freely
movable, even when pinned, because they do not rely on having a stable address.
This includes all the basic types (like <code>bool</code>, <code>i32</code>, references)
as well as types consisting solely of these types.
Types that do not care about pinning implement the <a href="../../std/marker/trait.Unpin.html"><code>Unpin</code></a> auto-trait, which
cancels the effect of <a href="struct.Pin.html"><code>Pin&lt;P&gt;</code></a>. For <code>T: Unpin</code>, <code>Pin&lt;Box&lt;T&gt;&gt;</code> and <code>Box&lt;T&gt;</code> function
identically, as do <code>Pin&lt;&amp;mut T&gt;</code> and <code>&amp;mut T</code>.</p>
<p>Note that pinning and <code>Unpin</code> only affect the pointed-to type <code>P::Target</code>, not the pointer
type <code>P</code> itself that got wrapped in <code>Pin&lt;P&gt;</code>. For example, whether or not <code>Box&lt;T&gt;</code> is
<code>Unpin</code> has no effect on the behavior of <code>Pin&lt;Box&lt;T&gt;&gt;</code> (here, <code>T</code> is the
pointed-to type).</p>
<h1 id="example-self-referential-struct" class="section-header"><a href="#example-self-referential-struct">Example: self-referential struct</a></h1>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">pin</span>::<span class="ident">Pin</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">marker</span>::<span class="ident">PhantomPinned</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ptr</span>::<span class="ident">NonNull</span>;

<span class="comment">// This is a self-referential struct since the slice field points to the data field.</span>
<span class="comment">// We cannot inform the compiler about that with a normal reference,</span>
<span class="comment">// since this pattern cannot be described with the usual borrowing rules.</span>
<span class="comment">// Instead we use a raw pointer, though one which is known to not be null,</span>
<span class="comment">// since we know it&#39;s pointing at the string.</span>
<span class="kw">struct</span> <span class="ident">Unmovable</span> {
    <span class="ident">data</span>: <span class="ident">String</span>,
    <span class="ident">slice</span>: <span class="ident">NonNull</span><span class="op">&lt;</span><span class="ident">String</span><span class="op">&gt;</span>,
    <span class="ident">_pin</span>: <span class="ident">PhantomPinned</span>,
}

<span class="kw">impl</span> <span class="ident">Unmovable</span> {
    <span class="comment">// To ensure the data doesn&#39;t move when the function returns,</span>
    <span class="comment">// we place it in the heap where it will stay for the lifetime of the object,</span>
    <span class="comment">// and the only way to access it would be through a pointer to it.</span>
    <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">data</span>: <span class="ident">String</span>) <span class="op">-&gt;</span> <span class="ident">Pin</span><span class="op">&lt;</span><span class="ident">Box</span><span class="op">&lt;</span><span class="self">Self</span><span class="op">&gt;&gt;</span> {
        <span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">Unmovable</span> {
            <span class="ident">data</span>,
            <span class="comment">// we only create the pointer once the data is in place</span>
            <span class="comment">// otherwise it will have already moved before we even started</span>
            <span class="ident">slice</span>: <span class="ident">NonNull</span>::<span class="ident">dangling</span>(),
            <span class="ident">_pin</span>: <span class="ident">PhantomPinned</span>,
        };
        <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">boxed</span> <span class="op">=</span> <span class="ident">Box</span>::<span class="ident">pin</span>(<span class="ident">res</span>);

        <span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> <span class="ident">NonNull</span>::<span class="ident">from</span>(<span class="kw-2">&amp;</span><span class="ident">boxed</span>.<span class="ident">data</span>);
        <span class="comment">// we know this is safe because modifying a field doesn&#39;t move the whole struct</span>
        <span class="kw">unsafe</span> {
            <span class="kw">let</span> <span class="ident">mut_ref</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="ident">Pin</span>::<span class="ident">as_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">boxed</span>);
            <span class="ident">Pin</span>::<span class="ident">get_unchecked_mut</span>(<span class="ident">mut_ref</span>).<span class="ident">slice</span> <span class="op">=</span> <span class="ident">slice</span>;
        }
        <span class="ident">boxed</span>
    }
}

<span class="kw">let</span> <span class="ident">unmoved</span> <span class="op">=</span> <span class="ident">Unmovable</span>::<span class="ident">new</span>(<span class="string">&quot;hello&quot;</span>.<span class="ident">to_string</span>());
<span class="comment">// The pointer should point to the correct location,</span>
<span class="comment">// so long as the struct hasn&#39;t moved.</span>
<span class="comment">// Meanwhile, we are free to move the pointer around.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">still_unmoved</span> <span class="op">=</span> <span class="ident">unmoved</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">still_unmoved</span>.<span class="ident">slice</span>, <span class="ident">NonNull</span>::<span class="ident">from</span>(<span class="kw-2">&amp;</span><span class="ident">still_unmoved</span>.<span class="ident">data</span>));

<span class="comment">// Since our type doesn&#39;t implement Unpin, this will fail to compile:</span>
<span class="comment">// let mut new_unmoved = Unmovable::new(&quot;world&quot;.to_string());</span>
<span class="comment">// std::mem::swap(&amp;mut *still_unmoved, &amp;mut *new_unmoved);</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Apin%3A%3APin%3B%0Ause%20std%3A%3Amarker%3A%3APhantomPinned%3B%0Ause%20std%3A%3Aptr%3A%3ANonNull%3B%0A%0A%2F%2F%20This%20is%20a%20self-referential%20struct%20since%20the%20slice%20field%20points%20to%20the%20data%20field.%0A%2F%2F%20We%20cannot%20inform%20the%20compiler%20about%20that%20with%20a%20normal%20reference%2C%0A%2F%2F%20since%20this%20pattern%20cannot%20be%20described%20with%20the%20usual%20borrowing%20rules.%0A%2F%2F%20Instead%20we%20use%20a%20raw%20pointer%2C%20though%20one%20which%20is%20known%20to%20not%20be%20null%2C%0A%2F%2F%20since%20we%20know%20it's%20pointing%20at%20the%20string.%0Astruct%20Unmovable%20%7B%0A%20%20%20%20data%3A%20String%2C%0A%20%20%20%20slice%3A%20NonNull%3CString%3E%2C%0A%20%20%20%20_pin%3A%20PhantomPinned%2C%0A%7D%0A%0Aimpl%20Unmovable%20%7B%0A%20%20%20%20%2F%2F%20To%20ensure%20the%20data%20doesn't%20move%20when%20the%20function%20returns%2C%0A%20%20%20%20%2F%2F%20we%20place%20it%20in%20the%20heap%20where%20it%20will%20stay%20for%20the%20lifetime%20of%20the%20object%2C%0A%20%20%20%20%2F%2F%20and%20the%20only%20way%20to%20access%20it%20would%20be%20through%20a%20pointer%20to%20it.%0A%20%20%20%20fn%20new(data%3A%20String)%20-%3E%20Pin%3CBox%3CSelf%3E%3E%20%7B%0A%20%20%20%20%20%20%20%20let%20res%20%3D%20Unmovable%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20we%20only%20create%20the%20pointer%20once%20the%20data%20is%20in%20place%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20otherwise%20it%20will%20have%20already%20moved%20before%20we%20even%20started%0A%20%20%20%20%20%20%20%20%20%20%20%20slice%3A%20NonNull%3A%3Adangling()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_pin%3A%20PhantomPinned%2C%0A%20%20%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%20%20%20%20let%20mut%20boxed%20%3D%20Box%3A%3Apin(res)%3B%0A%0A%20%20%20%20%20%20%20%20let%20slice%20%3D%20NonNull%3A%3Afrom(%26boxed.data)%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20we%20know%20this%20is%20safe%20because%20modifying%20a%20field%20doesn't%20move%20the%20whole%20struct%0A%20%20%20%20%20%20%20%20unsafe%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20let%20mut_ref%3A%20Pin%3C%26mut%20Self%3E%20%3D%20Pin%3A%3Aas_mut(%26mut%20boxed)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Pin%3A%3Aget_unchecked_mut(mut_ref).slice%20%3D%20slice%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20boxed%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20unmoved%20%3D%20Unmovable%3A%3Anew(%22hello%22.to_string())%3B%0A%2F%2F%20The%20pointer%20should%20point%20to%20the%20correct%20location%2C%0A%2F%2F%20so%20long%20as%20the%20struct%20hasn't%20moved.%0A%2F%2F%20Meanwhile%2C%20we%20are%20free%20to%20move%20the%20pointer%20around.%0A%23%5Ballow(unused_mut)%5D%0Alet%20mut%20still_unmoved%20%3D%20unmoved%3B%0Aassert_eq!(still_unmoved.slice%2C%20NonNull%3A%3Afrom(%26still_unmoved.data))%3B%0A%0A%2F%2F%20Since%20our%20type%20doesn't%20implement%20Unpin%2C%20this%20will%20fail%20to%20compile%3A%0A%2F%2F%20let%20mut%20new_unmoved%20%3D%20Unmovable%3A%3Anew(%22world%22.to_string())%3B%0A%2F%2F%20std%3A%3Amem%3A%3Aswap(%26mut%20*still_unmoved%2C%20%26mut%20*new_unmoved)%3B%0A%7D">Run</a></pre></div>
<h1 id="example-intrusive-doubly-linked-list" class="section-header"><a href="#example-intrusive-doubly-linked-list">Example: intrusive doubly-linked list</a></h1>
<p>In an intrusive doubly-linked list, the collection does not actually allocate
the memory for the elements itself. Allocation is controlled by the clients,
and elements can live on a stack frame that lives shorter than the collection does.</p>
<p>To make this work, every element has pointers to its predecessor and successor in
the list. Elements can only be added when they are pinned, because moving the elements
around would invalidate the pointers. Moreover, the <code>Drop</code> implementation of a linked
list element will patch the pointers of its predecessor and successor to remove itself
from the list.</p>
<p>Crucially, we have to be able to rely on <code>drop</code> being called. If an element
could be deallocated or otherwise invalidated without calling <code>drop</code>, the pointers into it
from its neighbouring elements would become invalid, which would break the data structure.</p>
<p>Therefore, pinning also comes with a <code>drop</code>-related guarantee.</p>
<h1 id="drop-guarantee" class="section-header"><a href="#drop-guarantee"><code>Drop</code> guarantee</a></h1>
<p>The purpose of pinning is to be able to rely on the placement of some data in memory.
To make this work, not just moving the data is restricted; deallocating, repurposing, or
otherwise invalidating the memory used to store the data is restricted, too.
Concretely, for pinned data you have to maintain the invariant
that <em>its memory will not get invalidated from the moment it gets pinned until
when <code>drop</code> is called</em>. Memory can be invalidated by deallocation, but also by
replacing a <a href="../../std/option/enum.Option.html#variant.Some"><code>Some(v)</code></a> by <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a>, or calling <a href="../../std/vec/struct.Vec.html#method.set_len"><code>Vec::set_len</code></a> to &quot;kill&quot; some elements
off of a vector.</p>
<p>This is exactly the kind of guarantee that the intrusive linked list from the previous
section needs to function correctly.</p>
<p>Notice that this guarantee does <em>not</em> mean that memory does not leak! It is still
completely okay not to ever call <code>drop</code> on a pinned element (e.g., you can still
call <a href="../../std/mem/fn.forget.html"><code>mem::forget</code></a> on a <code>Pin&lt;Box&lt;T&gt;&gt;</code>). In the example of the doubly-linked
list, that element would just stay in the list. However you may not free or reuse the storage
<em>without calling <code>drop</code></em>.</p>
<h1 id="drop-implementation" class="section-header"><a href="#drop-implementation"><code>Drop</code> implementation</a></h1>
<p>If your type uses pinning (such as the two examples above), you have to be careful
when implementing <code>Drop</code>. The <code>drop</code> function takes <code>&amp;mut self</code>, but this
is called <em>even if your type was previously pinned</em>! It is as if the
compiler automatically called <code>get_unchecked_mut</code>.</p>
<p>This can never cause a problem in safe code because implementing a type that relies on pinning
requires unsafe code, but be aware that deciding to make use of pinning
in your type (for example by implementing some operation on <code>Pin&lt;&amp;[mut] Self&gt;</code>)
has consequences for your <code>Drop</code> implementation as well: if an element
of your type could have been pinned, you must treat Drop as implicitly taking
<code>Pin&lt;&amp;mut Self&gt;</code>.</p>
<p>In particular, if your type is <code>#[repr(packed)]</code>, the compiler will automatically
move fields around to be able to drop them. As a consequence, you cannot use
pinning with a <code>#[repr(packed)]</code> type.</p>
<h1 id="projections-and-structural-pinning" class="section-header"><a href="#projections-and-structural-pinning">Projections and Structural Pinning</a></h1>
<p>One interesting question arises when considering the interaction of pinning and
the fields of a struct. When can a struct have a &quot;pinning projection&quot;, i.e.,
an operation with type <code>fn(Pin&lt;&amp;[mut] Struct&gt;) -&gt; Pin&lt;&amp;[mut] Field&gt;</code>?
In a similar vein, when can a generic wrapper type (such as <code>Vec&lt;T&gt;</code>, <code>Box&lt;T&gt;</code>, or <code>RefCell&lt;T&gt;</code>)
have an operation with type <code>fn(Pin&lt;&amp;[mut] Wrapper&lt;T&gt;&gt;) -&gt; Pin&lt;&amp;[mut] T&gt;</code>?</p>
<p>Having a pinning projection for some field means that pinning is &quot;structural&quot;:
when the wrapper is pinned, the field must be considered pinned, too.
After all, the pinning projection lets us get a <code>Pin&lt;&amp;[mut] Field&gt;</code>.</p>
<p>However, structural pinning comes with a few extra requirements, so not all
wrappers can be structural and hence not all wrappers can offer pinning projections:</p>
<ol>
<li>
<p>The wrapper must only be <a href="../../std/marker/trait.Unpin.html"><code>Unpin</code></a> if all the structural fields are
<code>Unpin</code>. This is the default, but <code>Unpin</code> is a safe trait, so as the author of
the wrapper it is your responsibility <em>not</em> to add something like
<code>impl&lt;T&gt; Unpin for Wrapper&lt;T&gt;</code>. (Notice that adding a projection operation
requires unsafe code, so the fact that <code>Unpin</code> is a safe trait  does not break
the principle that you only have to worry about any of this if you use <code>unsafe</code>.)</p>
</li>
<li>
<p>The destructor of the wrapper must not move structural fields out of its argument. This
is the exact point that was raised in the <a href="#drop-implementation">previous section</a>: <code>drop</code> takes
<code>&amp;mut self</code>, but the wrapper (and hence its fields) might have been pinned before.
You have to guarantee that you do not move a field inside your <code>Drop</code> implementation.
In particular, as explained previously, this means that your wrapper type must <em>not</em>
be <code>#[repr(packed)]</code>.</p>
</li>
<li>
<p>You must make sure that you uphold the <a href="#drop-guarantee"><code>Drop</code> guarantee</a>:
once your wrapper is pinned, the memory that contains the
content is not overwritten or deallocated without calling the content's destructors.
This can be tricky, as witnessed by <code>VecDeque&lt;T&gt;</code>: the destructor of <code>VecDeque&lt;T&gt;</code> can fail
to call <code>drop</code> on all elements if one of the destructors panics. This violates the
<code>Drop</code> guarantee, because it can lead to elements being deallocated without
their destructor being called. (<code>VecDeque</code> has no pinning projections, so this
does not cause unsoundness.)</p>
</li>
<li>
<p>You must not offer any other operations that could lead to data being moved out of
the fields when your type is pinned. For example, if the wrapper contains an
<code>Option&lt;T&gt;</code> and there is a <code>take</code>-like operation with type
<code>fn(Pin&lt;&amp;mut Wrapper&lt;T&gt;&gt;) -&gt; Option&lt;T&gt;</code>,
that operation can be used to move a <code>T</code> out of a pinned <code>Wrapper&lt;T&gt;</code> -- which means
pinning cannot be structural.</p>
<p>For a more complex example of moving data out of a pinned type, imagine if <code>RefCell&lt;T&gt;</code>
had a method <code>fn get_pin_mut(self: Pin&lt;&amp;mut Self&gt;) -&gt; Pin&lt;&amp;mut T&gt;</code>.
Then we could do the following:</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="kw">fn</span> <span class="ident">exploit_ref_cell</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>(<span class="ident">rc</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">RefCell</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;&gt;</span>) {
    { <span class="kw">let</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">as_mut</span>().<span class="ident">get_pin_mut</span>(); } <span class="comment">// Here we get pinned access to the `T`.</span>
    <span class="kw">let</span> <span class="ident">rc_shr</span>: <span class="kw-2">&amp;</span><span class="ident">RefCell</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">into_ref</span>().<span class="ident">get_ref</span>();
    <span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">rc_shr</span>.<span class="ident">borrow_mut</span>();
    <span class="kw">let</span> <span class="ident">content</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="kw-2">*</span><span class="ident">b</span>; <span class="comment">// And here we have `&amp;mut T` to the same data.</span>
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Afn%20exploit_ref_cell%3CT%3E(rc%3A%20Pin%3C%26mut%20RefCell%3CT%3E%3E)%20%7B%0A%20%20%20%20%7B%20let%20p%20%3D%20rc.as_mut().get_pin_mut()%3B%20%7D%20%2F%2F%20Here%20we%20get%20pinned%20access%20to%20the%20%60T%60.%0A%20%20%20%20let%20rc_shr%3A%20%26RefCell%3CT%3E%20%3D%20rc.into_ref().get_ref()%3B%0A%20%20%20%20let%20b%20%3D%20rc_shr.borrow_mut()%3B%0A%20%20%20%20let%20content%20%3D%20%26mut%20*b%3B%20%2F%2F%20And%20here%20we%20have%20%60%26mut%20T%60%20to%20the%20same%20data.%0A%7D%0A%7D">Run</a></pre></div>
<p>This is catastrophic, it means we can first pin the content of the <code>RefCell&lt;T&gt;</code>
(using <code>RefCell::get_pin_mut</code>) and then move that content using the mutable
reference we got later.</p>
</li>
</ol>
<p>For a type like <code>Vec&lt;T&gt;</code>, both possibilites (structural pinning or not) make sense,
and the choice is up to the author. A <code>Vec&lt;T&gt;</code> with structural pinning could
have <code>get_pin</code>/<code>get_pin_mut</code> projections. However, it could <em>not</em> allow calling
<code>pop</code> on a pinned <code>Vec&lt;T&gt;</code> because that would move the (structurally pinned) contents!
Nor could it allow <code>push</code>, which might reallocate and thus also move the contents.
A <code>Vec&lt;T&gt;</code> without structural pinning could <code>impl&lt;T&gt; Unpin for Vec&lt;T&gt;</code>, because the contents
are never pinned and the <code>Vec&lt;T&gt;</code> itself is fine with being moved as well.</p>
<p>In the standard library, pointer types generally do not have structural pinning,
and thus they do not offer pinning projections. This is why <code>Box&lt;T&gt;: Unpin</code> holds for all <code>T</code>.
It makes sense to do this for pointer types, because moving the <code>Box&lt;T&gt;</code>
does not actually move the <code>T</code>: the <code>Box&lt;T&gt;</code> can be freely movable (aka <code>Unpin</code>) even if the <code>T</code>
is not. In fact, even <code>Pin&lt;Box&lt;T&gt;&gt;</code> and <code>Pin&lt;&amp;mut T&gt;</code> are always <code>Unpin</code> themselves,
for the same reason: their contents (the <code>T</code>) are pinned, but the pointers themselves
can be moved without moving the pinned data. For both <code>Box&lt;T&gt;</code> and <code>Pin&lt;Box&lt;T&gt;&gt;</code>,
whether the content is pinned is entirely independent of whether the pointer is
pinned, meaning pinning is <em>not</em> structural.</p>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table><tr class='module-item'><td><a class="struct" href="struct.Pin.html" title='core::pin::Pin struct'>Pin</a></td><td class='docblock-short'><p>A pinned pointer.</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "core";</script><script src="../../aliases.js"></script><script src="../../main1.35.0.js"></script><script defer src="../../search-index.js"></script></body></html>