| <!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 trait which indicates that a type is a `#[repr(transparent)]` wrapper around the `Inner` value."><meta name="keywords" content="rust, rustlang, rust-lang, TransparentWrapper"><title>TransparentWrapper in bytemuck - 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 trait"><!--[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">☰</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></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><h2 class="location"><a href="#">TransparentWrapper</a></h2><div class="sidebar-elems"><section><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.peel">peel</a></li><li><a href="#method.peel_mut">peel_mut</a></li><li><a href="#method.peel_ref">peel_ref</a></li><li><a href="#method.peel_slice">peel_slice</a></li><li><a href="#method.peel_slice_mut">peel_slice_mut</a></li><li><a href="#method.wrap">wrap</a></li><li><a href="#method.wrap_mut">wrap_mut</a></li><li><a href="#method.wrap_ref">wrap_ref</a></li><li><a href="#method.wrap_slice">wrap_slice</a></li><li><a href="#method.wrap_slice_mut">wrap_slice_mut</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-TransparentWrapper%3CT%3E-for-Wrapping%3CT%3E">Wrapping<T></a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In bytemuck</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">Trait <a href="index.html">bytemuck</a>::<wbr><a class="trait" href="#">TransparentWrapper</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/bytemuck/transparent.rs.html#126-286">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub unsafe trait TransparentWrapper<Inner: ?Sized> { |
| fn <a href="#method.wrap" class="fnname">wrap</a>(s: Inner) -> Self<br> <span class="where">where<br> Self: Sized,<br> Inner: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.wrap_ref" class="fnname">wrap_ref</a>(s: &Inner) -> &Self { ... } |
| <span class="item-spacer"></span> fn <a href="#method.wrap_mut" class="fnname">wrap_mut</a>(s: &mut Inner) -> &mut Self { ... } |
| <span class="item-spacer"></span> fn <a href="#method.wrap_slice" class="fnname">wrap_slice</a>(s: &[Inner]) -> &[Self]<br> <span class="where">where<br> Self: Sized,<br> Inner: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.wrap_slice_mut" class="fnname">wrap_slice_mut</a>(s: &mut [Inner]) -> &mut [Self]<br> <span class="where">where<br> Self: Sized,<br> Inner: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.peel" class="fnname">peel</a>(s: Self) -> Inner<br> <span class="where">where<br> Self: Sized,<br> Inner: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.peel_ref" class="fnname">peel_ref</a>(s: &Self) -> &Inner { ... } |
| <span class="item-spacer"></span> fn <a href="#method.peel_mut" class="fnname">peel_mut</a>(s: &mut Self) -> &mut Inner { ... } |
| <span class="item-spacer"></span> fn <a href="#method.peel_slice" class="fnname">peel_slice</a>(s: &[Self]) -> &[Inner]<br> <span class="where">where<br> Self: Sized,<br> Inner: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.peel_slice_mut" class="fnname">peel_slice_mut</a>(s: &mut [Self]) -> &mut [Inner]<br> <span class="where">where<br> Self: Sized,<br> Inner: Sized</span>, |
| { ... } |
| }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A trait which indicates that a type is a <code>#[repr(transparent)]</code> wrapper |
| around the <code>Inner</code> value.</p> |
| <p>This allows safely copy transmuting between the <code>Inner</code> type and the |
| <code>TransparentWrapper</code> type. Functions like <code>wrap_{}</code> convert from the inner |
| type to the wrapper type and <code>peel_{}</code> functions do the inverse conversion |
| from the wrapper type to the inner type. We deliberately do not call the |
| wrapper-removing methods “unwrap” because at this point that word is too |
| strongly tied to the Option/ Result methods.</p> |
| <h2 id="safety"><a href="#safety">Safety</a></h2> |
| <p>The safety contract of <code>TransparentWrapper</code> is relatively simple:</p> |
| <p>For a given <code>Wrapper</code> which implements <code>TransparentWrapper<Inner></code>:</p> |
| <ol> |
| <li> |
| <p><code>Wrapper</code> must be a wrapper around <code>Inner</code> with an identical data |
| representations. This either means that it must be a |
| <code>#[repr(transparent)]</code> struct which contains a either a field of type |
| <code>Inner</code> (or a field of some other transparent wrapper for <code>Inner</code>) as |
| the only non-ZST field.</p> |
| </li> |
| <li> |
| <p>Any fields <em>other</em> than the <code>Inner</code> field must be trivially constructable |
| ZSTs, for example <code>PhantomData</code>, <code>PhantomPinned</code>, etc. (When deriving |
| <code>TransparentWrapper</code> on a type with ZST fields, the ZST fields must be |
| <a href="trait.Zeroable.html" title="Zeroable"><code>Zeroable</code></a>).</p> |
| </li> |
| <li> |
| <p>The <code>Wrapper</code> may not impose additional alignment requirements over |
| <code>Inner</code>.</p> |
| <ul> |
| <li>Note: this is currently guaranteed by <code>repr(transparent)</code>, but there |
| have been discussions of lifting it, so it’s stated here explicitly.</li> |
| </ul> |
| </li> |
| <li> |
| <p>All functions on <code>TransparentWrapper</code> <strong>may not</strong> be overridden.</p> |
| </li> |
| </ol> |
| <h3 id="caveats"><a href="#caveats">Caveats</a></h3> |
| <p>If the wrapper imposes additional constraints upon the inner type which are |
| required for safety, it’s responsible for ensuring those still hold – this |
| generally requires preventing access to instances of the inner type, as |
| implementing <code>TransparentWrapper<U> for T</code> means anybody can call |
| <code>T::cast_ref(any_instance_of_u)</code>.</p> |
| <p>For example, it would be invalid to implement TransparentWrapper for <code>str</code> |
| to implement <code>TransparentWrapper</code> around <code>[u8]</code> because of this.</p> |
| <h2 id="examples"><a href="#examples">Examples</a></h2><h3 id="basic"><a href="#basic">Basic</a></h3> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>bytemuck::TransparentWrapper; |
| |
| <span class="attribute">#[repr(transparent)] |
| </span><span class="kw">struct </span>MyWrapper(SomeStruct); |
| |
| <span class="kw">unsafe impl </span>TransparentWrapper<SomeStruct> <span class="kw">for </span>MyWrapper {} |
| |
| <span class="comment">// interpret a reference to &SomeStruct as a &MyWrapper |
| </span><span class="kw">let </span>thing = SomeStruct::default(); |
| <span class="kw">let </span>inner_ref: <span class="kw-2">&</span>MyWrapper = MyWrapper::wrap_ref(<span class="kw-2">&</span>thing); |
| |
| <span class="comment">// Works with &mut too. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>mut_thing = SomeStruct::default(); |
| <span class="kw">let </span>inner_mut: <span class="kw-2">&mut </span>MyWrapper = MyWrapper::wrap_mut(<span class="kw-2">&mut </span>mut_thing); |
| </code></pre></div> |
| <h3 id="use-with-dynamically-sized-types"><a href="#use-with-dynamically-sized-types">Use with dynamically sized types</a></h3> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>bytemuck::TransparentWrapper; |
| |
| <span class="attribute">#[repr(transparent)] |
| </span><span class="kw">struct </span>Slice<T>([T]); |
| |
| <span class="kw">unsafe impl</span><T> TransparentWrapper<[T]> <span class="kw">for </span>Slice<T> {} |
| |
| <span class="kw">let </span>s = Slice::wrap_ref(<span class="kw-2">&</span>[<span class="number">1u32</span>, <span class="number">2</span>, <span class="number">3</span>]); |
| <span class="macro">assert_eq!</span>(<span class="kw-2">&</span>s.<span class="number">0</span>, <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]); |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3u8</span>]; |
| <span class="kw">let </span>sm = Slice::wrap_mut(<span class="kw-2">&mut </span>buf);</code></pre></div> |
| <h3 id="deriving"><a href="#deriving">Deriving</a></h3> |
| <p>When deriving, the non-wrapped fields must uphold all the normal requirements, |
| and must also be <code>Zeroable</code>.</p> |
| |
| <div class="example-wrap ignore"><div class='tooltip'>ⓘ</div><pre class="rust rust-example-rendered"><code><span class="comment">// This example requires the `derive` feature. |
| </span><span class="kw">use </span>bytemuck::TransparentWrapper; |
| <span class="kw">use </span>std::marker::PhantomData; |
| |
| <span class="attribute">#[derive(TransparentWrapper)] |
| #[repr(transparent)] |
| #[transparent(usize)] |
| </span><span class="kw">struct </span>Wrapper<T: <span class="question-mark">?</span>Sized>(usize, PhantomData<T>); <span class="comment">// PhantomData<T> implements Zeroable for all T</span></code></pre></div> |
| <p>Here, an error will occur, because <code>MyZst</code> does not implement <code>Zeroable</code>.</p> |
| |
| <div class="example-wrap ignore"><div class='tooltip'>ⓘ</div><pre class="rust rust-example-rendered"><code><span class="comment">// This example requires the `derive` feature. |
| </span><span class="kw">use </span>bytemuck::TransparentWrapper; |
| <span class="kw">struct </span>MyZst; |
| |
| <span class="attribute">#[derive(TransparentWrapper)] |
| #[repr(transparent)] |
| #[transparent(usize)] |
| </span><span class="kw">struct </span>Wrapper(usize, MyZst); <span class="comment">// MyZst does not implement Zeroable</span></code></pre></div> |
| </div></details><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.wrap" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#129-137">source</a><h4 class="code-header">fn <a href="#method.wrap" class="fnname">wrap</a>(s: Inner) -> Self<span class="where fmt-newline">where<br> Self: Sized,<br> Inner: Sized,</span></h4></section></summary><div class="docblock"><p>Convert the inner type into the wrapper type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.wrap_ref" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#142-155">source</a><h4 class="code-header">fn <a href="#method.wrap_ref" class="fnname">wrap_ref</a>(s: &Inner) -> &Self</h4></section></summary><div class="docblock"><p>Convert a reference to the inner type into a reference to the wrapper |
| type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.wrap_mut" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#160-173">source</a><h4 class="code-header">fn <a href="#method.wrap_mut" class="fnname">wrap_mut</a>(s: &mut Inner) -> &mut Self</h4></section></summary><div class="docblock"><p>Convert a mutable reference to the inner type into a mutable reference to |
| the wrapper type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.wrap_slice" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#177-189">source</a><h4 class="code-header">fn <a href="#method.wrap_slice" class="fnname">wrap_slice</a>(s: &[Inner]) -> &[Self]<span class="where fmt-newline">where<br> Self: Sized,<br> Inner: Sized,</span></h4></section></summary><div class="docblock"><p>Convert a slice to the inner type into a slice to the wrapper type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.wrap_slice_mut" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#194-206">source</a><h4 class="code-header">fn <a href="#method.wrap_slice_mut" class="fnname">wrap_slice_mut</a>(s: &mut [Inner]) -> &mut [Self]<span class="where fmt-newline">where<br> Self: Sized,<br> Inner: Sized,</span></h4></section></summary><div class="docblock"><p>Convert a mutable slice to the inner type into a mutable slice to the |
| wrapper type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peel" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#210-216">source</a><h4 class="code-header">fn <a href="#method.peel" class="fnname">peel</a>(s: Self) -> Inner<span class="where fmt-newline">where<br> Self: Sized,<br> Inner: Sized,</span></h4></section></summary><div class="docblock"><p>Convert the wrapper type into the inner type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peel_ref" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#221-234">source</a><h4 class="code-header">fn <a href="#method.peel_ref" class="fnname">peel_ref</a>(s: &Self) -> &Inner</h4></section></summary><div class="docblock"><p>Convert a reference to the wrapper type into a reference to the inner |
| type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peel_mut" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#239-252">source</a><h4 class="code-header">fn <a href="#method.peel_mut" class="fnname">peel_mut</a>(s: &mut Self) -> &mut Inner</h4></section></summary><div class="docblock"><p>Convert a mutable reference to the wrapper type into a mutable reference |
| to the inner type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peel_slice" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#256-268">source</a><h4 class="code-header">fn <a href="#method.peel_slice" class="fnname">peel_slice</a>(s: &[Self]) -> &[Inner]<span class="where fmt-newline">where<br> Self: Sized,<br> Inner: Sized,</span></h4></section></summary><div class="docblock"><p>Convert a slice to the wrapped type into a slice to the inner type.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peel_slice_mut" class="method has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#273-285">source</a><h4 class="code-header">fn <a href="#method.peel_slice_mut" class="fnname">peel_slice_mut</a>(s: &mut [Self]) -> &mut [Inner]<span class="where fmt-newline">where<br> Self: Sized,<br> Inner: Sized,</span></h4></section></summary><div class="docblock"><p>Convert a mutable slice to the wrapped type into a mutable slice to the |
| inner type.</p> |
| </div></details></div><h2 id="foreign-impls" class="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><section id="impl-TransparentWrapper%3CT%3E-for-Wrapping%3CT%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/bytemuck/transparent.rs.html#288">source</a><a href="#impl-TransparentWrapper%3CT%3E-for-Wrapping%3CT%3E" class="anchor"></a><h3 class="code-header">impl<T> <a class="trait" href="trait.TransparentWrapper.html" title="trait bytemuck::TransparentWrapper">TransparentWrapper</a><T> for Wrapping<T></h3></section><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"></div><script src="../implementors/bytemuck/transparent/trait.TransparentWrapper.js" data-ignore-extern-crates="core" async></script></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 (5c8bff74b 2022-10-21)" ></div></body></html> |