blob: b8c5528ceaca1c2e6f936c2a8e8f0fa4c04f11f4 [file] [log] [blame] [edit]
<!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="Recoverable assertions, inspired by the use of `assert()` in SQLite."><title>stdx::assert - 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" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-84e720fa.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="stdx" data-themes="" data-resource-suffix="" data-rustdoc-version="1.89.0 (29483883e 2025-08-04)" data-channel="1.89.0" data-search-js="search-92309212.js" data-settings-js="settings-5514c975.js" ><script src="../../static.files/storage-4e99c027.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-fd3af306.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-32bb7600.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc mod"><!--[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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../stdx/index.html">stdx</a><span class="version">0.0.0</span></h2></div><div class="sidebar-elems"><div id="rustdoc-modnav"><h2 class="in-crate"><a href="../index.html">In crate stdx</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">stdx</a></div><h1>Module <span>assert</span><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/stdx/assert.rs.html#2-115">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Recoverable assertions, inspired by <a href="https://www.sqlite.org/assert.html">the use of <code>assert()</code> in
SQLite</a>.</p>
<p><code>never!</code> and <code>always!</code> return the actual value of the condition if
<code>debug_assertions</code> are disabled.</p>
<p>Use them when terminating on assertion failure is worse than continuing.</p>
<p>One example would be a critical application like a database:</p>
<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>stdx::never;
<span class="kw">fn </span>apply_transaction(<span class="kw-2">&amp;mut </span><span class="self">self</span>, tx: Transaction) -&gt; <span class="prelude-ty">Result</span>&lt;(), TransactionAborted&gt; {
<span class="kw">let </span>delta = <span class="self">self</span>.compute_delta(<span class="kw-2">&amp;</span>tx);
<span class="kw">if </span><span class="macro">never!</span>(!<span class="self">self</span>.check_internal_invariant(<span class="kw-2">&amp;</span>delta)) {
<span class="comment">// Ok, something in this transaction messed up our internal state.
// This really shouldn't be happening, and this signifies a bug.
// Luckily, we can recover by just rejecting the transaction.
</span><span class="kw">return </span>abort_transaction(tx);
}
<span class="self">self</span>.commit(delta);
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
<p>Another example is assertions about non-critical functionality in usual apps</p>
<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>stdx::never;
<span class="kw">let </span>english_message = <span class="string">"super app installed!"
</span><span class="kw">let </span><span class="kw-2">mut </span>local_message = localize(english_message);
<span class="kw">if </span><span class="macro">never!</span>(local_message.is_empty(), <span class="string">"missing localization for {}"</span>, english_message) {
<span class="comment">// We localized all the messages but this one slipper through the cracks?
// Better to show the english one then than to fail outright;
</span>local_message = english_message;
}
<span class="macro">println!</span>(<span class="string">"{}"</span>, local_message);</code></pre></div>
</div></details></section></div></main></body></html>