184 lines
No EOL
9.6 KiB
HTML
184 lines
No EOL
9.6 KiB
HTML
<!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 `any` mod in crate `bitflags`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, any">
|
||
|
||
<title>bitflags::__core::any - 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></p><script>window.sidebarCurrent = {name: 'any', ty: 'mod', 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 mod">
|
||
<h1 class='fqn'><span class='in-band'>Module <a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</a>::<wbr><a class='mod' href=''>any</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'>−</span>]
|
||
</a>
|
||
</span><a id='src-22588' class='srclink' href='https://doc.rust-lang.org/nightly/core/any/index.html?gotosrc=22588' title='goto source code'>[src]</a></span></h1>
|
||
<div class='docblock'><p>This module implements the <code>Any</code> trait, which enables dynamic typing
|
||
of any <code>'static</code> type through runtime reflection.</p>
|
||
|
||
<p><code>Any</code> itself can be used to get a <code>TypeId</code>, and has more features when used
|
||
as a trait object. As <code>&Any</code> (a borrowed trait object), it has the <code>is</code> and
|
||
<code>downcast_ref</code> methods, to test if the contained value is of a given type,
|
||
and to get a reference to the inner value as a type. As <code>&mut Any</code>, there
|
||
is also the <code>downcast_mut</code> method, for getting a mutable reference to the
|
||
inner value. <code>Box<Any></code> adds the <code>downcast</code> method, which attempts to
|
||
convert to a <code>Box<T></code>. See the <a href="../../std/boxed/struct.Box.html"><code>Box</code></a> documentation for the full details.</p>
|
||
|
||
<p>Note that &Any is limited to testing whether a value is of a specified
|
||
concrete type, and cannot be used to test whether a type implements a trait.</p>
|
||
|
||
<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
|
||
<p>Consider a situation where we want to log out a value passed to a function.
|
||
We know the value we're working on implements Debug, but we don't know its
|
||
concrete type. We want to give special treatment to certain types: in this
|
||
case printing out the length of String values prior to their value.
|
||
We don't know the concrete type of our value at compile time, so we need to
|
||
use runtime reflection instead.</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>fmt</span>::<span class='ident'>Debug</span>;
|
||
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>any</span>::<span class='ident'>Any</span>;
|
||
|
||
<span class='comment'>// Logger function for any type that implements Debug.</span>
|
||
<span class='kw'>fn</span> <span class='ident'>log</span><span class='op'><</span><span class='ident'>T</span>: <span class='ident'>Any</span> <span class='op'>+</span> <span class='ident'>Debug</span><span class='op'>></span>(<span class='ident'>value</span>: <span class='kw-2'>&</span><span class='ident'>T</span>) {
|
||
<span class='kw'>let</span> <span class='ident'>value_any</span> <span class='op'>=</span> <span class='ident'>value</span> <span class='kw'>as</span> <span class='kw-2'>&</span><span class='ident'>Any</span>;
|
||
|
||
<span class='comment'>// try to convert our value to a String. If successful, we want to</span>
|
||
<span class='comment'>// output the String's length as well as its value. If not, it's a</span>
|
||
<span class='comment'>// different type: just print it out unadorned.</span>
|
||
<span class='kw'>match</span> <span class='ident'>value_any</span>.<span class='ident'>downcast_ref</span>::<span class='op'><</span><span class='ident'>String</span><span class='op'>></span>() {
|
||
<span class='prelude-val'>Some</span>(<span class='ident'>as_string</span>) <span class='op'>=></span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"String ({}): {}"</span>, <span class='ident'>as_string</span>.<span class='ident'>len</span>(), <span class='ident'>as_string</span>);
|
||
}
|
||
<span class='prelude-val'>None</span> <span class='op'>=></span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{:?}"</span>, <span class='ident'>value</span>);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class='comment'>// This function wants to log its parameter out prior to doing work with it.</span>
|
||
<span class='kw'>fn</span> <span class='ident'>do_work</span><span class='op'><</span><span class='ident'>T</span>: <span class='ident'>Any</span> <span class='op'>+</span> <span class='ident'>Debug</span><span class='op'>></span>(<span class='ident'>value</span>: <span class='kw-2'>&</span><span class='ident'>T</span>) {
|
||
<span class='ident'>log</span>(<span class='ident'>value</span>);
|
||
<span class='comment'>// ...do some other work</span>
|
||
}
|
||
|
||
<span class='kw'>fn</span> <span class='ident'>main</span>() {
|
||
<span class='kw'>let</span> <span class='ident'>my_string</span> <span class='op'>=</span> <span class='string'>"Hello World"</span>.<span class='ident'>to_string</span>();
|
||
<span class='ident'>do_work</span>(<span class='kw-2'>&</span><span class='ident'>my_string</span>);
|
||
|
||
<span class='kw'>let</span> <span class='ident'>my_i8</span>: <span class='ident'>i8</span> <span class='op'>=</span> <span class='number'>100</span>;
|
||
<span class='ident'>do_work</span>(<span class='kw-2'>&</span><span class='ident'>my_i8</span>);
|
||
}</pre>
|
||
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class='struct' href='struct.TypeId.html'
|
||
title='bitflags::__core::any::TypeId'>TypeId</a></td>
|
||
<td class='docblock short'>
|
||
<p>A <code>TypeId</code> represents a globally unique identifier for a type.</p>
|
||
</td>
|
||
</tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class='trait' href='trait.Any.html'
|
||
title='bitflags::__core::any::Any'>Any</a></td>
|
||
<td class='docblock short'>
|
||
<p>A type to emulate dynamic typing.</p>
|
||
</td>
|
||
</tr></table></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>
|
||
</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> |