You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/doc/owning_ref/index.html

240 lines
38 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!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 `owning_ref` crate."><meta name="keywords" content="rust, rustlang, rust-lang, owning_ref"><title>owning_ref - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.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='../owning_ref/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Crate owning_ref</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all owning_ref's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'owning_ref', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.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="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><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/owning_ref/lib.rs.html#1-1918' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>owning_ref</a></span></h1><div class='docblock'><h1 id="an-owning-reference" class="section-header"><a href="#an-owning-reference">An owning reference.</a></h1>
<p>This crate provides the <em>owning reference</em> types <code>OwningRef</code> and <code>OwningRefMut</code>
that enables it to bundle a reference together with the owner of the data it points to.
This allows moving and dropping of a <code>OwningRef</code> without needing to recreate the reference.</p>
<p>This can sometimes be useful because Rust borrowing rules normally prevent
moving a type that has been moved from. For example, this kind of code gets rejected:</p>
<div class='information'><div class='tooltip ignore'><span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
<span class="kw">fn</span> <span class="ident">return_owned_and_referenced</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span>() <span class="op">-</span><span class="op">&gt;</span> (<span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span>, <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> [<span class="ident">u8</span>]) {
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];
<span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">v</span>[<span class="number">1</span>..<span class="number">3</span>];
(<span class="ident">v</span>, <span class="ident">s</span>)
}</pre></div>
<p>Even though, from a memory-layout point of view, this can be entirely safe
if the new location of the vector still lives longer than the lifetime <code>'a</code>
of the reference because the backing allocation of the vector does not change.</p>
<p>This library enables this safe usage by keeping the owner and the reference
bundled together in a wrapper type that ensure that lifetime constraint:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">fn</span> <span class="ident">return_owned_and_referenced</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="ident">OwningRef</span><span class="op">&lt;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span>, [<span class="ident">u8</span>]<span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];
<span class="kw">let</span> <span class="ident">or</span> <span class="op">=</span> <span class="ident">OwningRef</span>::<span class="ident">new</span>(<span class="ident">v</span>);
<span class="kw">let</span> <span class="ident">or</span> <span class="op">=</span> <span class="ident">or</span>.<span class="ident">map</span>(<span class="op">|</span><span class="ident">v</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">v</span>[<span class="number">1</span>..<span class="number">3</span>]);
<span class="ident">or</span>
}</pre></div>
<p>It works by requiring owner types to dereference to stable memory locations
and preventing mutable access to root containers, which in practice requires heap allocation
as provided by <code>Box&lt;T&gt;</code>, <code>Rc&lt;T&gt;</code>, etc.</p>
<p>Also provided are typedefs for common owner type combinations,
which allow for less verbose type signatures. For example, <code>BoxRef&lt;T&gt;</code> instead of <code>OwningRef&lt;Box&lt;T&gt;, T&gt;</code>.</p>
<p>The crate also provides the more advanced <code>OwningHandle</code> type,
which allows more freedom in bundling a dependent handle object
along with the data it depends on, at the cost of some unsafe needed in the API.
See the documentation around <code>OwningHandle</code> for more details.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1><h2 id="basics" class="section-header"><a href="#basics">Basics</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">BoxRef</span>;
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="comment">// Create an array owned by a Box.</span>
<span class="kw">let</span> <span class="ident">arr</span> <span class="op">=</span> <span class="ident">Box</span>::<span class="ident">new</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]) <span class="kw">as</span> <span class="ident">Box</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span>;
<span class="comment">// Transfer into a BoxRef.</span>
<span class="kw">let</span> <span class="ident">arr</span>: <span class="ident">BoxRef</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span> <span class="op">=</span> <span class="ident">BoxRef</span>::<span class="ident">new</span>(<span class="ident">arr</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">arr</span>, <span class="kw-2">&amp;</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
<span class="comment">// We can slice the array without losing ownership or changing type.</span>
<span class="kw">let</span> <span class="ident">arr</span>: <span class="ident">BoxRef</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span> <span class="op">=</span> <span class="ident">arr</span>.<span class="ident">map</span>(<span class="op">|</span><span class="ident">arr</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">arr</span>[<span class="number">1</span>..<span class="number">3</span>]);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">arr</span>, <span class="kw-2">&amp;</span>[<span class="number">2</span>, <span class="number">3</span>]);
<span class="comment">// Also works for Arc, Rc, String and Vec!</span>
}</pre></div>
<h2 id="caching-a-reference-to-a-struct-field" class="section-header"><a href="#caching-a-reference-to-a-struct-field">Caching a reference to a struct field</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">BoxRef</span>;
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">struct</span> <span class="ident">Foo</span> {
<span class="ident">tag</span>: <span class="ident">u32</span>,
<span class="ident">x</span>: <span class="ident">u16</span>,
<span class="ident">y</span>: <span class="ident">u16</span>,
<span class="ident">z</span>: <span class="ident">u16</span>,
}
<span class="kw">let</span> <span class="ident">foo</span> <span class="op">=</span> <span class="ident">Foo</span> { <span class="ident">tag</span>: <span class="number">1</span>, <span class="ident">x</span>: <span class="number">100</span>, <span class="ident">y</span>: <span class="number">200</span>, <span class="ident">z</span>: <span class="number">300</span> };
<span class="kw">let</span> <span class="ident">or</span> <span class="op">=</span> <span class="ident">BoxRef</span>::<span class="ident">new</span>(<span class="ident">Box</span>::<span class="ident">new</span>(<span class="ident">foo</span>)).<span class="ident">map</span>(<span class="op">|</span><span class="ident">foo</span><span class="op">|</span> {
<span class="kw">match</span> <span class="ident">foo</span>.<span class="ident">tag</span> {
<span class="number">0</span> <span class="op">=</span><span class="op">&gt;</span> <span class="kw-2">&amp;</span><span class="ident">foo</span>.<span class="ident">x</span>,
<span class="number">1</span> <span class="op">=</span><span class="op">&gt;</span> <span class="kw-2">&amp;</span><span class="ident">foo</span>.<span class="ident">y</span>,
<span class="number">2</span> <span class="op">=</span><span class="op">&gt;</span> <span class="kw-2">&amp;</span><span class="ident">foo</span>.<span class="ident">z</span>,
<span class="kw">_</span> <span class="op">=</span><span class="op">&gt;</span> <span class="macro">panic</span><span class="macro">!</span>(),
}
});
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">or</span>, <span class="number">200</span>);
}</pre></div>
<h2 id="caching-a-reference-to-an-entry-in-a-vector" class="section-header"><a href="#caching-a-reference-to-an-entry-in-a-vector">Caching a reference to an entry in a vector</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">VecRef</span>;
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="ident">VecRef</span>::<span class="ident">new</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]).<span class="ident">map</span>(<span class="op">|</span><span class="ident">v</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">v</span>[<span class="number">3</span>]);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">v</span>, <span class="number">4</span>);
}</pre></div>
<h2 id="caching-a-subslice-of-a-string" class="section-header"><a href="#caching-a-subslice-of-a-string">Caching a subslice of a String</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">StringRef</span>;
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">StringRef</span>::<span class="ident">new</span>(<span class="string">&quot;hello world&quot;</span>.<span class="ident">to_owned</span>())
.<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="ident">s</span>.<span class="ident">split</span>(<span class="string">&#39; &#39;</span>).<span class="ident">nth</span>(<span class="number">1</span>).<span class="ident">unwrap</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">s</span>, <span class="string">&quot;world&quot;</span>);
}</pre></div>
<h2 id="reference-counted-slices-that-share-ownership-of-the-backing-storage" class="section-header"><a href="#reference-counted-slices-that-share-ownership-of-the-backing-storage">Reference counted slices that share ownership of the backing storage</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">RcRef</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">rc</span>::<span class="ident">Rc</span>;
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">rc</span>: <span class="ident">RcRef</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span> <span class="op">=</span> <span class="ident">RcRef</span>::<span class="ident">new</span>(<span class="ident">Rc</span>::<span class="ident">new</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]) <span class="kw">as</span> <span class="ident">Rc</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">rc</span>, <span class="kw-2">&amp;</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
<span class="kw">let</span> <span class="ident">rc_a</span>: <span class="ident">RcRef</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">clone</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">s</span>[<span class="number">0</span>..<span class="number">2</span>]);
<span class="kw">let</span> <span class="ident">rc_b</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">clone</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">s</span>[<span class="number">1</span>..<span class="number">3</span>]);
<span class="kw">let</span> <span class="ident">rc_c</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">clone</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">s</span>[<span class="number">2</span>..<span class="number">4</span>]);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">rc_a</span>, <span class="kw-2">&amp;</span>[<span class="number">1</span>, <span class="number">2</span>]);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">rc_b</span>, <span class="kw-2">&amp;</span>[<span class="number">2</span>, <span class="number">3</span>]);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">rc_c</span>, <span class="kw-2">&amp;</span>[<span class="number">3</span>, <span class="number">4</span>]);
<span class="kw">let</span> <span class="ident">rc_c_a</span> <span class="op">=</span> <span class="ident">rc_c</span>.<span class="ident">clone</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">s</span>[<span class="number">1</span>]);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">rc_c_a</span>, <span class="kw-2">&amp;</span><span class="number">4</span>);
}</pre></div>
<h2 id="atomic-reference-counted-slices-that-share-ownership-of-the-backing-storage" class="section-header"><a href="#atomic-reference-counted-slices-that-share-ownership-of-the-backing-storage">Atomic reference counted slices that share ownership of the backing storage</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">ArcRef</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">Arc</span>;
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
<span class="kw">fn</span> <span class="ident">par_sum</span>(<span class="ident">rc</span>: <span class="ident">ArcRef</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">i32</span> {
<span class="kw">if</span> <span class="ident">rc</span>.<span class="ident">len</span>() <span class="op">=</span><span class="op">=</span> <span class="number">0</span> {
<span class="kw">return</span> <span class="number">0</span>;
} <span class="kw">else</span> <span class="kw">if</span> <span class="ident">rc</span>.<span class="ident">len</span>() <span class="op">=</span><span class="op">=</span> <span class="number">1</span> {
<span class="kw">return</span> <span class="ident">rc</span>[<span class="number">0</span>];
}
<span class="kw">let</span> <span class="ident">mid</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">len</span>() <span class="op">/</span> <span class="number">2</span>;
<span class="kw">let</span> <span class="ident">left</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">clone</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">s</span>[..<span class="ident">mid</span>]);
<span class="kw">let</span> <span class="ident">right</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">s</span>[<span class="ident">mid</span>..]);
<span class="kw">let</span> <span class="ident">left</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">par_sum</span>(<span class="ident">left</span>));
<span class="kw">let</span> <span class="ident">right</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">par_sum</span>(<span class="ident">right</span>));
<span class="ident">left</span>.<span class="ident">join</span>().<span class="ident">unwrap</span>() <span class="op">+</span> <span class="ident">right</span>.<span class="ident">join</span>().<span class="ident">unwrap</span>()
}
<span class="kw">let</span> <span class="ident">rc</span>: <span class="ident">Arc</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
<span class="kw">let</span> <span class="ident">rc</span>: <span class="ident">ArcRef</span><span class="op">&lt;</span>[<span class="ident">i32</span>]<span class="op">&gt;</span> <span class="op">=</span> <span class="ident">rc</span>.<span class="ident">into</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">par_sum</span>(<span class="ident">rc</span>), <span class="number">10</span>);
}</pre></div>
<h2 id="references-into-raii-locks" class="section-header"><a href="#references-into-raii-locks">References into RAII locks</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">RefRef</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">cell</span>::{<span class="ident">RefCell</span>, <span class="ident">Ref</span>};
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">refcell</span> <span class="op">=</span> <span class="ident">RefCell</span>::<span class="ident">new</span>((<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>));
<span class="comment">// Also works with Mutex and RwLock</span>
<span class="kw">let</span> <span class="ident">refref</span> <span class="op">=</span> {
<span class="kw">let</span> <span class="ident">refref</span> <span class="op">=</span> <span class="ident">RefRef</span>::<span class="ident">new</span>(<span class="ident">refcell</span>.<span class="ident">borrow</span>()).<span class="ident">map</span>(<span class="op">|</span><span class="ident">x</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="ident">x</span>.<span class="number">3</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">refref</span>, <span class="number">4</span>);
<span class="comment">// We move the RAII lock and the reference to one of</span>
<span class="comment">// the subfields in the data it guards here:</span>
<span class="ident">refref</span>
};
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">refref</span>, <span class="number">4</span>);
<span class="ident">drop</span>(<span class="ident">refref</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">refcell</span>.<span class="ident">borrow</span>(), (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>));
}</pre></div>
<h2 id="mutable-reference" class="section-header"><a href="#mutable-reference">Mutable reference</a></h2>
<p>When the owned container implements <code>DerefMut</code>, it is also possible to make
a <em>mutable owning reference</em>. (E.g. with <code>Box</code>, <code>RefMut</code>, <code>MutexGuard</code>)</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">owning_ref</span>;
<span class="kw">use</span> <span class="ident">owning_ref</span>::<span class="ident">RefMutRefMut</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">cell</span>::{<span class="ident">RefCell</span>, <span class="ident">RefMut</span>};
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">refcell</span> <span class="op">=</span> <span class="ident">RefCell</span>::<span class="ident">new</span>((<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>));
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">refmut_refmut</span> <span class="op">=</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">refmut_refmut</span> <span class="op">=</span> <span class="ident">RefMutRefMut</span>::<span class="ident">new</span>(<span class="ident">refcell</span>.<span class="ident">borrow_mut</span>()).<span class="ident">map_mut</span>(<span class="op">|</span><span class="ident">x</span><span class="op">|</span> <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">x</span>.<span class="number">3</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">refmut_refmut</span>, <span class="number">4</span>);
<span class="kw-2">*</span><span class="ident">refmut_refmut</span> <span class="kw-2">*</span><span class="op">=</span> <span class="number">2</span>;
<span class="ident">refmut_refmut</span>
};
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">refmut_refmut</span>, <span class="number">8</span>);
<span class="kw-2">*</span><span class="ident">refmut_refmut</span> <span class="kw-2">*</span><span class="op">=</span> <span class="number">2</span>;
<span class="ident">drop</span>(<span class="ident">refmut_refmut</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">refcell</span>.<span class="ident">borrow</span>(), (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">16</span>));
}</pre></div>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table><tr class='module-item'><td><a class="struct" href="struct.OwningHandle.html" title='owning_ref::OwningHandle struct'>OwningHandle</a></td><td class='docblock-short'><p><code>OwningHandle</code> is a complement to <code>OwningRef</code>. Where <code>OwningRef</code> allows
consumers to pass around an owned object and a dependent reference,
<code>OwningHandle</code> contains an owned object and a dependent <em>object</em>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.OwningRef.html" title='owning_ref::OwningRef struct'>OwningRef</a></td><td class='docblock-short'><p>An owning reference.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.OwningRefMut.html" title='owning_ref::OwningRefMut struct'>OwningRefMut</a></td><td class='docblock-short'><p>An mutable owning reference.</p>
</td></tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table><tr class='module-item'><td><a class="trait" href="trait.CloneStableAddress.html" title='owning_ref::CloneStableAddress trait'>CloneStableAddress</a></td><td class='docblock-short'><p>An unsafe marker trait for types where clones deref to the same address. This has all the requirements of StableDeref, and additionally requires that after calling clone(), both the old and new value deref to the same address. For example, Rc and Arc implement CloneStableDeref, but Box and Vec do not.</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.Erased.html" title='owning_ref::Erased trait'>Erased</a></td><td class='docblock-short'><p>Helper trait for an erased concrete type an owner dereferences to.
This is used in form of a trait object for keeping
something around to (virtually) call the destructor.</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.IntoErased.html" title='owning_ref::IntoErased trait'>IntoErased</a></td><td class='docblock-short'><p>Helper trait for erasing the concrete type of what an owner derferences to,
for example <code>Box&lt;T&gt; -&gt; Box&lt;Erased&gt;</code>. This would be unneeded with
higher kinded types support in the language.</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.StableAddress.html" title='owning_ref::StableAddress trait'>StableAddress</a></td><td class='docblock-short'><p>An unsafe marker trait for types that deref to a stable address, even when moved. For example, this is implemented by Box, Vec, Rc, Arc and String, among others. Even when a Box is moved, the underlying storage remains at a fixed location.</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.ToHandle.html" title='owning_ref::ToHandle trait'>ToHandle</a></td><td class='docblock-short'><p>Trait to implement the conversion of owner to handle for common types.</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.ToHandleMut.html" title='owning_ref::ToHandleMut trait'>ToHandleMut</a></td><td class='docblock-short'><p>Trait to implement the conversion of owner to mutable handle for common types.</p>
</td></tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2>
<table><tr class='module-item'><td><a class="type" href="type.ArcRef.html" title='owning_ref::ArcRef type'>ArcRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>Arc</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.BoxRef.html" title='owning_ref::BoxRef type'>BoxRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>Box</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.BoxRefMut.html" title='owning_ref::BoxRefMut type'>BoxRefMut</a></td><td class='docblock-short'><p>Typedef of a mutable owning reference that uses a <code>Box</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.ErasedArcRef.html" title='owning_ref::ErasedArcRef type'>ErasedArcRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses an erased <code>Arc</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.ErasedBoxRef.html" title='owning_ref::ErasedBoxRef type'>ErasedBoxRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses an erased <code>Box</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.ErasedBoxRefMut.html" title='owning_ref::ErasedBoxRefMut type'>ErasedBoxRefMut</a></td><td class='docblock-short'><p>Typedef of a mutable owning reference that uses an erased <code>Box</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.ErasedRcRef.html" title='owning_ref::ErasedRcRef type'>ErasedRcRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses an erased <code>Rc</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.MutexGuardRef.html" title='owning_ref::MutexGuardRef type'>MutexGuardRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>MutexGuard</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.MutexGuardRefMut.html" title='owning_ref::MutexGuardRefMut type'>MutexGuardRefMut</a></td><td class='docblock-short'><p>Typedef of a mutable owning reference that uses a <code>MutexGuard</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.RcRef.html" title='owning_ref::RcRef type'>RcRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>Rc</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.RefMutRef.html" title='owning_ref::RefMutRef type'>RefMutRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>RefMut</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.RefMutRefMut.html" title='owning_ref::RefMutRefMut type'>RefMutRefMut</a></td><td class='docblock-short'><p>Typedef of a mutable owning reference that uses a <code>RefMut</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.RefRef.html" title='owning_ref::RefRef type'>RefRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>Ref</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.RwLockReadGuardRef.html" title='owning_ref::RwLockReadGuardRef type'>RwLockReadGuardRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>RwLockReadGuard</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.RwLockWriteGuardRef.html" title='owning_ref::RwLockWriteGuardRef type'>RwLockWriteGuardRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>RwLockWriteGuard</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.RwLockWriteGuardRefMut.html" title='owning_ref::RwLockWriteGuardRefMut type'>RwLockWriteGuardRefMut</a></td><td class='docblock-short'><p>Typedef of a mutable owning reference that uses a <code>RwLockWriteGuard</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.StringRef.html" title='owning_ref::StringRef type'>StringRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>String</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.StringRefMut.html" title='owning_ref::StringRefMut type'>StringRefMut</a></td><td class='docblock-short'><p>Typedef of a mutable owning reference that uses a <code>String</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.VecRef.html" title='owning_ref::VecRef type'>VecRef</a></td><td class='docblock-short'><p>Typedef of a owning reference that uses a <code>Vec</code> as the owner.</p>
</td></tr><tr class='module-item'><td><a class="type" href="type.VecRefMut.html" title='owning_ref::VecRefMut type'>VecRefMut</a></td><td class='docblock-short'><p>Typedef of a mutable owning reference that uses a <code>Vec</code> as the owner.</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 = "owning_ref";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>