+/livesplit-wrapper

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

P1n3appl3deploy: 9b9f353f3b0e319547cabc5dcadc542d5ab54386b0170e4

gh-pages
38.4 KiB976 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/checked.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>checked.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<span id="136">136</span>
137<span id="137">137</span>
138<span id="138">138</span>
139<span id="139">139</span>
140<span id="140">140</span>
141<span id="141">141</span>
142<span id="142">142</span>
143<span id="143">143</span>
144<span id="144">144</span>
145<span id="145">145</span>
146<span id="146">146</span>
147<span id="147">147</span>
148<span id="148">148</span>
149<span id="149">149</span>
150<span id="150">150</span>
151<span id="151">151</span>
152<span id="152">152</span>
153<span id="153">153</span>
154<span id="154">154</span>
155<span id="155">155</span>
156<span id="156">156</span>
157<span id="157">157</span>
158<span id="158">158</span>
159<span id="159">159</span>
160<span id="160">160</span>
161<span id="161">161</span>
162<span id="162">162</span>
163<span id="163">163</span>
164<span id="164">164</span>
165<span id="165">165</span>
166<span id="166">166</span>
167<span id="167">167</span>
168<span id="168">168</span>
169<span id="169">169</span>
170<span id="170">170</span>
171<span id="171">171</span>
172<span id="172">172</span>
173<span id="173">173</span>
174<span id="174">174</span>
175<span id="175">175</span>
176<span id="176">176</span>
177<span id="177">177</span>
178<span id="178">178</span>
179<span id="179">179</span>
180<span id="180">180</span>
181<span id="181">181</span>
182<span id="182">182</span>
183<span id="183">183</span>
184<span id="184">184</span>
185<span id="185">185</span>
186<span id="186">186</span>
187<span id="187">187</span>
188<span id="188">188</span>
189<span id="189">189</span>
190<span id="190">190</span>
191<span id="191">191</span>
192<span id="192">192</span>
193<span id="193">193</span>
194<span id="194">194</span>
195<span id="195">195</span>
196<span id="196">196</span>
197<span id="197">197</span>
198<span id="198">198</span>
199<span id="199">199</span>
200<span id="200">200</span>
201<span id="201">201</span>
202<span id="202">202</span>
203<span id="203">203</span>
204<span id="204">204</span>
205<span id="205">205</span>
206<span id="206">206</span>
207<span id="207">207</span>
208<span id="208">208</span>
209<span id="209">209</span>
210<span id="210">210</span>
211<span id="211">211</span>
212<span id="212">212</span>
213<span id="213">213</span>
214<span id="214">214</span>
215<span id="215">215</span>
216<span id="216">216</span>
217<span id="217">217</span>
218<span id="218">218</span>
219<span id="219">219</span>
220<span id="220">220</span>
221<span id="221">221</span>
222<span id="222">222</span>
223<span id="223">223</span>
224<span id="224">224</span>
225<span id="225">225</span>
226<span id="226">226</span>
227<span id="227">227</span>
228<span id="228">228</span>
229<span id="229">229</span>
230<span id="230">230</span>
231<span id="231">231</span>
232<span id="232">232</span>
233<span id="233">233</span>
234<span id="234">234</span>
235<span id="235">235</span>
236<span id="236">236</span>
237<span id="237">237</span>
238<span id="238">238</span>
239<span id="239">239</span>
240<span id="240">240</span>
241<span id="241">241</span>
242<span id="242">242</span>
243<span id="243">243</span>
244<span id="244">244</span>
245<span id="245">245</span>
246<span id="246">246</span>
247<span id="247">247</span>
248<span id="248">248</span>
249<span id="249">249</span>
250<span id="250">250</span>
251<span id="251">251</span>
252<span id="252">252</span>
253<span id="253">253</span>
254<span id="254">254</span>
255<span id="255">255</span>
256<span id="256">256</span>
257<span id="257">257</span>
258<span id="258">258</span>
259<span id="259">259</span>
260<span id="260">260</span>
261<span id="261">261</span>
262<span id="262">262</span>
263<span id="263">263</span>
264<span id="264">264</span>
265<span id="265">265</span>
266<span id="266">266</span>
267<span id="267">267</span>
268<span id="268">268</span>
269<span id="269">269</span>
270<span id="270">270</span>
271<span id="271">271</span>
272<span id="272">272</span>
273<span id="273">273</span>
274<span id="274">274</span>
275<span id="275">275</span>
276<span id="276">276</span>
277<span id="277">277</span>
278<span id="278">278</span>
279<span id="279">279</span>
280<span id="280">280</span>
281<span id="281">281</span>
282<span id="282">282</span>
283<span id="283">283</span>
284<span id="284">284</span>
285<span id="285">285</span>
286<span id="286">286</span>
287<span id="287">287</span>
288<span id="288">288</span>
289<span id="289">289</span>
290<span id="290">290</span>
291<span id="291">291</span>
292<span id="292">292</span>
293<span id="293">293</span>
294<span id="294">294</span>
295<span id="295">295</span>
296<span id="296">296</span>
297<span id="297">297</span>
298<span id="298">298</span>
299<span id="299">299</span>
300<span id="300">300</span>
301<span id="301">301</span>
302<span id="302">302</span>
303<span id="303">303</span>
304<span id="304">304</span>
305<span id="305">305</span>
306<span id="306">306</span>
307<span id="307">307</span>
308<span id="308">308</span>
309<span id="309">309</span>
310<span id="310">310</span>
311<span id="311">311</span>
312<span id="312">312</span>
313<span id="313">313</span>
314<span id="314">314</span>
315<span id="315">315</span>
316<span id="316">316</span>
317<span id="317">317</span>
318<span id="318">318</span>
319<span id="319">319</span>
320<span id="320">320</span>
321<span id="321">321</span>
322<span id="322">322</span>
323<span id="323">323</span>
324<span id="324">324</span>
325<span id="325">325</span>
326<span id="326">326</span>
327<span id="327">327</span>
328<span id="328">328</span>
329<span id="329">329</span>
330<span id="330">330</span>
331<span id="331">331</span>
332<span id="332">332</span>
333<span id="333">333</span>
334<span id="334">334</span>
335<span id="335">335</span>
336<span id="336">336</span>
337<span id="337">337</span>
338<span id="338">338</span>
339<span id="339">339</span>
340<span id="340">340</span>
341<span id="341">341</span>
342<span id="342">342</span>
343<span id="343">343</span>
344<span id="344">344</span>
345<span id="345">345</span>
346<span id="346">346</span>
347<span id="347">347</span>
348<span id="348">348</span>
349<span id="349">349</span>
350<span id="350">350</span>
351<span id="351">351</span>
352<span id="352">352</span>
353<span id="353">353</span>
354<span id="354">354</span>
355<span id="355">355</span>
356<span id="356">356</span>
357<span id="357">357</span>
358<span id="358">358</span>
359<span id="359">359</span>
360<span id="360">360</span>
361<span id="361">361</span>
362<span id="362">362</span>
363<span id="363">363</span>
364<span id="364">364</span>
365<span id="365">365</span>
366<span id="366">366</span>
367<span id="367">367</span>
368<span id="368">368</span>
369<span id="369">369</span>
370<span id="370">370</span>
371<span id="371">371</span>
372<span id="372">372</span>
373<span id="373">373</span>
374<span id="374">374</span>
375<span id="375">375</span>
376<span id="376">376</span>
377<span id="377">377</span>
378<span id="378">378</span>
379<span id="379">379</span>
380<span id="380">380</span>
381<span id="381">381</span>
382<span id="382">382</span>
383<span id="383">383</span>
384<span id="384">384</span>
385<span id="385">385</span>
386<span id="386">386</span>
387<span id="387">387</span>
388<span id="388">388</span>
389<span id="389">389</span>
390<span id="390">390</span>
391<span id="391">391</span>
392<span id="392">392</span>
393<span id="393">393</span>
394<span id="394">394</span>
395<span id="395">395</span>
396<span id="396">396</span>
397<span id="397">397</span>
398<span id="398">398</span>
399<span id="399">399</span>
400<span id="400">400</span>
401<span id="401">401</span>
402<span id="402">402</span>
403<span id="403">403</span>
404<span id="404">404</span>
405<span id="405">405</span>
406<span id="406">406</span>
407<span id="407">407</span>
408<span id="408">408</span>
409<span id="409">409</span>
410<span id="410">410</span>
411<span id="411">411</span>
412<span id="412">412</span>
413<span id="413">413</span>
414<span id="414">414</span>
415<span id="415">415</span>
416<span id="416">416</span>
417<span id="417">417</span>
418<span id="418">418</span>
419<span id="419">419</span>
420<span id="420">420</span>
421<span id="421">421</span>
422<span id="422">422</span>
423<span id="423">423</span>
424<span id="424">424</span>
425<span id="425">425</span>
426<span id="426">426</span>
427<span id="427">427</span>
428<span id="428">428</span>
429<span id="429">429</span>
430<span id="430">430</span>
431<span id="431">431</span>
432<span id="432">432</span>
433<span id="433">433</span>
434<span id="434">434</span>
435<span id="435">435</span>
436<span id="436">436</span>
437<span id="437">437</span>
438<span id="438">438</span>
439<span id="439">439</span>
440<span id="440">440</span>
441<span id="441">441</span>
442<span id="442">442</span>
443<span id="443">443</span>
444<span id="444">444</span>
445<span id="445">445</span>
446<span id="446">446</span>
447<span id="447">447</span>
448<span id="448">448</span>
449<span id="449">449</span>
450<span id="450">450</span>
451<span id="451">451</span>
452<span id="452">452</span>
453<span id="453">453</span>
454<span id="454">454</span>
455<span id="455">455</span>
456<span id="456">456</span>
457<span id="457">457</span>
458<span id="458">458</span>
459<span id="459">459</span>
460<span id="460">460</span>
461<span id="461">461</span>
462<span id="462">462</span>
463<span id="463">463</span>
464<span id="464">464</span>
465<span id="465">465</span>
466<span id="466">466</span>
467<span id="467">467</span>
468<span id="468">468</span>
469<span id="469">469</span>
470<span id="470">470</span>
471<span id="471">471</span>
472<span id="472">472</span>
473<span id="473">473</span>
474<span id="474">474</span>
475<span id="475">475</span>
476<span id="476">476</span>
477<span id="477">477</span>
478<span id="478">478</span>
479<span id="479">479</span>
480<span id="480">480</span>
481<span id="481">481</span>
482<span id="482">482</span>
483<span id="483">483</span>
484<span id="484">484</span>
485<span id="485">485</span>
486<span id="486">486</span>
487<span id="487">487</span>
488</pre><pre class="rust"><code><span class="doccomment">//! Checked versions of the casting functions exposed in crate root
489//! that support [`CheckedBitPattern`] types.
490
491</span><span class="kw">use crate</span>::{
492  internal::{<span class="self">self</span>, something_went_wrong},
493  AnyBitPattern, NoUninit,
494};
495
496<span class="doccomment">/// A marker trait that allows types that have some invalid bit patterns to be
497/// used in places that otherwise require [`AnyBitPattern`] or [`Pod`] types by
498/// performing a runtime check on a perticular set of bits. This is particularly
499/// useful for types like fieldless (&#39;C-style&#39;) enums, [`char`], bool, and
500/// structs containing them.
501///
502/// To do this, we define a `Bits` type which is a type with equivalent layout
503/// to `Self` other than the invalid bit patterns which disallow `Self` from
504/// being [`AnyBitPattern`]. This `Bits` type must itself implement
505/// [`AnyBitPattern`]. Then, we implement a function that checks wheter a
506/// certain instance of the `Bits` is also a valid bit pattern of `Self`. If
507/// this check passes, then we can allow casting from the `Bits` to `Self` (and
508/// therefore, any type which is able to be cast to `Bits` is also able to be
509/// cast to `Self`).
510///
511/// [`AnyBitPattern`] is a subset of [`CheckedBitPattern`], meaning that any `T:
512/// AnyBitPattern` is also [`CheckedBitPattern`]. This means you can also use
513/// any [`AnyBitPattern`] type in the checked versions of casting functions in
514/// this module. If it&#39;s possible, prefer implementing [`AnyBitPattern`] for
515/// your type directly instead of [`CheckedBitPattern`] as it gives greater
516/// flexibility.
517///
518/// # Derive
519///
520/// A `#[derive(CheckedBitPattern)]` macro is provided under the `derive`
521/// feature flag which will automatically validate the requirements of this
522/// trait and implement the trait for you for both enums and structs. This is
523/// the recommended method for implementing the trait, however it&#39;s also
524/// possible to do manually.
525///
526/// # Example
527///
528/// If manually implementing the trait, we can do something like so:
529///
530/// ```rust
531/// use bytemuck::{CheckedBitPattern, NoUninit};
532///
533/// #[repr(u32)]
534/// #[derive(Copy, Clone)]
535/// enum MyEnum {
536///     Variant0 = 0,
537///     Variant1 = 1,
538///     Variant2 = 2,
539/// }
540///
541/// unsafe impl CheckedBitPattern for MyEnum {
542///     type Bits = u32;
543///
544///     fn is_valid_bit_pattern(bits: &amp;u32) -&gt; bool {
545///         match *bits {
546///             0 | 1 | 2 =&gt; true,
547///             _ =&gt; false,
548///         }
549///     }
550/// }
551///
552/// // It is often useful to also implement `NoUninit` on our `CheckedBitPattern` types.
553/// // This will allow us to do casting of mutable references (and mutable slices).
554/// // It is not always possible to do so, but in this case we have no padding so it is.
555/// unsafe impl NoUninit for MyEnum {}
556/// ```
557///
558/// We can now use relevant casting functions. For example,
559///
560/// ```rust
561/// # use bytemuck::{CheckedBitPattern, NoUninit};
562/// # #[repr(u32)]
563/// # #[derive(Copy, Clone, PartialEq, Eq, Debug)]
564/// # enum MyEnum {
565/// #     Variant0 = 0,
566/// #     Variant1 = 1,
567/// #     Variant2 = 2,
568/// # }
569/// # unsafe impl NoUninit for MyEnum {}
570/// # unsafe impl CheckedBitPattern for MyEnum {
571/// #     type Bits = u32;
572/// #     fn is_valid_bit_pattern(bits: &amp;u32) -&gt; bool {
573/// #         match *bits {
574/// #             0 | 1 | 2 =&gt; true,
575/// #             _ =&gt; false,
576/// #         }
577/// #     }
578/// # }
579/// use bytemuck::{bytes_of, bytes_of_mut};
580/// use bytemuck::checked;
581///
582/// let bytes = bytes_of(&amp;2u32);
583/// let result = checked::try_from_bytes::&lt;MyEnum&gt;(bytes);
584/// assert_eq!(result, Ok(&amp;MyEnum::Variant2));
585///
586/// // Fails for invalid discriminant
587/// let bytes = bytes_of(&amp;100u32);
588/// let result = checked::try_from_bytes::&lt;MyEnum&gt;(bytes);
589/// assert!(result.is_err());
590///
591/// // Since we implemented NoUninit, we can also cast mutably from an original type
592/// // that is `NoUninit + AnyBitPattern`:
593/// let mut my_u32 = 2u32;
594/// {
595///   let as_enum_mut = checked::cast_mut::&lt;_, MyEnum&gt;(&amp;mut my_u32);
596///   assert_eq!(as_enum_mut, &amp;mut MyEnum::Variant2);
597///   *as_enum_mut = MyEnum::Variant0;
598/// }
599/// assert_eq!(my_u32, 0u32);
600/// ```
601///
602/// # Safety
603///
604/// * `Self` *must* have the same layout as the specified `Bits` except for
605/// the possible invalid bit patterns being checked during
606/// [`is_valid_bit_pattern`].
607///   * This almost certainly means your type must be `#[repr(C)]` or a similar
608///   specified repr, but if you think you know better, you probably don&#39;t. If
609/// you   still think you know better, be careful and have fun. And don&#39;t mess
610/// it up   (I mean it).
611/// * If [`is_valid_bit_pattern`] returns true, then the bit pattern contained
612///   in `bits` must also be valid for an instance of `Self`.
613/// * Probably more, don&#39;t mess it up (I mean it 2.0)
614///
615/// [`is_valid_bit_pattern`]: CheckedBitPattern::is_valid_bit_pattern
616/// [`Pod`]: crate::Pod
617</span><span class="kw">pub unsafe trait </span>CheckedBitPattern: Copy {
618  <span class="doccomment">/// `Self` *must* have the same layout as the specified `Bits` except for
619  /// the possible invalid bit patterns being checked during
620  /// [`is_valid_bit_pattern`].
621  ///
622  /// [`is_valid_bit_pattern`]: CheckedBitPattern::is_valid_bit_pattern
623  </span><span class="kw">type </span>Bits: AnyBitPattern;
624
625  <span class="doccomment">/// If this function returns true, then it must be valid to reinterpret `bits`
626  /// as `&amp;Self`.
627  </span><span class="kw">fn </span>is_valid_bit_pattern(bits: <span class="kw-2">&amp;</span><span class="self">Self</span>::Bits) -&gt; bool;
628}
629
630<span class="kw">unsafe impl</span>&lt;T: AnyBitPattern&gt; CheckedBitPattern <span class="kw">for </span>T {
631  <span class="kw">type </span>Bits = T;
632
633  <span class="attribute">#[inline(always)]
634  </span><span class="kw">fn </span>is_valid_bit_pattern(_bits: <span class="kw-2">&amp;</span>T) -&gt; bool {
635    <span class="bool-val">true
636  </span>}
637}
638
639<span class="kw">unsafe impl </span>CheckedBitPattern <span class="kw">for </span>char {
640  <span class="kw">type </span>Bits = u32;
641
642  <span class="attribute">#[inline]
643  </span><span class="kw">fn </span>is_valid_bit_pattern(bits: <span class="kw-2">&amp;</span><span class="self">Self</span>::Bits) -&gt; bool {
644    core::char::from_u32(<span class="kw-2">*</span>bits).is_some()
645  }
646}
647
648<span class="kw">unsafe impl </span>CheckedBitPattern <span class="kw">for </span>bool {
649  <span class="kw">type </span>Bits = u8;
650
651  <span class="attribute">#[inline]
652  </span><span class="kw">fn </span>is_valid_bit_pattern(bits: <span class="kw-2">&amp;</span><span class="self">Self</span>::Bits) -&gt; bool {
653    <span class="kw">match </span><span class="kw-2">*</span>bits {
654      <span class="number">0 </span>| <span class="number">1 </span>=&gt; <span class="bool-val">true</span>,
655      <span class="kw">_ </span>=&gt; <span class="bool-val">false</span>,
656    }
657  }
658}
659
660<span class="doccomment">/// The things that can go wrong when casting between [`CheckedBitPattern`] data
661/// forms.
662</span><span class="attribute">#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
663</span><span class="kw">pub enum </span>CheckedCastError {
664  <span class="doccomment">/// An error occurred during a true-[`Pod`] cast
665  </span>PodCastError(<span class="kw">crate</span>::PodCastError),
666  <span class="doccomment">/// When casting to a [`CheckedBitPattern`] type, it is possible that the
667  /// original data contains an invalid bit pattern. If so, the cast will
668  /// fail and this error will be returned. Will never happen on casts
669  /// between [`Pod`] types.
670  </span>InvalidBitPattern,
671}
672
673<span class="attribute">#[cfg(not(target_arch = <span class="string">&quot;spirv&quot;</span>))]
674</span><span class="kw">impl </span>core::fmt::Display <span class="kw">for </span>CheckedCastError {
675  <span class="kw">fn </span>fmt(<span class="kw-2">&amp;</span><span class="self">self</span>, f: <span class="kw-2">&amp;mut </span>core::fmt::Formatter) -&gt; core::fmt::Result {
676    <span class="macro">write!</span>(f, <span class="string">&quot;{:?}&quot;</span>, <span class="self">self</span>)
677  }
678}
679<span class="attribute">#[cfg(feature = <span class="string">&quot;extern_crate_std&quot;</span>)]
680</span><span class="kw">impl </span>std::error::Error <span class="kw">for </span>CheckedCastError {}
681
682<span class="kw">impl </span>From&lt;<span class="kw">crate</span>::PodCastError&gt; <span class="kw">for </span>CheckedCastError {
683  <span class="kw">fn </span>from(err: <span class="kw">crate</span>::PodCastError) -&gt; CheckedCastError {
684    CheckedCastError::PodCastError(err)
685  }
686}
687
688<span class="doccomment">/// Re-interprets `&amp;[u8]` as `&amp;T`.
689///
690/// ## Failure
691///
692/// * If the slice isn&#39;t aligned for the new type
693/// * If the slice&#39;s length isn’t exactly the size of the new type
694/// * If the slice contains an invalid bit pattern for `T`
695</span><span class="attribute">#[inline]
696</span><span class="kw">pub fn </span>try_from_bytes&lt;T: CheckedBitPattern&gt;(
697  s: <span class="kw-2">&amp;</span>[u8],
698) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="kw-2">&amp;</span>T, CheckedCastError&gt; {
699  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_from_bytes(s) }<span class="question-mark">?</span>;
700
701  <span class="kw">if </span>&lt;T <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(pod) {
702    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{ <span class="kw-2">&amp;*</span>(pod <span class="kw">as </span><span class="kw-2">*const </span>&lt;T <span class="kw">as </span>CheckedBitPattern&gt;::Bits <span class="kw">as </span><span class="kw-2">*const </span>T) })
703  } <span class="kw">else </span>{
704    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
705  }
706}
707
708<span class="doccomment">/// Re-interprets `&amp;mut [u8]` as `&amp;mut T`.
709///
710/// ## Failure
711///
712/// * If the slice isn&#39;t aligned for the new type
713/// * If the slice&#39;s length isn’t exactly the size of the new type
714/// * If the slice contains an invalid bit pattern for `T`
715</span><span class="attribute">#[inline]
716</span><span class="kw">pub fn </span>try_from_bytes_mut&lt;T: CheckedBitPattern + NoUninit&gt;(
717  s: <span class="kw-2">&amp;mut </span>[u8],
718) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="kw-2">&amp;mut </span>T, CheckedCastError&gt; {
719  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_from_bytes_mut(s) }<span class="question-mark">?</span>;
720
721  <span class="kw">if </span>&lt;T <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(pod) {
722    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{ <span class="kw-2">&amp;mut *</span>(pod <span class="kw">as </span><span class="kw-2">*mut </span>&lt;T <span class="kw">as </span>CheckedBitPattern&gt;::Bits <span class="kw">as </span><span class="kw-2">*mut </span>T) })
723  } <span class="kw">else </span>{
724    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
725  }
726}
727
728<span class="doccomment">/// Reads from the bytes as if they were a `T`.
729///
730/// ## Failure
731/// * If the `bytes` length is not equal to `size_of::&lt;T&gt;()`.
732/// * If the slice contains an invalid bit pattern for `T`
733</span><span class="attribute">#[inline]
734</span><span class="kw">pub fn </span>try_pod_read_unaligned&lt;T: CheckedBitPattern&gt;(
735  bytes: <span class="kw-2">&amp;</span>[u8],
736) -&gt; <span class="prelude-ty">Result</span>&lt;T, CheckedCastError&gt; {
737  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_pod_read_unaligned(bytes) }<span class="question-mark">?</span>;
738
739  <span class="kw">if </span>&lt;T <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(pod) {
740    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{ <span class="macro">transmute!</span>(pod) })
741  } <span class="kw">else </span>{
742    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
743  }
744}
745
746<span class="doccomment">/// Try to cast `T` into `U`.
747///
748/// Note that for this particular type of cast, alignment isn&#39;t a factor. The
749/// input value is semantically copied into the function and then returned to a
750/// new memory location which will have whatever the required alignment of the
751/// output type is.
752///
753/// ## Failure
754///
755/// * If the types don&#39;t have the same size this fails.
756/// * If `a` contains an invalid bit pattern for `B` this fails.
757</span><span class="attribute">#[inline]
758</span><span class="kw">pub fn </span>try_cast&lt;A: NoUninit, B: CheckedBitPattern&gt;(
759  a: A,
760) -&gt; <span class="prelude-ty">Result</span>&lt;B, CheckedCastError&gt; {
761  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_cast(a) }<span class="question-mark">?</span>;
762
763  <span class="kw">if </span>&lt;B <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(<span class="kw-2">&amp;</span>pod) {
764    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{ <span class="macro">transmute!</span>(pod) })
765  } <span class="kw">else </span>{
766    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
767  }
768}
769
770<span class="doccomment">/// Try to convert a `&amp;T` into `&amp;U`.
771///
772/// ## Failure
773///
774/// * If the reference isn&#39;t aligned in the new type
775/// * If the source type and target type aren&#39;t the same size.
776/// * If `a` contains an invalid bit pattern for `B` this fails.
777</span><span class="attribute">#[inline]
778</span><span class="kw">pub fn </span>try_cast_ref&lt;A: NoUninit, B: CheckedBitPattern&gt;(
779  a: <span class="kw-2">&amp;</span>A,
780) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="kw-2">&amp;</span>B, CheckedCastError&gt; {
781  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_cast_ref(a) }<span class="question-mark">?</span>;
782
783  <span class="kw">if </span>&lt;B <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(pod) {
784    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{ <span class="kw-2">&amp;*</span>(pod <span class="kw">as </span><span class="kw-2">*const </span>&lt;B <span class="kw">as </span>CheckedBitPattern&gt;::Bits <span class="kw">as </span><span class="kw-2">*const </span>B) })
785  } <span class="kw">else </span>{
786    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
787  }
788}
789
790<span class="doccomment">/// Try to convert a `&amp;mut T` into `&amp;mut U`.
791///
792/// As [`checked_cast_ref`], but `mut`.
793</span><span class="attribute">#[inline]
794</span><span class="kw">pub fn </span>try_cast_mut&lt;
795  A: NoUninit + AnyBitPattern,
796  B: CheckedBitPattern + NoUninit,
797&gt;(
798  a: <span class="kw-2">&amp;mut </span>A,
799) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="kw-2">&amp;mut </span>B, CheckedCastError&gt; {
800  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_cast_mut(a) }<span class="question-mark">?</span>;
801
802  <span class="kw">if </span>&lt;B <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(pod) {
803    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{ <span class="kw-2">&amp;mut *</span>(pod <span class="kw">as </span><span class="kw-2">*mut </span>&lt;B <span class="kw">as </span>CheckedBitPattern&gt;::Bits <span class="kw">as </span><span class="kw-2">*mut </span>B) })
804  } <span class="kw">else </span>{
805    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
806  }
807}
808
809<span class="doccomment">/// Try to convert `&amp;[A]` into `&amp;[B]` (possibly with a change in length).
810///
811/// * `input.as_ptr() as usize == output.as_ptr() as usize`
812/// * `input.len() * size_of::&lt;A&gt;() == output.len() * size_of::&lt;B&gt;()`
813///
814/// ## Failure
815///
816/// * If the target type has a greater alignment requirement and the input slice
817///   isn&#39;t aligned.
818/// * If the target element type is a different size from the current element
819///   type, and the output slice wouldn&#39;t be a whole number of elements when
820///   accounting for the size change (eg: 3 `u16` values is 1.5 `u32` values, so
821///   that&#39;s a failure).
822/// * Similarly, you can&#39;t convert between a [ZST](https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts)
823///   and a non-ZST.
824/// * If any element of the converted slice would contain an invalid bit pattern
825///   for `B` this fails.
826</span><span class="attribute">#[inline]
827</span><span class="kw">pub fn </span>try_cast_slice&lt;A: NoUninit, B: CheckedBitPattern&gt;(
828  a: <span class="kw-2">&amp;</span>[A],
829) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="kw-2">&amp;</span>[B], CheckedCastError&gt; {
830  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_cast_slice(a) }<span class="question-mark">?</span>;
831
832  <span class="kw">if </span>pod.iter().all(|pod| &lt;B <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(pod)) {
833    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{
834      core::slice::from_raw_parts(pod.as_ptr() <span class="kw">as </span><span class="kw-2">*const </span>B, pod.len())
835    })
836  } <span class="kw">else </span>{
837    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
838  }
839}
840
841<span class="doccomment">/// Try to convert `&amp;mut [A]` into `&amp;mut [B]` (possibly with a change in
842/// length).
843///
844/// As [`checked_cast_slice`], but `&amp;mut`.
845</span><span class="attribute">#[inline]
846</span><span class="kw">pub fn </span>try_cast_slice_mut&lt;
847  A: NoUninit + AnyBitPattern,
848  B: CheckedBitPattern + NoUninit,
849&gt;(
850  a: <span class="kw-2">&amp;mut </span>[A],
851) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="kw-2">&amp;mut </span>[B], CheckedCastError&gt; {
852  <span class="kw">let </span>pod = <span class="kw">unsafe </span>{ internal::try_cast_slice_mut(a) }<span class="question-mark">?</span>;
853
854  <span class="kw">if </span>pod.iter().all(|pod| &lt;B <span class="kw">as </span>CheckedBitPattern&gt;::is_valid_bit_pattern(pod)) {
855    <span class="prelude-val">Ok</span>(<span class="kw">unsafe </span>{
856      core::slice::from_raw_parts_mut(pod.as_ptr() <span class="kw">as </span><span class="kw-2">*mut </span>B, pod.len())
857    })
858  } <span class="kw">else </span>{
859    <span class="prelude-val">Err</span>(CheckedCastError::InvalidBitPattern)
860  }
861}
862
863<span class="doccomment">/// Re-interprets `&amp;[u8]` as `&amp;T`.
864///
865/// ## Panics
866///
867/// This is [`try_from_bytes`] but will panic on error.
868</span><span class="attribute">#[inline]
869</span><span class="kw">pub fn </span>from_bytes&lt;T: CheckedBitPattern&gt;(s: <span class="kw-2">&amp;</span>[u8]) -&gt; <span class="kw-2">&amp;</span>T {
870  <span class="kw">match </span>try_from_bytes(s) {
871    <span class="prelude-val">Ok</span>(t) =&gt; t,
872    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;from_bytes&quot;</span>, e),
873  }
874}
875
876<span class="doccomment">/// Re-interprets `&amp;mut [u8]` as `&amp;mut T`.
877///
878/// ## Panics
879///
880/// This is [`try_from_bytes_mut`] but will panic on error.
881</span><span class="attribute">#[inline]
882</span><span class="kw">pub fn </span>from_bytes_mut&lt;T: NoUninit + CheckedBitPattern&gt;(s: <span class="kw-2">&amp;mut </span>[u8]) -&gt; <span class="kw-2">&amp;mut </span>T {
883  <span class="kw">match </span>try_from_bytes_mut(s) {
884    <span class="prelude-val">Ok</span>(t) =&gt; t,
885    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;from_bytes_mut&quot;</span>, e),
886  }
887}
888
889<span class="doccomment">/// Reads the slice into a `T` value.
890///
891/// ## Panics
892/// * This is like `try_pod_read_unaligned` but will panic on failure.
893</span><span class="attribute">#[inline]
894</span><span class="kw">pub fn </span>pod_read_unaligned&lt;T: AnyBitPattern&gt;(bytes: <span class="kw-2">&amp;</span>[u8]) -&gt; T {
895  <span class="kw">match </span>try_pod_read_unaligned(bytes) {
896    <span class="prelude-val">Ok</span>(t) =&gt; t,
897    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;pod_read_unaligned&quot;</span>, e),
898  }
899}
900
901<span class="doccomment">/// Cast `T` into `U`
902///
903/// ## Panics
904///
905/// * This is like [`try_cast`](try_cast), but will panic on a size mismatch.
906</span><span class="attribute">#[inline]
907</span><span class="kw">pub fn </span>cast&lt;A: NoUninit, B: CheckedBitPattern&gt;(a: A) -&gt; B {
908  <span class="kw">match </span>try_cast(a) {
909    <span class="prelude-val">Ok</span>(t) =&gt; t,
910    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;cast&quot;</span>, e),
911  }
912}
913
914<span class="doccomment">/// Cast `&amp;mut T` into `&amp;mut U`.
915///
916/// ## Panics
917///
918/// This is [`try_cast_mut`] but will panic on error.
919</span><span class="attribute">#[inline]
920</span><span class="kw">pub fn </span>cast_mut&lt;
921  A: NoUninit + AnyBitPattern,
922  B: NoUninit + CheckedBitPattern,
923&gt;(
924  a: <span class="kw-2">&amp;mut </span>A,
925) -&gt; <span class="kw-2">&amp;mut </span>B {
926  <span class="kw">match </span>try_cast_mut(a) {
927    <span class="prelude-val">Ok</span>(t) =&gt; t,
928    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;cast_mut&quot;</span>, e),
929  }
930}
931
932<span class="doccomment">/// Cast `&amp;T` into `&amp;U`.
933///
934/// ## Panics
935///
936/// This is [`try_cast_ref`] but will panic on error.
937</span><span class="attribute">#[inline]
938</span><span class="kw">pub fn </span>cast_ref&lt;A: NoUninit, B: CheckedBitPattern&gt;(a: <span class="kw-2">&amp;</span>A) -&gt; <span class="kw-2">&amp;</span>B {
939  <span class="kw">match </span>try_cast_ref(a) {
940    <span class="prelude-val">Ok</span>(t) =&gt; t,
941    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;cast_ref&quot;</span>, e),
942  }
943}
944
945<span class="doccomment">/// Cast `&amp;[A]` into `&amp;[B]`.
946///
947/// ## Panics
948///
949/// This is [`try_cast_slice`] but will panic on error.
950</span><span class="attribute">#[inline]
951</span><span class="kw">pub fn </span>cast_slice&lt;A: NoUninit, B: CheckedBitPattern&gt;(a: <span class="kw-2">&amp;</span>[A]) -&gt; <span class="kw-2">&amp;</span>[B] {
952  <span class="kw">match </span>try_cast_slice(a) {
953    <span class="prelude-val">Ok</span>(t) =&gt; t,
954    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;cast_slice&quot;</span>, e),
955  }
956}
957
958<span class="doccomment">/// Cast `&amp;mut [T]` into `&amp;mut [U]`.
959///
960/// ## Panics
961///
962/// This is [`try_cast_slice_mut`] but will panic on error.
963</span><span class="attribute">#[inline]
964</span><span class="kw">pub fn </span>cast_slice_mut&lt;
965  A: NoUninit + AnyBitPattern,
966  B: NoUninit + CheckedBitPattern,
967&gt;(
968  a: <span class="kw-2">&amp;mut </span>[A],
969) -&gt; <span class="kw-2">&amp;mut </span>[B] {
970  <span class="kw">match </span>try_cast_slice_mut(a) {
971    <span class="prelude-val">Ok</span>(t) =&gt; t,
972    <span class="prelude-val">Err</span>(e) =&gt; something_went_wrong(<span class="string">&quot;cast_slice_mut&quot;</span>, e),
973  }
974}
975</code></pre></div>
976</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>