Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 14880

rust-doc-1.28.0-1.mga6.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 `RwLock` struct in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, RwLock"><title>std::sync::RwLock - 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><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc struct"><!--[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='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Struct RwLock</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#methods">Methods</a><div class="sidebar-links"><a href="#method.new">new</a><a href="#method.read">read</a><a href="#method.try_read">try_read</a><a href="#method.write">write</a><a href="#method.try_write">try_write</a><a href="#method.is_poisoned">is_poisoned</a><a href="#method.into_inner">into_inner</a><a href="#method.get_mut">get_mut</a></div><a class="sidebar-title" href="#implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-UnwindSafe">UnwindSafe</a><a href="#impl-RefUnwindSafe">RefUnwindSafe</a><a href="#impl-Send">Send</a><a href="#impl-Sync">Sync</a><a href="#impl-Drop">Drop</a><a href="#impl-Debug">Debug</a><a href="#impl-Default">Default</a><a href="#impl-From%3CT%3E">From&lt;T&gt;</a></div></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>sync</a></p><script>window.sidebarCurrent = {name: 'RwLock', ty: 'struct', 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="../../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"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><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='in-band'>Struct <a href='../index.html'>std</a>::<wbr><a href='index.html'>sync</a>::<wbr><a class="struct" href=''>RwLock</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.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/std/sync/rwlock.rs.html#77-81' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust struct'>pub struct RwLock&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; { /* fields omitted */ }</pre></div><div class='docblock'><p>A reader-writer lock</p>
<p>This type of lock allows a number of readers or at most one writer at any
point in time. The write portion of this lock typically allows modification
of the underlying data (exclusive access) and the read portion of this lock
typically allows for read-only access (shared access).</p>
<p>In comparison, a <a href="struct.Mutex.html"><code>Mutex</code></a> does not distinguish between readers or writers
that acquire the lock, therefore blocking any threads waiting for the lock to
become available. An <code>RwLock</code> will allow any number of readers to acquire the
lock as long as a writer is not holding the lock.</p>
<p>The priority policy of the lock is dependent on the underlying operating
system's implementation, and this type does not guarantee that any
particular policy will be used.</p>
<p>The type parameter <code>T</code> represents the data that this lock protects. It is
required that <code>T</code> satisfies <a href="../../std/marker/trait.Send.html"><code>Send</code></a> to be shared across threads and
<a href="../../std/marker/trait.Sync.html"><code>Sync</code></a> to allow concurrent access through readers. The RAII guards
returned from the locking methods implement <a href="../../std/ops/trait.Deref.html"><code>Deref</code></a> (and <a href="../../std/ops/trait.DerefMut.html"><code>DerefMut</code></a>
for the <code>write</code> methods) to allow access to the content of the lock.</p>
<h1 id="poisoning" class="section-header"><a href="#poisoning">Poisoning</a></h1>
<p>An <code>RwLock</code>, like <a href="struct.Mutex.html"><code>Mutex</code></a>, will become poisoned on a panic. Note, however,
that an <code>RwLock</code> may only be poisoned if a panic occurs while it is locked
exclusively (write mode). If a panic occurs in any reader, then the lock
will not be poisoned.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">RwLock</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">5</span>);

<span class="comment">// many reader locks can be held at once</span>
{
    <span class="kw">let</span> <span class="ident">r1</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">read</span>().<span class="ident">unwrap</span>();
    <span class="kw">let</span> <span class="ident">r2</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">read</span>().<span class="ident">unwrap</span>();
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">r1</span>, <span class="number">5</span>);
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">r2</span>, <span class="number">5</span>);
} <span class="comment">// read locks are dropped at this point</span>

<span class="comment">// only one write lock may be held, however</span>
{
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">w</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">write</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">w</span> <span class="op">+=</span> <span class="number">1</span>;
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">w</span>, <span class="number">6</span>);
} <span class="comment">// write lock is dropped here</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%3Async%3A%3ARwLock%3B%0A%0Alet%20lock%20%3D%20RwLock%3A%3Anew(5)%3B%0A%0A%2F%2F%20many%20reader%20locks%20can%20be%20held%20at%20once%0A%7B%0A%20%20%20%20let%20r1%20%3D%20lock.read().unwrap()%3B%0A%20%20%20%20let%20r2%20%3D%20lock.read().unwrap()%3B%0A%20%20%20%20assert_eq!(*r1%2C%205)%3B%0A%20%20%20%20assert_eq!(*r2%2C%205)%3B%0A%7D%20%2F%2F%20read%20locks%20are%20dropped%20at%20this%20point%0A%0A%2F%2F%20only%20one%20write%20lock%20may%20be%20held%2C%20however%0A%7B%0A%20%20%20%20let%20mut%20w%20%3D%20lock.write().unwrap()%3B%0A%20%20%20%20*w%20%2B%3D%201%3B%0A%20%20%20%20assert_eq!(*w%2C%206)%3B%0A%7D%20%2F%2F%20write%20lock%20is%20dropped%20here%0A%7D">Run</a></pre>
</div>
                    <h2 id='methods' class='small-section-header'>
                      Methods<a href='#methods' class='anchor'></a>
                    </h2>
                <h3 id='impl' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T&gt; <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#131-149' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.new' class="method"><span id='new.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.new' class='fnname'>new</a>(t: T) -&gt; <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#142-148' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates a new instance of an <code>RwLock&lt;T&gt;</code> which is unlocked.</p>
<h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">RwLock</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">5</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%3Async%3A%3ARwLock%3B%0A%0Alet%20lock%20%3D%20RwLock%3A%3Anew(5)%3B%0A%7D">Run</a></pre>
</div></div><h3 id='impl-1' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-1' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#151-422' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.read' class="method"><span id='read.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.read' class='fnname'>read</a>(&amp;self) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;<a class="struct" href="../../std/sync/struct.RwLockReadGuard.html" title="struct std::sync::RwLockReadGuard">RwLockReadGuard</a>&lt;T&gt;&gt;</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#193-198' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Locks this rwlock with shared read access, blocking the current thread
until it can be acquired.</p>
<p>The calling thread will be blocked until there are no more writers which
hold the lock. There may be other readers currently inside the lock when
this method returns. This method does not provide any guarantees with
respect to the ordering of whether contentious readers or writers will
acquire the lock first.</p>
<p>Returns an RAII guard which will release this thread's shared access
once it is dropped.</p>
<h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1>
<p>This function will return an error if the RwLock is poisoned. An RwLock
is poisoned whenever a writer panics while holding an exclusive lock.
The failure will occur immediately after the lock has been acquired.</p>
<h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1>
<p>This function might panic when called if the lock is already held by the current thread.</p>
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::{<span class="ident">Arc</span>, <span class="ident">RwLock</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">1</span>));
<span class="kw">let</span> <span class="ident">c_lock</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">clone</span>();

<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">read</span>().<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">n</span>, <span class="number">1</span>);

<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">||</span> {
    <span class="kw">let</span> <span class="ident">r</span> <span class="op">=</span> <span class="ident">c_lock</span>.<span class="ident">read</span>();
    <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">r</span>.<span class="ident">is_ok</span>());
}).<span class="ident">join</span>().<span class="ident">unwrap</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%3Async%3A%3A%7BArc%2C%20RwLock%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20lock%20%3D%20Arc%3A%3Anew(RwLock%3A%3Anew(1))%3B%0Alet%20c_lock%20%3D%20lock.clone()%3B%0A%0Alet%20n%20%3D%20lock.read().unwrap()%3B%0Aassert_eq!(*n%2C%201)%3B%0A%0Athread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20let%20r%20%3D%20c_lock.read()%3B%0A%20%20%20%20assert!(r.is_ok())%3B%0A%7D).join().unwrap()%3B%0A%7D">Run</a></pre>
</div><h4 id='method.try_read' class="method"><span id='try_read.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.try_read' class='fnname'>try_read</a>(&amp;self) -&gt; <a class="type" href="../../std/sync/type.TryLockResult.html" title="type std::sync::TryLockResult">TryLockResult</a>&lt;<a class="struct" href="../../std/sync/struct.RwLockReadGuard.html" title="struct std::sync::RwLockReadGuard">RwLockReadGuard</a>&lt;T&gt;&gt;</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#232-240' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Attempts to acquire this rwlock with shared read access.</p>
<p>If the access could not be granted at this time, then <code>Err</code> is returned.
Otherwise, an RAII guard is returned which will release the shared access
when it is dropped.</p>
<p>This function does not block.</p>
<p>This function does not provide any guarantees with respect to the ordering
of whether contentious readers or writers will acquire the lock first.</p>
<h1 id="errors-1" class="section-header"><a href="#errors-1">Errors</a></h1>
<p>This function will return an error if the RwLock is poisoned. An RwLock
is poisoned whenever a writer panics while holding an exclusive lock. An
error will only be returned if the lock would have otherwise been
acquired.</p>
<h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">RwLock</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">1</span>);

<span class="kw">match</span> <span class="ident">lock</span>.<span class="ident">try_read</span>() {
    <span class="prelude-val">Ok</span>(<span class="ident">n</span>) <span class="op">=&gt;</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">n</span>, <span class="number">1</span>),
    <span class="prelude-val">Err</span>(<span class="kw">_</span>) <span class="op">=&gt;</span> <span class="macro">unreachable</span><span class="macro">!</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%3Async%3A%3ARwLock%3B%0A%0Alet%20lock%20%3D%20RwLock%3A%3Anew(1)%3B%0A%0Amatch%20lock.try_read()%20%7B%0A%20%20%20%20Ok(n)%20%3D%3E%20assert_eq!(*n%2C%201)%2C%0A%20%20%20%20Err(_)%20%3D%3E%20unreachable!()%2C%0A%7D%3B%0A%7D">Run</a></pre>
</div><h4 id='method.write' class="method"><span id='write.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.write' class='fnname'>write</a>(&amp;self) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;<a class="struct" href="../../std/sync/struct.RwLockWriteGuard.html" title="struct std::sync::RwLockWriteGuard">RwLockWriteGuard</a>&lt;T&gt;&gt;</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#275-280' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Locks this rwlock with exclusive write access, blocking the current
thread until it can be acquired.</p>
<p>This function will not return while other writers or other readers
currently have access to the lock.</p>
<p>Returns an RAII guard which will drop the write access of this rwlock
when dropped.</p>
<h1 id="errors-2" class="section-header"><a href="#errors-2">Errors</a></h1>
<p>This function will return an error if the RwLock is poisoned. An RwLock
is poisoned whenever a writer panics while holding an exclusive lock.
An error will be returned when the lock is acquired.</p>
<h1 id="panics-1" class="section-header"><a href="#panics-1">Panics</a></h1>
<p>This function might panic when called if the lock is already held by the current thread.</p>
<h1 id="examples-4" class="section-header"><a href="#examples-4">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">RwLock</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">1</span>);

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">n</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">write</span>().<span class="ident">unwrap</span>();
<span class="kw-2">*</span><span class="ident">n</span> <span class="op">=</span> <span class="number">2</span>;

<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">lock</span>.<span class="ident">try_read</span>().<span class="ident">is_err</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%3Async%3A%3ARwLock%3B%0A%0Alet%20lock%20%3D%20RwLock%3A%3Anew(1)%3B%0A%0Alet%20mut%20n%20%3D%20lock.write().unwrap()%3B%0A*n%20%3D%202%3B%0A%0Aassert!(lock.try_read().is_err())%3B%0A%7D">Run</a></pre>
</div><h4 id='method.try_write' class="method"><span id='try_write.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.try_write' class='fnname'>try_write</a>(&amp;self) -&gt; <a class="type" href="../../std/sync/type.TryLockResult.html" title="type std::sync::TryLockResult">TryLockResult</a>&lt;<a class="struct" href="../../std/sync/struct.RwLockWriteGuard.html" title="struct std::sync::RwLockWriteGuard">RwLockWriteGuard</a>&lt;T&gt;&gt;</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#314-322' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Attempts to lock this rwlock with exclusive write access.</p>
<p>If the lock could not be acquired at this time, then <code>Err</code> is returned.
Otherwise, an RAII guard is returned which will release the lock when
it is dropped.</p>
<p>This function does not block.</p>
<p>This function does not provide any guarantees with respect to the ordering
of whether contentious readers or writers will acquire the lock first.</p>
<h1 id="errors-3" class="section-header"><a href="#errors-3">Errors</a></h1>
<p>This function will return an error if the RwLock is poisoned. An RwLock
is poisoned whenever a writer panics while holding an exclusive lock. An
error will only be returned if the lock would have otherwise been
acquired.</p>
<h1 id="examples-5" class="section-header"><a href="#examples-5">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">RwLock</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">1</span>);

<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">read</span>().<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">n</span>, <span class="number">1</span>);

<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">lock</span>.<span class="ident">try_write</span>().<span class="ident">is_err</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%3Async%3A%3ARwLock%3B%0A%0Alet%20lock%20%3D%20RwLock%3A%3Anew(1)%3B%0A%0Alet%20n%20%3D%20lock.read().unwrap()%3B%0Aassert_eq!(*n%2C%201)%3B%0A%0Aassert!(lock.try_write().is_err())%3B%0A%7D">Run</a></pre>
</div><h4 id='method.is_poisoned' class="method"><span id='is_poisoned.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.is_poisoned' class='fnname'>is_poisoned</a>(&amp;self) -&gt; <a class="primitive" href="../primitive.bool.html">bool</a></code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#347-349' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Determines whether the lock is poisoned.</p>
<p>If another thread is active, the lock can still become poisoned at any
time.  You should not trust a <code>false</code> value for program correctness
without additional synchronization.</p>
<h1 id="examples-6" class="section-header"><a href="#examples-6">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::{<span class="ident">Arc</span>, <span class="ident">RwLock</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">0</span>));
<span class="kw">let</span> <span class="ident">c_lock</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">clone</span>();

<span class="kw">let</span> <span class="kw">_</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="kw">let</span> <span class="ident">_lock</span> <span class="op">=</span> <span class="ident">c_lock</span>.<span class="ident">write</span>().<span class="ident">unwrap</span>();
    <span class="macro">panic</span><span class="macro">!</span>(); <span class="comment">// the lock gets poisoned</span>
}).<span class="ident">join</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">lock</span>.<span class="ident">is_poisoned</span>(), <span class="bool-val">true</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%3Async%3A%3A%7BArc%2C%20RwLock%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20lock%20%3D%20Arc%3A%3Anew(RwLock%3A%3Anew(0))%3B%0Alet%20c_lock%20%3D%20lock.clone()%3B%0A%0Alet%20_%20%3D%20thread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20let%20_lock%20%3D%20c_lock.write().unwrap()%3B%0A%20%20%20%20panic!()%3B%20%2F%2F%20the%20lock%20gets%20poisoned%0A%7D).join()%3B%0Aassert_eq!(lock.is_poisoned()%2C%20true)%3B%0A%7D">Run</a></pre>
</div><h4 id='method.into_inner' class="method"><span id='into_inner.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.into_inner' class='fnname'>into_inner</a>(self) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,&nbsp;</span></code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#373-392' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Consumes this <code>RwLock</code>, returning the underlying data.</p>
<h1 id="errors-4" class="section-header"><a href="#errors-4">Errors</a></h1>
<p>This function will return an error if the RwLock is poisoned. An RwLock
is poisoned whenever a writer panics while holding an exclusive lock. An
error will only be returned if the lock would have otherwise been
acquired.</p>
<h1 id="examples-7" class="section-header"><a href="#examples-7">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">RwLock</span>;

<span class="kw">let</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="ident">String</span>::<span class="ident">new</span>());
{
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">write</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">s</span> <span class="op">=</span> <span class="string">&quot;modified&quot;</span>.<span class="ident">to_owned</span>();
}
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">lock</span>.<span class="ident">into_inner</span>().<span class="ident">unwrap</span>(), <span class="string">&quot;modified&quot;</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%3Async%3A%3ARwLock%3B%0A%0Alet%20lock%20%3D%20RwLock%3A%3Anew(String%3A%3Anew())%3B%0A%7B%0A%20%20%20%20let%20mut%20s%20%3D%20lock.write().unwrap()%3B%0A%20%20%20%20*s%20%3D%20%22modified%22.to_owned()%3B%0A%7D%0Aassert_eq!(lock.into_inner().unwrap()%2C%20%22modified%22)%3B%0A%7D">Run</a></pre>
</div><h4 id='method.get_mut' class="method"><span id='get_mut.v' class='invisible'><table class='table-display'><tbody><tr><td><code>pub fn <a href='#method.get_mut' class='fnname'>get_mut</a>(&amp;mut self) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;<a class="primitive" href="../primitive.reference.html">&amp;mut </a>T&gt;</code></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#416-421' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns a mutable reference to the underlying data.</p>
<p>Since this call borrows the <code>RwLock</code> mutably, no actual locking needs to
take place---the mutable borrow statically guarantees no locks exist.</p>
<h1 id="errors-5" class="section-header"><a href="#errors-5">Errors</a></h1>
<p>This function will return an error if the RwLock is poisoned. An RwLock
is poisoned whenever a writer panics while holding an exclusive lock. An
error will only be returned if the lock would have otherwise been
acquired.</p>
<h1 id="examples-8" class="section-header"><a href="#examples-8">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">RwLock</span>;

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">RwLock</span>::<span class="ident">new</span>(<span class="number">0</span>);
<span class="kw-2">*</span><span class="ident">lock</span>.<span class="ident">get_mut</span>().<span class="ident">unwrap</span>() <span class="op">=</span> <span class="number">10</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">lock</span>.<span class="ident">read</span>().<span class="ident">unwrap</span>(), <span class="number">10</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%3Async%3A%3ARwLock%3B%0A%0Alet%20mut%20lock%20%3D%20RwLock%3A%3Anew(0)%3B%0A*lock.get_mut().unwrap()%20%3D%2010%3B%0Aassert_eq!(*lock.read().unwrap()%2C%2010)%3B%0A%7D">Run</a></pre>
</div></div>
                <h2 id='implementations' class='small-section-header'>
                  Trait Implementations<a href='#implementations' class='anchor'></a>
                </h2>
                <div id='implementations-list'><h3 id='impl-UnwindSafe' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/panic/trait.UnwindSafe.html" title="trait std::panic::UnwindSafe">UnwindSafe</a> for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-UnwindSafe' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.9.0'>1.9.0</div><a class='srclink' href='../../src/std/panic.rs.html#221' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'></div><h3 id='impl-RefUnwindSafe' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/panic/trait.RefUnwindSafe.html" title="trait std::panic::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-RefUnwindSafe' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.12.0'>1.12.0</div><a class='srclink' href='../../src/std/panic.rs.html#245' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'></div><h3 id='impl-Send' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a> + <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a>&gt; <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-Send' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#84' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'></div><h3 id='impl-Sync' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a> + <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> + <a class="trait" href="../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a>&gt; <a class="trait" href="../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a> for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-Sync' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#86' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'></div><h3 id='impl-Drop' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/ops/trait.Drop.html" title="trait std::ops::Drop">Drop</a> for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-Drop' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#425-430' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.drop' class="method"><span id='drop.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../std/ops/trait.Drop.html#tymethod.drop' class='fnname'>drop</a>(&amp;mut self)</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#426-429' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Executes the destructor for this type. <a href="../../std/ops/trait.Drop.html#tymethod.drop">Read more</a></p>
</div></div><h3 id='impl-Debug' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a> + <a class="trait" href="../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a>&gt; <a class="trait" href="../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a> for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-Debug' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#433-450' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.fmt' class="method"><span id='fmt.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../std/fmt/trait.Debug.html#tymethod.fmt' class='fnname'>fmt</a>(&amp;self, f: &amp;mut <a class="struct" href="../../std/fmt/struct.Formatter.html" title="struct std::fmt::Formatter">Formatter</a>) -&gt; <a class="type" href="../../std/fmt/type.Result.html" title="type std::fmt::Result">Result</a></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#434-449' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Formats the value using the given formatter. <a href="../../std/fmt/trait.Debug.html#tymethod.fmt">Read more</a></p>
</div></div><h3 id='impl-Default' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;<a class="trait" href="../../std/default/trait.Default.html" title="trait std::default::Default">Default</a>&gt; <a class="trait" href="../../std/default/trait.Default.html" title="trait std::default::Default">Default</a> for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-Default' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.10.0'>1.10.0</div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#453-458' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.default' class="method"><span id='default.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../std/default/trait.Default.html#tymethod.default' class='fnname'>default</a>() -&gt; <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#455-457' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates a new <code>RwLock&lt;T&gt;</code>, with the <code>Default</code> value for T.</p>
</div></div><h3 id='impl-From%3CT%3E' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl&lt;T&gt; <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a>&lt;T&gt; for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a>&lt;T&gt;</code><a href='#impl-From%3CT%3E' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.24.0'>1.24.0</div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#461-469' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.from' class="method"><span id='from.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(t: T) -&gt; Self</code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/rwlock.rs.html#466-468' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Creates a new instance of an <code>RwLock&lt;T&gt;</code> which is unlocked.
This is equivalent to <a href="#method.new"><code>RwLock::new</code></a>.</p>
</div></div></div></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 = "std";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>