128 lines
15 KiB
HTML
128 lines
15 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="Provides a macro to simplify operator overloading."><title>overload - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.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-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="overload" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.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 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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../overload/index.html">overload</a><span class="version">0.1.1</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#introduction" title="Introduction">Introduction</a></li><li><a href="#owned-and-borrowed-types" title="Owned and borrowed types">Owned and borrowed types</a></li><li><a href="#binary-operators" title="Binary operators">Binary operators</a></li><li><a href="#assignment-operators" title="Assignment operators">Assignment operators</a></li><li><a href="#unary-operators" title="Unary operators">Unary operators</a></li><li><a href="#notes" title="Notes">Notes</a></li></ul><h3><a href="#macros">Crate Items</a></h3><ul class="block"><li><a href="#macros" title="Macros">Macros</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>overload</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/overload/lib.rs.html#1-257">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Provides a macro to simplify operator overloading.</p>
|
||
<p>To use, include the following:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">extern crate </span>overload;
|
||
<span class="kw">use </span>overload::overload;
|
||
<span class="kw">use </span>std::ops; <span class="comment">// <- don't forget this or you'll get nasty errors</span></code></pre></div>
|
||
<h2 id="introduction"><a class="doc-anchor" href="#introduction">§</a>Introduction</h2>
|
||
<p>Suppose we have the following <code>struct</code> definition:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(PartialEq, Debug)]
|
||
</span><span class="kw">struct </span>Val {
|
||
v: i32
|
||
}</code></pre></div>
|
||
<p>We can overload the addition of <code>Val</code>s like so:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">overload!</span>((a: Val) + (b: Val) -> Val { Val { v: a.v + b.v } });</code></pre></div>
|
||
<p>The macro call above generates the following code:</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">impl </span>ops::Add<Val> <span class="kw">for </span>Val {
|
||
<span class="kw">type </span>Output = Val;
|
||
<span class="kw">fn </span>add(<span class="self">self</span>, b: Val) -> <span class="self">Self</span>::Output {
|
||
<span class="kw">let </span>a = <span class="self">self</span>;
|
||
Val { v: a.v + b.v }
|
||
}
|
||
}</code></pre></div>
|
||
<p>We are now able to add <code>Val</code>s:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">assert_eq!</span>(Val{v:<span class="number">3</span>} + Val{v:<span class="number">5</span>}, Val{v:<span class="number">8</span>});</code></pre></div>
|
||
<h2 id="owned-and-borrowed-types"><a class="doc-anchor" href="#owned-and-borrowed-types">§</a>Owned and borrowed types</h2>
|
||
<p>If we also wanted to overload addition for the borrowed type <code>&Val</code> we could write:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">overload!</span>((a: <span class="kw-2">&</span>Val) + (b: <span class="kw-2">&</span>Val) -> Val { Val { v: a.v + b.v } });</code></pre></div>
|
||
<p>We might also want to overload addition between the owned and borrowed types:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">overload!</span>((a: Val) + (b: <span class="kw-2">&</span>Val) -> Val { Val { v: a.v + b.v } });
|
||
<span class="macro">overload!</span>((a: <span class="kw-2">&</span>Val) + (b: Val) -> Val { Val { v: a.v + b.v } });</code></pre></div>
|
||
<p>Let’s see how we can write these combinations more concisely.</p>
|
||
<p>We can include a <code>?</code> in front of a type to indicate that it should stand in for both the owned and borrowed type.</p>
|
||
<p>To overload addition for all four combinations between <code>Val</code> and <code>&Val</code> we can therefore simply include a <code>?</code> in front of both types:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">overload!</span>((a: <span class="question-mark">?</span>Val) + (b: <span class="question-mark">?</span>Val) -> Val { Val { v: a.v + b.v } });</code></pre></div>
|
||
<p>The macro call above generates the following code:</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">impl </span>ops::Add<Val> <span class="kw">for </span>Val {
|
||
<span class="kw">type </span>Output = Val;
|
||
<span class="kw">fn </span>add(<span class="self">self</span>, b: Val) -> <span class="self">Self</span>::Output {
|
||
<span class="kw">let </span>a = <span class="self">self</span>;
|
||
Val { v: a.v + b.v }
|
||
}
|
||
}
|
||
|
||
<span class="kw">impl </span>ops::Add<<span class="kw-2">&</span>Val> <span class="kw">for </span>Val {
|
||
<span class="kw">type </span>Output = Val;
|
||
<span class="kw">fn </span>add(<span class="self">self</span>, b: <span class="kw-2">&</span>Val) -> <span class="self">Self</span>::Output {
|
||
<span class="kw">let </span>a = <span class="self">self</span>;
|
||
Val { v: a.v + b.v }
|
||
}
|
||
}
|
||
|
||
<span class="kw">impl </span>ops::Add<Val> <span class="kw">for </span><span class="kw-2">&</span>Val {
|
||
<span class="kw">type </span>Output = Val;
|
||
<span class="kw">fn </span>add(<span class="self">self</span>, b: Val) -> <span class="self">Self</span>::Output {
|
||
<span class="kw">let </span>a = <span class="self">self</span>;
|
||
Val { v: a.v + b.v }
|
||
}
|
||
}
|
||
|
||
<span class="kw">impl </span>ops::Add<<span class="kw-2">&</span>Val> <span class="kw">for </span><span class="kw-2">&</span>Val {
|
||
<span class="kw">type </span>Output = Val;
|
||
<span class="kw">fn </span>add(<span class="self">self</span>, b: <span class="kw-2">&</span>Val) -> <span class="self">Self</span>::Output {
|
||
<span class="kw">let </span>a = <span class="self">self</span>;
|
||
Val { v: a.v + b.v }
|
||
}
|
||
}</code></pre></div>
|
||
<p>We are now able to add <code>Val</code>s and <code>&Val</code>s in any combination:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">assert_eq!</span>(Val{v:<span class="number">3</span>} + Val{v:<span class="number">5</span>}, Val{v:<span class="number">8</span>});
|
||
<span class="macro">assert_eq!</span>(Val{v:<span class="number">3</span>} + <span class="kw-2">&</span>Val{v:<span class="number">5</span>}, Val{v:<span class="number">8</span>});
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>Val{v:<span class="number">3</span>} + Val{v:<span class="number">5</span>}, Val{v:<span class="number">8</span>});
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>Val{v:<span class="number">3</span>} + <span class="kw-2">&</span>Val{v:<span class="number">5</span>}, Val{v:<span class="number">8</span>});</code></pre></div>
|
||
<h2 id="binary-operators"><a class="doc-anchor" href="#binary-operators">§</a>Binary operators</h2>
|
||
<p>The general syntax to overload a binary operator between types <code><a_type></code> and <code><b_type></code> is:</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="macro">overload!</span>((<a_ident>: <a_type>) <op> (<b_ident>: <b_type>) -> <out_type> { <span class="comment">/*body*/ </span>});</code></pre></div>
|
||
<p>Inside the body you can use <code><a_ident></code> and <code><b_ident></code> freely to perform any computation.</p>
|
||
<p>The last line of the body needs to be an expression (i.e. no <code>;</code> at the end of the line) of type <code><out_type></code>.</p>
|
||
<div><table><thead><tr><th>Operator</th><th>Example</th><th>Trait</th></tr></thead><tbody>
|
||
<tr><td>+</td><td><code>overload!((a: A) + (b: B) -> C { /*...*/ );</code></td><td>Add</td></tr>
|
||
<tr><td>-</td><td><code>overload!((a: A) - (b: B) -> C { /*...*/ );</code></td><td>Sub</td></tr>
|
||
<tr><td>*</td><td><code>overload!((a: A) * (b: B) -> C { /*...*/ );</code></td><td>Mul</td></tr>
|
||
<tr><td>/</td><td><code>overload!((a: A) / (b: B) -> C { /*...*/ );</code></td><td>Div</td></tr>
|
||
<tr><td>%</td><td><code>overload!((a: A) % (b: B) -> C { /*...*/ );</code></td><td>Rem</td></tr>
|
||
<tr><td>&</td><td><code>overload!((a: A) & (b: B) -> C { /*...*/ );</code></td><td>BitAnd</td></tr>
|
||
<tr><td>|</td><td><code>overload!((a: A) | (b: B) -> C { /*…*/ );</code></td><td>BitOr</td></tr>
|
||
<tr><td>^</td><td><code>overload!((a: A) ^ (b: B) -> C { /*...*/ );</code></td><td>BitXor</td></tr>
|
||
<tr><td><<</td><td><code>overload!((a: A) << (b: B) -> C { /*...*/ );</code></td><td>Shl</td></tr>
|
||
<tr><td>>></td><td><code>overload!((a: A) >> (b: B) -> C { /*...*/ );</code></td><td>Shr</td></tr>
|
||
</tbody></table>
|
||
</div><h2 id="assignment-operators"><a class="doc-anchor" href="#assignment-operators">§</a>Assignment operators</h2>
|
||
<p>The general syntax to overload an assignment operator between types <code><a_type></code> and <code><b_type></code> is:</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="macro">overload!</span>((<a_ident>: <span class="kw-2">&mut </span><a_type>) <op> (<b_ident>: <b_type>) { <span class="comment">/*body*/ </span>});</code></pre></div>
|
||
<p>Inside the body you can use <code><a_ident></code> and <code><b_ident></code> freely to perform any computation and mutate <code><a_ident></code> as desired.</p>
|
||
<div><table><thead><tr><th>Operator</th><th>Example</th><th>Trait</th></tr></thead><tbody>
|
||
<tr><td>+=</td><td><code>overload!((a: &mut A) += (b: B) { /*...*/ );</code></td><td>AddAssign</td></tr>
|
||
<tr><td>-=</td><td><code>overload!((a: &mut A) -= (b: B) { /*...*/ );</code></td><td>SubAssign</td></tr>
|
||
<tr><td>*=</td><td><code>overload!((a: &mut A) *= (b: B) { /*...*/ );</code></td><td>MulAssign</td></tr>
|
||
<tr><td>/=</td><td><code>overload!((a: &mut A) /= (b: B) { /*...*/ );</code></td><td>DivAssign</td></tr>
|
||
<tr><td>%=</td><td><code>overload!((a: &mut A) %= (b: B) { /*...*/ );</code></td><td>RemAssign</td></tr>
|
||
<tr><td>&=</td><td><code>overload!((a: &mut A) &= (b: B) { /*...*/ );</code></td><td>BitAndAssign</td></tr>
|
||
<tr><td>|=</td><td><code>overload!((a: &mut A) |= (b: B) { /*…*/ );</code></td><td>BitOrAssign</td></tr>
|
||
<tr><td>^=</td><td><code>overload!((a: &mut A) ^= (b: B) { /*...*/ );</code></td><td>BitXorAssign</td></tr>
|
||
<tr><td><<=</td><td><code>overload!((a: &mut A) <<= (b: B) { /*...*/ );</code></td><td>ShlAssign</td></tr>
|
||
<tr><td>>>=</td><td><code>overload!((a: &mut A) >>= (b: B) { /*...*/ );</code></td><td>ShrAssign</td></tr>
|
||
</tbody></table>
|
||
</div><h2 id="unary-operators"><a class="doc-anchor" href="#unary-operators">§</a>Unary operators</h2>
|
||
<p>The general syntax to overload a unary operator for type <code><a_type></code> is:</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="macro">overload!</span>(<op> (<a_ident>: <a_type>) -> <out_type> { <span class="comment">/*body*/ </span>});</code></pre></div>
|
||
<p>Inside the body you can use <code><a_ident></code> freely to perform any computation.</p>
|
||
<p>The last line of the body needs to be an expression (i.e. no <code>;</code> at the end of the line) of type <code><out_type></code>.</p>
|
||
<div><table><thead><tr><th>Operator</th><th>Example</th><th>Trait</th></tr></thead><tbody>
|
||
<tr><td>-</td><td><code>overload!(- (a: A) -> B { /*...*/ );</code></td><td>Neg</td></tr>
|
||
<tr><td>!</td><td><code>overload!(! (a: A) -> B { /*...*/ );</code></td><td>Not</td></tr>
|
||
</tbody></table>
|
||
</div><h2 id="notes"><a class="doc-anchor" href="#notes">§</a>Notes</h2>
|
||
<p>Remember that you can only overload operators between one or more types if at least one of the types is defined in the current crate.</p>
|
||
</div></details><h2 id="macros" class="section-header">Macros<a href="#macros" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="macro" href="macro.overload.html" title="macro overload::overload">overload</a></div><div class="desc docblock-short">Overloads an operator. See the <a href="index.html">module level documentation</a> for more information.</div></li></ul></section></div></main></body></html> |