<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="generator"content="rustdoc"><metaname="description"content="A boolean type which can be safely shared between threads."><title>AtomicBool in atomic_polyfill - Rust</title><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/SourceSerif4-Bold-a2c9cd1067f8b328.ttf.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><linkrel="stylesheet"href="../static.files/normalize-76eba96aa4d2e634.css"><linkrel="stylesheet"href="../static.files/rustdoc-cb6f1f67f1bcd037.css"id="mainThemeStyle"><metaname="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 (8131b9774 2023-08-02)"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"><scriptsrc="../static.files/storage-db41da1a38ea3cb8.js"></script><scriptdefersrc="sidebar-items.js"></script><scriptdefersrc="../static.files/main-0795b7d26be81095.js"></script><noscript><linkrel="stylesheet"media="(prefers-color-scheme:light)"href="../static.files/light-6d2c9675f3d09c26.css"><linkrel="stylesheet"media="(prefers-color-scheme:dark)"href="../static.files/dark-45ceb8f2e522f4d1.css"><linkrel="stylesheet"href="../static.files/noscript-cffde32267a19fd6.css"></noscript><linkrel="alternate icon"type="image/png"href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><linkrel="alternate icon"type="image/png"href="../static.files/favicon-32x32-422f7d1d52889060.png"><linkrel="icon"type="image/svg+xml"href="../static.files/favicon-2c020d218678b618.svg"></head><bodyclass="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><navclass="mobile-topbar"><buttonclass="sidebar-menu-toggle">☰</button><aclass="logo-container"href="../atomic_polyfill/index.html"><imgclass="rust-logo"src="../static.files/rust-logo-151179464ae7ed46.svg"alt="logo"></a><h2></h2></nav><navclass="sidebar"><aclass="logo-container"href="../atomic_polyfill/index.html"><imgclass="rust-logo"src="../static.files/rust-logo-151179464ae7ed46.svg"alt="logo"></a><h2class="location"><ahref="#">AtomicBool</a></h2><divclass="sidebar-elems"><section><h3><ahref="#implementations">Methods</a></h3><ulclass="block"><li><ahref="#method.as_ptr">as_ptr</a></li><li><ahref="#method.from_mut">from_mut</a></li><li><ahref="#method.from_mut_slice">from_mut_slice</a></li><li><ahref="#method.from_ptr">from_ptr</a></li><li><ahref="#method.get_mut">get_mut</a></li><li><ahref="#method.get_mut_slice">get_mut_slice</a></li><li><ahref="#method.into_inner">into_inner</a></li><li><ahref="#method.load">load</a></li><li><ahref="#method.new">new</a></li><li><ahref="#method.store">store</a></li></ul><h3><ahref="#trait-implementations">Trait Implementations</a></h3><ulclass="block"><li><ahref="#impl-Debug-for-AtomicBool">Debug</a></li><li><ahref="#impl-Default-for-AtomicBool">Default</a></li><li><ahref="#impl-From%3Cbool%3E-for-AtomicBool">From<bool></a></li><li><ahref="
</div></details><detailsclass="toggle method-toggle"open><summary><sectionid="method.from_ptr"class="method"><spanclass="since rightside"title="const unstable">const: <ahref="https://github.com/rust-lang/rust/issues/108652"title="Tracking issue for atomic_from_ptr">unstable</a></span><h4class="code-header">pub unsafe fn <ahref="#method.from_ptr"class="fn">from_ptr</a><'a>(ptr: *mut bool) ->&'a <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section><spanclass="item-info"><divclass="stab unstable"><spanclass="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>atomic_from_ptr</code>)</span></div></span></summary><divclass="docblock"><p>Creates a new <code>AtomicBool</code> from a pointer.</p>
<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 <ahref="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><detailsclass="toggle method-toggle"open><summary><sectionid="method.get_mut"class="method"><spanclass="since rightside"title="Stable since Rust version 1.15.0">1.15.0</span><h4class="code-header">pub fn <ahref="#method.get_mut"class="fn">get_mut</a>(&mut self) ->&mut bool</h4></section></summary><divclass="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
</div></details><detailsclass="toggle method-toggle"open><summary><sectionid="method.into_inner"class="method"><spanclass="since rightside"title="Stable since Rust version 1.15.0, const unstable">1.15.0 (const: <ahref="https://github.com/rust-lang/rust/issues/78729"title="Tracking issue for const_cell_into_inner">unstable</a>)</span><h4class="code-header">pub fn <ahref="#method.into_inner"class="fn">into_inner</a>(self) -> bool</h4></section></summary><divclass="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
</div></details><detailsclass="toggle method-toggle"open><summary><sectionid="method.load"class="method"><h4class="code-header">pub fn <ahref="#method.load"class="fn">load</a>(&self, order: <aclass="enum"href="enum.Ordering.html"title="enum atomic_polyfill::Ordering">Ordering</a>) -> bool</h4></section></summary><divclass="docblock"><p>Loads a value from the bool.</p>
<p><code>load</code> takes an <ahref="enum.Ordering.html"title="enum atomic_polyfill::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
of this operation. Possible values are <ahref="enum.Ordering.html#variant.SeqCst"title="variant atomic_polyfill::Ordering::SeqCst"><code>SeqCst</code></a>, <ahref="enum.Ordering.html#variant.Acquire"title="variant atomic_polyfill::Ordering::Acquire"><code>Acquire</code></a> and <ahref="enum.Ordering.html#variant.Relaxed"title="variant atomic_polyfill::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
<h5id="panics"><ahref="#panics">Panics</a></h5>
<p>Panics if <code>order</code> is <ahref="enum.Ordering.html#variant.Release"title="variant atomic_polyfill::Ordering::Release"><code>Release</code></a> or <ahref="enum.Ordering.html#variant.AcqRel"title="variant atomic_polyfill::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
</div></details><detailsclass="toggle method-toggle"open><summary><sectionid="method.store"class="method"><h4class="code-header">pub fn <ahref="#method.store"class="fn">store</a>(&self, val: bool, order: <aclass="enum"href="enum.Ordering.html"title="enum atomic_polyfill::Ordering">Ordering</a>)</h4></section></summary><divclass="docblock"><p>Stores a value into the bool.</p>
<p><code>store</code> takes an <ahref="enum.Ordering.html"title="enum atomic_polyfill::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
of this operation. Possible values are <ahref="enum.Ordering.html#variant.SeqCst"title="variant atomic_polyfill::Ordering::SeqCst"><code>SeqCst</code></a>, <ahref="enum.Ordering.html#variant.Release"title="variant atomic_polyfill::Ordering::Release"><code>Release</code></a> and <ahref="enum.Ordering.html#variant.Relaxed"title="variant atomic_polyfill::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
<p>Panics if <code>order</code> is <ahref="enum.Ordering.html#variant.Acquire"title="variant atomic_polyfill::Ordering::Acquire"><code>Acquire</code></a> or <ahref="enum.Ordering.html#variant.AcqRel"title="variant atomic_polyfill::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
</div></details><detailsclass="toggle method-toggle"open><summary><sectionid="method.as_ptr"class="method"><spanclass="since rightside"title="Stable since Rust version 1.70.0, const since 1.70.0">1.70.0 (const: 1.70.0)</span><h4class="code-header">pub const fn <ahref="#method.as_ptr"class="fn">as_ptr</a>(&self) -> *mut bool</h4></section></summary><divclass="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
<divclass="example-wrap ignore"><ahref="#"class="tooltip"title="This example is not tested">ⓘ</a><preclass="rust rust-example-rendered"><code><spanclass="kw">use </span>std::sync::atomic::AtomicBool;
</div></details></div></details></div><h2id="trait-implementations"class="small-section-header">Trait Implementations<ahref="#trait-implementations"class="anchor">§</a></h2><divid="trait-implementations-list"><detailsclass="toggle implementors-toggle"open><summary><sectionid="impl-Debug-for-AtomicBool"class="impl"><spanclass="since rightside"title="Stable since Rust version 1.3.0">1.3.0</span><ahref="#impl-Debug-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl Debug for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></summary><divclass="impl-items"><detailsclass="toggle method-toggle"open><summary><sectionid="method.fmt"class="method trait-impl"><ahref="#method.fmt"class="anchor">§</a><h4class="code-header">fn <aclass="fn">fmt</a>(&self, f: &mut Formatter<'_>) -> Result<(), Error></h4></section></summary><divclass='docblock'>Formats the value using the given formatter. <a>Read more</a></div></details></div></details><detailsclass="toggle implementors-toggle"open><summary><sectionid="impl-Default-for-AtomicBool"class="impl"><ahref="#impl-Default-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl Default for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></summary><divclass="impl-items"><detailsclass="toggle method-toggle"open><summary><sectionid="method.default"class="method trait-impl"><ahref="#method.default"class="anchor">§</a><h4class="code-header">fn <aclass="fn">default</a>() -><aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section></summary><divclass="docblock"><p>Creates an <code>AtomicBool</code> initialized to <code>false</code>.</p>
</div></details></div></details><detailsclass="toggle implementors-toggle"open><summary><sectionid="impl-From%3Cbool%3E-for-AtomicBool"class="impl"><spanclass="since rightside"title="Stable since Rust version 1.24.0">1.24.0</span><ahref="#impl-From%3Cbool%3E-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl From<bool> for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></summary><divclass="impl-items"><detailsclass="toggle method-toggle"open><summary><sectionid="method.from"class="method trait-impl"><ahref="#method.from"class="anchor">§</a><h4class="code-header">fn <aclass="fn">from</a>(b: bool) -><aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h4></section></summary><divclass="docblock"><p>Converts a <code>bool</code> into an <code>AtomicBool</code>.</p>
</div></details></div></details><sectionid="impl-RefUnwindSafe-for-AtomicBool"class="impl"><spanclass="since rightside"title="Stable since Rust version 1.14.0">1.14.0</span><ahref="#impl-RefUnwindSafe-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl RefUnwindSafe for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section><sectionid="impl-Sync-for-AtomicBool"class="impl"><ahref="#impl-Sync-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl Sync for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></div><h2id="synthetic-implementations"class="small-section-header">Auto Trait Implementations<ahref="#synthetic-implementations"class="anchor">§</a></h2><divid="synthetic-implementations-list"><sectionid="impl-Send-for-AtomicBool"class="impl"><ahref="#impl-Send-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl Send for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section><sectionid="impl-Unpin-for-AtomicBool"class="impl"><ahref="#impl-Unpin-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl Unpin for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section><sectionid="impl-UnwindSafe-for-AtomicBool"class="impl"><ahref="#impl-UnwindSafe-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl UnwindSafe for <aclass="struct"href="struct.AtomicBool.html"title="struct atomic_polyfill::AtomicBool">AtomicBool</a></h3></section></div><h2id="blanket-implementations"class="small-section-header">Blanket Implementations<ahref="#blanket-implementations"class="anchor">§</a></h2><divid="blanket-implementations-list"><detailsclass="toggle implementors-toggle"><summary><sectionid="impl-Any-for-AtomicBool"class="impl"><ahref="#impl-Any-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl<T> Any for T<spanclass="where fmt-newline">where
T: 'static + ?Sized,</span></h3></section></summary><divclass="impl-items"><detailsclass="toggle method-toggle"open><summary><sectionid="method.type_id"class="method trait-impl"><ahref="#method.type_id"class="anchor">§</a><h4class="code-header">fn <aclass="fn">type_id</a>(&self) -> TypeId</h4></section></summary><divclass='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a>Read more</a></div></details></div></details><detailsclass="toggle implementors-toggle"><summary><sectionid="impl-Borrow%3CT%3E-for-AtomicBool"class="impl"><ahref="#impl-Borrow%3CT%3E-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl<T> Borrow<T> for T<spanclass="where fmt-newline">where
T: ?Sized,</span></h3></section></summary><divclass="impl-items"><detailsclass="toggle method-toggle"open><summary><sectionid="method.borrow"class="method trait-impl"><ahref="#method.borrow"class="anchor">§</a><h4class="code-header">fn <aclass="fn">borrow</a>(&self) ->&T</h4></section></summary><divclass='docblock'>Immutably borrows from an owned value. <a>Read more</a></div></details></div></details><detailsclass="toggle implementors-toggle"><summary><sectionid="impl-BorrowMut%3CT%3E-for-AtomicBool"class="impl"><ahref="#impl-BorrowMut%3CT%3E-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl<T> BorrowMut<T> for T<spanclass="where fmt-newline">where
T: ?Sized,</span></h3></section></summary><divclass="impl-items"><detailsclass="toggle method-toggle"open><summary><sectionid="method.borrow_mut"class="method trait-impl"><ahref="#method.borrow_mut"class="anchor">§</a><h4class="code-header">fn <aclass="fn">borrow_mut</a>(&mut self) ->&mut T</h4></section></summary><divclass='docblock'>Mutably borrows from an owned value. <a>Read more</a></div></details></div></details><detailsclass="toggle implementors-toggle"><summary><sectionid="impl-From%3CT%3E-for-AtomicBool"class="impl"><ahref="#impl-From%3CT%3E-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl<T> From<T> for T</h3></section></summary><divclass="impl-items"><detailsclass="toggle method-toggle"open><summary><sectionid="method.from-1"class="method trait-impl"><ahref="#method.from-1"class="anchor">§</a><h4class="code-header">fn <aclass="fn">from</a>(t: T) -> T</h4></section></summary><divclass="docblock"><p>Returns the argument unchanged.</p>
</div></details></div></details><detailsclass="toggle implementors-toggle"><summary><sectionid="impl-Into%3CU%3E-for-AtomicBool"class="impl"><ahref="#impl-Into%3CU%3E-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl<T, U> Into<U> for T<spanclass="where fmt-newline">where
U: Into<T>,</span></h3></section></summary><divclass="impl-items"><detailsclass="toggle"open><summary><sectionid="associatedtype.Error-1"class="associatedtype trait-impl"><ahref="#associatedtype.Error-1"class="anchor">§</a><h4class="code-header">type <aclass="associatedtype">Error</a> = Infallible</h4></section></summary><divclass='docblock'>The type returned in the event of a conversion error.</div></details><detailsclass="toggle method-toggle"open><summary><sectionid="method.try_from"class="method trait-impl"><ahref="#method.try_from"class="anchor">§</a><h4class="code-header">fn <aclass="fn">try_from</a>(value: U) -> Result<T, <T as TryFrom<U>>::Error></h4></section></summary><divclass='docblock'>Performs the conversion.</div></details></div></details><detailsclass="toggle implementors-toggle"><summary><sectionid="impl-TryInto%3CU%3E-for-AtomicBool"class="impl"><ahref="#impl-TryInto%3CU%3E-for-AtomicBool"class="anchor">§</a><h3class="code-header">impl<T, U> TryInto<U> for T<spanclass="where fmt-newline">where
U: TryFrom<T>,</span></h3></section></summary><divclass="impl-items"><detailsclass="toggle"open><summary><sectionid="associatedtype.Error"class="associatedtype trait-impl"><ahref="#associatedtype.Error"class="anchor">§</a><h4class="code-header">type <aclass="associatedtype">Error</a> = <U as TryFrom<T>>::Error</h4></section></summary><divclass='docblock'>The type returned in the event of a conversion error.</div></details><detailsclass="toggle method-toggle"open><summary><sectionid="method.try_into"class="method trait-impl"><ahref="#method.try_into"class="anchor">§</a><h4class="code-header">fn <aclass="fn">try_into</a>(self) -> Result<U, <U as TryFrom<T>>::Error></h4></section></summary><divclass='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html>