blob: ff27c1996ffeab4dfebf8f5e2bf19f6f4dadc2e7 [file] [log] [blame] [edit]
<!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="The `Cast` trait is used to make annoying upcasts between logically equivalent types that imply wrappers. For example, one could convert a `DomainGoal` into a `Goal` by doing:"><title>Cast in hir_ty - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-84e720fa.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="hir_ty" data-themes="" data-resource-suffix="" data-rustdoc-version="1.89.0 (29483883e 2025-08-04)" data-channel="1.89.0" data-search-js="search-92309212.js" data-settings-js="settings-5514c975.js" ><script src="../static.files/storage-4e99c027.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-fd3af306.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-32bb7600.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc trait"><!--[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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../hir_ty/index.html">hir_ty</a><span class="version">0.0.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Cast</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#invariant" title="Invariant">Invariant</a></li><li><a href="#iterators" title="Iterators">Iterators</a></li><li><a href="#implementing-cast" title="Implementing Cast">Implementing Cast</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.cast" title="cast">cast</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate hir_<wbr>ty</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">hir_ty</a></div><h1>Trait <span class="trait">Cast</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"></span></div><pre class="rust item-decl"><code>pub trait Cast: <a class="trait" href="https://doc.rust-lang.org/1.89.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
// Provided method
fn <a href="#method.cast" class="fn">cast</a>&lt;U&gt;(self, interner: &lt;U as HasInterner&gt;::Interner) -&gt; U
<span class="where">where Self: CastTo&lt;U&gt;,
U: HasInterner</span> { ... }
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>The <code>Cast</code> trait is used to make annoying upcasts between
logically equivalent types that imply wrappers. For example, one
could convert a <code>DomainGoal</code> into a <code>Goal</code> by doing:</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested"></a><pre class="rust rust-example-rendered"><code><span class="kw">let </span>goal: Goal = domain_goal.cast();</code></pre></div>
<p>This is equivalent to the more explicit:</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested"></a><pre class="rust rust-example-rendered"><code><span class="kw">let </span>goal: Goal = Goal::DomainGoal(domain_goal)</code></pre></div>
<p>Another useful trick is the <code>casted()</code> iterator adapter, which
casts each element in the iterator as it is produced (you must
have the <code>Caster</code> trait in scope for that).</p>
<h2 id="invariant"><a class="doc-anchor" href="#invariant">§</a>Invariant</h2>
<p><code>Cast</code> imposes a key invariant. You can only implement <code>T: Cast&lt;U&gt;</code> if both <code>T</code> and <code>U</code> have the same semantic meaning. Also,
as part of this, they should always use the same set of free
variables (the <code>Canonical</code> implementation, for example, relies on
that).</p>
<h2 id="iterators"><a class="doc-anchor" href="#iterators">§</a>Iterators</h2>
<p>If you import the <code>Caster</code> trait, you can also write <code>.casted()</code> on an
iterator chain to cast every instance within.</p>
<h2 id="implementing-cast"><a class="doc-anchor" href="#implementing-cast">§</a>Implementing Cast</h2>
<p>Do not implement <code>Cast</code> directly. Instead, implement <code>CastTo</code>.
This split setup allows us to write <code>foo.cast::&lt;T&gt;()</code> to mean
“cast to T”.</p>
</div></details><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.cast" class="method"><h4 class="code-header">fn <a href="#method.cast" class="fn">cast</a>&lt;U&gt;(self, interner: &lt;U as HasInterner&gt;::Interner) -&gt; U<div class="where">where
Self: CastTo&lt;U&gt;,
U: HasInterner,</div></h4></section></summary><div class="docblock"><p>Cast a value to type <code>U</code> using <code>CastTo</code>.</p>
</div></details></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.89.0/reference/items/traits.html#dyn-compatibility">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-Cast-for-T" class="impl"><a href="#impl-Cast-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.Cast.html" title="trait hir_ty::Cast">Cast</a> for T</h3></section></div><script src="../trait.impl/chalk_ir/cast/trait.Cast.js" async></script></section></div></main></body></html>