167 lines
No EOL
33 KiB
HTML
167 lines
No EOL
33 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="An integer type which can be safely shared between threads."><title>AtomicU8 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 (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" ><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="#">AtomicU8</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-AtomicU8">Debug</a></li><li><a href="#impl-Default-for-AtomicU8">Default</a></li><li><a href="#impl-From%3Cu8%3E-for-AtomicU8">From<u8></a></li><li><a href="#impl-RefUnwindSafe-for-AtomicU8">RefUnwindSafe</a></li><li><a href="#impl-Sync-for-AtomicU8">Sync</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block"><li><a href="#impl-Send-for-AtomicU8">Send</a></li><li><a href="#impl-Unpin-for-AtomicU8">Unpin</a></li><li><a href="#impl-UnwindSafe-for-AtomicU8">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block"><li><a href="#impl-Any-for-AtomicU8">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-AtomicU8">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-AtomicU8">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-AtomicU8">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-AtomicU8">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-AtomicU8">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-AtomicU8">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="#">AtomicU8</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.34.0">1.34.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 AtomicU8 { /* private fields */ }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An integer type which can be safely shared between threads.</p>
|
||
<p>This type has the same in-memory representation as the underlying
|
||
integer type, [<code>u8</code>]. For more about the differences between atomic types and
|
||
non-atomic types as well as information about the portability of
|
||
this type, please see the <a href="crate::sync::atomic">module-level documentation</a>.</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-AtomicU8" class="impl"><a href="#impl-AtomicU8" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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.34.0">const: 1.34.0</span><h4 class="code-header">pub const fn <a href="#method.new" class="fn">new</a>(v: u8) -> <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</a></h4></section></summary><div class="docblock"><p>Creates a new atomic integer.</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::AtomicU8;
|
||
|
||
<span class="kw">let </span>atomic_forty_two = AtomicU8::new(<span class="number">42</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 u8) -> &'a <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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 reference to an atomic integer 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>, AtomicU8};
|
||
<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>u8 = Box::into_raw(Box::new(<span class="number">0</span>));
|
||
|
||
<span class="macro">assert!</span>(ptr.is_aligned_to(align_of::<AtomicU8>()));
|
||
|
||
{
|
||
<span class="comment">// Create an atomic view of the allocated value
|
||
</span><span class="kw">let </span>atomic = <span class="kw">unsafe </span>{AtomicU8::from_ptr(ptr) };
|
||
|
||
<span class="comment">// Use `atomic` for atomic operations, possibly share it with other threads
|
||
</span>atomic.store(<span class="number">1</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="number">1</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 aligned to <code>align_of::<AtomicU8>()</code> (note that on some platforms this can be bigger than <code>align_of::<u8>()</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"><h4 class="code-header">pub fn <a href="#method.get_mut" class="fn">get_mut</a>(&mut self) -> &mut u8</h4></section></summary><div class="docblock"><p>Returns a mutable reference to the underlying integer.</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::{AtomicU8, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_var = AtomicU8::new(<span class="number">10</span>);
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">*</span>some_var.get_mut(), <span class="number">10</span>);
|
||
<span class="kw-2">*</span>some_var.get_mut() = <span class="number">5</span>;
|
||
<span class="macro">assert_eq!</span>(some_var.load(Ordering::SeqCst), <span class="number">5</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 u8) -> &mut <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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 u8</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::{AtomicU8, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_int = <span class="number">123</span>;
|
||
<span class="kw">let </span>a = AtomicU8::from_mut(<span class="kw-2">&mut </span>some_int);
|
||
a.store(<span class="number">100</span>, Ordering::Relaxed);
|
||
<span class="macro">assert_eq!</span>(some_int, <span class="number">100</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.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</a>]) -> &mut [u8]</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 [AtomicU8]</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::{AtomicU8, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_ints = [<span class="kw">const </span>{ AtomicU8::new(<span class="number">0</span>) }; <span class="number">10</span>];
|
||
|
||
<span class="kw">let </span>view: <span class="kw-2">&mut </span>[u8] = AtomicU8::get_mut_slice(<span class="kw-2">&mut </span>some_ints);
|
||
<span class="macro">assert_eq!</span>(view, [<span class="number">0</span>; <span class="number">10</span>]);
|
||
view
|
||
.iter_mut()
|
||
.enumerate()
|
||
.for_each(|(idx, int)| <span class="kw-2">*</span>int = idx <span class="kw">as _</span>);
|
||
|
||
std::thread::scope(|s| {
|
||
some_ints
|
||
.iter()
|
||
.enumerate()
|
||
.for_each(|(idx, int)| {
|
||
s.spawn(<span class="kw">move </span>|| <span class="macro">assert_eq!</span>(int.load(Ordering::Relaxed), idx <span class="kw">as _</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 [u8]) -> &mut [<a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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 [u8]</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::{AtomicU8, Ordering};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>some_ints = [<span class="number">0</span>; <span class="number">10</span>];
|
||
<span class="kw">let </span>a = <span class="kw-2">&*</span>AtomicU8::from_mut_slice(<span class="kw-2">&mut </span>some_ints);
|
||
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(i <span class="kw">as _</span>, Ordering::Relaxed));
|
||
}
|
||
});
|
||
<span class="kw">for </span>(i, n) <span class="kw">in </span>some_ints.into_iter().enumerate() {
|
||
<span class="macro">assert_eq!</span>(i, n <span class="kw">as </span>usize);
|
||
}</code></pre></div>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.into_inner" class="method"><span class="since rightside" title="const unstable">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) -> u8</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::AtomicU8;
|
||
|
||
<span class="kw">let </span>some_var = AtomicU8::new(<span class="number">5</span>);
|
||
<span class="macro">assert_eq!</span>(some_var.into_inner(), <span class="number">5</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>) -> u8</h4></section></summary><div class="docblock"><p>Loads a value from the atomic integer.</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::{AtomicU8, Ordering};
|
||
|
||
<span class="kw">let </span>some_var = AtomicU8::new(<span class="number">5</span>);
|
||
|
||
<span class="macro">assert_eq!</span>(some_var.load(Ordering::Relaxed), <span class="number">5</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: u8, 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 atomic integer.</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::{AtomicU8, Ordering};
|
||
|
||
<span class="kw">let </span>some_var = AtomicU8::new(<span class="number">5</span>);
|
||
|
||
some_var.store(<span class="number">10</span>, Ordering::Relaxed);
|
||
<span class="macro">assert_eq!</span>(some_var.load(Ordering::Relaxed), <span class="number">10</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 u8</h4></section></summary><div class="docblock"><p>Returns a mutable pointer to the underlying integer.</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 u8</code> instead of <code>&AtomicU8</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::AtomicU8;
|
||
|
||
<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>u8);
|
||
}
|
||
|
||
<span class="kw">let </span>atomic = AtomicU8::new(<span class="number">1</span>);
|
||
|
||
<span class="comment">// SAFETY: Safe as long as `my_atomic_op` is atomic.
|
||
</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-AtomicU8" class="impl"><a href="#impl-Debug-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl Debug for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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-AtomicU8" class="impl"><a href="#impl-Default-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl Default for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</a></h4></section></summary><div class='docblock'>Returns the “default value” for a type. <a>Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-From%3Cu8%3E-for-AtomicU8" class="impl"><a href="#impl-From%3Cu8%3E-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl From<u8> for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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>(v: u8) -> <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</a></h4></section></summary><div class="docblock"><p>Converts an <code>u8</code> into an <code>AtomicU8</code>.</p>
|
||
</div></details></div></details><section id="impl-RefUnwindSafe-for-AtomicU8" class="impl"><a href="#impl-RefUnwindSafe-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl RefUnwindSafe for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</a></h3></section><section id="impl-Sync-for-AtomicU8" class="impl"><a href="#impl-Sync-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl Sync for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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-AtomicU8" class="impl"><a href="#impl-Send-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl Send for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</a></h3></section><section id="impl-Unpin-for-AtomicU8" class="impl"><a href="#impl-Unpin-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl Unpin for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</a></h3></section><section id="impl-UnwindSafe-for-AtomicU8" class="impl"><a href="#impl-UnwindSafe-for-AtomicU8" class="anchor">§</a><h3 class="code-header">impl UnwindSafe for <a class="struct" href="struct.AtomicU8.html" title="struct atomic_polyfill::AtomicU8">AtomicU8</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-AtomicU8" class="impl"><a href="#impl-Any-for-AtomicU8" 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-AtomicU8" class="impl"><a href="#impl-Borrow%3CT%3E-for-AtomicU8" 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-AtomicU8" class="impl"><a href="#impl-BorrowMut%3CT%3E-for-AtomicU8" 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-AtomicU8" class="impl"><a href="#impl-From%3CT%3E-for-AtomicU8" 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-AtomicU8" class="impl"><a href="#impl-Into%3CU%3E-for-AtomicU8" 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-AtomicU8" class="impl"><a href="#impl-TryFrom%3CU%3E-for-AtomicU8" 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-1" class="associatedtype trait-impl"><a href="#associatedtype.Error-1" 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-AtomicU8" class="impl"><a href="#impl-TryInto%3CU%3E-for-AtomicU8" 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" class="associatedtype trait-impl"><a href="#associatedtype.Error" 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> |