47 lines
6.3 KiB
HTML
47 lines
6.3 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 macro that matches flags values, similar to Rust’s `match` statement."><title>bitflags_match in bitflags - 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="bitflags" 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="sidebar-items.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 macro"><!--[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="#">bitflags_match</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../bitflags/index.html">bitflags</a><span class="version">2.11.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">bitflags_<wbr>match</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#syntax" title="Syntax">Syntax</a></li><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#how-it-works" title="How it works">How it works</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate bitflags</a></h2></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"><div class="rustdoc-breadcrumbs"><a href="index.html">bitflags</a></div><h1>Macro <span class="macro">bitflags_<wbr>match</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/bitflags/lib.rs.html#817-827">Source</a> </span></div><pre class="rust item-decl"><code>macro_rules! bitflags_match {
|
||
($operation:expr, {
|
||
$($t:tt)*
|
||
}) => { ... };
|
||
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A macro that matches flags values, similar to Rust’s <code>match</code> statement.</p>
|
||
<p>In a regular <code>match</code> statement, the syntax <code>Flag::A | Flag::B</code> is interpreted as an or-pattern,
|
||
instead of the bitwise-or of <code>Flag::A</code> and <code>Flag::B</code>. This can be surprising when combined with flags types
|
||
because <code>Flag::A | Flag::B</code> won’t match the pattern <code>Flag::A | Flag::B</code>. This macro is an alternative to
|
||
<code>match</code> for flags values that doesn’t have this issue.</p>
|
||
<h2 id="syntax"><a class="doc-anchor" href="#syntax">§</a>Syntax</h2>
|
||
<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="macro">bitflags_match!</span>(expression, {
|
||
pattern1 => result1,
|
||
pattern2 => result2,
|
||
..
|
||
<span class="kw">_ </span>=> default_result,
|
||
})</code></pre></div>
|
||
<p>The final <code>_ => default_result</code> arm is required, otherwise the macro will fail to compile.</p>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>bitflags::{bitflags, bitflags_match};
|
||
|
||
<span class="macro">bitflags!</span> {
|
||
<span class="attr">#[derive(PartialEq)]
|
||
</span><span class="kw">struct </span>Flags: u8 {
|
||
<span class="kw">const </span>A = <span class="number">1 </span><< <span class="number">0</span>;
|
||
<span class="kw">const </span>B = <span class="number">1 </span><< <span class="number">1</span>;
|
||
<span class="kw">const </span>C = <span class="number">1 </span><< <span class="number">2</span>;
|
||
}
|
||
}
|
||
|
||
<span class="kw">let </span>flags = Flags::A | Flags::B;
|
||
|
||
<span class="comment">// Prints `the value is A and B`
|
||
</span><span class="macro">bitflags_match!</span>(flags, {
|
||
Flags::A | Flags::B => <span class="macro">println!</span>(<span class="string">"the value is A and B"</span>),
|
||
<span class="kw">_ </span>=> <span class="macro">println!</span>(<span class="string">"the value is not A and B"</span>),
|
||
});
|
||
|
||
<span class="comment">// Prints `the value is not A`
|
||
</span><span class="macro">bitflags_match!</span>(flags, {
|
||
Flags::A => <span class="macro">println!</span>(<span class="string">"the value is A"</span>),
|
||
<span class="kw">_ </span>=> <span class="macro">println!</span>(<span class="string">"the value is not A"</span>),
|
||
});</code></pre></div><h2 id="how-it-works"><a class="doc-anchor" href="#how-it-works">§</a>How it works</h2>
|
||
<p>The macro expands to a series of <code>if</code> statements, <strong>checking equality</strong> between the input expression
|
||
and each pattern. This allows for correct matching of bitflag combinations, which is not possible
|
||
with a regular match expression due to the way bitflags are implemented.</p>
|
||
<p>Patterns are evaluated in the order they appear in the macro.</p>
|
||
</div></details></section></div></main></body></html> |