blob: 3d6b768887393b13e28765d1e50ea7b43752f5c2 [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 describing smart reference counted pointers."><meta name="keywords" content="rust, rustlang, rust-lang, RefCnt"><title>RefCnt in arc_swap - 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="../arc_swap/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="../arc_swap/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">RefCnt</a></h2><div class="sidebar-elems"><section><h3><a href="#required-associated-types">Required Associated Types</a></h3><ul class="block"><li><a href="#associatedtype.Base">Base</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.as_ptr">as_ptr</a></li><li><a href="#tymethod.from_ptr">from_ptr</a></li><li><a href="#tymethod.into_ptr">into_ptr</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.dec">dec</a></li><li><a href="#method.inc">inc</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-RefCnt-for-Arc%3CT%3E">Arc&lt;T&gt;</a></li><li><a href="#impl-RefCnt-for-Option%3CT%3E">Option&lt;T&gt;</a></li><li><a href="#impl-RefCnt-for-Rc%3CT%3E">Rc&lt;T&gt;</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In arc_swap</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">arc_swap</a>::<wbr><a class="trait" href="#">RefCnt</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/arc_swap/ref_cnt.rs.html#38-88">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 RefCnt: Clone {
type <a href="#associatedtype.Base" class="associatedtype">Base</a>;
fn <a href="#tymethod.into_ptr" class="fnname">into_ptr</a>(me: Self) -&gt; *mut Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a>;
<span class="item-spacer"></span> fn <a href="#tymethod.as_ptr" class="fnname">as_ptr</a>(me: &amp;Self) -&gt; *mut Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a>;
<span class="item-spacer"></span> unsafe fn <a href="#tymethod.from_ptr" class="fnname">from_ptr</a>(ptr: *const Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a>) -&gt; Self;
fn <a href="#method.inc" class="fnname">inc</a>(me: &amp;Self) -&gt; *mut Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a> { ... }
<span class="item-spacer"></span> unsafe fn <a href="#method.dec" class="fnname">dec</a>(ptr: *const Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a>) { ... }
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A trait describing smart reference counted pointers.</p>
<p>Note that in a way [<code>Option&lt;Arc&lt;T&gt;&gt;</code>][Option] is also a smart reference counted pointer, just
one that can hold NULL.</p>
<p>The trait is unsafe, because a wrong implementation will break the <a href="struct.ArcSwapAny.html">ArcSwapAny</a>
implementation and lead to UB.</p>
<p>This is not actually expected for downstream crate to implement, this is just means to reuse
code for <a href="std::sync::Arc">Arc</a> and [<code>Option&lt;Arc&gt;</code>][Option] variants. However, it is theoretically possible (if
you have your own <a href="std::sync::Arc">Arc</a> implementation).</p>
<p>It is also implemented for <a href="std::rc::Rc">Rc</a>, but that is not considered very useful (because the
<a href="struct.ArcSwapAny.html">ArcSwapAny</a> is not <code>Send</code> or <code>Sync</code>, therefore there’s very little advantage for it to be
atomic).</p>
<h2 id="safety"><a href="#safety">Safety</a></h2>
<p>Aside from the obvious properties (like that incrementing and decrementing a reference count
cancel each out and that having less references tracked than how many things actually point to
the value is fine as long as the count doesn’t drop to 0), it also must satisfy that if two
pointers have the same value, they point to the same object. This is specifically not true for
ZSTs, but it is true for <code>Arc</code>s of ZSTs, because they have the reference counts just after the
value. It would be fine to point to a type-erased version of the same object, though (if one
could use this trait with unsized types in the first place).</p>
<p>Furthermore, the type should be Pin (eg. if the type is cloned or moved, it should still
point/deref to the same place in memory).</p>
</div></details><h2 id="required-associated-types" class="small-section-header">Required Associated Types<a href="#required-associated-types" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="associatedtype.Base" class="method has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#40">source</a><h4 class="code-header">type <a href="#associatedtype.Base" class="associatedtype">Base</a></h4></section></summary><div class="docblock"><p>The base type the pointer points to.</p>
</div></details></div><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.into_ptr" class="method has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#49">source</a><h4 class="code-header">fn <a href="#tymethod.into_ptr" class="fnname">into_ptr</a>(me: Self) -&gt; *mut Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a></h4></section></summary><div class="docblock"><p>Converts the smart pointer into a raw pointer, without affecting the reference count.</p>
<p>This can be seen as kind of freezing the pointer ‒ it’ll be later converted back using
<a href="#method.from_ptr"><code>from_ptr</code></a>.</p>
<p>The pointer must point to the value stored (and the value must be the same as one returned
by <a href="#method.as_ptr"><code>as_ptr</code></a>.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.as_ptr" class="method has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#54">source</a><h4 class="code-header">fn <a href="#tymethod.as_ptr" class="fnname">as_ptr</a>(me: &amp;Self) -&gt; *mut Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a></h4></section></summary><div class="docblock"><p>Provides a view into the smart pointer as a raw pointer.</p>
<p>This must not affect the reference count ‒ the pointer is only borrowed.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.from_ptr" class="method has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#67">source</a><h4 class="code-header">unsafe fn <a href="#tymethod.from_ptr" class="fnname">from_ptr</a>(ptr: *const Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a>) -&gt; Self</h4></section></summary><div class="docblock"><p>Converts a raw pointer back into the smart pointer, without affecting the reference count.</p>
<p>This is only called on values previously returned by <a href="#method.into_ptr"><code>into_ptr</code></a>.
However, it is not guaranteed to be 1:1 relation ‒ <code>from_ptr</code> may be called more times than
<code>into_ptr</code> temporarily provided the reference count never drops under 1 during that time
(the implementation sometimes owes a reference). These extra pointers will either be
converted back using <code>into_ptr</code> or forgotten.</p>
<h5 id="safety-1"><a href="#safety-1">Safety</a></h5>
<p>This must not be called by code outside of this crate.</p>
</div></details></div><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.inc" class="method has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#72-74">source</a><h4 class="code-header">fn <a href="#method.inc" class="fnname">inc</a>(me: &amp;Self) -&gt; *mut Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a></h4></section></summary><div class="docblock"><p>Increments the reference count by one.</p>
<p>Return the pointer to the inner thing as a side effect.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.dec" class="method has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#85-87">source</a><h4 class="code-header">unsafe fn <a href="#method.dec" class="fnname">dec</a>(ptr: *const Self::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a>)</h4></section></summary><div class="docblock"><p>Decrements the reference count by one.</p>
<p>Note this is called on a raw pointer (one previously returned by
<a href="#method.into_ptr"><code>into_ptr</code></a>. This may lead to dropping of the reference count to 0 and
destruction of the internal pointer.</p>
<h5 id="safety-2"><a href="#safety-2">Safety</a></h5>
<p>This must not be called by code outside of this crate.</p>
</div></details></div><h2 id="foreign-impls" class="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-RefCnt-for-Arc%3CT%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#90-123">source</a><a href="#impl-RefCnt-for-Arc%3CT%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.RefCnt.html" title="trait arc_swap::RefCnt">RefCnt</a> for Arc&lt;T&gt;</h3></section></summary><div class="impl-items"><section id="associatedtype.Base-1" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Base-1" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Base" class="associatedtype">Base</a> = T</h4></section><section id="method.into_ptr" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#92-94">source</a><a href="#method.into_ptr" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.into_ptr" class="fnname">into_ptr</a>(me: Arc&lt;T&gt;) -&gt; *mut T</h4></section><section id="method.as_ptr" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#95-119">source</a><a href="#method.as_ptr" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.as_ptr" class="fnname">as_ptr</a>(me: &amp;Arc&lt;T&gt;) -&gt; *mut T</h4></section><section id="method.from_ptr" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#120-122">source</a><a href="#method.from_ptr" class="anchor"></a><h4 class="code-header">unsafe fn <a href="#tymethod.from_ptr" class="fnname">from_ptr</a>(ptr: *const T) -&gt; Arc&lt;T&gt;</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-RefCnt-for-Rc%3CT%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#125-158">source</a><a href="#impl-RefCnt-for-Rc%3CT%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.RefCnt.html" title="trait arc_swap::RefCnt">RefCnt</a> for Rc&lt;T&gt;</h3></section></summary><div class="impl-items"><section id="associatedtype.Base-2" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Base-2" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Base" class="associatedtype">Base</a> = T</h4></section><section id="method.into_ptr-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#127-129">source</a><a href="#method.into_ptr-1" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.into_ptr" class="fnname">into_ptr</a>(me: Rc&lt;T&gt;) -&gt; *mut T</h4></section><section id="method.as_ptr-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#130-154">source</a><a href="#method.as_ptr-1" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.as_ptr" class="fnname">as_ptr</a>(me: &amp;Rc&lt;T&gt;) -&gt; *mut T</h4></section><section id="method.from_ptr-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#155-157">source</a><a href="#method.from_ptr-1" class="anchor"></a><h4 class="code-header">unsafe fn <a href="#tymethod.from_ptr" class="fnname">from_ptr</a>(ptr: *const T) -&gt; Rc&lt;T&gt;</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-RefCnt-for-Option%3CT%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#160-175">source</a><a href="#impl-RefCnt-for-Option%3CT%3E" class="anchor"></a><h3 class="code-header">impl&lt;T:&nbsp;<a class="trait" href="trait.RefCnt.html" title="trait arc_swap::RefCnt">RefCnt</a>&gt; <a class="trait" href="trait.RefCnt.html" title="trait arc_swap::RefCnt">RefCnt</a> for Option&lt;T&gt;</h3></section></summary><div class="impl-items"><section id="associatedtype.Base-3" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Base-3" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Base" class="associatedtype">Base</a> = &lt;T as <a class="trait" href="trait.RefCnt.html" title="trait arc_swap::RefCnt">RefCnt</a>&gt;::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a></h4></section><section id="method.into_ptr-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#162-164">source</a><a href="#method.into_ptr-2" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.into_ptr" class="fnname">into_ptr</a>(me: Option&lt;T&gt;) -&gt; *mut T::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a></h4></section><section id="method.as_ptr-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#165-167">source</a><a href="#method.as_ptr-2" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.as_ptr" class="fnname">as_ptr</a>(me: &amp;Option&lt;T&gt;) -&gt; *mut T::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a></h4></section><section id="method.from_ptr-2" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/arc_swap/ref_cnt.rs.html#168-174">source</a><a href="#method.from_ptr-2" class="anchor"></a><h4 class="code-header">unsafe fn <a href="#tymethod.from_ptr" class="fnname">from_ptr</a>(ptr: *const T::<a class="associatedtype" href="trait.RefCnt.html#associatedtype.Base" title="type arc_swap::RefCnt::Base">Base</a>) -&gt; Option&lt;T&gt;</h4></section></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/arc_swap/ref_cnt/trait.RefCnt.js" data-ignore-extern-crates="alloc,core" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="arc_swap" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>