blob: f76601c5a0612e1409752ea4789b02f79ca1daa7 [file] [log] [blame]
<!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="Produces a range instance representing the sub-slice containing the specified member."><meta name="keywords" content="rust, rustlang, rust-lang, span_of"><title>span_of in memoffset - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../memoffset/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../memoffset/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In memoffset</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" 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="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Macro <a href="index.html">memoffset</a>::<wbr><a class="macro" href="#">span_of</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/memoffset/span_of.rs.html#95-154">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><div class="example-wrap"><pre class="rust macro"><code><span class="macro">macro_rules! </span>span_of {
(@helper <span class="macro-nonterminal">$root</span>:ident, [] ..=) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, [] ..) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, [] ..) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, [] ..= <span class="macro-nonterminal">$end</span>:tt) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, [] .. <span class="macro-nonterminal">$end</span>:tt) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, # <span class="macro-nonterminal">$begin</span>:tt [] ..= <span class="macro-nonterminal">$end</span>:tt) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, # <span class="macro-nonterminal">$begin</span>:tt [] .. <span class="macro-nonterminal">$end</span>:tt) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, # <span class="macro-nonterminal">$begin</span>:tt [] ..) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, # <span class="macro-nonterminal">$begin</span>:tt [] ..=) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, # <span class="macro-nonterminal">$field</span>:tt []) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, $(# <span class="macro-nonterminal">$begin</span>:tt)+ [] <span class="macro-nonterminal">$tt</span>:tt $(<span class="macro-nonterminal">$rest</span>:tt)<span class="kw-2">*</span>) =&gt; { ... };
(@helper <span class="macro-nonterminal">$root</span>:ident, <span class="macro-nonterminal">$parent</span>:path, [] <span class="macro-nonterminal">$tt</span>:tt $(<span class="macro-nonterminal">$rest</span>:tt)<span class="kw-2">*</span>) =&gt; { ... };
(<span class="macro-nonterminal">$sty</span>:path, $(<span class="macro-nonterminal">$exp</span>:tt)+) =&gt; { ... };
}</code></pre></div>
</div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Produces a range instance representing the sub-slice containing the specified member.</p>
<p>This macro provides 2 forms of differing functionalities.</p>
<p>The first form is identical to the appearance of the <code>offset_of!</code> macro.</p>
<div class="example-wrap ignore"><div class='tooltip'></div><pre class="rust rust-example-rendered"><code><span class="macro">span_of!</span>(Struct, member)</code></pre></div>
<p>The second form of <code>span_of!</code> returns a sub-slice which starts at one field, and ends at another.
The general pattern of this form is:</p>
<div class="example-wrap ignore"><div class='tooltip'></div><pre class="rust rust-example-rendered"><code><span class="comment">// Exclusive
</span><span class="macro">span_of!</span>(Struct, member_a .. member_b)
<span class="comment">// Inclusive
</span><span class="macro">span_of!</span>(Struct, member_a ..= member_b)
<span class="comment">// Open-ended ranges
</span><span class="macro">span_of!</span>(Struct, .. end)
<span class="macro">span_of!</span>(Struct, start ..)</code></pre></div>
<h4 id="note"><a href="#note">Note</a></h4>
<p>This macro uses recursion in order to resolve the range expressions, so there is a limit to
the complexity of the expression.
In order to raise the limit, the compiler’s recursion limit should be lifted.</p>
<h4 id="safety"><a href="#safety">Safety</a></h4>
<p>The inter-field form mentioned above assumes that the first field is positioned before the
second.
This is only guarenteed for <code>repr(C)</code> structs.
Usage with <code>repr(Rust)</code> structs may yield unexpected results, like downward-going ranges,
spans that include unexpected fields, empty spans, or spans that include <em>unexpected</em> padding bytes.</p>
<h3 id="examples"><a href="#examples">Examples</a></h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>memoffset::span_of;
<span class="attribute">#[repr(C)]
</span><span class="kw">struct </span>Florp {
a: u32
}
<span class="attribute">#[repr(C)]
</span><span class="kw">struct </span>Blarg {
x: [u32; <span class="number">2</span>],
y: [u8; <span class="number">56</span>],
z: Florp,
egg: [[u8; <span class="number">4</span>]; <span class="number">4</span>]
}
<span class="kw">fn </span>main() {
<span class="macro">assert_eq!</span>(<span class="number">0</span>..<span class="number">84</span>, <span class="macro">span_of!</span>(Blarg, ..));
<span class="macro">assert_eq!</span>(<span class="number">0</span>..<span class="number">8</span>, <span class="macro">span_of!</span>(Blarg, .. y));
<span class="macro">assert_eq!</span>(<span class="number">0</span>..<span class="number">64</span>, <span class="macro">span_of!</span>(Blarg, ..= y));
<span class="macro">assert_eq!</span>(<span class="number">0</span>..<span class="number">8</span>, <span class="macro">span_of!</span>(Blarg, x));
<span class="macro">assert_eq!</span>(<span class="number">8</span>..<span class="number">84</span>, <span class="macro">span_of!</span>(Blarg, y ..));
<span class="macro">assert_eq!</span>(<span class="number">0</span>..<span class="number">8</span>, <span class="macro">span_of!</span>(Blarg, x .. y));
<span class="macro">assert_eq!</span>(<span class="number">0</span>..<span class="number">64</span>, <span class="macro">span_of!</span>(Blarg, x ..= y));
}</code></pre></div>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="memoffset" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>