Files
GopherGate/target/doc/try_lock/index.html
2026-02-26 12:00:21 -05:00

34 lines
5.0 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 light-weight lock guarded by an atomic boolean."><title>try_lock - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="try_lock" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate try_lock</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../try_lock/index.html">try_<wbr>lock</a><span class="version">0.2.5</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li></ul><h3><a href="#structs">Crate Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>try_<wbr>lock</span>&nbsp;<button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/try_lock/lib.rs.html#1-266">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A light-weight lock guarded by an atomic boolean.</p>
<p>Most efficient when contention is low, acquiring the lock is a single
atomic swap, and releasing it just 1 more atomic swap.</p>
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::sync::Arc;
<span class="kw">use </span>try_lock::TryLock;
<span class="comment">// a thing we want to share
</span><span class="kw">struct </span>Widget {
name: String,
}
<span class="comment">// lock it up!
</span><span class="kw">let </span>widget1 = Arc::new(TryLock::new(Widget {
name: <span class="string">"Spanner"</span>.into(),
}));
<span class="kw">let </span>widget2 = widget1.clone();
<span class="comment">// mutate the widget
</span><span class="kw">let </span><span class="kw-2">mut </span>locked = widget1.try_lock().expect(<span class="string">"example isn't locked yet"</span>);
locked.name.push_str(<span class="string">" Bundle"</span>);
<span class="comment">// hands off, buddy
</span><span class="kw">let </span>not_locked = widget2.try_lock();
<span class="macro">assert!</span>(not_locked.is_none(), <span class="string">"widget1 has the lock"</span>);
<span class="comment">// ok, you can have it
</span>drop(locked);
<span class="kw">let </span>locked2 = widget2.try_lock().expect(<span class="string">"widget1 lock is released"</span>);
<span class="macro">assert_eq!</span>(locked2.name, <span class="string">"Spanner Bundle"</span>);</code></pre></div></div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Locked.html" title="struct try_lock::Locked">Locked</a></dt><dd>A locked value acquired from a <code>TryLock</code>.</dd><dt><a class="struct" href="struct.TryLock.html" title="struct try_lock::TryLock">TryLock</a></dt><dd>A light-weight lock guarded by an atomic boolean.</dd></dl></section></div></main></body></html>