163 lines
No EOL
34 KiB
HTML
163 lines
No EOL
34 KiB
HTML
<!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 boolean type which can be safely shared between threads."><title>AtomicBool in atomic_polyfill - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Bold-a2c9cd1067f8b328.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-cb6f1f67f1bcd037.css" id="mainThemeStyle"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="atomic_polyfill" data-themes="" data-resource-suffix="" data-rustdoc-version="1.73.0-nightly (500647fd8 2023-07-27)" data-channel="nightly" data-search-js="search-6dfdfced5eff6596.js" data-settings-js="settings-de11bff964e9d4e5.js" data-settings-css="settings-8c76f75bfb6bd192.css" data-theme-light-css="light-6d2c9675f3d09c26.css" data-theme-dark-css="dark-45ceb8f2e522f4d1.css" data-theme-ayu-css="ayu-fd19013d6ce078bf.css" ><script src="../static.files/storage-db41da1a38ea3cb8.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-0795b7d26be81095.js"></script><noscript><link rel="stylesheet" media="(prefers-color-scheme:light)" href="../static.files/light-6d2c9675f3d09c26.css"><link rel="stylesheet" media="(prefers-color-scheme:dark)" href="../static.files/dark-45ceb8f2e522f4d1.css"><link rel="stylesheet" href="../static.files/noscript-cffde32267a19fd6.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc struct"><!--[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">☰</button><a class="logo-container" href="../atomic_polyfill/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2></h2></nav><nav class="sidebar"><a class="logo-container" href="../atomic_polyfill/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2 class="location"><a href="#">AtomicBool</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block"><li><a href="#method.as_ptr">as_ptr</a></li><li><a href="#method.from_mut">from_mut</a></li><li><a href="#method.from_mut_slice">from_mut_slice</a></li><li><a href="#method.from_ptr">from_ptr</a></li><li><a href="#method.get_mut">get_mut</a></li><li><a href="#method.get_mut_slice">get_mut_slice</a></li><li><a href="#method.into_inner">into_inner</a></li><li><a href="#method.load">load</a></li><li><a href="#method.new">new</a></li><li><a href="#method.store">store</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block"><li><a href="#impl-Debug-for-AtomicBool">Debug</a></li><li><a href="#impl-Default-for-AtomicBool">Default</a></li><li><a href="#impl-From%3Cbool%3E-for-AtomicBool">From<bool></a></li><li><a href="#impl-RefUnwindSafe-for-AtomicBool">RefUnwindSafe</a></li><li><a href="#impl-Sync-for-AtomicBool">Sync</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block"><li><a href="#impl-Send-for-AtomicBool">Send</a></li><li><a href="#impl-Unpin-for-AtomicBool">Unpin</a></li><li><a href="#impl-UnwindSafe-for-AtomicBool">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block"><li><a href="#impl-Any-for-AtomicBool">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-AtomicBool">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-AtomicBool">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-AtomicBool">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-AtomicBool">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-AtomicBool">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-AtomicBool">TryInto<U></a></li></ul></section><h2><a href="index.html">In atomic_polyfill</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" 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="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Struct <a href="index.html">atomic_polyfill</a>::<wbr><a class="struct" href="#">AtomicBool</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><span class="since" title="Stable since Rust version 1.0.0">1.0.0</span> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><pre class="rust item-decl"><code><div class="code-attribute">#[repr(C, align(1))]</div>pub struct AtomicBool { /* private fields */ }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A boolean type which can be safely shared between threads.</p>
|
||
<p>This type has the same in-memory representation as a [<code>bool</code>].</p>
|
||
<p><strong>Note</strong>: This type is only available on platforms that support atomic
|
||
loads and stores of <code>u8</code>.</p>
|
||
</div></details><h2 id="implementations" class="small-section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-AtomicBool" class="impl"><a href="#impl-AtomicBool" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.new" class="method"><span class="since rightside" title="const since 1.24.0">const: 1.24.0</span><h4 class="code-header">pub const fn <a href="#method.new" class="fn">new</a>(v: bool) -> <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section></summary><div class="docblock"><p>Creates a new <code>AtomicBool</code>.</p>
|
||
<h5 id="examples"><a href="#examples">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::atomic::AtomicBool;
|
||
|
||
<span class="kw">let </span>atomic_true = AtomicBool::new(<span class="bool-val">true</span>);
|
||
<span class="kw">let </span>atomic_false = AtomicBool::new(<span class="bool-val">false</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_ptr" class="method"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/108652" title="Tracking issue for atomic_from_ptr">unstable</a></span><h4 class="code-header">pub unsafe fn <a href="#method.from_ptr" class="fn">from_ptr</a><'a>(ptr: *mut bool) -> &'a <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section><span class="item-info"><div class="stab unstable"><span class="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>atomic_from_ptr</code>)</span></div></span></summary><div class="docblock"><p>Creates a new <code>AtomicBool</code> from a pointer.</p>
|
||
<h5 id="examples-1"><a href="#examples-1">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#![feature(atomic_from_ptr, pointer_is_aligned)]
|
||
</span><span class="kw">use </span>std::sync::atomic::{<span class="self">self</span>, AtomicBool};
|
||
<span class="kw">use </span>std::mem::align_of;
|
||
|
||
<span class="comment">// Get a pointer to an allocated value
|
||
</span><span class="kw">let </span>ptr: <span class="kw-2">*mut </span>bool = Box::into_raw(Box::new(<span class="bool-val">false</span>));
|
||
|
||
<span class="macro">assert!</span>(ptr.is_aligned_to(align_of::<AtomicBool>()));
|
||
|
||
{
|
||
<span class="comment">// Create an atomic view of the allocated value
|
||
</span><span class="kw">let </span>atomic = <span class="kw">unsafe </span>{ AtomicBool::from_ptr(ptr) };
|
||
|
||
<span class="comment">// Use `atomic` for atomic operations, possibly share it with other threads
|
||
</span>atomic.store(<span class="bool-val">true</span>, atomic::Ordering::Relaxed);
|
||
}
|
||
|
||
<span class="comment">// It's ok to non-atomically access the value behind `ptr`,
|
||
// since the reference to the atomic ended its lifetime in the block above
|
||
</span><span class="macro">assert_eq!</span>(<span class="kw">unsafe </span>{ <span class="kw-2">*</span>ptr }, <span class="bool-val">true</span>);
|
||
|
||
<span class="comment">// Deallocate the value
|
||
</span><span class="kw">unsafe </span>{ drop(Box::from_raw(ptr)) }</code></pre></div>
|
||
<h5 id="safety"><a href="#safety">Safety</a></h5>
|
||
<ul>
|
||
<li><code>ptr</code> must be aligned to <code>align_of::<AtomicBool>()</code> (note that on some platforms this can be bigger than <code>align_of::<bool>()</code>).</li>
|
||
<li><code>ptr</code> must be <a href="crate::ptr#safety">valid</a> for both reads and writes for the whole lifetime <code>'a</code>.</li>
|
||
<li>The value behind <code>ptr</code> must not be accessed through non-atomic operations for the whole lifetime <code>'a</code>.</li>
|
||
</ul>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_mut" class="method"><span class="since rightside" title="Stable since Rust version 1.15.0">1.15.0</span><h4 class="code-header">pub fn <a href="#method.get_mut" class="fn">get_mut</a>(&mut self) -> &mut bool</h4></section></summary><div class="docblock"><p>Returns a mutable reference to the underlying [<code>bool</code>].</p>
|
||
<p>This is safe because the mutable reference guarantees that no other threads are
|
||
concurrently accessing the atomic data.</p>
|
||
<h5 id="examples-2"><a href="#examples-2">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_bool = AtomicBool::new(<span class="bool-val">true</span>);
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">*</span>some_bool.get_mut(), <span class="bool-val">true</span>);
|
||
<span class="kw-2">*</span>some_bool.get_mut() = <span class="bool-val">false</span>;
|
||
<span class="macro">assert_eq!</span>(some_bool.load(Ordering::SeqCst), <span class="bool-val">false</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_mut" class="method"><h4 class="code-header">pub fn <a href="#method.from_mut" class="fn">from_mut</a>(v: &mut bool) -> &mut <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section><span class="item-info"><div class="stab unstable"><span class="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>atomic_from_mut</code>)</span></div></span></summary><div class="docblock"><p>Get atomic access to a <code>&mut bool</code>.</p>
|
||
<h5 id="examples-3"><a href="#examples-3">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#![feature(atomic_from_mut)]
|
||
</span><span class="kw">use </span>std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_bool = <span class="bool-val">true</span>;
|
||
<span class="kw">let </span>a = AtomicBool::from_mut(<span class="kw-2">&mut </span>some_bool);
|
||
a.store(<span class="bool-val">false</span>, Ordering::Relaxed);
|
||
<span class="macro">assert_eq!</span>(some_bool, <span class="bool-val">false</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_mut_slice" class="method"><h4 class="code-header">pub fn <a href="#method.get_mut_slice" class="fn">get_mut_slice</a>(this: &mut [<a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a>]) -> &mut [bool]</h4></section><span class="item-info"><div class="stab unstable"><span class="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>atomic_from_mut</code>)</span></div></span></summary><div class="docblock"><p>Get non-atomic access to a <code>&mut [AtomicBool]</code> slice.</p>
|
||
<p>This is safe because the mutable reference guarantees that no other threads are
|
||
concurrently accessing the atomic data.</p>
|
||
<h5 id="examples-4"><a href="#examples-4">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#![feature(atomic_from_mut, inline_const)]
|
||
</span><span class="kw">use </span>std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_bools = [<span class="kw">const </span>{ AtomicBool::new(<span class="bool-val">false</span>) }; <span class="number">10</span>];
|
||
|
||
<span class="kw">let </span>view: <span class="kw-2">&mut </span>[bool] = AtomicBool::get_mut_slice(<span class="kw-2">&mut </span>some_bools);
|
||
<span class="macro">assert_eq!</span>(view, [<span class="bool-val">false</span>; <span class="number">10</span>]);
|
||
view[..<span class="number">5</span>].copy_from_slice(<span class="kw-2">&</span>[<span class="bool-val">true</span>; <span class="number">5</span>]);
|
||
|
||
std::thread::scope(|s| {
|
||
<span class="kw">for </span>t <span class="kw">in </span><span class="kw-2">&</span>some_bools[..<span class="number">5</span>] {
|
||
s.spawn(<span class="kw">move </span>|| <span class="macro">assert_eq!</span>(t.load(Ordering::Relaxed), <span class="bool-val">true</span>));
|
||
}
|
||
|
||
<span class="kw">for </span>f <span class="kw">in </span><span class="kw-2">&</span>some_bools[<span class="number">5</span>..] {
|
||
s.spawn(<span class="kw">move </span>|| <span class="macro">assert_eq!</span>(f.load(Ordering::Relaxed), <span class="bool-val">false</span>));
|
||
}
|
||
});</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_mut_slice" class="method"><h4 class="code-header">pub fn <a href="#method.from_mut_slice" class="fn">from_mut_slice</a>(v: &mut [bool]) -> &mut [<a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a>]</h4></section><span class="item-info"><div class="stab unstable"><span class="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>atomic_from_mut</code>)</span></div></span></summary><div class="docblock"><p>Get atomic access to a <code>&mut [bool]</code> slice.</p>
|
||
<h5 id="examples-5"><a href="#examples-5">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#![feature(atomic_from_mut)]
|
||
</span><span class="kw">use </span>std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_bools = [<span class="bool-val">false</span>; <span class="number">10</span>];
|
||
<span class="kw">let </span>a = <span class="kw-2">&*</span>AtomicBool::from_mut_slice(<span class="kw-2">&mut </span>some_bools);
|
||
std::thread::scope(|s| {
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..a.len() {
|
||
s.spawn(<span class="kw">move </span>|| a[i].store(<span class="bool-val">true</span>, Ordering::Relaxed));
|
||
}
|
||
});
|
||
<span class="macro">assert_eq!</span>(some_bools, [<span class="bool-val">true</span>; <span class="number">10</span>]);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.into_inner" class="method"><span class="since rightside" title="Stable since Rust version 1.15.0, const unstable">1.15.0 (const: <a href="https://github.com/rust-lang/rust/issues/78729" title="Tracking issue for const_cell_into_inner">unstable</a>)</span><h4 class="code-header">pub fn <a href="#method.into_inner" class="fn">into_inner</a>(self) -> bool</h4></section></summary><div class="docblock"><p>Consumes the atomic and returns the contained value.</p>
|
||
<p>This is safe because passing <code>self</code> by value guarantees that no other threads are
|
||
concurrently accessing the atomic data.</p>
|
||
<h5 id="examples-6"><a href="#examples-6">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::atomic::AtomicBool;
|
||
|
||
<span class="kw">let </span>some_bool = AtomicBool::new(<span class="bool-val">true</span>);
|
||
<span class="macro">assert_eq!</span>(some_bool.into_inner(), <span class="bool-val">true</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.load" class="method"><h4 class="code-header">pub fn <a href="#method.load" class="fn">load</a>(&self, order: <a class="enum" href="enum.Ordering.html" title="enum atomic_polyfill::Ordering">Ordering</a>) -> bool</h4></section></summary><div class="docblock"><p>Loads a value from the bool.</p>
|
||
<p><code>load</code> takes an <a href="enum.Ordering.html" title="enum atomic_polyfill::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
||
of this operation. Possible values are <a href="enum.Ordering.html#variant.SeqCst" title="variant atomic_polyfill::Ordering::SeqCst"><code>SeqCst</code></a>, <a href="enum.Ordering.html#variant.Acquire" title="variant atomic_polyfill::Ordering::Acquire"><code>Acquire</code></a> and <a href="enum.Ordering.html#variant.Relaxed" title="variant atomic_polyfill::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
||
<h5 id="panics"><a href="#panics">Panics</a></h5>
|
||
<p>Panics if <code>order</code> is <a href="enum.Ordering.html#variant.Release" title="variant atomic_polyfill::Ordering::Release"><code>Release</code></a> or <a href="enum.Ordering.html#variant.AcqRel" title="variant atomic_polyfill::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
|
||
<h5 id="examples-7"><a href="#examples-7">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
<span class="kw">let </span>some_bool = AtomicBool::new(<span class="bool-val">true</span>);
|
||
|
||
<span class="macro">assert_eq!</span>(some_bool.load(Ordering::Relaxed), <span class="bool-val">true</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.store" class="method"><h4 class="code-header">pub fn <a href="#method.store" class="fn">store</a>(&self, val: bool, order: <a class="enum" href="enum.Ordering.html" title="enum atomic_polyfill::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Stores a value into the bool.</p>
|
||
<p><code>store</code> takes an <a href="enum.Ordering.html" title="enum atomic_polyfill::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
||
of this operation. Possible values are <a href="enum.Ordering.html#variant.SeqCst" title="variant atomic_polyfill::Ordering::SeqCst"><code>SeqCst</code></a>, <a href="enum.Ordering.html#variant.Release" title="variant atomic_polyfill::Ordering::Release"><code>Release</code></a> and <a href="enum.Ordering.html#variant.Relaxed" title="variant atomic_polyfill::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
||
<h5 id="panics-1"><a href="#panics-1">Panics</a></h5>
|
||
<p>Panics if <code>order</code> is <a href="enum.Ordering.html#variant.Acquire" title="variant atomic_polyfill::Ordering::Acquire"><code>Acquire</code></a> or <a href="enum.Ordering.html#variant.AcqRel" title="variant atomic_polyfill::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
|
||
<h5 id="examples-8"><a href="#examples-8">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
<span class="kw">let </span>some_bool = AtomicBool::new(<span class="bool-val">true</span>);
|
||
|
||
some_bool.store(<span class="bool-val">false</span>, Ordering::Relaxed);
|
||
<span class="macro">assert_eq!</span>(some_bool.load(Ordering::Relaxed), <span class="bool-val">false</span>);</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_ptr" class="method"><span class="since rightside" title="Stable since Rust version 1.70.0, const since 1.70.0">1.70.0 (const: 1.70.0)</span><h4 class="code-header">pub const fn <a href="#method.as_ptr" class="fn">as_ptr</a>(&self) -> *mut bool</h4></section></summary><div class="docblock"><p>Returns a mutable pointer to the underlying [<code>bool</code>].</p>
|
||
<p>Doing non-atomic reads and writes on the resulting integer can be a data race.
|
||
This method is mostly useful for FFI, where the function signature may use
|
||
<code>*mut bool</code> instead of <code>&AtomicBool</code>.</p>
|
||
<p>Returning an <code>*mut</code> pointer from a shared reference to this atomic is safe because the
|
||
atomic types work with interior mutability. All modifications of an atomic change the value
|
||
through a shared reference, and can do so safely as long as they use atomic operations. Any
|
||
use of the returned raw pointer requires an <code>unsafe</code> block and still has to uphold the same
|
||
restriction: operations on it must be atomic.</p>
|
||
<h5 id="examples-9"><a href="#examples-9">Examples</a></h5>
|
||
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::atomic::AtomicBool;
|
||
|
||
<span class="kw">extern </span><span class="string">"C" </span>{
|
||
<span class="kw">fn </span>my_atomic_op(arg: <span class="kw-2">*mut </span>bool);
|
||
}
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>atomic = AtomicBool::new(<span class="bool-val">true</span>);
|
||
<span class="kw">unsafe </span>{
|
||
my_atomic_op(atomic.as_ptr());
|
||
}</code></pre></div>
|
||
</div></details></div></details></div><h2 id="trait-implementations" class="small-section-header">Trait Implementations<a href="#trait-implementations" class="anchor">§</a></h2><div id="trait-implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-Debug-for-AtomicBool" class="impl"><span class="since rightside" title="Stable since Rust version 1.3.0">1.3.0</span><a href="#impl-Debug-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl Debug for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl"><a href="#method.fmt" class="anchor">§</a><h4 class="code-header">fn <a class="fn">fmt</a>(&self, f: &mut Formatter<'_>) -> Result<(), Error></h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a>Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Default-for-AtomicBool" class="impl"><a href="#impl-Default-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl Default for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.default" class="method trait-impl"><a href="#method.default" class="anchor">§</a><h4 class="code-header">fn <a class="fn">default</a>() -> <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section></summary><div class="docblock"><p>Creates an <code>AtomicBool</code> initialized to <code>false</code>.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-From%3Cbool%3E-for-AtomicBool" class="impl"><span class="since rightside" title="Stable since Rust version 1.24.0">1.24.0</span><a href="#impl-From%3Cbool%3E-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl From<bool> for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a class="fn">from</a>(b: bool) -> <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section></summary><div class="docblock"><p>Converts a <code>bool</code> into an <code>AtomicBool</code>.</p>
|
||
<h5 id="examples-10"><a href="#examples-10">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::atomic::AtomicBool;
|
||
<span class="kw">let </span>atomic_bool = AtomicBool::from(<span class="bool-val">true</span>);
|
||
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{atomic_bool:?}"</span>), <span class="string">"true"</span>)</code></pre></div>
|
||
</div></details></div></details><section id="impl-RefUnwindSafe-for-AtomicBool" class="impl"><span class="since rightside" title="Stable since Rust version 1.14.0">1.14.0</span><a href="#impl-RefUnwindSafe-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl RefUnwindSafe for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section><section id="impl-Sync-for-AtomicBool" class="impl"><a href="#impl-Sync-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl Sync for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></div><h2 id="synthetic-implementations" class="small-section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor">§</a></h2><div id="synthetic-implementations-list"><section id="impl-Send-for-AtomicBool" class="impl"><a href="#impl-Send-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl Send for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section><section id="impl-Unpin-for-AtomicBool" class="impl"><a href="#impl-Unpin-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl Unpin for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section><section id="impl-UnwindSafe-for-AtomicBool" class="impl"><a href="#impl-UnwindSafe-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl UnwindSafe for <a class="struct" href="struct.AtomicBool.html" title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></div><h2 id="blanket-implementations" class="small-section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor">§</a></h2><div id="blanket-implementations-list"><details class="toggle implementors-toggle"><summary><section id="impl-Any-for-AtomicBool" class="impl"><a href="#impl-Any-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl<T> Any for T<span class="where fmt-newline">where
|
||
T: 'static + ?Sized,</span></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a class="fn">type_id</a>(&self) -> TypeId</h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a>Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-AtomicBool" class="impl"><a href="#impl-Borrow%3CT%3E-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl<T> Borrow<T> for T<span class="where fmt-newline">where
|
||
T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a class="fn">borrow</a>(&self) -> &T</h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-AtomicBool" class="impl"><a href="#impl-BorrowMut%3CT%3E-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl<T> BorrowMut<T> for T<span class="where fmt-newline">where
|
||
T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a class="fn">borrow_mut</a>(&mut self) -> &mut T</h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-AtomicBool" class="impl"><a href="#impl-From%3CT%3E-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl<T> From<T> for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from-1" class="method trait-impl"><a href="#method.from-1" class="anchor">§</a><h4 class="code-header">fn <a class="fn">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-AtomicBool" class="impl"><a href="#impl-Into%3CU%3E-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl<T, U> Into<U> for T<span class="where fmt-newline">where
|
||
U: From<T>,</span></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a class="fn">into</a>(self) -> U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
|
||
<p>That is, this conversion is whatever the implementation of
|
||
<code>[From]<T> for U</code> chooses to do.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-AtomicBool" class="impl"><a href="#impl-TryFrom%3CU%3E-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl<T, U> TryFrom<U> for T<span class="where fmt-newline">where
|
||
U: Into<T>,</span></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a class="associatedtype">Error</a> = Infallible</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a class="fn">try_from</a>(value: U) -> Result<T, <T as TryFrom<U>>::Error></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-AtomicBool" class="impl"><a href="#impl-TryInto%3CU%3E-for-AtomicBool" class="anchor">§</a><h3 class="code-header">impl<T, U> TryInto<U> for T<span class="where fmt-newline">where
|
||
U: TryFrom<T>,</span></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a class="associatedtype">Error</a> = <U as TryFrom<T>>::Error</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a class="fn">try_into</a>(self) -> Result<U, <U as TryFrom<T>>::Error></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html> |