<!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"> <link rel="stylesheet" type="text/css" href="../../main.css"> <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"> <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="block items"><ul><li><a href="#implementors">Implementors</a></li></ul></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> </nav> <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.rs.html#2769-2771' 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 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'>Implementors</h2> <ul class='item-list' id='implementors-list'> <li><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><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><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> <li><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><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><code>impl<'a, T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*const U</a>> for &'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><code>impl<'a, 'b, T, U> CoerceUnsized<&'a U> for &'b mut 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><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><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><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><code>impl<'a, T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*mut U</a>> for &'a mut 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><code>impl<T, U> 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> <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></li> <li><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><code>impl<'a, T, U> CoerceUnsized<&'a mut U> for &'a mut 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><code>impl<'a, T, U> CoerceUnsized<<a class="primitive" href="../primitive.pointer.html">*const U</a>> for &'a mut 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><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><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><code>impl<'a, 'b, T, U> CoerceUnsized<&'a U> for &'b 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><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><code>impl<T, U> CoerceUnsized<<a class="struct" href="../../std/ptr/struct.Shared.html" title="struct std::ptr::Shared">Shared</a><U>> for <a class="struct" href="../../std/ptr/struct.Shared.html" title="struct std::ptr::Shared">Shared</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><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><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> </ul><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>?</dt> <dd>Show this help dialog</dd> <dt>S</dt> <dd>Focus the search field</dd> <dt>⇤</dt> <dd>Move up in search results</dd> <dt>⇥</dt> <dd>Move down in search results</dd> <dt>⏎</dt> <dd>Go to active search result</dd> <dt>+</dt> <dd>Collapse/expand 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>