blob: c7c3b65a8f225548c9295b3c7082b99a9d1b04b3 [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="An attribute used for custom implementations of [`Drop`]."><meta name="keywords" content="rust, rustlang, rust-lang, pinned_drop"><title>pinned_drop 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 attr"><!--[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><div class="sidebar-elems"><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">Attribute Macro <a href="index.html">pin_project</a>::<wbr><a class="attr" href="#">pinned_drop</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_internal/lib.rs.html#573">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 attr"><code>#[pinned_drop]</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An attribute used for custom implementations of <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="Drop"><code>Drop</code></a>.</p>
<p>This attribute is used in conjunction with the <code>PinnedDrop</code> argument to
the <a href="attr.pin_project.html" title="pin_project"><code>#[pin_project]</code></a> attribute.</p>
<p>The impl block annotated with this attribute acts just like a normal
<a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="Drop"><code>Drop</code></a> impl, except for the following two:</p>
<ul>
<li><code>drop</code> method takes <a href="https://doc.rust-lang.org/nightly/core/pin/struct.Pin.html"><code>Pin</code></a><code>&lt;&amp;mut Self&gt;</code></li>
<li>Name of the trait is <code>PinnedDrop</code>.</li>
</ul>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub trait </span>PinnedDrop {
<span class="kw">fn </span>drop(<span class="self">self</span>: Pin&lt;<span class="kw-2">&amp;mut </span><span class="self">Self</span>&gt;);
}</code></pre></div>
<p><code>#[pin_project]</code> implements the actual <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="Drop"><code>Drop</code></a> trait via <code>PinnedDrop</code> you
implemented. To drop a type that implements <code>PinnedDrop</code>, use the <a href="https://doc.rust-lang.org/nightly/core/mem/fn.drop.html" title="drop"><code>drop</code></a>
function just like dropping a type that directly implements <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="Drop"><code>Drop</code></a>.</p>
<p>In particular, it will never be called more than once, just like
<a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop" title="Drop::drop"><code>Drop::drop</code></a>.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<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, pinned_drop};
<span class="attribute">#[pin_project(PinnedDrop)]
</span><span class="kw">struct </span>PrintOnDrop {
<span class="attribute">#[pin]
</span>field: u8,
}
<span class="attribute">#[pinned_drop]
</span><span class="kw">impl </span>PinnedDrop <span class="kw">for </span>PrintOnDrop {
<span class="kw">fn </span>drop(<span class="self">self</span>: Pin&lt;<span class="kw-2">&amp;mut </span><span class="self">Self</span>&gt;) {
<span class="macro">println!</span>(<span class="string">&quot;Dropping: {}&quot;</span>, <span class="self">self</span>.field);
}
}
<span class="kw">fn </span>main() {
<span class="kw">let </span>_x = PrintOnDrop { field: <span class="number">50 </span>};
}</code></pre></div>
<p>See also <a href="attr.pin_project.html#pinned_drop">“pinned-drop” section of <code>#[pin_project]</code> attribute</a>.</p>
<h2 id="why-pinned_drop-attribute-is-needed"><a href="#why-pinned_drop-attribute-is-needed">Why <code>#[pinned_drop]</code> attribute is needed?</a></h2>
<p>Implementing <code>PinnedDrop::drop</code> is safe, but calling it is not safe.
This is because destructors can be called multiple times in safe code and
<a href="https://github.com/rust-lang/rust/pull/62360">double dropping is unsound</a>.</p>
<p>Ideally, it would be desirable to be able to forbid manual calls in
the same way as <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop" title="Drop::drop"><code>Drop::drop</code></a>, but the library cannot do it. So, by using
macros and replacing them with private traits like the following,
this crate prevent users from calling <code>PinnedDrop::drop</code> in safe code.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub trait </span>PinnedDrop {
<span class="kw">unsafe fn </span>drop(<span class="self">self</span>: Pin&lt;<span class="kw-2">&amp;mut </span><span class="self">Self</span>&gt;);
}</code></pre></div>
<p>This allows implementing <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="Drop"><code>Drop</code></a> safely using <code>#[pinned_drop]</code>.
Also by using the <a href="https://doc.rust-lang.org/nightly/core/mem/fn.drop.html" title="drop"><code>drop</code></a> function just like dropping a type that directly
implements <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="Drop"><code>Drop</code></a>, can drop safely a type that implements <code>PinnedDrop</code>.</p>
</div></details></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>