oxipng/doc/bitflags/__core/raw/struct.TraitObject.html
2016-04-20 15:59:23 -04:00

188 lines
No EOL
12 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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 `bitflags`.">
<meta name="keywords" content="rust, rustlang, rust-lang, TraitObject">
<title>bitflags::__core::raw::TraitObject - Rust</title>
<link rel="stylesheet" type="text/css" href="../../../rustdoc.css">
<link rel="stylesheet" type="text/css" href="../../../main.css">
</head>
<body class="rustdoc">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<p class='location'><a href='../../index.html'>bitflags</a>::<wbr><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>
</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 struct">
<h1 class='fqn'><span class='in-band'>Struct <a href='../../index.html'>bitflags</a>::<wbr><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 id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>&#x2212;</span>]
</a>
</span><a id='src-30565' class='srclink' href='https://doc.rust-lang.org/nightly/core/raw/struct.TraitObject.html?gotosrc=30565' title='goto source code'>[src]</a></span></h1>
<pre class='rust struct'>pub struct TraitObject {
pub data: <a class='primitive' href='https://doc.rust-lang.org/nightly/bitflags/primitive.pointer.html'>*mut </a><a class='primitive' href='https://doc.rust-lang.org/nightly/bitflags/primitive.tuple.html'>()</a>,
pub vtable: <a class='primitive' href='https://doc.rust-lang.org/nightly/bitflags/primitive.pointer.html'>*mut </a><a class='primitive' href='https://doc.rust-lang.org/nightly/bitflags/primitive.tuple.html'>()</a>,
}</pre><div class='stability'><em class='stab unstable'>Unstable (<code>raw</code>)</em></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/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 <code>Repr</code> implementation for <code>TraitObject</code> because 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
<code>std::mem::transmute</code>. 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='op'>!</span>[<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='kw'>use</span> <span class='ident'>std</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='op'>*</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='op'>*</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='op'>*</span><span class='kw'>const</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='op'>*</span><span class='kw'>const</span> _ <span class='kw'>as</span> <span class='op'>*</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 like we 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>);</pre>
</div><h2 class='fields'>Fields</h2>
<table><tr class='stab unstable'>
<td id='structfield.data'><code>data</code></td><td><div class='stability'><em class='stab unstable'>Unstable (<code>raw</code>)</em></div></td></tr><tr class='stab unstable'>
<td id='structfield.vtable'><code>vtable</code></td><td><div class='stability'><em class='stab unstable'>Unstable (<code>raw</code>)</em></div></td></tr></table><h2 id='implementations'>Trait Implementations</h2><h3 id='derived_implementations'>Derived Implementations </h3><h3 class='impl'><code>impl <a class='trait' href='../../../bitflags/__core/marker/trait.Copy.html' title='bitflags::__core::marker::Copy'>Copy</a> for <a class='struct' href='../../../bitflags/__core/raw/struct.TraitObject.html' title='bitflags::__core::raw::TraitObject'>TraitObject</a></code></h3><div class='impl-items'></div><h3 class='impl'><code>impl <a class='trait' href='../../../bitflags/__core/clone/trait.Clone.html' title='bitflags::__core::clone::Clone'>Clone</a> for <a class='struct' href='../../../bitflags/__core/raw/struct.TraitObject.html' title='bitflags::__core::raw::TraitObject'>TraitObject</a></code></h3><div class='impl-items'><h4 id='method.clone' class='method'><code>fn <a href='../../../bitflags/__core/clone/trait.Clone.html#tymethod.clone' class='fnname'>clone</a>(&amp;self) -&gt; <a class='struct' href='../../../bitflags/__core/raw/struct.TraitObject.html' title='bitflags::__core::raw::TraitObject'>TraitObject</a></code></h4>
<div class='stability'><em class='stab unstable'>Unstable (<code>raw</code>)</em></div><h4 id='method.clone_from' class='method'><span class="since">1.0.0</span><code>fn <a href='../../../bitflags/__core/clone/trait.Clone.html#method.clone_from' class='fnname'>clone_from</a>(&amp;mut self, source: &amp;Self)</code></h4>
</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>&larrb;</dt>
<dd>Move up in search results</dd>
<dt>&rarrb;</dt>
<dd>Move down in search results</dd>
<dt>&#9166;</dt>
<dd>Go to active search result</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 = "bitflags";
window.playgroundUrl = "";
</script>
<script src="../../../jquery.js"></script>
<script src="../../../main.js"></script>
<script defer src="../../../search-index.js"></script>
</body>
</html>