blob: 85f5a7d28b0252a99d486d1f9fce193dd0ad470d [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="A crate used for calculating offsets of struct members and their spans."><meta name="keywords" content="rust, rustlang, rust-lang, memoffset"><title>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="../crates.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 mod crate"><!--[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><h2 class="location"><a href="#">Crate memoffset</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.8.0</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#macros">Macros</a></li></ul></section></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">Crate <a class="mod" href="#">memoffset</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/lib.rs.html#21-93">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A crate used for calculating offsets of struct members and their spans.</p>
<p>This functionality currently can not be used in compile time code such as <code>const</code> or <code>const fn</code> definitions.</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::{offset_of, span_of};
<span class="attribute">#[repr(C, packed)]
</span><span class="kw">struct </span>HelpMeIAmTrappedInAStructFactory {
help_me_before_they_: [u8; <span class="number">15</span>],
a: u32
}
<span class="kw">fn </span>main() {
<span class="macro">assert_eq!</span>(<span class="macro">offset_of!</span>(HelpMeIAmTrappedInAStructFactory, a), <span class="number">15</span>);
<span class="macro">assert_eq!</span>(<span class="macro">span_of!</span>(HelpMeIAmTrappedInAStructFactory, a), <span class="number">15</span>..<span class="number">19</span>);
<span class="macro">assert_eq!</span>(<span class="macro">span_of!</span>(HelpMeIAmTrappedInAStructFactory, help_me_before_they_ .. a), <span class="number">0</span>..<span class="number">15</span>);
}</code></pre></div>
<p>This functionality can be useful, for example, for checksum calculations:</p>
<div class="example-wrap ignore"><div class='tooltip'></div><pre class="rust rust-example-rendered"><code><span class="attribute">#[repr(C, packed)]
</span><span class="kw">struct </span>Message {
header: MessageHeader,
fragment_index: u32,
fragment_count: u32,
payload: [u8; <span class="number">1024</span>],
checksum: u16
}
<span class="kw">let </span>checksum_range = <span class="kw-2">&amp;</span>raw[<span class="macro">span_of!</span>(Message, header..checksum)];
<span class="kw">let </span>checksum = crc16(checksum_range);</code></pre></div>
</div></details><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.offset_of.html" title="memoffset::offset_of macro">offset_of</a></div><div class="item-right docblock-short">Calculates the offset of the specified field from the start of the named struct.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.offset_of_tuple.html" title="memoffset::offset_of_tuple macro">offset_of_tuple</a></div><div class="item-right docblock-short">Calculates the offset of the specified field from the start of the tuple.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.offset_of_union.html" title="memoffset::offset_of_union macro">offset_of_union</a></div><div class="item-right docblock-short">Calculates the offset of the specified union member from the start of the union.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.raw_field.html" title="memoffset::raw_field macro">raw_field</a></div><div class="item-right docblock-short">Computes a const raw pointer to the given field of the given base pointer
to the given parent type.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.raw_field_tuple.html" title="memoffset::raw_field_tuple macro">raw_field_tuple</a></div><div class="item-right docblock-short">Computes a const raw pointer to the given field of the given base pointer
to the given parent tuple typle.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.raw_field_union.html" title="memoffset::raw_field_union macro">raw_field_union</a></div><div class="item-right docblock-short">Computes a const raw pointer to the given field of the given base pointer
to the given parent tuple typle.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.span_of.html" title="memoffset::span_of macro">span_of</a></div><div class="item-right docblock-short">Produces a range instance representing the sub-slice containing the specified member.</div></div></div></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>