Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 10115

rust-doc-1.28.0-1.mga6.armv7hl.rpm

<!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 `TraitObject` struct in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, TraitObject"><title>core::raw::TraitObject - 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 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"><div class="sidebar-menu">&#9776;</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'>Struct TraitObject</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#fields">Fields</a><div class="sidebar-links"><a href="#structfield.data">data</a><a href="#structfield.vtable">vtable</a></div><a class="sidebar-title" href="#implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-Copy">Copy</a><a href="#impl-Clone">Clone</a></div><a class="sidebar-title" href="#synthetic-implementations">Auto Trait Implementations</a><div class="sidebar-links"><a href="#impl-Send">!Send</a><a href="#impl-Sync">!Sync</a></div></div><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>raw</a></p><script>window.sidebarCurrent = {name: 'TraitObject', ty: 'struct', 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'>Struct <a href='../index.html'>core</a>::<wbr><a href='index.html'>raw</a>::<wbr><a class="struct" href=''>TraitObject</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'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/core/raw.rs.html#96-99' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust struct'><div class="docblock attributes">#[repr(C)]
</div>pub struct TraitObject {
    pub data: *mut (),
    pub vtable: *mut (),
}</pre></div><div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API.  (<code>raw </code><a href="https://github.com/rust-lang/rust/issues/27751">#27751</a>)</div></div><div class='docblock'><p>The representation of a trait object like <code>&amp;SomeTrait</code>.</p>
<p>This struct has the same layout as types like <code>&amp;SomeTrait</code> and
<code>Box&lt;AnotherTrait&gt;</code>. The <a href="../../book/first-edition/trait-objects.html#representation">Trait Objects chapter of the
Book</a> contains more details about the precise nature of
these internals.</p>
<p><code>TraitObject</code> is guaranteed to match layouts, but it is not the
type of trait objects (e.g. the fields are not directly accessible
on a <code>&amp;SomeTrait</code>) nor does it control that layout (changing the
definition will not change the layout of a <code>&amp;SomeTrait</code>). It is
only designed to be used by unsafe code that needs to manipulate
the low-level details.</p>
<p>There is no way to refer to all trait objects generically, so the only
way to create values of this type is with functions like
<a href="../intrinsics/fn.transmute.html"><code>std::mem::transmute</code></a>. Similarly, the only way to create a true
trait object from a <code>TraitObject</code> value is with <code>transmute</code>.</p>
<p>Synthesizing a trait object with mismatched types—one where the
vtable does not correspond to the type of the value to which the
data pointer points—is highly likely to lead to undefined
behavior.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="attribute">#![<span class="ident">feature</span>(<span class="ident">raw</span>)]</span>

<span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">mem</span>, <span class="ident">raw</span>};

<span class="comment">// an example trait</span>
<span class="kw">trait</span> <span class="ident">Foo</span> {
    <span class="kw">fn</span> <span class="ident">bar</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">i32</span>;
}

<span class="kw">impl</span> <span class="ident">Foo</span> <span class="kw">for</span> <span class="ident">i32</span> {
    <span class="kw">fn</span> <span class="ident">bar</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">i32</span> {
         <span class="kw-2">*</span><span class="self">self</span> <span class="op">+</span> <span class="number">1</span>
    }
}

<span class="kw">let</span> <span class="ident">value</span>: <span class="ident">i32</span> <span class="op">=</span> <span class="number">123</span>;

<span class="comment">// let the compiler make a trait object</span>
<span class="kw">let</span> <span class="ident">object</span>: <span class="kw-2">&amp;</span><span class="ident">Foo</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">value</span>;

<span class="comment">// look at the raw representation</span>
<span class="kw">let</span> <span class="ident">raw_object</span>: <span class="ident">raw</span>::<span class="ident">TraitObject</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">mem</span>::<span class="ident">transmute</span>(<span class="ident">object</span>) };

<span class="comment">// the data pointer is the address of `value`</span>
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">raw_object</span>.<span class="ident">data</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw">const</span> <span class="ident">i32</span>, <span class="kw-2">&amp;</span><span class="ident">value</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw">const</span> <span class="kw">_</span>);

<span class="kw">let</span> <span class="ident">other_value</span>: <span class="ident">i32</span> <span class="op">=</span> <span class="number">456</span>;

<span class="comment">// construct a new object, pointing to a different `i32`, being</span>
<span class="comment">// careful to use the `i32` vtable from `object`</span>
<span class="kw">let</span> <span class="ident">synthesized</span>: <span class="kw-2">&amp;</span><span class="ident">Foo</span> <span class="op">=</span> <span class="kw">unsafe</span> {
     <span class="ident">mem</span>::<span class="ident">transmute</span>(<span class="ident">raw</span>::<span class="ident">TraitObject</span> {
         <span class="ident">data</span>: <span class="kw-2">&amp;</span><span class="ident">other_value</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw">const</span> <span class="kw">_</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw-2">mut</span> (),
         <span class="ident">vtable</span>: <span class="ident">raw_object</span>.<span class="ident">vtable</span>,
     })
};

<span class="comment">// it should work just as if we had constructed a trait object out of</span>
<span class="comment">// `other_value` directly</span>
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">synthesized</span>.<span class="ident">bar</span>(), <span class="number">457</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(raw)%5D%0A%0Afn%20main()%20%7B%0Ause%20std%3A%3A%7Bmem%2C%20raw%7D%3B%0A%0A%2F%2F%20an%20example%20trait%0Atrait%20Foo%20%7B%0A%20%20%20%20fn%20bar(%26self)%20-%3E%20i32%3B%0A%7D%0A%0Aimpl%20Foo%20for%20i32%20%7B%0A%20%20%20%20fn%20bar(%26self)%20-%3E%20i32%20%7B%0A%20%20%20%20%20%20%20%20%20*self%20%2B%201%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20value%3A%20i32%20%3D%20123%3B%0A%0A%2F%2F%20let%20the%20compiler%20make%20a%20trait%20object%0Alet%20object%3A%20%26Foo%20%3D%20%26value%3B%0A%0A%2F%2F%20look%20at%20the%20raw%20representation%0Alet%20raw_object%3A%20raw%3A%3ATraitObject%20%3D%20unsafe%20%7B%20mem%3A%3Atransmute(object)%20%7D%3B%0A%0A%2F%2F%20the%20data%20pointer%20is%20the%20address%20of%20%60value%60%0Aassert_eq!(raw_object.data%20as%20*const%20i32%2C%20%26value%20as%20*const%20_)%3B%0A%0Alet%20other_value%3A%20i32%20%3D%20456%3B%0A%0A%2F%2F%20construct%20a%20new%20object%2C%20pointing%20to%20a%20different%20%60i32%60%2C%20being%0A%2F%2F%20careful%20to%20use%20the%20%60i32%60%20vtable%20from%20%60object%60%0Alet%20synthesized%3A%20%26Foo%20%3D%20unsafe%20%7B%0A%20%20%20%20%20mem%3A%3Atransmute(raw%3A%3ATraitObject%20%7B%0A%20%20%20%20%20%20%20%20%20data%3A%20%26other_value%20as%20*const%20_%20as%20*mut%20()%2C%0A%20%20%20%20%20%20%20%20%20vtable%3A%20raw_object.vtable%2C%0A%20%20%20%20%20%7D)%0A%7D%3B%0A%0A%2F%2F%20it%20should%20work%20just%20as%20if%20we%20had%20constructed%20a%20trait%20object%20out%20of%0A%2F%2F%20%60other_value%60%20directly%0Aassert_eq!(synthesized.bar()%2C%20457)%3B%0A%7D&amp;version=nightly">Run</a></pre>
</div><h2 id='fields' class='fields small-section-header'>
                       Fields<a href='#fields' class='anchor'></a></h2><span id="structfield.data" class="structfield small-section-header">
                           <a href="#structfield.data" class="anchor field"></a>
                           <span id="data.v" class='invisible'>
                           <code>data: *mut ()</code>
                           </span></span><span class='stab unstable'></span><div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API.  (<code>raw </code><a href="https://github.com/rust-lang/rust/issues/27751">#27751</a>)</div></div><span id="structfield.vtable" class="structfield small-section-header">
                           <a href="#structfield.vtable" class="anchor field"></a>
                           <span id="vtable.v" class='invisible'>
                           <code>vtable: *mut ()</code>
                           </span></span><span class='stab unstable'></span><div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API.  (<code>raw </code><a href="https://github.com/rust-lang/rust/issues/27751">#27751</a>)</div></div>
                <h2 id='implementations' class='small-section-header'>
                  Trait Implementations<a href='#implementations' class='anchor'></a>
                </h2>
                <div id='implementations-list'><h3 id='impl-Copy' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl <a class="trait" href="../../core/marker/trait.Copy.html" title="trait core::marker::Copy">Copy</a> for <a class="struct" href="../../core/raw/struct.TraitObject.html" title="struct core::raw::TraitObject">TraitObject</a></code><a href='#impl-Copy' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/raw.rs.html#94' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'></div><h3 id='impl-Clone' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl <a class="trait" href="../../core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> for <a class="struct" href="../../core/raw/struct.TraitObject.html" title="struct core::raw::TraitObject">TraitObject</a></code><a href='#impl-Clone' class='anchor'></a></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/raw.rs.html#94' title='goto source code'>[src]</a></span></td></tr></tbody></table></h3><div class='impl-items'><h4 id='method.clone' class="method"><span id='clone.v' class='invisible'><table class='table-display'><tbody><tr><td><code>fn <a href='../../core/clone/trait.Clone.html#tymethod.clone' class='fnname'>clone</a>(&amp;self) -&gt; <a class="struct" href="../../core/raw/struct.TraitObject.html" title="struct core::raw::TraitObject">TraitObject</a></code></span></td><td><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/raw.rs.html#94' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Returns a copy of the value. <a href="../../core/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'><table class='table-display'><tbody><tr><td><code>fn <a href='../../core/clone/trait.Clone.html#method.clone_from' class='fnname'>clone_from</a>(&amp;mut self, source: &amp;Self)</code></span></td><td><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#130-132' title='goto source code'>[src]</a></td></tr></tbody></table></span></h4><div class='docblock'><p>Performs copy-assignment from <code>source</code>. <a href="../../core/clone/trait.Clone.html#method.clone_from">Read more</a></p>
</div></div></div>
                <h2 id='synthetic-implementations' class='small-section-header'>
                  Auto Trait Implementations<a href='#synthetic-implementations' class='anchor'></a>
                </h2>
                <div id='synthetic-implementations-list'>
            <h3 id='impl-Send' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl !<a class="trait" href="../../core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="../../core/raw/struct.TraitObject.html" title="struct core::raw::TraitObject">TraitObject</a></code><a href='#impl-Send' class='anchor'></a></span></td><td><span class='out-of-band'></span></td></tr></tbody></table></h3><div class='impl-items'></div><h3 id='impl-Sync' class='impl'><span class='in-band'><table class='table-display'><tbody><tr><td><code>impl !<a class="trait" href="../../core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="../../core/raw/struct.TraitObject.html" title="struct core::raw::TraitObject">TraitObject</a></code><a href='#impl-Sync' class='anchor'></a></span></td><td><span class='out-of-band'></span></td></tr></tbody></table></h3><div class='impl-items'></div></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><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>&#9166;</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>