mirror of
https://github.com/NotAShelf/mpvrc.git
synced 2026-04-17 08:19:51 +00:00
37 lines
No EOL
7.1 KiB
HTML
37 lines
No EOL
7.1 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="Safely transmutes a mutable reference of one type to an mutable reference of another type of the same size."><title>transmute_mut in zerocopy - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.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" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="zerocopy" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../zerocopy/index.html">zerocopy</a><span class="version">0.7.35</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">transmute_<wbr>mut</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#alignment-increase-error-message" title="Alignment increase error message">Alignment increase error message</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate zerocopy</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">zerocopy</a></span><h1>Macro <span class="macro">transmute_mut</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/zerocopy/lib.rs.html#3982-4054">Source</a> </span></div><pre class="rust item-decl"><code>macro_rules! transmute_mut {
|
|
($e:expr) => { ... };
|
|
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Safely transmutes a mutable reference of one type to an mutable reference of
|
|
another type of the same size.</p>
|
|
<p>The expression <code>$e</code> must have a concrete type, <code>&mut T</code>, where <code>T: Sized + AsBytes</code>. The <code>transmute_mut!</code> expression must also have a concrete type,
|
|
<code>&mut U</code> (<code>U</code> is inferred from the calling context), where <code>U: Sized + FromBytes</code>. It must be the case that <code>align_of::<T>() >= align_of::<U>()</code>.</p>
|
|
<p>The lifetime of the input type, <code>&mut T</code>, must be the same as or outlive the
|
|
lifetime of the output type, <code>&mut U</code>.</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">let </span><span class="kw-2">mut </span>one_dimensional: [u8; <span class="number">8</span>] = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>];
|
|
|
|
<span class="kw">let </span>two_dimensional: <span class="kw-2">&mut </span>[[u8; <span class="number">4</span>]; <span class="number">2</span>] = <span class="macro">transmute_mut!</span>(<span class="kw-2">&mut </span>one_dimensional);
|
|
|
|
<span class="macro">assert_eq!</span>(two_dimensional, <span class="kw-2">&</span>[[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]]);
|
|
|
|
two_dimensional.reverse();
|
|
|
|
<span class="macro">assert_eq!</span>(one_dimensional, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</code></pre></div>
|
|
<h2 id="alignment-increase-error-message"><a class="doc-anchor" href="#alignment-increase-error-message">§</a>Alignment increase error message</h2>
|
|
<p>Because of limitations on macros, the error message generated when
|
|
<code>transmute_mut!</code> is used to transmute from a type of lower alignment to a
|
|
type of higher alignment is somewhat confusing. For example, the following
|
|
code:</p>
|
|
|
|
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">const </span>INCREASE_ALIGNMENT: <span class="kw-2">&mut </span>u16 = <span class="macro">zerocopy::transmute_mut!</span>(<span class="kw-2">&mut </span>[<span class="number">0u8</span>; <span class="number">2</span>]);</code></pre></div>
|
|
<p>…generates the following error:</p>
|
|
<div class="example-wrap"><pre class="language-text"><code>error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
|
|
--> src/lib.rs:1524:34
|
|
|
|
|
5 | const INCREASE_ALIGNMENT: &mut u16 = zerocopy::transmute_mut!(&mut [0u8; 2]);
|
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
= note: source type: `AlignOf<[u8; 2]>` (8 bits)
|
|
= note: target type: `MaxAlignsOf<[u8; 2], u16>` (16 bits)
|
|
= note: this error originates in the macro `$crate::assert_align_gt_eq` which comes from the expansion of the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)</code></pre></div>
|
|
<p>This is saying that <code>max(align_of::<T>(), align_of::<U>()) != align_of::<T>()</code>, which is equivalent to <code>align_of::<T>() < align_of::<U>()</code>.</p>
|
|
</div></details></section></div></main></body></html> |