+/livesplit-wrapper

git clone https://git.julia.blue/+/livesplit-wrapper

P1n3appl3deploy: 9b9f353f3b0e319547cabc5dcadc542d5ab54386b0170e4

gh-pages
12.8 KiB272 linesraw
1<!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="Source of the Rust file `/home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/bytemuck-1.12.1/src/offset_of.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>offset_of.rs - source</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="../../source-script.js"></script><script defer src="../../source-files.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 source"><!--[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="../../bytemuck/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../bytemuck/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../bytemuck/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><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"><button type="button">?</button></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></div><section id="main-content" class="content"><div class="example-wrap"><pre class="line-numbers"><span id="1">1</span>
2<span id="2">2</span>
3<span id="3">3</span>
4<span id="4">4</span>
5<span id="5">5</span>
6<span id="6">6</span>
7<span id="7">7</span>
8<span id="8">8</span>
9<span id="9">9</span>
10<span id="10">10</span>
11<span id="11">11</span>
12<span id="12">12</span>
13<span id="13">13</span>
14<span id="14">14</span>
15<span id="15">15</span>
16<span id="16">16</span>
17<span id="17">17</span>
18<span id="18">18</span>
19<span id="19">19</span>
20<span id="20">20</span>
21<span id="21">21</span>
22<span id="22">22</span>
23<span id="23">23</span>
24<span id="24">24</span>
25<span id="25">25</span>
26<span id="26">26</span>
27<span id="27">27</span>
28<span id="28">28</span>
29<span id="29">29</span>
30<span id="30">30</span>
31<span id="31">31</span>
32<span id="32">32</span>
33<span id="33">33</span>
34<span id="34">34</span>
35<span id="35">35</span>
36<span id="36">36</span>
37<span id="37">37</span>
38<span id="38">38</span>
39<span id="39">39</span>
40<span id="40">40</span>
41<span id="41">41</span>
42<span id="42">42</span>
43<span id="43">43</span>
44<span id="44">44</span>
45<span id="45">45</span>
46<span id="46">46</span>
47<span id="47">47</span>
48<span id="48">48</span>
49<span id="49">49</span>
50<span id="50">50</span>
51<span id="51">51</span>
52<span id="52">52</span>
53<span id="53">53</span>
54<span id="54">54</span>
55<span id="55">55</span>
56<span id="56">56</span>
57<span id="57">57</span>
58<span id="58">58</span>
59<span id="59">59</span>
60<span id="60">60</span>
61<span id="61">61</span>
62<span id="62">62</span>
63<span id="63">63</span>
64<span id="64">64</span>
65<span id="65">65</span>
66<span id="66">66</span>
67<span id="67">67</span>
68<span id="68">68</span>
69<span id="69">69</span>
70<span id="70">70</span>
71<span id="71">71</span>
72<span id="72">72</span>
73<span id="73">73</span>
74<span id="74">74</span>
75<span id="75">75</span>
76<span id="76">76</span>
77<span id="77">77</span>
78<span id="78">78</span>
79<span id="79">79</span>
80<span id="80">80</span>
81<span id="81">81</span>
82<span id="82">82</span>
83<span id="83">83</span>
84<span id="84">84</span>
85<span id="85">85</span>
86<span id="86">86</span>
87<span id="87">87</span>
88<span id="88">88</span>
89<span id="89">89</span>
90<span id="90">90</span>
91<span id="91">91</span>
92<span id="92">92</span>
93<span id="93">93</span>
94<span id="94">94</span>
95<span id="95">95</span>
96<span id="96">96</span>
97<span id="97">97</span>
98<span id="98">98</span>
99<span id="99">99</span>
100<span id="100">100</span>
101<span id="101">101</span>
102<span id="102">102</span>
103<span id="103">103</span>
104<span id="104">104</span>
105<span id="105">105</span>
106<span id="106">106</span>
107<span id="107">107</span>
108<span id="108">108</span>
109<span id="109">109</span>
110<span id="110">110</span>
111<span id="111">111</span>
112<span id="112">112</span>
113<span id="113">113</span>
114<span id="114">114</span>
115<span id="115">115</span>
116<span id="116">116</span>
117<span id="117">117</span>
118<span id="118">118</span>
119<span id="119">119</span>
120<span id="120">120</span>
121<span id="121">121</span>
122<span id="122">122</span>
123<span id="123">123</span>
124<span id="124">124</span>
125<span id="125">125</span>
126<span id="126">126</span>
127<span id="127">127</span>
128<span id="128">128</span>
129<span id="129">129</span>
130<span id="130">130</span>
131<span id="131">131</span>
132<span id="132">132</span>
133<span id="133">133</span>
134<span id="134">134</span>
135<span id="135">135</span>
136</pre><pre class="rust"><code><span class="attribute">#![forbid(unsafe_code)]
137
138</span><span class="doccomment">/// Find the offset in bytes of the given `$field` of `$Type`. Requires an
139/// already initialized `$instance` value to work with.
140///
141/// This is similar to the macro from [`memoffset`](https://docs.rs/memoffset),
142/// however it uses no `unsafe` code.
143///
144/// This macro has a 3-argument and 2-argument version.
145/// * In the 3-arg version you specify an instance of the type, the type itself,
146///   and the field name.
147/// * In the 2-arg version the macro will call the [`default`](Default::default)
148///   method to make a temporary instance of the type for you.
149///
150/// The output of this macro is the byte offset of the field (as a `usize`). The
151/// calculations of the macro are fixed across the entire program, but if the
152/// type used is `repr(Rust)` then they&#39;re *not* fixed across compilations or
153/// compilers.
154///
155/// ## Examples
156///
157/// ### 3-arg Usage
158///
159/// ```rust
160/// # use bytemuck::offset_of;
161/// // enums can&#39;t derive default, and for this example we don&#39;t pick one
162/// enum MyExampleEnum {
163///   A,
164///   B,
165///   C,
166/// }
167///
168/// // so now our struct here doesn&#39;t have Default
169/// #[repr(C)]
170/// struct MyNotDefaultType {
171///   pub counter: i32,
172///   pub some_field: MyExampleEnum,
173/// }
174///
175/// // but we provide an instance of the type and it&#39;s all good.
176/// let val = MyNotDefaultType { counter: 5, some_field: MyExampleEnum::A };
177/// assert_eq!(offset_of!(val, MyNotDefaultType, some_field), 4);
178/// ```
179///
180/// ### 2-arg Usage
181///
182/// ```rust
183/// # use bytemuck::offset_of;
184/// #[derive(Default)]
185/// #[repr(C)]
186/// struct Vertex {
187///   pub loc: [f32; 3],
188///   pub color: [f32; 3],
189/// }
190/// // if the type impls Default the macro can make its own default instance.
191/// assert_eq!(offset_of!(Vertex, loc), 0);
192/// assert_eq!(offset_of!(Vertex, color), 12);
193/// ```
194///
195/// # Usage with `#[repr(packed)]` structs
196///
197/// Attempting to compute the offset of a `#[repr(packed)]` struct with
198/// `bytemuck::offset_of!` requires an `unsafe` block. We hope to relax this in
199/// the future, but currently it is required to work around a soundness hole in
200/// Rust (See [rust-lang/rust#27060]).
201///
202/// [rust-lang/rust#27060]: https://github.com/rust-lang/rust/issues/27060
203///
204/// &lt;p style=&quot;background:rgba(255,181,77,0.16);padding:0.75em;&quot;&gt;
205/// &lt;strong&gt;Warning:&lt;/strong&gt; This is only true for versions of bytemuck &gt;
206/// 1.4.0. Previous versions of
207/// &lt;code style=&quot;background:rgba(41,24,0,0.1);&quot;&gt;bytemuck::offset_of!&lt;/code&gt;
208/// will only emit a warning when used on the field of a packed struct in safe
209/// code, which can lead to unsoundness.
210/// &lt;/p&gt;
211///
212/// For example, the following will fail to compile:
213///
214/// ```compile_fail
215/// #[repr(C, packed)]
216/// #[derive(Default)]
217/// struct Example {
218///   field: u32,
219/// }
220/// // Doesn&#39;t compile:
221/// let _offset = bytemuck::offset_of!(Example, field);
222/// ```
223///
224/// While the error message this generates will mention the
225/// `safe_packed_borrows` lint, the macro will still fail to compile even if
226/// that lint is `#[allow]`ed:
227///
228/// ```compile_fail
229/// # #[repr(C, packed)] #[derive(Default)] struct Example { field: u32 }
230/// // Still doesn&#39;t compile:
231/// #[allow(safe_packed_borrows)]
232/// {
233///   let _offset = bytemuck::offset_of!(Example, field);
234/// }
235/// ```
236///
237/// This *can* be worked around by using `unsafe`, but it is only sound to do so
238/// if you can guarantee that taking a reference to the field is sound.
239///
240/// In practice, this means it only works for fields of align(1) types, or if
241/// you know the field&#39;s offset in advance (defeating the point of `offset_of`)
242/// and can prove that the struct&#39;s alignment and the field&#39;s offset are enough
243/// to prove the field&#39;s alignment.
244///
245/// Once the `raw_ref` macros are available, a future version of this crate will
246/// use them to lift the limitations of packed structs. For the duration of the
247/// `1.x` version of this crate that will be behind an on-by-default cargo
248/// feature (to maintain minimum rust version support).
249</span><span class="attribute">#[macro_export]
250</span><span class="macro">macro_rules! </span>offset_of {
251  (<span class="macro-nonterminal">$instance</span>:expr, <span class="macro-nonterminal">$Type</span>:path, <span class="macro-nonterminal">$field</span>:tt) =&gt; {{
252    <span class="attribute">#[forbid(safe_packed_borrows)]
253    </span>{
254      <span class="comment">// This helps us guard against field access going through a Deref impl.
255      </span><span class="attribute">#[allow(clippy::unneeded_field_pattern)]
256      </span><span class="kw">let </span><span class="macro-nonterminal">$Type </span>{ <span class="macro-nonterminal">$field</span>: <span class="kw">_</span>, .. };
257      <span class="kw">let </span>reference: <span class="kw-2">&amp;</span><span class="macro-nonterminal">$Type </span>= <span class="kw-2">&amp;</span><span class="macro-nonterminal">$instance</span>;
258      <span class="kw">let </span>address = reference <span class="kw">as </span><span class="kw-2">*const </span><span class="kw">_ as </span>usize;
259      <span class="kw">let </span>field_pointer = <span class="kw-2">&amp;</span>reference.<span class="macro-nonterminal">$field </span><span class="kw">as </span><span class="kw-2">*const </span><span class="kw">_ as </span>usize;
260      <span class="comment">// These asserts/unwraps are compiled away at release, and defend against
261      // the case where somehow a deref impl is still invoked.
262      </span><span class="kw">let </span>result = field_pointer.checked_sub(address).unwrap();
263      <span class="macro">assert!</span>(result &lt;= <span class="macro-nonterminal">$crate::__core::mem::size_of</span>::&lt;<span class="macro-nonterminal">$Type</span>&gt;());
264      result
265    }
266  }};
267  (<span class="macro-nonterminal">$Type</span>:path, <span class="macro-nonterminal">$field</span>:tt) =&gt; {{
268    <span class="macro-nonterminal">$</span><span class="macro">crate::offset_of!</span>(&lt;<span class="macro-nonterminal">$Type </span><span class="kw">as </span>Default&gt;::default(), <span class="macro-nonterminal">$Type</span>, <span class="macro-nonterminal">$field</span>)
269  }};
270}
271</code></pre></div>
272</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="bytemuck" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (4a1467723 2022-09-23)" ></div></body></html>