<!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 `Unique` struct in crate `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, Unique"> <title>std::ptr::Unique - 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 struct"> <!--[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'>Struct Unique</p><div class="block items"><ul><li><a href="#methods">Methods</a></li><li><a href="#implementations">Trait Implementations</a></li></ul></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>ptr</a></p><script>window.sidebarCurrent = {name: 'Unique', ty: 'struct', 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'>Struct <a href='../index.html'>std</a>::<wbr><a href='index.html'>ptr</a>::<wbr><a class="struct" href=''>Unique</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/ptr.rs.html#1082-1090' title='goto source code'>[src]</a></span></h1> <pre class='rust struct'>pub struct Unique<T> <span class="where fmt-newline">where<br> T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span> { /* fields omitted */ }</pre><div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>unique </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</summary><p>needs an RFC to flesh out design</p> </details></div></div><div class='docblock'><p>A wrapper around a raw non-null <code>*mut T</code> that indicates that the possessor of this wrapper owns the referent. Useful for building abstractions like <code>Box<T></code>, <code>Vec<T></code>, <code>String</code>, and <code>HashMap<K, V></code>.</p> <p>Unlike <code>*mut T</code>, <code>Unique<T></code> behaves "as if" it were an instance of <code>T</code>. It implements <code>Send</code>/<code>Sync</code> if <code>T</code> is <code>Send</code>/<code>Sync</code>. It also implies the kind of strong aliasing guarantees an instance of <code>T</code> can expect: the referent of the pointer should not be modified without a unique path to its owning Unique.</p> <p>If you're uncertain of whether it's correct to use <code>Unique</code> for your purposes, consider using <code>Shared</code>, which has weaker semantics.</p> <p>Unlike <code>*mut T</code>, the pointer must always be non-null, even if the pointer is never dereferenced. This is so that enums may use this forbidden value as a discriminant -- <code>Option<Unique<T>></code> has the same size as <code>Unique<T></code>. However the pointer may still dangle if it isn't dereferenced.</p> <p>Unlike <code>*mut T</code>, <code>Unique<T></code> is covariant over <code>T</code>. This should always be correct for any type which upholds Unique's aliasing requirements.</p> </div> <h2 id='methods' class='small-section-header'> Methods<a href='#methods' class='anchor'></a> </h2> <h3 id='impl' class='impl'><span class='in-band'><code>impl<T> <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T></code><a href='#impl' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1107-1118' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.empty' class="method"><span id='empty.v' class='invisible'><code>fn <a href='#method.empty' class='fnname'>empty</a>() -> <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1112-1117' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>unique </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Creates a new <code>Unique</code> that is dangling, but well-aligned.</p> <p>This is useful for initializing types which lazily allocate, like <code>Vec::new</code> does.</p> </div></div><h3 id='impl-1' class='impl'><span class='in-band'><code>impl<T> <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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-1' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1121-1158' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.new_unchecked' class="method"><span id='new_unchecked.v' class='invisible'><code>const unsafe fn <a href='#method.new_unchecked' class='fnname'>new_unchecked</a>(ptr: <a class="primitive" href="../primitive.pointer.html">*mut T</a>) -> <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1127-1129' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>unique </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Creates a new <code>Unique</code>.</p> <h1 id='safety' class='section-header'><a href='#safety'>Safety</a></h1> <p><code>ptr</code> must be non-null.</p> </div><h4 id='method.new' class="method"><span id='new.v' class='invisible'><code>fn <a href='#method.new' class='fnname'>new</a>(ptr: <a class="primitive" href="../primitive.pointer.html">*mut T</a>) -> <a class="enum" href="../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><<a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1132-1134' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>unique </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Creates a new <code>Unique</code> if <code>ptr</code> is non-null.</p> </div><h4 id='method.as_ptr' class="method"><span id='as_ptr.v' class='invisible'><code>fn <a href='#method.as_ptr' class='fnname'>as_ptr</a>(self) -> <a class="primitive" href="../primitive.pointer.html">*mut T</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1137-1139' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>unique </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Acquires the underlying <code>*mut</code> pointer.</p> </div><h4 id='method.as_ref' class="method"><span id='as_ref.v' class='invisible'><code>unsafe fn <a href='#method.as_ref' class='fnname'>as_ref</a>(&self) -> <a class="primitive" href="../primitive.reference.html">&</a>T</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1146-1148' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>unique </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Dereferences the content.</p> <p>The resulting lifetime is bound to self so this behaves "as if" it were actually an instance of T that is getting borrowed. If a longer (unbound) lifetime is needed, use <code>&*my_ptr.ptr()</code>.</p> </div><h4 id='method.as_mut' class="method"><span id='as_mut.v' class='invisible'><code>unsafe fn <a href='#method.as_mut' class='fnname'>as_mut</a>(&mut self) -> <a class="primitive" href="../primitive.reference.html">&mut </a>T</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1155-1157' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>unique </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Mutably dereferences the content.</p> <p>The resulting lifetime is bound to self so this behaves "as if" it were actually an instance of T that is getting borrowed. If a longer (unbound) lifetime is needed, use <code>&mut *my_ptr.ptr()</code>.</p> </div></div> <h2 id='implementations' class='small-section-header'> Trait Implementations<a href='#implementations' class='anchor'></a> </h2> <h3 id='impl-CoerceUnsized<Unique<U>>' 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="../../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><a href='#impl-CoerceUnsized<Unique<U>>' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1171' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 id='impl-From<&'a T>' class='impl'><span class='in-band'><code>impl<'a, T> <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="primitive" href="../primitive.reference.html">&'a </a>T> 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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-From<&'a T>' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1188-1192' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from' class="method"><span id='from.v' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(reference: <a class="primitive" href="../primitive.reference.html">&'a </a>T) -> <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1189-1191' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From<&'a mut T>' class='impl'><span class='in-band'><code>impl<'a, T> <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="primitive" href="../primitive.reference.html">&'a mut </a>T> 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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-From<&'a mut T>' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1181-1185' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-1' class="method"><span id='from.v-1' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(reference: <a class="primitive" href="../primitive.reference.html">&'a mut </a>T) -> <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1182-1184' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-Clone' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/clone/trait.Clone.html" title="trait std::clone::Clone">Clone</a> 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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-Clone' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1161-1165' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.clone' class="method"><span id='clone.v' class='invisible'><code>fn <a href='../../std/clone/trait.Clone.html#tymethod.clone' class='fnname'>clone</a>(&self) -> <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1162-1164' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a copy of the value. <a href="../../std/clone/trait.Clone.html#tymethod.clone">Read more</a></p> </div><h4 id='method.clone_from' class="method"><span id='clone_from.v' class='invisible'><code>fn <a href='../../std/clone/trait.Clone.html#method.clone_from' class='fnname'>clone_from</a>(&mut self, source: <a class="primitive" href="../primitive.reference.html">&</a>Self)</code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div><a class='srclink' href='../../src/core/clone.rs.html#112-114' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs copy-assignment from <code>source</code>. <a href="../../std/clone/trait.Clone.html#method.clone_from">Read more</a></p> </div></div><h3 id='impl-Pointer' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/fmt/trait.Pointer.html" title="trait std::fmt::Pointer">Pointer</a> 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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-Pointer' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1174-1178' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.fmt' class="method"><span id='fmt.v' class='invisible'><code>fn <a href='../../std/fmt/trait.Pointer.html#tymethod.fmt' class='fnname'>fmt</a>(&self, f: &mut <a class="struct" href="../../std/fmt/struct.Formatter.html" title="struct std::fmt::Formatter">Formatter</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="primitive" href="../primitive.tuple.html">()</a>, <a class="struct" href="../../std/fmt/struct.Error.html" title="struct std::fmt::Error">Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1175-1177' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Formats the value using the given formatter.</p> </div></div><h3 id='impl-Copy' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/marker/trait.Copy.html" title="trait std::marker::Copy">Copy</a> 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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-Copy' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1168' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 id='impl-Send' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> 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.Send.html" title="trait std::marker::Send">Send</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-Send' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1097' title='goto source code'>[src]</a></span></h3> <div class='docblock'><p><code>Unique</code> pointers are <code>Send</code> if <code>T</code> is <code>Send</code> because the data they reference is unaliased. Note that this aliasing invariant is unenforced by the type system; the abstraction using the <code>Unique</code> must enforce it.</p> </div><div class='impl-items'></div><h3 id='impl-Sync' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a> 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.Sync.html" title="trait std::marker::Sync">Sync</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code><a href='#impl-Sync' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1104' title='goto source code'>[src]</a></span></h3> <div class='docblock'><p><code>Unique</code> pointers are <code>Sync</code> if <code>T</code> is <code>Sync</code> because the data they reference is unaliased. Note that this aliasing invariant is unenforced by the type system; the abstraction using the <code>Unique</code> must enforce it.</p> </div><div class='impl-items'></div><h3 id='impl-UnwindSafe' class='impl'><span class='in-band'><code>impl<T: <a class="trait" href="../../std/panic/trait.UnwindSafe.html" title="trait std::panic::UnwindSafe">UnwindSafe</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/panic/trait.UnwindSafe.html" title="trait std::panic::UnwindSafe">UnwindSafe</a> for <a class="struct" href="../../std/ptr/struct.Unique.html" title="struct std::ptr::Unique">Unique</a><T></code><a href='#impl-UnwindSafe' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/panic.rs.html#201' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div></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>