<!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 `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, CoerceUnsized"> <title>std::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="../../main.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='../../std/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="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-CoerceUnsized%3CNonZero%3CU%3E%3E">NonZero<T></a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>std</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"> </div> </form> </nav> <section id='main' class="content"> <h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>std</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> <pre class='rust trait'><div class="docblock attributes">#[lang = "coerce_unsized"] </div>pub trait CoerceUnsized<T> { }</pre><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='foreign-impls' class='small-section-header'> Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a> </h2> <h3 id='impl-CoerceUnsized%3CNonZero%3CU%3E%3E' class='impl'><span class='in-band'><code>impl<T, U> <a class="trait" href="../../std/ops/trait.CoerceUnsized.html" title="trait std::ops::CoerceUnsized">CoerceUnsized</a><<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> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../core/nonzero/trait.Zeroable.html" title="trait core::nonzero::Zeroable">Zeroable</a> + <a class="trait" href="../../std/ops/trait.CoerceUnsized.html" title="trait std::ops::CoerceUnsized">CoerceUnsized</a><U>,<br> U: <a class="trait" href="../../core/nonzero/trait.Zeroable.html" title="trait core::nonzero::Zeroable">Zeroable</a>, </span></code><a href='#impl-CoerceUnsized%3CNonZero%3CU%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/nonzero.rs.html#94' title='goto source code'>[src]</a></span></h3> <span class='docblock autohide'><div class='impl-items'></div></span> <h2 id='implementors' class='small-section-header'> Implementors<a href='#implementors' class='anchor'></a> </h2> <ul class='item-list' id='implementors-list'> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#79' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*const U</a>> for <a class="primitive" href="../primitive.pointer.html">*const T</a> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#68' title='goto source code'>[src]</a></div><code>impl<'a, T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*const U</a>> for <a class="primitive" href="../primitive.reference.html">&'a </a>T <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1148' title='goto source code'>[src]</a></div><code>impl<'b, T, U> CoerceUnsized<<a class="struct" href="../../std/cell/struct.RefMut.html" title="struct std::cell::RefMut">RefMut</a><'b, U>> for <a class="struct" href="../../std/cell/struct.RefMut.html" title="struct std::cell::RefMut">RefMut</a><'b, T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ptr.rs.html#2577' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/ptr/struct.NonNull.html" title="struct std::ptr::NonNull">NonNull</a><U>> for <a class="struct" href="../../std/ptr/struct.NonNull.html" title="struct std::ptr::NonNull">NonNull</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1047' title='goto source code'>[src]</a></div><code>impl<'b, T, U> CoerceUnsized<<a class="struct" href="../../std/cell/struct.Ref.html" title="struct std::cell::Ref">Ref</a><'b, U>> for <a class="struct" href="../../std/cell/struct.Ref.html" title="struct std::cell::Ref">Ref</a><'b, T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ptr.rs.html#2433' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><U>> for <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#61' title='goto source code'>[src]</a></div><code>impl<'a, T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*const U</a>> for <a class="primitive" href="../primitive.reference.html">&'a mut </a>T <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#72' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*mut U</a>> for <a class="primitive" href="../primitive.pointer.html">*mut T</a> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#936' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/cell/struct.RefCell.html" title="struct std::cell::RefCell">RefCell</a><U>> for <a class="struct" href="../../std/cell/struct.RefCell.html" title="struct std::cell::RefCell">RefCell</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/ops/trait.CoerceUnsized.html" title="trait std::ops::CoerceUnsized">CoerceUnsized</a><U>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#65' title='goto source code'>[src]</a></div><code>impl<'a, 'b, T, U> CoerceUnsized<<a class="primitive" href="../primitive.reference.html">&'a </a>U> for <a class="primitive" href="../primitive.reference.html">&'b </a>T <span class="where fmt-newline">where<br> 'b: 'a,<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#55' title='goto source code'>[src]</a></div><code>impl<'a, 'b, T, U> CoerceUnsized<<a class="primitive" href="../primitive.reference.html">&'a </a>U> for <a class="primitive" href="../primitive.reference.html">&'b mut </a>T <span class="where fmt-newline">where<br> 'b: 'a,<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1279' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/cell/struct.UnsafeCell.html" title="struct std::cell::UnsafeCell">UnsafeCell</a><U>> for <a class="struct" href="../../std/cell/struct.UnsafeCell.html" title="struct std::cell::UnsafeCell">UnsafeCell</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/ops/trait.CoerceUnsized.html" title="trait std::ops::CoerceUnsized">CoerceUnsized</a><U>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#478' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/cell/struct.Cell.html" title="struct std::cell::Cell">Cell</a><U>> for <a class="struct" href="../../std/cell/struct.Cell.html" title="struct std::cell::Cell">Cell</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/ops/trait.CoerceUnsized.html" title="trait std::ops::CoerceUnsized">CoerceUnsized</a><U>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#52' title='goto source code'>[src]</a></div><code>impl<'a, T, U> CoerceUnsized<<a class="primitive" href="../primitive.reference.html">&'a mut </a>U> for <a class="primitive" href="../primitive.reference.html">&'a mut </a>T <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#58' title='goto source code'>[src]</a></div><code>impl<'a, T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*mut U</a>> for <a class="primitive" href="../primitive.reference.html">&'a mut </a>T <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/unsize.rs.html#75' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*const U</a>> for <a class="primitive" href="../primitive.pointer.html">*mut T</a> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/alloc/arc.rs.html#246' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/sync/struct.Weak.html" title="struct std::sync::Weak">Weak</a><U>> for std::sync::<a class="struct" href="../../std/sync/struct.Weak.html" title="struct std::sync::Weak">Weak</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/alloc/rc.rs.html#1172' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/rc/struct.Weak.html" title="struct std::rc::Weak">Weak</a><U>> for std::rc::<a class="struct" href="../../std/rc/struct.Weak.html" title="struct std::rc::Weak">Weak</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/alloc/boxed.rs.html#813' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><U>> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/alloc/rc.rs.html#295' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/rc/struct.Rc.html" title="struct std::rc::Rc">Rc</a><U>> for <a class="struct" href="../../std/rc/struct.Rc.html" title="struct std::rc::Rc">Rc</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><div class='out-of-band'><a class='srclink' href='../../src/alloc/arc.rs.html#210' title='goto source code'>[src]</a></div><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/sync/struct.Arc.html" title="struct std::sync::Arc">Arc</a><U>> for <a class="struct" href="../../std/sync/struct.Arc.html" title="struct std::sync::Arc">Arc</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/marker/trait.Unsize.html" title="trait std::marker::Unsize">Unsize</a><U> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,<br> U: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> </ul><script type="text/javascript" async src="../../implementors/core/ops/unsize/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> </div> </div> </aside> <script> window.rootPath = "../../"; window.currentCrate = "std"; </script> <script src="../../main.js"></script> <script defer src="../../search-index.js"></script> </body> </html>