<!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 `Shared` struct in crate `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, Shared"> <title>std::ptr::Shared - 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 Shared</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: 'Shared', 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=''>Shared</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#1099-1107' title='goto source code'>[src]</a></span></h1> <pre class='rust struct'>pub struct Shared<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>shared </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 <code>*mut T</code> that indicates that the possessor of this wrapper has shared ownership of the referent. Useful for building abstractions like <code>Rc<T></code>, <code>Arc<T></code>, or doubly-linked lists, which internally use aliased raw pointers to manage the memory that they own.</p> <p>This is similar to <code>Unique</code>, except that it doesn't make any aliasing guarantees, and doesn't derive Send and Sync. Note that unlike <code>&T</code>, Shared has no special mutability requirements. Shared may mutate data aliased by other Shared pointers. More precise rules require Rust to develop an actual aliasing model.</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<Shared<T>></code> has the same size as <code>Shared<T></code>. However the pointer may still dangle if it isn't dereferenced.</p> <p>Unlike <code>*mut T</code>, <code>Shared<T></code> is covariant over <code>T</code>. If this is incorrect for your use case, you should include some PhantomData in your type to provide invariance, such as <code>PhantomData<Cell<T>></code> or <code>PhantomData<&'a mut T></code>. Usually this won't be necessary; covariance is correct for Rc, Arc, and LinkedList because they provide a public API that follows the normal shared XOR mutable rules of Rust.</p> </div><h2 id='methods'>Methods</h2><h3 class='impl'><span class='in-band'><code>impl<T> <a class="struct" href="../../std/ptr/struct.Shared.html" title="struct std::ptr::Shared">Shared</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1120-1131' 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.Shared.html" title="struct std::ptr::Shared">Shared</a><T></code></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>shared </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Creates a new <code>Shared</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 class='impl'><span class='in-band'><code>impl<T> <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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1134-1173' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.new' class="method"><span id='new.v' class='invisible'><code>unsafe fn <a href='#method.new' class='fnname'>new</a>(ptr: <a class="primitive" href="../primitive.pointer.html">*mut T</a>) -> <a class="struct" href="../../std/ptr/struct.Shared.html" title="struct std::ptr::Shared">Shared</a><T></code></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>shared </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Creates a new <code>Shared</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.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></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>shared </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) -> &T</code></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>shared </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) -> &mut T</code></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>shared </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_mut()</code>.</p> </div><h4 id='method.as_mut_ptr' class="method"><span id='as_mut_ptr.v' class='invisible'><code>unsafe fn <a href='#method.as_mut_ptr' class='fnname'>as_mut_ptr</a>(&self) -> <a class="primitive" href="../primitive.pointer.html">*mut T</a></code></span></h4> <div class='stability'><div class='stab deprecated'>Deprecated since 1.19<p>: renamed to <code>as_ptr</code> for ergonomics/consistency</p> </div><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>shared </code><a href="https://github.com/rust-lang/rust/issues/27730">#27730</a>)</div></div><div class='docblock'><p>Acquires the underlying pointer as a <code>*mut</code> pointer.</p> </div></div><h2 id='implementations'>Trait Implementations</h2><h3 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.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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1189-1193' 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></h4> <div class='docblock'><p>Formats the value using the given formatter.</p> </div></div><h3 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.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></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1186' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 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.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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1112' title='goto source code'>[src]</a></span></h3> <div class='docblock'><p><code>Shared</code> pointers are not <code>Send</code> because the data they reference may be aliased.</p> </div><div class='impl-items'></div><h3 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.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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1117' title='goto source code'>[src]</a></span></h3> <div class='docblock'><p><code>Shared</code> pointers are not <code>Sync</code> because the data they reference may be aliased.</p> </div><div class='impl-items'></div><h3 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.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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1176-1180' 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.Shared.html" title="struct std::ptr::Shared">Shared</a><T></code></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: &Self)</code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></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 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.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.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/ptr.rs.html#1183' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 class='impl'><span class='in-band'><code>impl<T: <a class="trait" href="../../std/panic/trait.RefUnwindSafe.html" title="trait std::panic::RefUnwindSafe">RefUnwindSafe</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.Shared.html" title="struct std::ptr::Shared">Shared</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/panic.rs.html#203' 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>