blob: d9379bad16ff6ad7e854a7d6df4c31d222e6428d [file] [log] [blame]
<!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="Build status Latest version Documentation"><meta name="keywords" content="rust, rustlang, rust-lang, downcast_rs"><title>downcast_rs - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../downcast_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../downcast_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate downcast_rs</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 1.2.0</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#macros">Macros</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">downcast_rs</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/downcast_rs/lib.rs.html#1-692">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><a href="https://github.com/marcianx/downcast-rs/actions"><img src="https://img.shields.io/github/workflow/status/marcianx/downcast-rs/CI/master" alt="Build status" /></a>
<a href="https://crates.io/crates/downcast-rs"><img src="https://img.shields.io/crates/v/downcast-rs.svg" alt="Latest version" /></a>
<a href="https://docs.rs/downcast-rs"><img src="https://docs.rs/downcast-rs/badge.svg" alt="Documentation" /></a></p>
<p>Rust enums are great for types where all variations are known beforehand. But a
container of user-defined types requires an open-ended type like a <strong>trait
object</strong>. Some applications may want to cast these trait objects back to the
original concrete types to access additional functionality and performant
inlined implementations.</p>
<p><code>downcast-rs</code> adds this downcasting support to trait objects using only safe
Rust. It supports <strong>type parameters</strong>, <strong>associated types</strong>, and <strong>constraints</strong>.</p>
<h2 id="usage"><a href="#usage">Usage</a></h2>
<p>Add the following to your <code>Cargo.toml</code>:</p>
<div class="example-wrap"><pre class="language-toml"><code>[dependencies]
downcast-rs = &quot;1.2.0&quot;</code></pre></div>
<p>This crate is <code>no_std</code> compatible. To use it without <code>std</code>:</p>
<div class="example-wrap"><pre class="language-toml"><code>[dependencies]
downcast-rs = { version = &quot;1.2.0&quot;, default-features = false }</code></pre></div>
<p>To make a trait downcastable, make it extend either <code>downcast::Downcast</code> or
<code>downcast::DowncastSync</code> and invoke <code>impl_downcast!</code> on it as in the examples
below.</p>
<p>Since 1.1.0, the minimum supported Rust version is 1.33 to support <code>Rc</code> and <code>Arc</code>
in the receiver position.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">trait </span>Trait: Downcast {}
<span class="macro">impl_downcast!</span>(Trait);
<span class="comment">// Also supports downcasting `Arc`-ed trait objects by extending `DowncastSync`
// and starting `impl_downcast!` with `sync`.
</span><span class="kw">trait </span>TraitSync: DowncastSync {}
<span class="macro">impl_downcast!</span>(sync TraitSync);
<span class="comment">// With type parameters.
</span><span class="kw">trait </span>TraitGeneric1&lt;T&gt;: Downcast {}
<span class="macro">impl_downcast!</span>(TraitGeneric1&lt;T&gt;);
<span class="comment">// With associated types.
</span><span class="kw">trait </span>TraitGeneric2: Downcast { <span class="kw">type </span>G; <span class="kw">type </span>H; }
<span class="macro">impl_downcast!</span>(TraitGeneric2 assoc G, H);
<span class="comment">// With constraints on types.
</span><span class="kw">trait </span>TraitGeneric3&lt;T: Copy&gt;: Downcast {
<span class="kw">type </span>H: Clone;
}
<span class="macro">impl_downcast!</span>(TraitGeneric3&lt;T&gt; assoc H <span class="kw">where </span>T: Copy, H: Clone);
<span class="comment">// With concrete types.
</span><span class="kw">trait </span>TraitConcrete1&lt;T: Copy&gt;: Downcast {}
<span class="macro">impl_downcast!</span>(concrete TraitConcrete1&lt;u32&gt;);
<span class="kw">trait </span>TraitConcrete2&lt;T: Copy&gt;: Downcast { <span class="kw">type </span>H; }
<span class="macro">impl_downcast!</span>(concrete TraitConcrete2&lt;u32&gt; assoc H=f64);</code></pre></div>
<h2 id="example-without-generics"><a href="#example-without-generics">Example without generics</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Import macro via `macro_use` pre-1.30.
</span><span class="attribute">#[macro_use]
</span><span class="kw">extern crate </span>downcast_rs;
<span class="kw">use </span>downcast_rs::DowncastSync;
<span class="comment">// To create a trait with downcasting methods, extend `Downcast` or `DowncastSync`
// and run `impl_downcast!()` on the trait.
</span><span class="kw">trait </span>Base: DowncastSync {}
<span class="macro">impl_downcast!</span>(sync Base); <span class="comment">// `sync` =&gt; also produce `Arc` downcasts.
// Concrete types implementing Base.
</span><span class="attribute">#[derive(Debug)]
</span><span class="kw">struct </span>Foo(u32);
<span class="kw">impl </span>Base <span class="kw">for </span>Foo {}
<span class="attribute">#[derive(Debug)]
</span><span class="kw">struct </span>Bar(f64);
<span class="kw">impl </span>Base <span class="kw">for </span>Bar {}
<span class="kw">fn </span>main() {
<span class="comment">// Create a trait object.
</span><span class="kw">let </span><span class="kw-2">mut </span>base: Box&lt;Base&gt; = Box::new(Foo(<span class="number">42</span>));
<span class="comment">// Try sequential downcasts.
</span><span class="kw">if let </span><span class="prelude-val">Some</span>(foo) = base.downcast_ref::&lt;Foo&gt;() {
<span class="macro">assert_eq!</span>(foo.<span class="number">0</span>, <span class="number">42</span>);
} <span class="kw">else if let </span><span class="prelude-val">Some</span>(bar) = base.downcast_ref::&lt;Bar&gt;() {
<span class="macro">assert_eq!</span>(bar.<span class="number">0</span>, <span class="number">42.0</span>);
}
<span class="macro">assert!</span>(base.is::&lt;Foo&gt;());
<span class="comment">// Fail to convert `Box&lt;Base&gt;` into `Box&lt;Bar&gt;`.
</span><span class="kw">let </span>res = base.downcast::&lt;Bar&gt;();
<span class="macro">assert!</span>(res.is_err());
<span class="kw">let </span>base = res.unwrap_err();
<span class="comment">// Convert `Box&lt;Base&gt;` into `Box&lt;Foo&gt;`.
</span><span class="macro">assert_eq!</span>(<span class="number">42</span>, base.downcast::&lt;Foo&gt;().map_err(|<span class="kw">_</span>| <span class="string">&quot;Shouldn&#39;t happen.&quot;</span>).unwrap().<span class="number">0</span>);
<span class="comment">// Also works with `Rc`.
</span><span class="kw">let </span><span class="kw-2">mut </span>rc: Rc&lt;Base&gt; = Rc::new(Foo(<span class="number">42</span>));
<span class="macro">assert_eq!</span>(<span class="number">42</span>, rc.downcast_rc::&lt;Foo&gt;().map_err(|<span class="kw">_</span>| <span class="string">&quot;Shouldn&#39;t happen.&quot;</span>).unwrap().<span class="number">0</span>);
<span class="comment">// Since this trait is `Sync`, it also supports `Arc` downcasts.
</span><span class="kw">let </span><span class="kw-2">mut </span>arc: Arc&lt;Base&gt; = Arc::new(Foo(<span class="number">42</span>));
<span class="macro">assert_eq!</span>(<span class="number">42</span>, arc.downcast_arc::&lt;Foo&gt;().map_err(|<span class="kw">_</span>| <span class="string">&quot;Shouldn&#39;t happen.&quot;</span>).unwrap().<span class="number">0</span>);
}</code></pre></div>
<h2 id="example-with-a-generic-trait-with-associated-types-and-constraints"><a href="#example-with-a-generic-trait-with-associated-types-and-constraints">Example with a generic trait with associated types and constraints</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Can call macro via namespace since rust 1.30.
</span><span class="kw">extern crate </span>downcast_rs;
<span class="kw">use </span>downcast_rs::Downcast;
<span class="comment">// To create a trait with downcasting methods, extend `Downcast` or `DowncastSync`
// and run `impl_downcast!()` on the trait.
</span><span class="kw">trait </span>Base&lt;T: Clone&gt;: Downcast { <span class="kw">type </span>H: Copy; }
<span class="macro">downcast_rs::impl_downcast!</span>(Base&lt;T&gt; assoc H <span class="kw">where </span>T: Clone, H: Copy);
<span class="comment">// or: impl_downcast!(concrete Base&lt;u32&gt; assoc H=f32)
// Concrete types implementing Base.
</span><span class="kw">struct </span>Foo(u32);
<span class="kw">impl </span>Base&lt;u32&gt; <span class="kw">for </span>Foo { <span class="kw">type </span>H = f32; }
<span class="kw">struct </span>Bar(f64);
<span class="kw">impl </span>Base&lt;u32&gt; <span class="kw">for </span>Bar { <span class="kw">type </span>H = f32; }
<span class="kw">fn </span>main() {
<span class="comment">// Create a trait object.
</span><span class="kw">let </span><span class="kw-2">mut </span>base: Box&lt;Base&lt;u32, H=f32&gt;&gt; = Box::new(Bar(<span class="number">42.0</span>));
<span class="comment">// Try sequential downcasts.
</span><span class="kw">if let </span><span class="prelude-val">Some</span>(foo) = base.downcast_ref::&lt;Foo&gt;() {
<span class="macro">assert_eq!</span>(foo.<span class="number">0</span>, <span class="number">42</span>);
} <span class="kw">else if let </span><span class="prelude-val">Some</span>(bar) = base.downcast_ref::&lt;Bar&gt;() {
<span class="macro">assert_eq!</span>(bar.<span class="number">0</span>, <span class="number">42.0</span>);
}
<span class="macro">assert!</span>(base.is::&lt;Bar&gt;());
}</code></pre></div>
</div></details><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.impl_downcast.html" title="downcast_rs::impl_downcast macro">impl_downcast</a></div><div class="item-right docblock-short">Adds downcasting support to traits that extend <code>downcast::Downcast</code> by defining forwarding
methods to the corresponding implementations on <code>std::any::Any</code> in the standard library.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Downcast.html" title="downcast_rs::Downcast trait">Downcast</a></div><div class="item-right docblock-short">Supports conversion to <code>Any</code>. Traits to be extended by <code>impl_downcast!</code> must extend <code>Downcast</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.DowncastSync.html" title="downcast_rs::DowncastSync trait">DowncastSync</a></div><div class="item-right docblock-short">Extends <code>Downcast</code> to support <code>Sync</code> traits that thus support <code>Arc</code> downcasting as well.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="downcast_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>