<!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="API documentation for the Rust `CoerceUnsized` trait in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, CoerceUnsized"><title>core::ops::CoerceUnsized - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../core/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Trait CoerceUnsized</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>ops</a></p><script>window.sidebarCurrent = {name: 'CoerceUnsized', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>core</a>::<wbr><a href='index.html'>ops</a>::<wbr><a class="trait" href=''>CoerceUnsized</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version '></span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/core/ops/unsize.rs.html#46-48' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust trait'><div class="docblock attributes">#[lang = "coerce_unsized"] </div>pub trait CoerceUnsized<T> { }</pre></div><div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>coerce_unsized </code><a href="https://github.com/rust-lang/rust/issues/27732">#27732</a>)</div></div><div class='docblock'><p>Trait that indicates that this is a pointer or a wrapper for one, where unsizing can be performed on the pointee.</p> <p>See the <a href="https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md">DST coercion RfC</a> and <a href="../../nomicon/coercions.html">the nomicon entry on coercion</a> for more details.</p> <p>For builtin pointer types, pointers to <code>T</code> will coerce to pointers to <code>U</code> if <code>T: Unsize<U></code> by converting from a thin pointer to a fat pointer.</p> <p>For custom types, the coercion here works by coercing <code>Foo<T></code> to <code>Foo<U></code> provided an impl of <code>CoerceUnsized<Foo<U>> for Foo<T></code> exists. Such an impl can only be written if <code>Foo<T></code> has only a single non-phantomdata field involving <code>T</code>. If the type of that field is <code>Bar<T></code>, an implementation of <code>CoerceUnsized<Bar<U>> for Bar<T></code> must exist. The coercion will work by coercing the <code>Bar<T></code> field into <code>Bar<U></code> and filling in the rest of the fields from <code>Foo<T></code> to create a <code>Foo<U></code>. This will effectively drill down to a pointer field and coerce that.</p> <p>Generally, for smart pointers you will implement <code>CoerceUnsized<Ptr<U>> for Ptr<T> where T: Unsize<U>, U: ?Sized</code>, with an optional <code>?Sized</code> bound on <code>T</code> itself. For wrapper types that directly embed <code>T</code> like <code>Cell<T></code> and <code>RefCell<T></code>, you can directly implement <code>CoerceUnsized<Wrap<U>> for Wrap<T> where T: CoerceUnsized<U></code>. This will let coercions of types like <code>Cell<Box<T>></code> work.</p> <p><a href="../marker/trait.Unsize.html"><code>Unsize</code></a> is used to mark types which can be coerced to DSTs if behind pointers. It is implemented automatically by the compiler.</p> </div> <h2 id='implementors' class='small-section-header'> Implementors<a href='#implementors' class='anchor'></a> </h2> <ul class='item-list' id='implementors-list'> <li><table class='table-display'><tbody><tr><td><code>impl<'a, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<<a class="struct" href="../../core/mem/struct.Pin.html" title="struct core::mem::Pin">Pin</a><'a, U>> for <a class="struct" href="../../core/mem/struct.Pin.html" title="struct core::mem::Pin">Pin</a><'a, T></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/mem.rs.html#1203' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: <a class="trait" href="../../core/nonzero/trait.Zeroable.html" title="trait core::nonzero::Zeroable">Zeroable</a> + <a class="trait" href="../../core/ops/trait.CoerceUnsized.html" title="trait core::ops::CoerceUnsized">CoerceUnsized</a><U>, U: <a class="trait" href="../../core/nonzero/trait.Zeroable.html" title="trait core::nonzero::Zeroable">Zeroable</a>> CoerceUnsized<<a class="struct" href="../../core/nonzero/struct.NonZero.html" title="struct core::nonzero::NonZero">NonZero</a><U>> for <a class="struct" href="../../core/nonzero/struct.NonZero.html" title="struct core::nonzero::NonZero">NonZero</a><T></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/nonzero.rs.html#92' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<<a class="struct" href="../../core/ptr/struct.NonNull.html" title="struct core::ptr::NonNull">NonNull</a><U>> for <a class="struct" href="../../core/ptr/struct.NonNull.html" title="struct core::ptr::NonNull">NonNull</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, </span></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ptr.rs.html#2772' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'a, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<&'a mut U> for &'a mut T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#52' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'a, 'b: 'a, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<&'a U> for &'b mut T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#55' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'a, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<*mut U> for &'a mut T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#58' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'a, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<*const U> for &'a mut T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#61' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'a, 'b: 'a, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<&'a U> for &'b T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#65' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'a, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<*const U> for &'a T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#68' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<*mut U> for *mut T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#72' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<*const U> for *mut T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#75' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<*const U> for *const T</code><td><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#79' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: <a class="trait" href="../../core/ops/trait.CoerceUnsized.html" title="trait core::ops::CoerceUnsized">CoerceUnsized</a><U>, U> CoerceUnsized<<a class="struct" href="../../core/cell/struct.Cell.html" title="struct core::cell::Cell">Cell</a><U>> for <a class="struct" href="../../core/cell/struct.Cell.html" title="struct core::cell::Cell">Cell</a><T></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#522' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: <a class="trait" href="../../core/ops/trait.CoerceUnsized.html" title="trait core::ops::CoerceUnsized">CoerceUnsized</a><U>, U> CoerceUnsized<<a class="struct" href="../../core/cell/struct.RefCell.html" title="struct core::cell::RefCell">RefCell</a><U>> for <a class="struct" href="../../core/cell/struct.RefCell.html" title="struct core::cell::RefCell">RefCell</a><T></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1004' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'b, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<<a class="struct" href="../../core/cell/struct.Ref.html" title="struct core::cell::Ref">Ref</a><'b, U>> for <a class="struct" href="../../core/cell/struct.Ref.html" title="struct core::cell::Ref">Ref</a><'b, T></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1115' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<'b, T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> CoerceUnsized<<a class="struct" href="../../core/cell/struct.RefMut.html" title="struct core::cell::RefMut">RefMut</a><'b, U>> for <a class="struct" href="../../core/cell/struct.RefMut.html" title="struct core::cell::RefMut">RefMut</a><'b, T></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1216' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> <li><table class='table-display'><tbody><tr><td><code>impl<T: <a class="trait" href="../../core/ops/trait.CoerceUnsized.html" title="trait core::ops::CoerceUnsized">CoerceUnsized</a><U>, U> CoerceUnsized<<a class="struct" href="../../core/cell/struct.UnsafeCell.html" title="struct core::cell::UnsafeCell">UnsafeCell</a><U>> for <a class="struct" href="../../core/cell/struct.UnsafeCell.html" title="struct core::cell::UnsafeCell">UnsafeCell</a><T></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1368' title='goto source code'>[src]</a></div></td></tr></tbody></table></li> </ul><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async src="../../implementors/core/ops/trait.CoerceUnsized.js"> </script></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "core";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>