diff options
-rw-r--r-- | docs/index.html | 17 | ||||
-rw-r--r-- | docs/index.xml | 613 | ||||
-rw-r--r-- | docs/posts/auto-currying_rust_functions/index.html | 512 | ||||
-rw-r--r-- | docs/posts/rapid_refactoring_with_vim/index.html | 50 | ||||
-rw-r--r-- | docs/posts/static_sites_with_bash/index.html | 36 | ||||
-rw-r--r-- | docs/posts/turing_complete_type_systems/index.html | 73 | ||||
-rw-r--r-- | posts/turing_complete_type_systems.md | 22 |
7 files changed, 725 insertions, 598 deletions
diff --git a/docs/index.html b/docs/index.html index f745423..003835c 100644 --- a/docs/index.html +++ b/docs/index.html | |||
@@ -36,6 +36,23 @@ | |||
36 | <tr> | 36 | <tr> |
37 | <td class=table-post> | 37 | <td class=table-post> |
38 | <div class="date"> | 38 | <div class="date"> |
39 | 18/06 — 2020 | ||
40 | </div> | ||
41 | <a href="/posts/turing_complete_type_systems" class="post-link"> | ||
42 | <span class="post-link">Turing Complete Type Systems</span> | ||
43 | </a> | ||
44 | </td> | ||
45 | <td class=table-stats> | ||
46 | <span class="stats-number"> | ||
47 | 0.8 | ||
48 | </span> | ||
49 | <span class=stats-unit>min</span> | ||
50 | </td> | ||
51 | </tr> | ||
52 | |||
53 | <tr> | ||
54 | <td class=table-post> | ||
55 | <div class="date"> | ||
39 | 09/05 — 2020 | 56 | 09/05 — 2020 |
40 | </div> | 57 | </div> |
41 | <a href="/posts/auto-currying_rust_functions" class="post-link"> | 58 | <a href="/posts/auto-currying_rust_functions" class="post-link"> |
diff --git a/docs/index.xml b/docs/index.xml index 07120bc..17d6bd8 100644 --- a/docs/index.xml +++ b/docs/index.xml | |||
@@ -12,6 +12,21 @@ | |||
12 | <language>en-us</language> | 12 | <language>en-us</language> |
13 | <copyright>Creative Commons BY-NC-SA 4.0</copyright> | 13 | <copyright>Creative Commons BY-NC-SA 4.0</copyright> |
14 | <item> | 14 | <item> |
15 | <title>Turing Complete Type Systems</title> | ||
16 | <description><p>Rust’s type system is Turing complete:</p> | ||
17 | <ul> | ||
18 | <li><a href="https://github.com/doctorn/trait-eval/">FizzBuzz with Rust Traits</a></li> | ||
19 | <li><a href="https://github.com/Ashymad/fortraith">A Forth implementation with Rust Traits</a></li> | ||
20 | </ul> | ||
21 | <p>It is impossible to determine if a program written in a generally Turing complete system will ever stop. That is, it is impossible to write a program <code>f</code> that determines if a program <code>g</code>, where <code>g</code> is written in a Turing complete programming language, will ever halt. The <a href="https://en.wikipedia.org/wiki/Halting_problem">Halting Problem</a> is in fact, an <a href="https://en.wikipedia.org/wiki/Undecidable_problem">undecidable problem</a>.</p> | ||
22 | <p><em>How is any of this relevant?</em></p> | ||
23 | <p>Rust performs compile-time type inference. The type checker, in turn, compiles and infers types, I would describe it as a compiler inside a compiler. It is possible that <code>rustc</code> may never finish compiling your Rust program!</p> | ||
24 | <p>I understand that this post lacks content.</p></description> | ||
25 | <link>https://peppe.rs/posts/turing_complete_type_systems/</link> | ||
26 | <pubDate>Thu, 18 Jun 2020 05:07:00 +0000</pubDate> | ||
27 | <guid>https://peppe.rs/posts/turing_complete_type_systems/</guid> | ||
28 | </item> | ||
29 | <item> | ||
15 | <title>Auto-currying Rust Functions</title> | 30 | <title>Auto-currying Rust Functions</title> |
16 | <description><p>This post contains a gentle introduction to procedural macros in Rust and a guide to writing a procedural macro to curry Rust functions. The source code for the entire library can be found <a href="https://github.com/nerdypepper/cutlass">here</a>. It is also available on <a href="https://crates.io/crates/cutlass">crates.io</a>.</p> | 31 | <description><p>This post contains a gentle introduction to procedural macros in Rust and a guide to writing a procedural macro to curry Rust functions. The source code for the entire library can be found <a href="https://github.com/nerdypepper/cutlass">here</a>. It is also available on <a href="https://crates.io/crates/cutlass">crates.io</a>.</p> |
17 | <p>The following links might prove to be useful before getting started:</p> | 32 | <p>The following links might prove to be useful before getting started:</p> |
@@ -69,17 +84,17 @@ h(x)(y)(z) = g(y)(z) = k(z) = v</code></pre> | |||
69 | <p>We will be using Attribute macros to convert a Rust function into a curried Rust function, which we should be able to call via: <code>function(arg1)(arg2)</code>.</p> | 84 | <p>We will be using Attribute macros to convert a Rust function into a curried Rust function, which we should be able to call via: <code>function(arg1)(arg2)</code>.</p> |
70 | <h3 id="definitions">Definitions</h3> | 85 | <h3 id="definitions">Definitions</h3> |
71 | <p>Being respectable programmers, we define the input to and the output from our proc-macro. Here’s a good non-trivial function to start out with:</p> | 86 | <p>Being respectable programmers, we define the input to and the output from our proc-macro. Here’s a good non-trivial function to start out with:</p> |
72 | <div class="sourceCode" id="cb4"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb4-1"><a href="#cb4-1"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 87 | <div class="sourceCode" id="cb4"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
73 | <span id="cb4-2"><a href="#cb4-2"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 88 | <span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
74 | <span id="cb4-3"><a href="#cb4-3"></a><span class="op">}</span></span></code></pre></div> | 89 | <span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
75 | <p>Hmm, what would our output look like? What should our proc-macro generate ideally? Well, if we understood currying correctly, we should accept an argument and return a function that accepts an argument and returns … you get the point. Something like this should do:</p> | 90 | <p>Hmm, what would our output look like? What should our proc-macro generate ideally? Well, if we understood currying correctly, we should accept an argument and return a function that accepts an argument and returns … you get the point. Something like this should do:</p> |
76 | <div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1"></a><span class="kw">fn</span> add_curried1(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> | 91 | <div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true"></a><span class="kw">fn</span> add_curried1(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> |
77 | <span id="cb5-2"><a href="#cb5-2"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried2 (y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> | 92 | <span id="cb5-2"><a href="#cb5-2" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried2 (y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> |
78 | <span id="cb5-3"><a href="#cb5-3"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried3 (z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 93 | <span id="cb5-3"><a href="#cb5-3" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried3 (z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
79 | <span id="cb5-4"><a href="#cb5-4"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 94 | <span id="cb5-4"><a href="#cb5-4" aria-hidden="true"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
80 | <span id="cb5-5"><a href="#cb5-5"></a> <span class="op">}</span></span> | 95 | <span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a> <span class="op">}</span></span> |
81 | <span id="cb5-6"><a href="#cb5-6"></a> <span class="op">}</span></span> | 96 | <span id="cb5-6"><a href="#cb5-6" aria-hidden="true"></a> <span class="op">}</span></span> |
82 | <span id="cb5-7"><a href="#cb5-7"></a><span class="op">}</span></span></code></pre></div> | 97 | <span id="cb5-7"><a href="#cb5-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
83 | <p>A couple of things to note:</p> | 98 | <p>A couple of things to note:</p> |
84 | <p><strong>Return types</strong><br /> | 99 | <p><strong>Return types</strong><br /> |
85 | We have placed <code>?</code>s in place of return types. Let’s try to fix that. <code>add_curried3</code> returns the ‘value’, so <code>u32</code> is accurate. <code>add_curried2</code> returns <code>add_curried3</code>. What is the type of <code>add_curried3</code>? It is a function that takes in a <code>u32</code> and returns a <code>u32</code>. So a <code>fn(u32) -> u32</code> will do right? No, I’ll explain why in the next point, but for now, we will make use of the <code>Fn</code> trait, our return type is <code>impl Fn(u32) -> u32</code>. This basically tells the compiler that we will be returning something function-like, a.k.a, behaves like a <code>Fn</code>. Cool!</p> | 100 | We have placed <code>?</code>s in place of return types. Let’s try to fix that. <code>add_curried3</code> returns the ‘value’, so <code>u32</code> is accurate. <code>add_curried2</code> returns <code>add_curried3</code>. What is the type of <code>add_curried3</code>? It is a function that takes in a <code>u32</code> and returns a <code>u32</code>. So a <code>fn(u32) -> u32</code> will do right? No, I’ll explain why in the next point, but for now, we will make use of the <code>Fn</code> trait, our return type is <code>impl Fn(u32) -> u32</code>. This basically tells the compiler that we will be returning something function-like, a.k.a, behaves like a <code>Fn</code>. Cool!</p> |
@@ -92,9 +107,9 @@ We have placed <code>?</code>s in place of return types. Let’s try to fix that | |||
92 | A function cannot access it’s environment. Our solution will not work. <code>add_curried3</code> attempts to access <code>x</code>, which is not allowed! A closure<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> however, can. If we are returning a closure, our return type must be <code>impl Fn</code>, and not <code>fn</code>. The difference between the <code>Fn</code> trait and function pointers is beyond the scope of this post.</p> | 107 | A function cannot access it’s environment. Our solution will not work. <code>add_curried3</code> attempts to access <code>x</code>, which is not allowed! A closure<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> however, can. If we are returning a closure, our return type must be <code>impl Fn</code>, and not <code>fn</code>. The difference between the <code>Fn</code> trait and function pointers is beyond the scope of this post.</p> |
93 | <h3 id="refinement">Refinement</h3> | 108 | <h3 id="refinement">Refinement</h3> |
94 | <p>Armed with knowledge, we refine our expected output, this time, employing closures:</p> | 109 | <p>Armed with knowledge, we refine our expected output, this time, employing closures:</p> |
95 | <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 110 | <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
96 | <span id="cb8-2"><a href="#cb8-2"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 111 | <span id="cb8-2"><a href="#cb8-2" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
97 | <span id="cb8-3"><a href="#cb8-3"></a><span class="op">}</span></span></code></pre></div> | 112 | <span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
98 | <p>Alas, that does not compile either! It errors out with the following message:</p> | 113 | <p>Alas, that does not compile either! It errors out with the following message:</p> |
99 | <pre><code>error[E0562]: `impl Trait` not allowed outside of function | 114 | <pre><code>error[E0562]: `impl Trait` not allowed outside of function |
100 | and inherent method return types | 115 | and inherent method return types |
@@ -105,15 +120,15 @@ and inherent method return types | |||
105 | </code></pre> | 120 | </code></pre> |
106 | <p>You are allowed to return an <code>impl Fn</code> only inside a function. We are currently returning it from another return! Or at least, that was the most I could make out of the error message.</p> | 121 | <p>You are allowed to return an <code>impl Fn</code> only inside a function. We are currently returning it from another return! Or at least, that was the most I could make out of the error message.</p> |
107 | <p>We are going to have to cheat a bit to fix this issue; with type aliases and a convenient nightly feature <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a>:</p> | 122 | <p>We are going to have to cheat a bit to fix this issue; with type aliases and a convenient nightly feature <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a>:</p> |
108 | <div class="sourceCode" id="cb10"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb10-1"><a href="#cb10-1"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span> <span class="co">// allows us to use `impl Fn` in type aliases!</span></span> | 123 | <div class="sourceCode" id="cb10"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span> <span class="co">// allows us to use `impl Fn` in type aliases!</span></span> |
109 | <span id="cb10-2"><a href="#cb10-2"></a></span> | 124 | <span id="cb10-2"><a href="#cb10-2" aria-hidden="true"></a></span> |
110 | <span id="cb10-3"><a href="#cb10-3"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span> <span class="co">// the return value when zero args are to be applied</span></span> | 125 | <span id="cb10-3"><a href="#cb10-3" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span> <span class="co">// the return value when zero args are to be applied</span></span> |
111 | <span id="cb10-4"><a href="#cb10-4"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span> <span class="co">// the return value when one arg is to be applied</span></span> | 126 | <span id="cb10-4"><a href="#cb10-4" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span> <span class="co">// the return value when one arg is to be applied</span></span> |
112 | <span id="cb10-5"><a href="#cb10-5"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span> <span class="co">// the return value when two args are to be applied</span></span> | 127 | <span id="cb10-5"><a href="#cb10-5" aria-hidden="true"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span> <span class="co">// the return value when two args are to be applied</span></span> |
113 | <span id="cb10-6"><a href="#cb10-6"></a></span> | 128 | <span id="cb10-6"><a href="#cb10-6" aria-hidden="true"></a></span> |
114 | <span id="cb10-7"><a href="#cb10-7"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> T2 <span class="op">{</span></span> | 129 | <span id="cb10-7"><a href="#cb10-7" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> T2 <span class="op">{</span></span> |
115 | <span id="cb10-8"><a href="#cb10-8"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 130 | <span id="cb10-8"><a href="#cb10-8" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
116 | <span id="cb10-9"><a href="#cb10-9"></a><span class="op">}</span></span></code></pre></div> | 131 | <span id="cb10-9"><a href="#cb10-9" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
117 | <p>Drop that into a cargo project, call <code>add(4)(5)(6)</code>, cross your fingers, and run <code>cargo +nightly run</code>. You should see a 15 unless you forgot to print it!</p> | 132 | <p>Drop that into a cargo project, call <code>add(4)(5)(6)</code>, cross your fingers, and run <code>cargo +nightly run</code>. You should see a 15 unless you forgot to print it!</p> |
118 | <h3 id="the-in-betweens">The In-Betweens</h3> | 133 | <h3 id="the-in-betweens">The In-Betweens</h3> |
119 | <p>Let us write the magical bits that take us from function to curried function.</p> | 134 | <p>Let us write the magical bits that take us from function to curried function.</p> |
@@ -147,19 +162,19 @@ proc-macro = true # this is important!</code></pre> | |||
147 | <p>We will be using an external <code>proc-macro2</code> crate as well as an internal <code>proc-macro</code> crate. Not confusing at all!</p> | 162 | <p>We will be using an external <code>proc-macro2</code> crate as well as an internal <code>proc-macro</code> crate. Not confusing at all!</p> |
148 | <h4 id="the-attribute-macro">The attribute macro</h4> | 163 | <h4 id="the-attribute-macro">The attribute macro</h4> |
149 | <p>Drop this into <code>src/lib.rs</code>, to get the ball rolling.</p> | 164 | <p>Drop this into <code>src/lib.rs</code>, to get the ball rolling.</p> |
150 | <div class="sourceCode" id="cb13"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb13-1"><a href="#cb13-1"></a><span class="co">// src/lib.rs</span></span> | 165 | <div class="sourceCode" id="cb13"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
151 | <span id="cb13-2"><a href="#cb13-2"></a></span> | 166 | <span id="cb13-2"><a href="#cb13-2" aria-hidden="true"></a></span> |
152 | <span id="cb13-3"><a href="#cb13-3"></a><span class="kw">use</span> <span class="pp">proc_macro::</span>TokenStream<span class="op">;</span> <span class="co">// 1</span></span> | 167 | <span id="cb13-3"><a href="#cb13-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">proc_macro::</span>TokenStream<span class="op">;</span> <span class="co">// 1</span></span> |
153 | <span id="cb13-4"><a href="#cb13-4"></a><span class="kw">use</span> <span class="pp">quote::</span>quote<span class="op">;</span></span> | 168 | <span id="cb13-4"><a href="#cb13-4" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">quote::</span>quote<span class="op">;</span></span> |
154 | <span id="cb13-5"><a href="#cb13-5"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> ItemFn<span class="op">};</span></span> | 169 | <span id="cb13-5"><a href="#cb13-5" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> ItemFn<span class="op">};</span></span> |
155 | <span id="cb13-6"><a href="#cb13-6"></a></span> | 170 | <span id="cb13-6"><a href="#cb13-6" aria-hidden="true"></a></span> |
156 | <span id="cb13-7"><a href="#cb13-7"></a><span class="at">#[</span>proc_macro_attribute<span class="at">]</span> <span class="co">// 2</span></span> | 171 | <span id="cb13-7"><a href="#cb13-7" aria-hidden="true"></a><span class="at">#[</span>proc_macro_attribute<span class="at">]</span> <span class="co">// 2</span></span> |
157 | <span id="cb13-8"><a href="#cb13-8"></a><span class="kw">pub</span> <span class="kw">fn</span> curry(_attr<span class="op">:</span> TokenStream<span class="op">,</span> item<span class="op">:</span> TokenStream) <span class="op">-></span> TokenStream <span class="op">{</span></span> | 172 | <span id="cb13-8"><a href="#cb13-8" aria-hidden="true"></a><span class="kw">pub</span> <span class="kw">fn</span> curry(_attr<span class="op">:</span> TokenStream<span class="op">,</span> item<span class="op">:</span> TokenStream) <span class="op">-></span> TokenStream <span class="op">{</span></span> |
158 | <span id="cb13-9"><a href="#cb13-9"></a> <span class="kw">let</span> parsed <span class="op">=</span> <span class="pp">parse_macro_input!</span>(item <span class="kw">as</span> ItemFn)<span class="op">;</span> <span class="co">// 3</span></span> | 173 | <span id="cb13-9"><a href="#cb13-9" aria-hidden="true"></a> <span class="kw">let</span> parsed <span class="op">=</span> <span class="pp">parse_macro_input!</span>(item <span class="kw">as</span> ItemFn)<span class="op">;</span> <span class="co">// 3</span></span> |
159 | <span id="cb13-10"><a href="#cb13-10"></a> generate_curry(parsed)<span class="op">.</span>into() <span class="co">// 4</span></span> | 174 | <span id="cb13-10"><a href="#cb13-10" aria-hidden="true"></a> generate_curry(parsed)<span class="op">.</span>into() <span class="co">// 4</span></span> |
160 | <span id="cb13-11"><a href="#cb13-11"></a><span class="op">}</span></span> | 175 | <span id="cb13-11"><a href="#cb13-11" aria-hidden="true"></a><span class="op">}</span></span> |
161 | <span id="cb13-12"><a href="#cb13-12"></a></span> | 176 | <span id="cb13-12"><a href="#cb13-12" aria-hidden="true"></a></span> |
162 | <span id="cb13-13"><a href="#cb13-13"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{}</span></span></code></pre></div> | 177 | <span id="cb13-13"><a href="#cb13-13" aria-hidden="true"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{}</span></span></code></pre></div> |
163 | <p><strong>1. Imports</strong></p> | 178 | <p><strong>1. Imports</strong></p> |
164 | <p>A <code>Tokenstream</code> holds (hopefully valid) Rust code, this is the type of our input and output. Note that we are importing this type from <code>proc_macro</code> and not <code>proc_macro2</code>.</p> | 179 | <p>A <code>Tokenstream</code> holds (hopefully valid) Rust code, this is the type of our input and output. Note that we are importing this type from <code>proc_macro</code> and not <code>proc_macro2</code>.</p> |
165 | <p><code>quote!</code> from the <code>quote</code> crate is a macro that allows us to quickly produce <code>TokenStream</code>s. Much like the LISP <code>quote</code> procedure, you can use the <code>quote!</code> macro for symbolic transformations.</p> | 180 | <p><code>quote!</code> from the <code>quote</code> crate is a macro that allows us to quickly produce <code>TokenStream</code>s. Much like the LISP <code>quote</code> procedure, you can use the <code>quote!</code> macro for symbolic transformations.</p> |
@@ -171,16 +186,16 @@ proc-macro = true # this is important!</code></pre> | |||
171 | <p><strong>4. Returning <code>TokenStream</code>s </strong></p> | 186 | <p><strong>4. Returning <code>TokenStream</code>s </strong></p> |
172 | <p>We haven’t filled in <code>generate_curry</code> yet, but we can see that it returns a <code>proc_macro2::TokenStream</code> and not a <code>proc_macro::TokenStream</code>, so drop a <code>.into()</code> to convert it.</p> | 187 | <p>We haven’t filled in <code>generate_curry</code> yet, but we can see that it returns a <code>proc_macro2::TokenStream</code> and not a <code>proc_macro::TokenStream</code>, so drop a <code>.into()</code> to convert it.</p> |
173 | <p>Lets move on, and fill in <code>generate_curry</code>, I would suggest keeping the documentation for <a href="https://docs.rs/syn/1.0.19/syn/struct.ItemFn.html"><code>syn::ItemFn</code></a> and <a href="https://docs.rs/syn/1.0.19/syn/struct.Signature.html"><code>syn::Signature</code></a> open.</p> | 188 | <p>Lets move on, and fill in <code>generate_curry</code>, I would suggest keeping the documentation for <a href="https://docs.rs/syn/1.0.19/syn/struct.ItemFn.html"><code>syn::ItemFn</code></a> and <a href="https://docs.rs/syn/1.0.19/syn/struct.Signature.html"><code>syn::Signature</code></a> open.</p> |
174 | <div class="sourceCode" id="cb14"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb14-1"><a href="#cb14-1"></a><span class="co">// src/lib.rs</span></span> | 189 | <div class="sourceCode" id="cb14"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
175 | <span id="cb14-2"><a href="#cb14-2"></a></span> | 190 | <span id="cb14-2"><a href="#cb14-2" aria-hidden="true"></a></span> |
176 | <span id="cb14-3"><a href="#cb14-3"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> | 191 | <span id="cb14-3"><a href="#cb14-3" aria-hidden="true"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> |
177 | <span id="cb14-4"><a href="#cb14-4"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span> <span class="co">// function body</span></span> | 192 | <span id="cb14-4"><a href="#cb14-4" aria-hidden="true"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span> <span class="co">// function body</span></span> |
178 | <span id="cb14-5"><a href="#cb14-5"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span> <span class="co">// function signature</span></span> | 193 | <span id="cb14-5"><a href="#cb14-5" aria-hidden="true"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span> <span class="co">// function signature</span></span> |
179 | <span id="cb14-6"><a href="#cb14-6"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span> <span class="co">// visibility, pub or not</span></span> | 194 | <span id="cb14-6"><a href="#cb14-6" aria-hidden="true"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span> <span class="co">// visibility, pub or not</span></span> |
180 | <span id="cb14-7"><a href="#cb14-7"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span> <span class="co">// function name/identifier</span></span> | 195 | <span id="cb14-7"><a href="#cb14-7" aria-hidden="true"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span> <span class="co">// function name/identifier</span></span> |
181 | <span id="cb14-8"><a href="#cb14-8"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span> <span class="co">// comma separated args</span></span> | 196 | <span id="cb14-8"><a href="#cb14-8" aria-hidden="true"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span> <span class="co">// comma separated args</span></span> |
182 | <span id="cb14-9"><a href="#cb14-9"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span> <span class="co">// return type</span></span> | 197 | <span id="cb14-9"><a href="#cb14-9" aria-hidden="true"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span> <span class="co">// return type</span></span> |
183 | <span id="cb14-10"><a href="#cb14-10"></a><span class="op">}</span></span></code></pre></div> | 198 | <span id="cb14-10"><a href="#cb14-10" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
184 | <p>We are simply extracting the bits of the function, we will be reusing the original function’s visibility and name. Take a look at what <code>syn::Signature</code> can tell us about a function:</p> | 199 | <p>We are simply extracting the bits of the function, we will be reusing the original function’s visibility and name. Take a look at what <code>syn::Signature</code> can tell us about a function:</p> |
185 | <pre><code> .-- syn::Ident (ident) | 200 | <pre><code> .-- syn::Ident (ident) |
186 | / | 201 | / |
@@ -192,220 +207,220 @@ syn::token::Fn --' / \ (output) | |||
192 | <p>Enough analysis, lets produce our first bit of Rust code.</p> | 207 | <p>Enough analysis, lets produce our first bit of Rust code.</p> |
193 | <h4 id="function-body">Function Body</h4> | 208 | <h4 id="function-body">Function Body</h4> |
194 | <p>Recall that the body of a curried <code>add</code> should look like this:</p> | 209 | <p>Recall that the body of a curried <code>add</code> should look like this:</p> |
195 | <div class="sourceCode" id="cb16"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb16-1"><a href="#cb16-1"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span></code></pre></div> | 210 | <div class="sourceCode" id="cb16"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span></code></pre></div> |
196 | <p>And in general:</p> | 211 | <p>And in general:</p> |
197 | <div class="sourceCode" id="cb17"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb17-1"><a href="#cb17-1"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>arg2<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>arg3<span class="op">|</span> <span class="op">...</span> <span class="op">|</span>argN<span class="op">|</span> <span class="op"><</span>function body here<span class="op">></span></span></code></pre></div> | 212 | <div class="sourceCode" id="cb17"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>arg2<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>arg3<span class="op">|</span> <span class="op">...</span> <span class="op">|</span>argN<span class="op">|</span> <span class="op"><</span>function body here<span class="op">></span></span></code></pre></div> |
198 | <p>We already have the function’s body, provided by <code>fn_body</code>, in our <code>generate_curry</code> function. All that’s left to add is the <code>move |arg2| move |arg3| ...</code> stuff, for which we need to extract the argument identifiers (doc: <a href="https://docs.rs/syn/1.0.18/syn/punctuated/struct.Punctuated.html">Punctuated</a>, <a href="https://docs.rs/syn/1.0.18/syn/enum.FnArg.html">FnArg</a>, <a href="https://docs.rs/syn/1.0.18/syn/struct.PatType.html">PatType</a>):</p> | 213 | <p>We already have the function’s body, provided by <code>fn_body</code>, in our <code>generate_curry</code> function. All that’s left to add is the <code>move |arg2| move |arg3| ...</code> stuff, for which we need to extract the argument identifiers (doc: <a href="https://docs.rs/syn/1.0.18/syn/punctuated/struct.Punctuated.html">Punctuated</a>, <a href="https://docs.rs/syn/1.0.18/syn/enum.FnArg.html">FnArg</a>, <a href="https://docs.rs/syn/1.0.18/syn/struct.PatType.html">PatType</a>):</p> |
199 | <div class="sourceCode" id="cb18"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb18-1"><a href="#cb18-1"></a><span class="co">// src/lib.rs</span></span> | 214 | <div class="sourceCode" id="cb18"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
200 | <span id="cb18-2"><a href="#cb18-2"></a><span class="kw">use</span> <span class="pp">syn::punctuated::</span>Punctuated<span class="op">;</span></span> | 215 | <span id="cb18-2"><a href="#cb18-2" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::punctuated::</span>Punctuated<span class="op">;</span></span> |
201 | <span id="cb18-3"><a href="#cb18-3"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> FnArg<span class="op">,</span> Pat<span class="op">,</span> ItemFn<span class="op">,</span> Block<span class="op">};</span></span> | 216 | <span id="cb18-3"><a href="#cb18-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> FnArg<span class="op">,</span> Pat<span class="op">,</span> ItemFn<span class="op">,</span> Block<span class="op">};</span></span> |
202 | <span id="cb18-4"><a href="#cb18-4"></a></span> | 217 | <span id="cb18-4"><a href="#cb18-4" aria-hidden="true"></a></span> |
203 | <span id="cb18-5"><a href="#cb18-5"></a><span class="kw">fn</span> extract_arg_idents(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Pat<span class="op">>></span> <span class="op">{</span> </span> | 218 | <span id="cb18-5"><a href="#cb18-5" aria-hidden="true"></a><span class="kw">fn</span> extract_arg_idents(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Pat<span class="op">>></span> <span class="op">{</span> </span> |
204 | <span id="cb18-6"><a href="#cb18-6"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_arg_pat)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> | 219 | <span id="cb18-6"><a href="#cb18-6" aria-hidden="true"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_arg_pat)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> |
205 | <span id="cb18-7"><a href="#cb18-7"></a><span class="op">}</span></span></code></pre></div> | 220 | <span id="cb18-7"><a href="#cb18-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
206 | <p>Alright, so we are iterating over function args (<code>Punctuated</code> is a collection that you can iterate over) and mapping an <code>extract_arg_pat</code> to every item. What’s <code>extract_arg_pat</code>?</p> | 221 | <p>Alright, so we are iterating over function args (<code>Punctuated</code> is a collection that you can iterate over) and mapping an <code>extract_arg_pat</code> to every item. What’s <code>extract_arg_pat</code>?</p> |
207 | <div class="sourceCode" id="cb19"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb19-1"><a href="#cb19-1"></a><span class="co">// src/lib.rs</span></span> | 222 | <div class="sourceCode" id="cb19"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
208 | <span id="cb19-2"><a href="#cb19-2"></a></span> | 223 | <span id="cb19-2"><a href="#cb19-2" aria-hidden="true"></a></span> |
209 | <span id="cb19-3"><a href="#cb19-3"></a><span class="kw">fn</span> extract_arg_pat(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span> <span class="op">{</span></span> | 224 | <span id="cb19-3"><a href="#cb19-3" aria-hidden="true"></a><span class="kw">fn</span> extract_arg_pat(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span> <span class="op">{</span></span> |
210 | <span id="cb19-4"><a href="#cb19-4"></a> <span class="kw">match</span> a <span class="op">{</span></span> | 225 | <span id="cb19-4"><a href="#cb19-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> |
211 | <span id="cb19-5"><a href="#cb19-5"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>pat<span class="op">,</span></span> | 226 | <span id="cb19-5"><a href="#cb19-5" aria-hidden="true"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>pat<span class="op">,</span></span> |
212 | <span id="cb19-6"><a href="#cb19-6"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> | 227 | <span id="cb19-6"><a href="#cb19-6" aria-hidden="true"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> |
213 | <span id="cb19-7"><a href="#cb19-7"></a> <span class="op">}</span></span> | 228 | <span id="cb19-7"><a href="#cb19-7" aria-hidden="true"></a> <span class="op">}</span></span> |
214 | <span id="cb19-8"><a href="#cb19-8"></a><span class="op">}</span></span></code></pre></div> | 229 | <span id="cb19-8"><a href="#cb19-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
215 | <p><code>FnArg</code> is an enum type as you might have guessed. The <code>Typed</code> variant encompasses args that are written as <code>name: type</code> and the other variant, <code>Reciever</code> refers to <code>self</code> types. Ignore those for now, keep it simple.</p> | 230 | <p><code>FnArg</code> is an enum type as you might have guessed. The <code>Typed</code> variant encompasses args that are written as <code>name: type</code> and the other variant, <code>Reciever</code> refers to <code>self</code> types. Ignore those for now, keep it simple.</p> |
216 | <p>Every <code>FnArg::Typed</code> value contains a <code>pat</code>, which is in essence, the name of the argument. The type of the arg is accessible via <code>p.ty</code> (we will be using this later).</p> | 231 | <p>Every <code>FnArg::Typed</code> value contains a <code>pat</code>, which is in essence, the name of the argument. The type of the arg is accessible via <code>p.ty</code> (we will be using this later).</p> |
217 | <p>With that done, we should be able to write the codegen for the function body:</p> | 232 | <p>With that done, we should be able to write the codegen for the function body:</p> |
218 | <div class="sourceCode" id="cb20"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb20-1"><a href="#cb20-1"></a><span class="co">// src/lib.rs</span></span> | 233 | <div class="sourceCode" id="cb20"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
219 | <span id="cb20-2"><a href="#cb20-2"></a></span> | 234 | <span id="cb20-2"><a href="#cb20-2" aria-hidden="true"></a></span> |
220 | <span id="cb20-3"><a href="#cb20-3"></a><span class="kw">fn</span> generate_body(fn_args<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span>]<span class="op">,</span> body<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Block<span class="op">></span>) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> | 235 | <span id="cb20-3"><a href="#cb20-3" aria-hidden="true"></a><span class="kw">fn</span> generate_body(fn_args<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span>]<span class="op">,</span> body<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Block<span class="op">></span>) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> |
221 | <span id="cb20-4"><a href="#cb20-4"></a> <span class="pp">quote!</span> <span class="op">{</span></span> | 236 | <span id="cb20-4"><a href="#cb20-4" aria-hidden="true"></a> <span class="pp">quote!</span> <span class="op">{</span></span> |
222 | <span id="cb20-5"><a href="#cb20-5"></a> <span class="kw">return</span> #( <span class="kw">move</span> <span class="op">|</span>#fn_args<span class="op">|</span> )<span class="op">*</span> #body</span> | 237 | <span id="cb20-5"><a href="#cb20-5" aria-hidden="true"></a> <span class="kw">return</span> #( <span class="kw">move</span> <span class="op">|</span>#fn_args<span class="op">|</span> )<span class="op">*</span> #body</span> |
223 | <span id="cb20-6"><a href="#cb20-6"></a> <span class="op">}</span></span> | 238 | <span id="cb20-6"><a href="#cb20-6" aria-hidden="true"></a> <span class="op">}</span></span> |
224 | <span id="cb20-7"><a href="#cb20-7"></a><span class="op">}</span></span></code></pre></div> | 239 | <span id="cb20-7"><a href="#cb20-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
225 | <p>That is some scary looking syntax! Allow me to explain. The <code>quote!{ ... }</code> returns a <code>proc_macro2::TokenStream</code>, if we wrote <code>quote!{ let x = 1 + 2; }</code>, it wouldn’t create a new variable <code>x</code> with value 3, it would literally produce a stream of tokens with that expression.</p> | 240 | <p>That is some scary looking syntax! Allow me to explain. The <code>quote!{ ... }</code> returns a <code>proc_macro2::TokenStream</code>, if we wrote <code>quote!{ let x = 1 + 2; }</code>, it wouldn’t create a new variable <code>x</code> with value 3, it would literally produce a stream of tokens with that expression.</p> |
226 | <p>The <code>#</code> enables variable interpolation. <code>#body</code> will look for <code>body</code> in the current scope, take its value, and insert it in the returned <code>TokenStream</code>. Kinda like quasi quoting in LISPs, you have written one.</p> | 241 | <p>The <code>#</code> enables variable interpolation. <code>#body</code> will look for <code>body</code> in the current scope, take its value, and insert it in the returned <code>TokenStream</code>. Kinda like quasi quoting in LISPs, you have written one.</p> |
227 | <p>What about <code>#( move |#fn_args| )*</code>? That is repetition. <code>quote</code> iterates through <code>fn_args</code>, and drops a <code>move</code> behind each one, it then places pipes (<code>|</code>), around it.</p> | 242 | <p>What about <code>#( move |#fn_args| )*</code>? That is repetition. <code>quote</code> iterates through <code>fn_args</code>, and drops a <code>move</code> behind each one, it then places pipes (<code>|</code>), around it.</p> |
228 | <p>Let us test our first bit of codegen! Modify <code>generate_curry</code> like so:</p> | 243 | <p>Let us test our first bit of codegen! Modify <code>generate_curry</code> like so:</p> |
229 | <div class="sourceCode" id="cb21"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb21-1"><a href="#cb21-1"></a><span class="co">// src/lib.rs</span></span> | 244 | <div class="sourceCode" id="cb21"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
230 | <span id="cb21-2"><a href="#cb21-2"></a></span> | 245 | <span id="cb21-2"><a href="#cb21-2" aria-hidden="true"></a></span> |
231 | <span id="cb21-3"><a href="#cb21-3"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> TokenStream <span class="op">{</span></span> | 246 | <span id="cb21-3"><a href="#cb21-3" aria-hidden="true"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> TokenStream <span class="op">{</span></span> |
232 | <span id="cb21-4"><a href="#cb21-4"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> | 247 | <span id="cb21-4"><a href="#cb21-4" aria-hidden="true"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> |
233 | <span id="cb21-5"><a href="#cb21-5"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> | 248 | <span id="cb21-5"><a href="#cb21-5" aria-hidden="true"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> |
234 | <span id="cb21-6"><a href="#cb21-6"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> | 249 | <span id="cb21-6"><a href="#cb21-6" aria-hidden="true"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> |
235 | <span id="cb21-7"><a href="#cb21-7"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> | 250 | <span id="cb21-7"><a href="#cb21-7" aria-hidden="true"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> |
236 | <span id="cb21-8"><a href="#cb21-8"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> | 251 | <span id="cb21-8"><a href="#cb21-8" aria-hidden="true"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> |
237 | <span id="cb21-9"><a href="#cb21-9"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> | 252 | <span id="cb21-9"><a href="#cb21-9" aria-hidden="true"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> |
238 | <span id="cb21-10"><a href="#cb21-10"></a></span> | 253 | <span id="cb21-10"><a href="#cb21-10" aria-hidden="true"></a></span> |
239 | <span id="cb21-11"><a href="#cb21-11"></a><span class="op">+</span> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> | 254 | <span id="cb21-11"><a href="#cb21-11" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> |
240 | <span id="cb21-12"><a href="#cb21-12"></a><span class="op">+</span> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> | 255 | <span id="cb21-12"><a href="#cb21-12" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> |
241 | <span id="cb21-13"><a href="#cb21-13"></a></span> | 256 | <span id="cb21-13"><a href="#cb21-13" aria-hidden="true"></a></span> |
242 | <span id="cb21-14"><a href="#cb21-14"></a><span class="op">+</span> <span class="co">// remember, our curried body starts with the second argument!</span></span> | 257 | <span id="cb21-14"><a href="#cb21-14" aria-hidden="true"></a><span class="op">+</span> <span class="co">// remember, our curried body starts with the second argument!</span></span> |
243 | <span id="cb21-15"><a href="#cb21-15"></a><span class="op">+</span> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> | 258 | <span id="cb21-15"><a href="#cb21-15" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> |
244 | <span id="cb21-16"><a href="#cb21-16"></a><span class="op">+</span> <span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> curried_body)<span class="op">;</span></span> | 259 | <span id="cb21-16"><a href="#cb21-16" aria-hidden="true"></a><span class="op">+</span> <span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> curried_body)<span class="op">;</span></span> |
245 | <span id="cb21-17"><a href="#cb21-17"></a></span> | 260 | <span id="cb21-17"><a href="#cb21-17" aria-hidden="true"></a></span> |
246 | <span id="cb21-18"><a href="#cb21-18"></a> <span class="kw">return</span> <span class="pp">TokenStream::</span>new()<span class="op">;</span></span> | 261 | <span id="cb21-18"><a href="#cb21-18" aria-hidden="true"></a> <span class="kw">return</span> <span class="pp">TokenStream::</span>new()<span class="op">;</span></span> |
247 | <span id="cb21-19"><a href="#cb21-19"></a> <span class="op">}</span></span></code></pre></div> | 262 | <span id="cb21-19"><a href="#cb21-19" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> |
248 | <p>Add a little test to <code>tests/</code>:</p> | 263 | <p>Add a little test to <code>tests/</code>:</p> |
249 | <div class="sourceCode" id="cb22"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb22-1"><a href="#cb22-1"></a><span class="co">// tests/smoke.rs</span></span> | 264 | <div class="sourceCode" id="cb22"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true"></a><span class="co">// tests/smoke.rs</span></span> |
250 | <span id="cb22-2"><a href="#cb22-2"></a></span> | 265 | <span id="cb22-2"><a href="#cb22-2" aria-hidden="true"></a></span> |
251 | <span id="cb22-3"><a href="#cb22-3"></a><span class="at">#[</span><span class="pp">currying::</span>curry<span class="at">]</span></span> | 266 | <span id="cb22-3"><a href="#cb22-3" aria-hidden="true"></a><span class="at">#[</span><span class="pp">currying::</span>curry<span class="at">]</span></span> |
252 | <span id="cb22-4"><a href="#cb22-4"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 267 | <span id="cb22-4"><a href="#cb22-4" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
253 | <span id="cb22-5"><a href="#cb22-5"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> | 268 | <span id="cb22-5"><a href="#cb22-5" aria-hidden="true"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> |
254 | <span id="cb22-6"><a href="#cb22-6"></a><span class="op">}</span></span> | 269 | <span id="cb22-6"><a href="#cb22-6" aria-hidden="true"></a><span class="op">}</span></span> |
255 | <span id="cb22-7"><a href="#cb22-7"></a></span> | 270 | <span id="cb22-7"><a href="#cb22-7" aria-hidden="true"></a></span> |
256 | <span id="cb22-8"><a href="#cb22-8"></a><span class="at">#[</span>test<span class="at">]</span></span> | 271 | <span id="cb22-8"><a href="#cb22-8" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> |
257 | <span id="cb22-9"><a href="#cb22-9"></a><span class="kw">fn</span> works() <span class="op">{</span></span> | 272 | <span id="cb22-9"><a href="#cb22-9" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> |
258 | <span id="cb22-10"><a href="#cb22-10"></a> <span class="pp">assert!</span>(<span class="cn">true</span>)<span class="op">;</span></span> | 273 | <span id="cb22-10"><a href="#cb22-10" aria-hidden="true"></a> <span class="pp">assert!</span>(<span class="cn">true</span>)<span class="op">;</span></span> |
259 | <span id="cb22-11"><a href="#cb22-11"></a><span class="op">}</span></span></code></pre></div> | 274 | <span id="cb22-11"><a href="#cb22-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
260 | <p>You should find something like this in the output of <code>cargo test</code>:</p> | 275 | <p>You should find something like this in the output of <code>cargo test</code>:</p> |
261 | <pre><code>return move | y | move | z | { x + y + z }</code></pre> | 276 | <pre><code>return move | y | move | z | { x + y + z }</code></pre> |
262 | <p>Glorious <code>println!</code> debugging!</p> | 277 | <p>Glorious <code>println!</code> debugging!</p> |
263 | <h4 id="function-signature">Function signature</h4> | 278 | <h4 id="function-signature">Function signature</h4> |
264 | <p>This section gets into the more complicated bits of the macro, generating type aliases and the function signature. By the end of this section, we should have a full working auto-currying macro!</p> | 279 | <p>This section gets into the more complicated bits of the macro, generating type aliases and the function signature. By the end of this section, we should have a full working auto-currying macro!</p> |
265 | <p>Recall what our generated type aliases should look like, for our <code>add</code> function:</p> | 280 | <p>Recall what our generated type aliases should look like, for our <code>add</code> function:</p> |
266 | <div class="sourceCode" id="cb24"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb24-1"><a href="#cb24-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 281 | <div class="sourceCode" id="cb24"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
267 | <span id="cb24-2"><a href="#cb24-2"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> | 282 | <span id="cb24-2"><a href="#cb24-2" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> |
268 | <span id="cb24-3"><a href="#cb24-3"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span></span></code></pre></div> | 283 | <span id="cb24-3"><a href="#cb24-3" aria-hidden="true"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span></span></code></pre></div> |
269 | <p>In general:</p> | 284 | <p>In general:</p> |
270 | <div class="sourceCode" id="cb25"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb25-1"><a href="#cb25-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op"><</span><span class="kw">return</span> <span class="kw">type</span>><span class="op">;</span></span> | 285 | <div class="sourceCode" id="cb25"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op"><</span><span class="kw">return</span> <span class="kw">type</span>><span class="op">;</span></span> |
271 | <span id="cb25-2"><a href="#cb25-2"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N>) -> T0<span class="op">;</span></span> | 286 | <span id="cb25-2"><a href="#cb25-2" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N>) -> T0<span class="op">;</span></span> |
272 | <span id="cb25-3"><a href="#cb25-3"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N - 1>) -> T1<span class="op">;</span></span> | 287 | <span id="cb25-3"><a href="#cb25-3" aria-hidden="true"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N - 1>) -> T1<span class="op">;</span></span> |
273 | <span id="cb25-4"><a href="#cb25-4"></a><span class="op">.</span></span> | 288 | <span id="cb25-4"><a href="#cb25-4" aria-hidden="true"></a><span class="op">.</span></span> |
274 | <span id="cb25-5"><a href="#cb25-5"></a><span class="op">.</span></span> | 289 | <span id="cb25-5"><a href="#cb25-5" aria-hidden="true"></a><span class="op">.</span></span> |
275 | <span id="cb25-6"><a href="#cb25-6"></a><span class="op">.</span></span> | 290 | <span id="cb25-6"><a href="#cb25-6" aria-hidden="true"></a><span class="op">.</span></span> |
276 | <span id="cb25-7"><a href="#cb25-7"></a><span class="kw">type</span> T(N-1) <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg 2>) -> T(N-2)<span class="op">;</span></span></code></pre></div> | 291 | <span id="cb25-7"><a href="#cb25-7" aria-hidden="true"></a><span class="kw">type</span> T(N-1) <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg 2>) -> T(N-2)<span class="op">;</span></span></code></pre></div> |
277 | <p>To codegen that, we need the types of:</p> | 292 | <p>To codegen that, we need the types of:</p> |
278 | <ul> | 293 | <ul> |
279 | <li>all our inputs (arguments)</li> | 294 | <li>all our inputs (arguments)</li> |
280 | <li>the output (the return type)</li> | 295 | <li>the output (the return type)</li> |
281 | </ul> | 296 | </ul> |
282 | <p>To fetch the types of all our inputs, we can simply reuse the bits we wrote to fetch the names of all our inputs! (doc: <a href="https://docs.rs/syn/1.0.18/syn/enum.Type.html">Type</a>)</p> | 297 | <p>To fetch the types of all our inputs, we can simply reuse the bits we wrote to fetch the names of all our inputs! (doc: <a href="https://docs.rs/syn/1.0.18/syn/enum.Type.html">Type</a>)</p> |
283 | <div class="sourceCode" id="cb26"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb26-1"><a href="#cb26-1"></a><span class="co">// src/lib.rs</span></span> | 298 | <div class="sourceCode" id="cb26"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
284 | <span id="cb26-2"><a href="#cb26-2"></a></span> | 299 | <span id="cb26-2"><a href="#cb26-2" aria-hidden="true"></a></span> |
285 | <span id="cb26-3"><a href="#cb26-3"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> Block<span class="op">,</span> FnArg<span class="op">,</span> ItemFn<span class="op">,</span> Pat<span class="op">,</span> ReturnType<span class="op">,</span> Type<span class="op">};</span></span> | 300 | <span id="cb26-3"><a href="#cb26-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> Block<span class="op">,</span> FnArg<span class="op">,</span> ItemFn<span class="op">,</span> Pat<span class="op">,</span> ReturnType<span class="op">,</span> Type<span class="op">};</span></span> |
286 | <span id="cb26-4"><a href="#cb26-4"></a></span> | 301 | <span id="cb26-4"><a href="#cb26-4" aria-hidden="true"></a></span> |
287 | <span id="cb26-5"><a href="#cb26-5"></a><span class="kw">fn</span> extract_type(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> | 302 | <span id="cb26-5"><a href="#cb26-5" aria-hidden="true"></a><span class="kw">fn</span> extract_type(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> |
288 | <span id="cb26-6"><a href="#cb26-6"></a> <span class="kw">match</span> a <span class="op">{</span></span> | 303 | <span id="cb26-6"><a href="#cb26-6" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> |
289 | <span id="cb26-7"><a href="#cb26-7"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>ty<span class="op">,</span> <span class="co">// notice `ty` instead of `pat`</span></span> | 304 | <span id="cb26-7"><a href="#cb26-7" aria-hidden="true"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>ty<span class="op">,</span> <span class="co">// notice `ty` instead of `pat`</span></span> |
290 | <span id="cb26-8"><a href="#cb26-8"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> | 305 | <span id="cb26-8"><a href="#cb26-8" aria-hidden="true"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> |
291 | <span id="cb26-9"><a href="#cb26-9"></a> <span class="op">}</span></span> | 306 | <span id="cb26-9"><a href="#cb26-9" aria-hidden="true"></a> <span class="op">}</span></span> |
292 | <span id="cb26-10"><a href="#cb26-10"></a><span class="op">}</span></span> | 307 | <span id="cb26-10"><a href="#cb26-10" aria-hidden="true"></a><span class="op">}</span></span> |
293 | <span id="cb26-11"><a href="#cb26-11"></a></span> | 308 | <span id="cb26-11"><a href="#cb26-11" aria-hidden="true"></a></span> |
294 | <span id="cb26-12"><a href="#cb26-12"></a><span class="kw">fn</span> extract_arg_types(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Type<span class="op">>></span> <span class="op">{</span></span> | 309 | <span id="cb26-12"><a href="#cb26-12" aria-hidden="true"></a><span class="kw">fn</span> extract_arg_types(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Type<span class="op">>></span> <span class="op">{</span></span> |
295 | <span id="cb26-13"><a href="#cb26-13"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_type)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> | 310 | <span id="cb26-13"><a href="#cb26-13" aria-hidden="true"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_type)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> |
296 | <span id="cb26-14"><a href="#cb26-14"></a></span> | 311 | <span id="cb26-14"><a href="#cb26-14" aria-hidden="true"></a></span> |
297 | <span id="cb26-15"><a href="#cb26-15"></a><span class="op">}</span></span></code></pre></div> | 312 | <span id="cb26-15"><a href="#cb26-15" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
298 | <p>A good reader would have looked at the docs for output member of the <code>syn::Signature</code> struct. It has the type <code>syn::ReturnType</code>. So there is no extraction to do here right? There are actually a couple of things we have to ensure here:</p> | 313 | <p>A good reader would have looked at the docs for output member of the <code>syn::Signature</code> struct. It has the type <code>syn::ReturnType</code>. So there is no extraction to do here right? There are actually a couple of things we have to ensure here:</p> |
299 | <ol type="1"> | 314 | <ol type="1"> |
300 | <li><p>We need to ensure that the function returns! A function that does not return is pointless in this case, and I will tell you why, in the <a href="#notes">Notes</a> section.</p></li> | 315 | <li><p>We need to ensure that the function returns! A function that does not return is pointless in this case, and I will tell you why, in the <a href="#notes">Notes</a> section.</p></li> |
301 | <li><p>A <code>ReturnType</code> encloses the arrow of the return as well, we need to get rid of that. Recall:</p> | 316 | <li><p>A <code>ReturnType</code> encloses the arrow of the return as well, we need to get rid of that. Recall:</p> |
302 | <div class="sourceCode" id="cb27"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb27-1"><a href="#cb27-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span></span> | 317 | <div class="sourceCode" id="cb27"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span></span> |
303 | <span id="cb27-2"><a href="#cb27-2"></a><span class="co">// and not</span></span> | 318 | <span id="cb27-2"><a href="#cb27-2" aria-hidden="true"></a><span class="co">// and not</span></span> |
304 | <span id="cb27-3"><a href="#cb27-3"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op">-></span> <span class="dt">u32</span></span></code></pre></div></li> | 319 | <span id="cb27-3"><a href="#cb27-3" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op">-></span> <span class="dt">u32</span></span></code></pre></div></li> |
305 | </ol> | 320 | </ol> |
306 | <p>Here is the snippet that handles extraction of the return type (doc: <a href="https://docs.rs/syn/1.0.19/syn/enum.ReturnType.html">syn::ReturnType</a>):</p> | 321 | <p>Here is the snippet that handles extraction of the return type (doc: <a href="https://docs.rs/syn/1.0.19/syn/enum.ReturnType.html">syn::ReturnType</a>):</p> |
307 | <div class="sourceCode" id="cb28"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb28-1"><a href="#cb28-1"></a><span class="co">// src/lib.rs</span></span> | 322 | <div class="sourceCode" id="cb28"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
308 | <span id="cb28-2"><a href="#cb28-2"></a></span> | 323 | <span id="cb28-2"><a href="#cb28-2" aria-hidden="true"></a></span> |
309 | <span id="cb28-3"><a href="#cb28-3"></a><span class="kw">fn</span> extract_return_type(a<span class="op">:</span> ReturnType) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> | 324 | <span id="cb28-3"><a href="#cb28-3" aria-hidden="true"></a><span class="kw">fn</span> extract_return_type(a<span class="op">:</span> ReturnType) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> |
310 | <span id="cb28-4"><a href="#cb28-4"></a> <span class="kw">match</span> a <span class="op">{</span></span> | 325 | <span id="cb28-4"><a href="#cb28-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> |
311 | <span id="cb28-5"><a href="#cb28-5"></a> <span class="pp">ReturnType::</span>Type(_<span class="op">,</span> p) <span class="op">=></span> p<span class="op">,</span></span> | 326 | <span id="cb28-5"><a href="#cb28-5" aria-hidden="true"></a> <span class="pp">ReturnType::</span>Type(_<span class="op">,</span> p) <span class="op">=></span> p<span class="op">,</span></span> |
312 | <span id="cb28-6"><a href="#cb28-6"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on functions without return types!"</span>)<span class="op">,</span></span> | 327 | <span id="cb28-6"><a href="#cb28-6" aria-hidden="true"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on functions without return types!"</span>)<span class="op">,</span></span> |
313 | <span id="cb28-7"><a href="#cb28-7"></a> <span class="op">}</span></span> | 328 | <span id="cb28-7"><a href="#cb28-7" aria-hidden="true"></a> <span class="op">}</span></span> |
314 | <span id="cb28-8"><a href="#cb28-8"></a><span class="op">}</span></span></code></pre></div> | 329 | <span id="cb28-8"><a href="#cb28-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
315 | <p>You might notice that we are making extensive use of the <code>panic!</code> macro. Well, that is because it is a good idea to quit on receiving an unsatisfactory <code>TokenStream</code>.</p> | 330 | <p>You might notice that we are making extensive use of the <code>panic!</code> macro. Well, that is because it is a good idea to quit on receiving an unsatisfactory <code>TokenStream</code>.</p> |
316 | <p>With all our types ready, we can get on with generating type aliases:</p> | 331 | <p>With all our types ready, we can get on with generating type aliases:</p> |
317 | <div class="sourceCode" id="cb29"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb29-1"><a href="#cb29-1"></a><span class="co">// src/lib.rs</span></span> | 332 | <div class="sourceCode" id="cb29"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
318 | <span id="cb29-2"><a href="#cb29-2"></a></span> | 333 | <span id="cb29-2"><a href="#cb29-2" aria-hidden="true"></a></span> |
319 | <span id="cb29-3"><a href="#cb29-3"></a><span class="kw">use</span> <span class="pp">quote::</span><span class="op">{</span>quote<span class="op">,</span> format_ident<span class="op">};</span></span> | 334 | <span id="cb29-3"><a href="#cb29-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">quote::</span><span class="op">{</span>quote<span class="op">,</span> format_ident<span class="op">};</span></span> |
320 | <span id="cb29-4"><a href="#cb29-4"></a></span> | 335 | <span id="cb29-4"><a href="#cb29-4" aria-hidden="true"></a></span> |
321 | <span id="cb29-5"><a href="#cb29-5"></a><span class="kw">fn</span> generate_type_aliases(</span> | 336 | <span id="cb29-5"><a href="#cb29-5" aria-hidden="true"></a><span class="kw">fn</span> generate_type_aliases(</span> |
322 | <span id="cb29-6"><a href="#cb29-6"></a> fn_arg_types<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span>]<span class="op">,</span></span> | 337 | <span id="cb29-6"><a href="#cb29-6" aria-hidden="true"></a> fn_arg_types<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span>]<span class="op">,</span></span> |
323 | <span id="cb29-7"><a href="#cb29-7"></a> fn_return_type<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">>,</span></span> | 338 | <span id="cb29-7"><a href="#cb29-7" aria-hidden="true"></a> fn_return_type<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">>,</span></span> |
324 | <span id="cb29-8"><a href="#cb29-8"></a> fn_name<span class="op">:</span> <span class="op">&</span><span class="pp">syn::</span>Ident<span class="op">,</span></span> | 339 | <span id="cb29-8"><a href="#cb29-8" aria-hidden="true"></a> fn_name<span class="op">:</span> <span class="op">&</span><span class="pp">syn::</span>Ident<span class="op">,</span></span> |
325 | <span id="cb29-9"><a href="#cb29-9"></a>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="pp">proc_macro2::</span>TokenStream<span class="op">></span> <span class="op">{</span> <span class="co">// 1</span></span> | 340 | <span id="cb29-9"><a href="#cb29-9" aria-hidden="true"></a>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="pp">proc_macro2::</span>TokenStream<span class="op">></span> <span class="op">{</span> <span class="co">// 1</span></span> |
326 | <span id="cb29-10"><a href="#cb29-10"></a></span> | 341 | <span id="cb29-10"><a href="#cb29-10" aria-hidden="true"></a></span> |
327 | <span id="cb29-11"><a href="#cb29-11"></a> <span class="kw">let</span> type_t0 <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_T0"</span><span class="op">,</span> fn_name)<span class="op">;</span> <span class="co">// 2</span></span> | 342 | <span id="cb29-11"><a href="#cb29-11" aria-hidden="true"></a> <span class="kw">let</span> type_t0 <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_T0"</span><span class="op">,</span> fn_name)<span class="op">;</span> <span class="co">// 2</span></span> |
328 | <span id="cb29-12"><a href="#cb29-12"></a> <span class="kw">let</span> <span class="kw">mut</span> type_aliases <span class="op">=</span> <span class="pp">vec!</span>[<span class="pp">quote!</span> <span class="op">{</span> <span class="kw">type</span> #type_t0 <span class="op">=</span> #fn_return_type <span class="op">}</span>]<span class="op">;</span></span> | 343 | <span id="cb29-12"><a href="#cb29-12" aria-hidden="true"></a> <span class="kw">let</span> <span class="kw">mut</span> type_aliases <span class="op">=</span> <span class="pp">vec!</span>[<span class="pp">quote!</span> <span class="op">{</span> <span class="kw">type</span> #type_t0 <span class="op">=</span> #fn_return_type <span class="op">}</span>]<span class="op">;</span></span> |
329 | <span id="cb29-13"><a href="#cb29-13"></a></span> | 344 | <span id="cb29-13"><a href="#cb29-13" aria-hidden="true"></a></span> |
330 | <span id="cb29-14"><a href="#cb29-14"></a> <span class="co">// 3</span></span> | 345 | <span id="cb29-14"><a href="#cb29-14" aria-hidden="true"></a> <span class="co">// 3</span></span> |
331 | <span id="cb29-15"><a href="#cb29-15"></a> <span class="kw">for</span> (i<span class="op">,</span> t) <span class="kw">in</span> (<span class="dv">1</span><span class="op">..</span>)<span class="op">.</span>zip(fn_arg_types<span class="op">.</span>into_iter()<span class="op">.</span>rev()) <span class="op">{</span></span> | 346 | <span id="cb29-15"><a href="#cb29-15" aria-hidden="true"></a> <span class="kw">for</span> (i<span class="op">,</span> t) <span class="kw">in</span> (<span class="dv">1</span><span class="op">..</span>)<span class="op">.</span>zip(fn_arg_types<span class="op">.</span>into_iter()<span class="op">.</span>rev()) <span class="op">{</span></span> |
332 | <span id="cb29-16"><a href="#cb29-16"></a> <span class="kw">let</span> p <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> | 347 | <span id="cb29-16"><a href="#cb29-16" aria-hidden="true"></a> <span class="kw">let</span> p <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> |
333 | <span id="cb29-17"><a href="#cb29-17"></a> <span class="kw">let</span> n <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i))<span class="op">;</span></span> | 348 | <span id="cb29-17"><a href="#cb29-17" aria-hidden="true"></a> <span class="kw">let</span> n <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i))<span class="op">;</span></span> |
334 | <span id="cb29-18"><a href="#cb29-18"></a></span> | 349 | <span id="cb29-18"><a href="#cb29-18" aria-hidden="true"></a></span> |
335 | <span id="cb29-19"><a href="#cb29-19"></a> type_aliases<span class="op">.</span>push(<span class="pp">quote!</span> <span class="op">{</span></span> | 350 | <span id="cb29-19"><a href="#cb29-19" aria-hidden="true"></a> type_aliases<span class="op">.</span>push(<span class="pp">quote!</span> <span class="op">{</span></span> |
336 | <span id="cb29-20"><a href="#cb29-20"></a> <span class="kw">type</span> #n <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(#t) <span class="op">-></span> #p</span> | 351 | <span id="cb29-20"><a href="#cb29-20" aria-hidden="true"></a> <span class="kw">type</span> #n <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(#t) <span class="op">-></span> #p</span> |
337 | <span id="cb29-21"><a href="#cb29-21"></a> <span class="op">}</span>)<span class="op">;</span></span> | 352 | <span id="cb29-21"><a href="#cb29-21" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> |
338 | <span id="cb29-22"><a href="#cb29-22"></a> <span class="op">}</span></span> | 353 | <span id="cb29-22"><a href="#cb29-22" aria-hidden="true"></a> <span class="op">}</span></span> |
339 | <span id="cb29-23"><a href="#cb29-23"></a></span> | 354 | <span id="cb29-23"><a href="#cb29-23" aria-hidden="true"></a></span> |
340 | <span id="cb29-24"><a href="#cb29-24"></a> <span class="kw">return</span> type_aliases<span class="op">;</span></span> | 355 | <span id="cb29-24"><a href="#cb29-24" aria-hidden="true"></a> <span class="kw">return</span> type_aliases<span class="op">;</span></span> |
341 | <span id="cb29-25"><a href="#cb29-25"></a><span class="op">}</span></span></code></pre></div> | 356 | <span id="cb29-25"><a href="#cb29-25" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
342 | <p><strong>1. The return value</strong><br /> | 357 | <p><strong>1. The return value</strong><br /> |
343 | We are returning a <code>Vec<proc_macro2::TokenStream></code>, i. e., a list of <code>TokenStream</code>s, where each item is a type alias.</p> | 358 | We are returning a <code>Vec<proc_macro2::TokenStream></code>, i. e., a list of <code>TokenStream</code>s, where each item is a type alias.</p> |
344 | <p><strong>2. Format identifier?</strong><br /> | 359 | <p><strong>2. Format identifier?</strong><br /> |
345 | I’ve got some explanation to do on this line. Clearly, we are trying to write the first type alias, and initialize our <code>TokenStream</code> vector with <code>T0</code>, because it is different from the others:</p> | 360 | I’ve got some explanation to do on this line. Clearly, we are trying to write the first type alias, and initialize our <code>TokenStream</code> vector with <code>T0</code>, because it is different from the others:</p> |
346 | <div class="sourceCode" id="cb30"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb30-1"><a href="#cb30-1"></a><span class="kw">type</span> T0 <span class="op">=</span> something</span> | 361 | <div class="sourceCode" id="cb30"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> something</span> |
347 | <span id="cb30-2"><a href="#cb30-2"></a><span class="co">// the others are of the form</span></span> | 362 | <span id="cb30-2"><a href="#cb30-2" aria-hidden="true"></a><span class="co">// the others are of the form</span></span> |
348 | <span id="cb30-3"><a href="#cb30-3"></a><span class="kw">type</span> Tr <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(something) <span class="op">-></span> something</span></code></pre></div> | 363 | <span id="cb30-3"><a href="#cb30-3" aria-hidden="true"></a><span class="kw">type</span> Tr <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(something) <span class="op">-></span> something</span></code></pre></div> |
349 | <p><code>format_ident!</code> is similar to <code>format!</code>. Instead of returning a formatted string, it returns a <code>syn::Ident</code>. Therefore, <code>type_t0</code> is actually an identifier for, in the case of our <code>add</code> function, <code>_add_T0</code>. Why is this formatting important? Namespacing.</p> | 364 | <p><code>format_ident!</code> is similar to <code>format!</code>. Instead of returning a formatted string, it returns a <code>syn::Ident</code>. Therefore, <code>type_t0</code> is actually an identifier for, in the case of our <code>add</code> function, <code>_add_T0</code>. Why is this formatting important? Namespacing.</p> |
350 | <p>Picture this, we have two functions, <code>add</code> and <code>subtract</code>, that we wish to curry with our macro:</p> | 365 | <p>Picture this, we have two functions, <code>add</code> and <code>subtract</code>, that we wish to curry with our macro:</p> |
351 | <div class="sourceCode" id="cb31"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb31-1"><a href="#cb31-1"></a><span class="at">#[</span>curry<span class="at">]</span></span> | 366 | <div class="sourceCode" id="cb31"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> |
352 | <span id="cb31-2"><a href="#cb31-2"></a><span class="kw">fn</span> add(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> | 367 | <span id="cb31-2"><a href="#cb31-2" aria-hidden="true"></a><span class="kw">fn</span> add(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> |
353 | <span id="cb31-3"><a href="#cb31-3"></a></span> | 368 | <span id="cb31-3"><a href="#cb31-3" aria-hidden="true"></a></span> |
354 | <span id="cb31-4"><a href="#cb31-4"></a><span class="at">#[</span>curry<span class="at">]</span></span> | 369 | <span id="cb31-4"><a href="#cb31-4" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> |
355 | <span id="cb31-5"><a href="#cb31-5"></a><span class="kw">fn</span> sub(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> | 370 | <span id="cb31-5"><a href="#cb31-5" aria-hidden="true"></a><span class="kw">fn</span> sub(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> |
356 | <p>Here is the same but with macros expanded:</p> | 371 | <p>Here is the same but with macros expanded:</p> |
357 | <div class="sourceCode" id="cb32"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb32-1"><a href="#cb32-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 372 | <div class="sourceCode" id="cb32"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
358 | <span id="cb32-2"><a href="#cb32-2"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> | 373 | <span id="cb32-2"><a href="#cb32-2" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> |
359 | <span id="cb32-3"><a href="#cb32-3"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> | 374 | <span id="cb32-3"><a href="#cb32-3" aria-hidden="true"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> |
360 | <span id="cb32-4"><a href="#cb32-4"></a></span> | 375 | <span id="cb32-4"><a href="#cb32-4" aria-hidden="true"></a></span> |
361 | <span id="cb32-5"><a href="#cb32-5"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 376 | <span id="cb32-5"><a href="#cb32-5" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
362 | <span id="cb32-6"><a href="#cb32-6"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> | 377 | <span id="cb32-6"><a href="#cb32-6" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> |
363 | <span id="cb32-7"><a href="#cb32-7"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> | 378 | <span id="cb32-7"><a href="#cb32-7" aria-hidden="true"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> |
364 | <p>We end up with two definitions of <code>T0</code>! Now, if we do the little <code>format_ident!</code> dance we did up there:</p> | 379 | <p>We end up with two definitions of <code>T0</code>! Now, if we do the little <code>format_ident!</code> dance we did up there:</p> |
365 | <div class="sourceCode" id="cb33"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb33-1"><a href="#cb33-1"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 380 | <div class="sourceCode" id="cb33"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
366 | <span id="cb33-2"><a href="#cb33-2"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> | 381 | <span id="cb33-2"><a href="#cb33-2" aria-hidden="true"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> |
367 | <span id="cb33-3"><a href="#cb33-3"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> _add_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> | 382 | <span id="cb33-3"><a href="#cb33-3" aria-hidden="true"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> _add_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> |
368 | <span id="cb33-4"><a href="#cb33-4"></a></span> | 383 | <span id="cb33-4"><a href="#cb33-4" aria-hidden="true"></a></span> |
369 | <span id="cb33-5"><a href="#cb33-5"></a><span class="kw">type</span> _sub_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 384 | <span id="cb33-5"><a href="#cb33-5" aria-hidden="true"></a><span class="kw">type</span> _sub_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
370 | <span id="cb33-6"><a href="#cb33-6"></a><span class="kw">type</span> _sub_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _sub_T0<span class="op">;</span></span> | 385 | <span id="cb33-6"><a href="#cb33-6" aria-hidden="true"></a><span class="kw">type</span> _sub_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _sub_T0<span class="op">;</span></span> |
371 | <span id="cb33-7"><a href="#cb33-7"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> _sub_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> | 386 | <span id="cb33-7"><a href="#cb33-7" aria-hidden="true"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> _sub_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> |
372 | <p>Voilà! The type aliases don’t tread on each other. Remember to import <code>format_ident</code> from the <code>quote</code> crate.</p> | 387 | <p>Voilà! The type aliases don’t tread on each other. Remember to import <code>format_ident</code> from the <code>quote</code> crate.</p> |
373 | <p><strong>3. The TokenStream Vector</strong></p> | 388 | <p><strong>3. The TokenStream Vector</strong></p> |
374 | <p>We iterate over our types in reverse order (<code>T0</code> is the last return, <code>T1</code> is the second last, so on), assign a number to each iteration with <code>zip</code>, generate type names with <code>format_ident</code>, push a <code>TokenStream</code> with the help of <code>quote</code> and variable interpolation.</p> | 389 | <p>We iterate over our types in reverse order (<code>T0</code> is the last return, <code>T1</code> is the second last, so on), assign a number to each iteration with <code>zip</code>, generate type names with <code>format_ident</code>, push a <code>TokenStream</code> with the help of <code>quote</code> and variable interpolation.</p> |
375 | <p>If you are wondering why we used <code>(1..).zip()</code> instead of <code>.enumerate()</code>, it’s because we wanted to start counting from 1 instead of 0 (we are already done with <code>T0</code>!).</p> | 390 | <p>If you are wondering why we used <code>(1..).zip()</code> instead of <code>.enumerate()</code>, it’s because we wanted to start counting from 1 instead of 0 (we are already done with <code>T0</code>!).</p> |
376 | <h4 id="getting-it-together">Getting it together</h4> | 391 | <h4 id="getting-it-together">Getting it together</h4> |
377 | <p>I promised we’d have a fully working macro by the end of last section. I lied, we have to tie everything together in our <code>generate_curry</code> function:</p> | 392 | <p>I promised we’d have a fully working macro by the end of last section. I lied, we have to tie everything together in our <code>generate_curry</code> function:</p> |
378 | <div class="sourceCode" id="cb34"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb34-1"><a href="#cb34-1"></a><span class="co">// src/lib.rs</span></span> | 393 | <div class="sourceCode" id="cb34"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
379 | <span id="cb34-2"><a href="#cb34-2"></a></span> | 394 | <span id="cb34-2"><a href="#cb34-2" aria-hidden="true"></a></span> |
380 | <span id="cb34-3"><a href="#cb34-3"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> | 395 | <span id="cb34-3"><a href="#cb34-3" aria-hidden="true"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> |
381 | <span id="cb34-4"><a href="#cb34-4"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> | 396 | <span id="cb34-4"><a href="#cb34-4" aria-hidden="true"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> |
382 | <span id="cb34-5"><a href="#cb34-5"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> | 397 | <span id="cb34-5"><a href="#cb34-5" aria-hidden="true"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> |
383 | <span id="cb34-6"><a href="#cb34-6"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> | 398 | <span id="cb34-6"><a href="#cb34-6" aria-hidden="true"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> |
384 | <span id="cb34-7"><a href="#cb34-7"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> | 399 | <span id="cb34-7"><a href="#cb34-7" aria-hidden="true"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> |
385 | <span id="cb34-8"><a href="#cb34-8"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> | 400 | <span id="cb34-8"><a href="#cb34-8" aria-hidden="true"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> |
386 | <span id="cb34-9"><a href="#cb34-9"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> | 401 | <span id="cb34-9"><a href="#cb34-9" aria-hidden="true"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> |
387 | <span id="cb34-10"><a href="#cb34-10"></a></span> | 402 | <span id="cb34-10"><a href="#cb34-10" aria-hidden="true"></a></span> |
388 | <span id="cb34-11"><a href="#cb34-11"></a> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> | 403 | <span id="cb34-11"><a href="#cb34-11" aria-hidden="true"></a> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> |
389 | <span id="cb34-12"><a href="#cb34-12"></a> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> | 404 | <span id="cb34-12"><a href="#cb34-12" aria-hidden="true"></a> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> |
390 | <span id="cb34-13"><a href="#cb34-13"></a> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> | 405 | <span id="cb34-13"><a href="#cb34-13" aria-hidden="true"></a> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> |
391 | <span id="cb34-14"><a href="#cb34-14"></a></span> | 406 | <span id="cb34-14"><a href="#cb34-14" aria-hidden="true"></a></span> |
392 | <span id="cb34-15"><a href="#cb34-15"></a><span class="op">+</span> <span class="kw">let</span> arg_types <span class="op">=</span> extract_arg_types(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> | 407 | <span id="cb34-15"><a href="#cb34-15" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> arg_types <span class="op">=</span> extract_arg_types(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> |
393 | <span id="cb34-16"><a href="#cb34-16"></a><span class="op">+</span> <span class="kw">let</span> first_type <span class="op">=</span> <span class="op">&</span>arg_types<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> | 408 | <span id="cb34-16"><a href="#cb34-16" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> first_type <span class="op">=</span> <span class="op">&</span>arg_types<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> |
394 | <span id="cb34-17"><a href="#cb34-17"></a><span class="op">+</span> <span class="kw">let</span> type_aliases <span class="op">=</span> generate_type_aliases(</span> | 409 | <span id="cb34-17"><a href="#cb34-17" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> type_aliases <span class="op">=</span> generate_type_aliases(</span> |
395 | <span id="cb34-18"><a href="#cb34-18"></a><span class="op">+</span> <span class="op">&</span>arg_types[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span></span> | 410 | <span id="cb34-18"><a href="#cb34-18" aria-hidden="true"></a><span class="op">+</span> <span class="op">&</span>arg_types[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span></span> |
396 | <span id="cb34-19"><a href="#cb34-19"></a><span class="op">+</span> extract_return_type(fn_return_type)<span class="op">,</span></span> | 411 | <span id="cb34-19"><a href="#cb34-19" aria-hidden="true"></a><span class="op">+</span> extract_return_type(fn_return_type)<span class="op">,</span></span> |
397 | <span id="cb34-20"><a href="#cb34-20"></a><span class="op">+</span> <span class="op">&</span>fn_name<span class="op">,</span></span> | 412 | <span id="cb34-20"><a href="#cb34-20" aria-hidden="true"></a><span class="op">+</span> <span class="op">&</span>fn_name<span class="op">,</span></span> |
398 | <span id="cb34-21"><a href="#cb34-21"></a><span class="op">+</span> )<span class="op">;</span></span> | 413 | <span id="cb34-21"><a href="#cb34-21" aria-hidden="true"></a><span class="op">+</span> )<span class="op">;</span></span> |
399 | <span id="cb34-22"><a href="#cb34-22"></a></span> | 414 | <span id="cb34-22"><a href="#cb34-22" aria-hidden="true"></a></span> |
400 | <span id="cb34-23"><a href="#cb34-23"></a><span class="op">+</span> <span class="kw">let</span> return_type <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> <span class="op">&</span>fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> type_aliases<span class="op">.</span>len() <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> | 415 | <span id="cb34-23"><a href="#cb34-23" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> return_type <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> <span class="op">&</span>fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> type_aliases<span class="op">.</span>len() <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> |
401 | <span id="cb34-24"><a href="#cb34-24"></a></span> | 416 | <span id="cb34-24"><a href="#cb34-24" aria-hidden="true"></a></span> |
402 | <span id="cb34-25"><a href="#cb34-25"></a><span class="op">+</span> <span class="kw">return</span> <span class="pp">quote!</span> <span class="op">{</span></span> | 417 | <span id="cb34-25"><a href="#cb34-25" aria-hidden="true"></a><span class="op">+</span> <span class="kw">return</span> <span class="pp">quote!</span> <span class="op">{</span></span> |
403 | <span id="cb34-26"><a href="#cb34-26"></a><span class="op">+</span> #(#type_aliases)<span class="op">;*</span> <span class="op">;</span></span> | 418 | <span id="cb34-26"><a href="#cb34-26" aria-hidden="true"></a><span class="op">+</span> #(#type_aliases)<span class="op">;*</span> <span class="op">;</span></span> |
404 | <span id="cb34-27"><a href="#cb34-27"></a><span class="op">+</span> #vis <span class="kw">fn</span> #fn_name (#first_ident<span class="op">:</span> #first_type) <span class="op">-></span> #return_type <span class="op">{</span></span> | 419 | <span id="cb34-27"><a href="#cb34-27" aria-hidden="true"></a><span class="op">+</span> #vis <span class="kw">fn</span> #fn_name (#first_ident<span class="op">:</span> #first_type) <span class="op">-></span> #return_type <span class="op">{</span></span> |
405 | <span id="cb34-28"><a href="#cb34-28"></a><span class="op">+</span> #curried_body <span class="op">;</span></span> | 420 | <span id="cb34-28"><a href="#cb34-28" aria-hidden="true"></a><span class="op">+</span> #curried_body <span class="op">;</span></span> |
406 | <span id="cb34-29"><a href="#cb34-29"></a><span class="op">+</span> <span class="op">}</span></span> | 421 | <span id="cb34-29"><a href="#cb34-29" aria-hidden="true"></a><span class="op">+</span> <span class="op">}</span></span> |
407 | <span id="cb34-30"><a href="#cb34-30"></a><span class="op">+</span> <span class="op">};</span></span> | 422 | <span id="cb34-30"><a href="#cb34-30" aria-hidden="true"></a><span class="op">+</span> <span class="op">};</span></span> |
408 | <span id="cb34-31"><a href="#cb34-31"></a> <span class="op">}</span></span></code></pre></div> | 423 | <span id="cb34-31"><a href="#cb34-31" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> |
409 | <p>Most of the additions are self explanatory, I’ll go through the return statement with you. We are returning a <code>quote!{ ... }</code>, so a <code>proc_macro2::TokenStream</code>. We are iterating through the <code>type_aliases</code> variable, which you might recall, is a <code>Vec<TokenStream></code>. You might notice the sneaky semicolon before the <code>*</code>. This basically tells <code>quote</code>, to insert an item, then a semicolon, and then the next one, another semicolon, and so on. The semicolon is a separator. We need to manually insert another semicolon at the end of it all, <code>quote</code> doesn’t insert a separator at the end of the iteration.</p> | 424 | <p>Most of the additions are self explanatory, I’ll go through the return statement with you. We are returning a <code>quote!{ ... }</code>, so a <code>proc_macro2::TokenStream</code>. We are iterating through the <code>type_aliases</code> variable, which you might recall, is a <code>Vec<TokenStream></code>. You might notice the sneaky semicolon before the <code>*</code>. This basically tells <code>quote</code>, to insert an item, then a semicolon, and then the next one, another semicolon, and so on. The semicolon is a separator. We need to manually insert another semicolon at the end of it all, <code>quote</code> doesn’t insert a separator at the end of the iteration.</p> |
410 | <p>We retain the visibility and name of our original function. Our curried function takes as args, just the first argument of our original function. The return type of our curried function is actually, the last type alias we create. If you think back to our manually curried <code>add</code> function, we returned <code>T2</code>, which was in fact, the last type alias we created.</p> | 425 | <p>We retain the visibility and name of our original function. Our curried function takes as args, just the first argument of our original function. The return type of our curried function is actually, the last type alias we create. If you think back to our manually curried <code>add</code> function, we returned <code>T2</code>, which was in fact, the last type alias we created.</p> |
411 | <p>I am sure, at this point, you are itching to test this out, but before that, let me introduce you to some good methods of debugging proc-macro code.</p> | 426 | <p>I am sure, at this point, you are itching to test this out, but before that, let me introduce you to some good methods of debugging proc-macro code.</p> |
@@ -437,57 +452,57 @@ fn main() { | |||
437 | <p>Writing proc-macros without <code>cargo-expand</code> is tantamount to driving a vehicle without rear view mirrors! Keep an eye on what is going on behind your back.</p> | 452 | <p>Writing proc-macros without <code>cargo-expand</code> is tantamount to driving a vehicle without rear view mirrors! Keep an eye on what is going on behind your back.</p> |
438 | <p>Now, your macro won’t always compile, you might just recieve the bee movie script as an error. <code>cargo-expand</code> will not work in such cases. I would suggest printing out your variables to inspect them. <code>TokenStream</code> implements <code>Display</code> as well as <code>Debug</code>. We don’t always have to be respectable programmers. Just print it.</p> | 453 | <p>Now, your macro won’t always compile, you might just recieve the bee movie script as an error. <code>cargo-expand</code> will not work in such cases. I would suggest printing out your variables to inspect them. <code>TokenStream</code> implements <code>Display</code> as well as <code>Debug</code>. We don’t always have to be respectable programmers. Just print it.</p> |
439 | <p>Enough of that, lets get testing:</p> | 454 | <p>Enough of that, lets get testing:</p> |
440 | <div class="sourceCode" id="cb37"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb37-1"><a href="#cb37-1"></a><span class="co">// tests/smoke.rs</span></span> | 455 | <div class="sourceCode" id="cb37"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true"></a><span class="co">// tests/smoke.rs</span></span> |
441 | <span id="cb37-2"><a href="#cb37-2"></a></span> | 456 | <span id="cb37-2"><a href="#cb37-2" aria-hidden="true"></a></span> |
442 | <span id="cb37-3"><a href="#cb37-3"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span></span> | 457 | <span id="cb37-3"><a href="#cb37-3" aria-hidden="true"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span></span> |
443 | <span id="cb37-4"><a href="#cb37-4"></a></span> | 458 | <span id="cb37-4"><a href="#cb37-4" aria-hidden="true"></a></span> |
444 | <span id="cb37-5"><a href="#cb37-5"></a><span class="at">#[</span><span class="pp">crate_name::</span>curry<span class="at">]</span></span> | 459 | <span id="cb37-5"><a href="#cb37-5" aria-hidden="true"></a><span class="at">#[</span><span class="pp">crate_name::</span>curry<span class="at">]</span></span> |
445 | <span id="cb37-6"><a href="#cb37-6"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 460 | <span id="cb37-6"><a href="#cb37-6" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
446 | <span id="cb37-7"><a href="#cb37-7"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> | 461 | <span id="cb37-7"><a href="#cb37-7" aria-hidden="true"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> |
447 | <span id="cb37-8"><a href="#cb37-8"></a><span class="op">}</span></span> | 462 | <span id="cb37-8"><a href="#cb37-8" aria-hidden="true"></a><span class="op">}</span></span> |
448 | <span id="cb37-9"><a href="#cb37-9"></a></span> | 463 | <span id="cb37-9"><a href="#cb37-9" aria-hidden="true"></a></span> |
449 | <span id="cb37-10"><a href="#cb37-10"></a><span class="at">#[</span>test<span class="at">]</span></span> | 464 | <span id="cb37-10"><a href="#cb37-10" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> |
450 | <span id="cb37-11"><a href="#cb37-11"></a><span class="kw">fn</span> works() <span class="op">{</span></span> | 465 | <span id="cb37-11"><a href="#cb37-11" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> |
451 | <span id="cb37-12"><a href="#cb37-12"></a> <span class="pp">assert_eq!</span>(<span class="dv">15</span><span class="op">,</span> add(<span class="dv">4</span>)(<span class="dv">5</span>)(<span class="dv">6</span>))<span class="op">;</span></span> | 466 | <span id="cb37-12"><a href="#cb37-12" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(<span class="dv">15</span><span class="op">,</span> add(<span class="dv">4</span>)(<span class="dv">5</span>)(<span class="dv">6</span>))<span class="op">;</span></span> |
452 | <span id="cb37-13"><a href="#cb37-13"></a><span class="op">}</span></span></code></pre></div> | 467 | <span id="cb37-13"><a href="#cb37-13" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
453 | <p>Run <code>cargo +nightly test</code>. You should see a pleasing message:</p> | 468 | <p>Run <code>cargo +nightly test</code>. You should see a pleasing message:</p> |
454 | <pre><code>running 1 test | 469 | <pre><code>running 1 test |
455 | test tests::works ... ok</code></pre> | 470 | test tests::works ... ok</code></pre> |
456 | <p>Take a look at the expansion for our curry macro, via <code>cargo +nightly expand --tests smoke</code>:</p> | 471 | <p>Take a look at the expansion for our curry macro, via <code>cargo +nightly expand --tests smoke</code>:</p> |
457 | <div class="sourceCode" id="cb39"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb39-1"><a href="#cb39-1"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 472 | <div class="sourceCode" id="cb39"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
458 | <span id="cb39-2"><a href="#cb39-2"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> | 473 | <span id="cb39-2"><a href="#cb39-2" aria-hidden="true"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> |
459 | <span id="cb39-3"><a href="#cb39-3"></a><span class="kw">type</span> _add_T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T1<span class="op">;</span></span> | 474 | <span id="cb39-3"><a href="#cb39-3" aria-hidden="true"></a><span class="kw">type</span> _add_T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T1<span class="op">;</span></span> |
460 | <span id="cb39-4"><a href="#cb39-4"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> _add_T2 <span class="op">{</span></span> | 475 | <span id="cb39-4"><a href="#cb39-4" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> _add_T2 <span class="op">{</span></span> |
461 | <span id="cb39-5"><a href="#cb39-5"></a> <span class="kw">return</span> (<span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="op">{</span></span> | 476 | <span id="cb39-5"><a href="#cb39-5" aria-hidden="true"></a> <span class="kw">return</span> (<span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="op">{</span></span> |
462 | <span id="cb39-6"><a href="#cb39-6"></a> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> <span class="op">{</span></span> | 477 | <span id="cb39-6"><a href="#cb39-6" aria-hidden="true"></a> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> <span class="op">{</span></span> |
463 | <span id="cb39-7"><a href="#cb39-7"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 478 | <span id="cb39-7"><a href="#cb39-7" aria-hidden="true"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
464 | <span id="cb39-8"><a href="#cb39-8"></a> <span class="op">}</span></span> | 479 | <span id="cb39-8"><a href="#cb39-8" aria-hidden="true"></a> <span class="op">}</span></span> |
465 | <span id="cb39-9"><a href="#cb39-9"></a> <span class="op">}</span>)<span class="op">;</span></span> | 480 | <span id="cb39-9"><a href="#cb39-9" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> |
466 | <span id="cb39-10"><a href="#cb39-10"></a><span class="op">}</span></span> | 481 | <span id="cb39-10"><a href="#cb39-10" aria-hidden="true"></a><span class="op">}</span></span> |
467 | <span id="cb39-11"><a href="#cb39-11"></a></span> | 482 | <span id="cb39-11"><a href="#cb39-11" aria-hidden="true"></a></span> |
468 | <span id="cb39-12"><a href="#cb39-12"></a><span class="co">// a bunch of other stuff generated by #[test] and assert_eq!</span></span></code></pre></div> | 483 | <span id="cb39-12"><a href="#cb39-12" aria-hidden="true"></a><span class="co">// a bunch of other stuff generated by #[test] and assert_eq!</span></span></code></pre></div> |
469 | <p>A sight for sore eyes.</p> | 484 | <p>A sight for sore eyes.</p> |
470 | <p>Here is a more complex example that generates ten multiples of the first ten natural numbers:</p> | 485 | <p>Here is a more complex example that generates ten multiples of the first ten natural numbers:</p> |
471 | <div class="sourceCode" id="cb40"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb40-1"><a href="#cb40-1"></a><span class="at">#[</span>curry<span class="at">]</span></span> | 486 | <div class="sourceCode" id="cb40"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> |
472 | <span id="cb40-2"><a href="#cb40-2"></a><span class="kw">fn</span> product(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 487 | <span id="cb40-2"><a href="#cb40-2" aria-hidden="true"></a><span class="kw">fn</span> product(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
473 | <span id="cb40-3"><a href="#cb40-3"></a> x <span class="op">*</span> y</span> | 488 | <span id="cb40-3"><a href="#cb40-3" aria-hidden="true"></a> x <span class="op">*</span> y</span> |
474 | <span id="cb40-4"><a href="#cb40-4"></a><span class="op">}</span></span> | 489 | <span id="cb40-4"><a href="#cb40-4" aria-hidden="true"></a><span class="op">}</span></span> |
475 | <span id="cb40-5"><a href="#cb40-5"></a></span> | 490 | <span id="cb40-5"><a href="#cb40-5" aria-hidden="true"></a></span> |
476 | <span id="cb40-6"><a href="#cb40-6"></a><span class="kw">fn</span> multiples() <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span><span class="dt">u32</span><span class="op">>>{</span></span> | 491 | <span id="cb40-6"><a href="#cb40-6" aria-hidden="true"></a><span class="kw">fn</span> multiples() <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span><span class="dt">u32</span><span class="op">>>{</span></span> |
477 | <span id="cb40-7"><a href="#cb40-7"></a> <span class="kw">let</span> v <span class="op">=</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)<span class="op">.</span>map(product)<span class="op">;</span></span> | 492 | <span id="cb40-7"><a href="#cb40-7" aria-hidden="true"></a> <span class="kw">let</span> v <span class="op">=</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)<span class="op">.</span>map(product)<span class="op">;</span></span> |
478 | <span id="cb40-8"><a href="#cb40-8"></a> <span class="kw">return</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)</span> | 493 | <span id="cb40-8"><a href="#cb40-8" aria-hidden="true"></a> <span class="kw">return</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)</span> |
479 | <span id="cb40-9"><a href="#cb40-9"></a> <span class="op">.</span>map(<span class="op">|</span>x<span class="op">|</span> v<span class="op">.</span>clone()<span class="op">.</span>map(<span class="op">|</span>f<span class="op">|</span> f(x))<span class="op">.</span>collect())</span> | 494 | <span id="cb40-9"><a href="#cb40-9" aria-hidden="true"></a> <span class="op">.</span>map(<span class="op">|</span>x<span class="op">|</span> v<span class="op">.</span>clone()<span class="op">.</span>map(<span class="op">|</span>f<span class="op">|</span> f(x))<span class="op">.</span>collect())</span> |
480 | <span id="cb40-10"><a href="#cb40-10"></a> <span class="op">.</span>collect()<span class="op">;</span></span> | 495 | <span id="cb40-10"><a href="#cb40-10" aria-hidden="true"></a> <span class="op">.</span>collect()<span class="op">;</span></span> |
481 | <span id="cb40-11"><a href="#cb40-11"></a><span class="op">}</span></span></code></pre></div> | 496 | <span id="cb40-11"><a href="#cb40-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
482 | <h3 id="notes">Notes</h3> | 497 | <h3 id="notes">Notes</h3> |
483 | <p>I didn’t quite explain why we use <code>move |arg|</code> in our closure. This is because we want to take ownership of the variable supplied to us. Take a look at this example:</p> | 498 | <p>I didn’t quite explain why we use <code>move |arg|</code> in our closure. This is because we want to take ownership of the variable supplied to us. Take a look at this example:</p> |
484 | <div class="sourceCode" id="cb41"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb41-1"><a href="#cb41-1"></a><span class="kw">let</span> v <span class="op">=</span> add(<span class="dv">5</span>)<span class="op">;</span></span> | 499 | <div class="sourceCode" id="cb41"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true"></a><span class="kw">let</span> v <span class="op">=</span> add(<span class="dv">5</span>)<span class="op">;</span></span> |
485 | <span id="cb41-2"><a href="#cb41-2"></a><span class="kw">let</span> g<span class="op">;</span></span> | 500 | <span id="cb41-2"><a href="#cb41-2" aria-hidden="true"></a><span class="kw">let</span> g<span class="op">;</span></span> |
486 | <span id="cb41-3"><a href="#cb41-3"></a><span class="op">{</span></span> | 501 | <span id="cb41-3"><a href="#cb41-3" aria-hidden="true"></a><span class="op">{</span></span> |
487 | <span id="cb41-4"><a href="#cb41-4"></a> <span class="kw">let</span> x <span class="op">=</span> <span class="dv">5</span><span class="op">;</span></span> | 502 | <span id="cb41-4"><a href="#cb41-4" aria-hidden="true"></a> <span class="kw">let</span> x <span class="op">=</span> <span class="dv">5</span><span class="op">;</span></span> |
488 | <span id="cb41-5"><a href="#cb41-5"></a> g <span class="op">=</span> v(x)<span class="op">;</span></span> | 503 | <span id="cb41-5"><a href="#cb41-5" aria-hidden="true"></a> g <span class="op">=</span> v(x)<span class="op">;</span></span> |
489 | <span id="cb41-6"><a href="#cb41-6"></a><span class="op">}</span></span> | 504 | <span id="cb41-6"><a href="#cb41-6" aria-hidden="true"></a><span class="op">}</span></span> |
490 | <span id="cb41-7"><a href="#cb41-7"></a><span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> g(<span class="dv">2</span>))<span class="op">;</span></span></code></pre></div> | 505 | <span id="cb41-7"><a href="#cb41-7" aria-hidden="true"></a><span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> g(<span class="dv">2</span>))<span class="op">;</span></span></code></pre></div> |
491 | <p>Variable <code>x</code> goes out of scope before <code>g</code> can return a concrete value. If we take ownership of <code>x</code> by <code>move</code>ing it into our closure, we can expect this to work reliably. In fact, rustc understands this, and forces you to use <code>move</code>.</p> | 506 | <p>Variable <code>x</code> goes out of scope before <code>g</code> can return a concrete value. If we take ownership of <code>x</code> by <code>move</code>ing it into our closure, we can expect this to work reliably. In fact, rustc understands this, and forces you to use <code>move</code>.</p> |
492 | <p>This usage of <code>move</code> is exactly why <strong>a curried function without a return is useless</strong>. Every variable we pass to our curried function gets moved into its local scope. Playing with these variables cannot cause a change outside this scope. Returning is our only method of interaction with anything beyond this function.</p> | 507 | <p>This usage of <code>move</code> is exactly why <strong>a curried function without a return is useless</strong>. Every variable we pass to our curried function gets moved into its local scope. Playing with these variables cannot cause a change outside this scope. Returning is our only method of interaction with anything beyond this function.</p> |
493 | <h3 id="conclusion">Conclusion</h3> | 508 | <h3 id="conclusion">Conclusion</h3> |
@@ -564,34 +579,34 @@ test tests::works ... ok</code></pre> | |||
564 | <title>Rapid Refactoring With Vim</title> | 579 | <title>Rapid Refactoring With Vim</title> |
565 | <description><p>Last weekend, I was tasked with refactoring the 96 unit tests on <a href="https://github.com/ruma/ruma-events/pull/70">ruma-events</a> to use strictly typed json objects using <code>serde_json::json!</code> instead of raw strings. It was rather painless thanks to vim :)</p> | 580 | <description><p>Last weekend, I was tasked with refactoring the 96 unit tests on <a href="https://github.com/ruma/ruma-events/pull/70">ruma-events</a> to use strictly typed json objects using <code>serde_json::json!</code> instead of raw strings. It was rather painless thanks to vim :)</p> |
566 | <p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> | 581 | <p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> |
567 | <div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_str<span class="op">};</span></span> | 582 | <div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_str<span class="op">};</span></span> |
568 | <span id="cb1-2"><a href="#cb1-2"></a> </span> | 583 | <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a> </span> |
569 | <span id="cb1-3"><a href="#cb1-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> | 584 | <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> |
570 | <span id="cb1-4"><a href="#cb1-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> | 585 | <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> |
571 | <span id="cb1-5"><a href="#cb1-5"></a> <span class="pp">assert_eq!</span>(</span> | 586 | <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> |
572 | <span id="cb1-6"><a href="#cb1-6"></a>→ <span class="pp">from_str::</span><span class="op"><</span>Action<span class="op">></span>(<span class="st">r#"{"set_tweak": "highlight"}"#</span>)<span class="op">,</span></span> | 587 | <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a>→ <span class="pp">from_str::</span><span class="op"><</span>Action<span class="op">></span>(<span class="st">r#"{"set_tweak": "highlight"}"#),</span></span> |
573 | <span id="cb1-7"><a href="#cb1-7"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> | 588 | <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a><span class="st"> Action::SetTweak(Tweak::Highlight { value: true })</span></span> |
574 | <span id="cb1-8"><a href="#cb1-8"></a> )<span class="op">;</span></span> | 589 | <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="st"> );</span></span> |
575 | <span id="cb1-9"><a href="#cb1-9"></a> <span class="op">}</span></span></code></pre></div> | 590 | <span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a><span class="st"> }</span></span></code></pre></div> |
576 | <p>had to be converted to:</p> | 591 | <p>had to be converted to:</p> |
577 | <div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_value<span class="op">};</span></span> | 592 | <div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_value<span class="op">};</span></span> |
578 | <span id="cb2-2"><a href="#cb2-2"></a> </span> | 593 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> </span> |
579 | <span id="cb2-3"><a href="#cb2-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> | 594 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> |
580 | <span id="cb2-4"><a href="#cb2-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> | 595 | <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> |
581 | <span id="cb2-5"><a href="#cb2-5"></a> <span class="pp">assert_eq!</span>(</span> | 596 | <span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> |
582 | <span id="cb2-6"><a href="#cb2-6"></a>→ <span class="pp">from_value::</span><span class="op"><</span>Action<span class="op">></span>(<span class="pp">json!</span>(<span class="op">{</span><span class="st">"set_tweak"</span><span class="op">:</span> <span class="st">"highlight"</span><span class="op">}</span>))<span class="op">,</span></span> | 597 | <span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a>→ <span class="pp">from_value::</span><span class="op"><</span>Action<span class="op">></span>(<span class="pp">json!</span>(<span class="op">{</span><span class="st">"set_tweak"</span><span class="op">:</span> <span class="st">"highlight"</span><span class="op">}</span>))<span class="op">,</span></span> |
583 | <span id="cb2-7"><a href="#cb2-7"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> | 598 | <span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> |
584 | <span id="cb2-8"><a href="#cb2-8"></a> )<span class="op">;</span></span> | 599 | <span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a> )<span class="op">;</span></span> |
585 | <span id="cb2-9"><a href="#cb2-9"></a> <span class="op">}</span></span></code></pre></div> | 600 | <span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> |
586 | <h3 id="the-arglist">The arglist</h3> | 601 | <h3 id="the-arglist">The arglist</h3> |
587 | <p>For the initial pass, I decided to handle imports, this was a simple find and replace operation, done to all the files containing tests. Luckily, modules (and therefore files) containing tests in Rust are annotated with the <code>#[cfg(test)]</code> attribute. I opened all such files:</p> | 602 | <p>For the initial pass, I decided to handle imports, this was a simple find and replace operation, done to all the files containing tests. Luckily, modules (and therefore files) containing tests in Rust are annotated with the <code>#[cfg(test)]</code> attribute. I opened all such files:</p> |
588 | <div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1"></a><span class="co"># `grep -l pattern files` lists all the files</span></span> | 603 | <div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="co"># `grep -l pattern files` lists all the files</span></span> |
589 | <span id="cb3-2"><a href="#cb3-2"></a><span class="co"># matching the pattern</span></span> | 604 | <span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a><span class="co"># matching the pattern</span></span> |
590 | <span id="cb3-3"><a href="#cb3-3"></a></span> | 605 | <span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a></span> |
591 | <span id="cb3-4"><a href="#cb3-4"></a><span class="ex">vim</span> <span class="va">$(</span><span class="fu">grep</span> -l <span class="st">'cfg\(test\)'</span> ./**/*.rs<span class="va">)</span></span> | 606 | <span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a><span class="ex">vim</span> <span class="va">$(</span><span class="fu">grep</span> -l <span class="st">'cfg\(test\)'</span> ./**/*.rs<span class="va">)</span></span> |
592 | <span id="cb3-5"><a href="#cb3-5"></a></span> | 607 | <span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a></span> |
593 | <span id="cb3-6"><a href="#cb3-6"></a><span class="co"># expands to something like:</span></span> | 608 | <span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a><span class="co"># expands to something like:</span></span> |
594 | <span id="cb3-7"><a href="#cb3-7"></a><span class="ex">vim</span> push_rules.rs room/member.rs key/verification/lib.rs</span></code></pre></div> | 609 | <span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a><span class="ex">vim</span> push_rules.rs room/member.rs key/verification/lib.rs</span></code></pre></div> |
595 | <p>Starting vim with more than one file at the shell prompt populates the arglist. Hit <code>:args</code> to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with <code>:next</code> and <code>:prev</code>. I use tpope’s vim-unimpaired <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>, which adds <code>]a</code> and <code>[a</code>, mapped to <code>:next</code> and <code>:prev</code>.</p> | 610 | <p>Starting vim with more than one file at the shell prompt populates the arglist. Hit <code>:args</code> to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with <code>:next</code> and <code>:prev</code>. I use tpope’s vim-unimpaired <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>, which adds <code>]a</code> and <code>[a</code>, mapped to <code>:next</code> and <code>:prev</code>.</p> |
596 | <p>All that’s left to do is the find and replace, for which we will be using vim’s <code>argdo</code>, applying a substitution to every file in the arglist:</p> | 611 | <p>All that’s left to do is the find and replace, for which we will be using vim’s <code>argdo</code>, applying a substitution to every file in the arglist:</p> |
597 | <pre><code>:argdo s/from_str/from_value/g</code></pre> | 612 | <pre><code>:argdo s/from_str/from_value/g</code></pre> |
@@ -773,26 +788,26 @@ Reading symbols from main... # yay!</code></pre> | |||
773 | <p>I chose to write in markdown, and convert to html with <a href="https://kristaps.bsd.lv/lowdown/">lowdown</a>.</p> | 788 | <p>I chose to write in markdown, and convert to html with <a href="https://kristaps.bsd.lv/lowdown/">lowdown</a>.</p> |
774 | <h3 id="directory-structure">Directory structure</h3> | 789 | <h3 id="directory-structure">Directory structure</h3> |
775 | <p>I host my site on GitHub pages, so <code>docs/</code> has to be the entry point. Markdown formatted posts go into <code>posts/</code>, get converted into html, and end up in <code>docs/index.html</code>, something like this:</p> | 790 | <p>I host my site on GitHub pages, so <code>docs/</code> has to be the entry point. Markdown formatted posts go into <code>posts/</code>, get converted into html, and end up in <code>docs/index.html</code>, something like this:</p> |
776 | <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1"></a><span class="va">posts=$(</span><span class="fu">ls</span> -t ./posts<span class="va">)</span> <span class="co"># chronological order!</span></span> | 791 | <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="va">posts=$(</span><span class="fu">ls</span> -t ./posts<span class="va">)</span> <span class="co"># chronological order!</span></span> |
777 | <span id="cb1-2"><a href="#cb1-2"></a><span class="kw">for</span> <span class="ex">f</span> in <span class="va">$posts</span><span class="kw">;</span> <span class="kw">do</span></span> | 792 | <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a><span class="kw">for</span> <span class="ex">f</span> in <span class="va">$posts</span><span class="kw">;</span> <span class="kw">do</span></span> |
778 | <span id="cb1-3"><a href="#cb1-3"></a> <span class="va">file=</span><span class="st">"./posts/"</span><span class="va">$f</span> <span class="co"># `ls` mangled our file paths</span></span> | 793 | <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="va">file=</span><span class="st">"./posts/"</span><span class="va">$f</span> <span class="co"># `ls` mangled our file paths</span></span> |
779 | <span id="cb1-4"><a href="#cb1-4"></a> <span class="bu">echo</span> <span class="st">"generating post </span><span class="va">$file</span><span class="st">"</span></span> | 794 | <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="bu">echo</span> <span class="st">"generating post </span><span class="va">$file</span><span class="st">"</span></span> |
780 | <span id="cb1-5"><a href="#cb1-5"></a></span> | 795 | <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a></span> |
781 | <span id="cb1-6"><a href="#cb1-6"></a> <span class="va">html=$(</span><span class="ex">lowdown</span> <span class="st">"</span><span class="va">$file</span><span class="st">"</span><span class="va">)</span></span> | 796 | <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a> <span class="va">html=$(</span><span class="ex">lowdown</span> <span class="st">"</span><span class="va">$file</span><span class="st">"</span><span class="va">)</span></span> |
782 | <span id="cb1-7"><a href="#cb1-7"></a> <span class="bu">echo</span> -e <span class="st">"html"</span> <span class="op">>></span> docs/index.html</span> | 797 | <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a> <span class="bu">echo</span> -e <span class="st">"html"</span> <span class="op">>></span> docs/index.html</span> |
783 | <span id="cb1-8"><a href="#cb1-8"></a><span class="kw">done</span></span></code></pre></div> | 798 | <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="kw">done</span></span></code></pre></div> |
784 | <h3 id="assets">Assets</h3> | 799 | <h3 id="assets">Assets</h3> |
785 | <p>Most static site generators recommend dropping image assets into the site source itself. That does have it’s merits, but I prefer hosting images separately:</p> | 800 | <p>Most static site generators recommend dropping image assets into the site source itself. That does have it’s merits, but I prefer hosting images separately:</p> |
786 | <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1"></a><span class="co"># strip file extension</span></span> | 801 | <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a><span class="co"># strip file extension</span></span> |
787 | <span id="cb2-2"><a href="#cb2-2"></a><span class="va">ext=</span><span class="st">"</span><span class="va">${1##</span>*.<span class="va">}</span><span class="st">"</span></span> | 802 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a><span class="va">ext=</span><span class="st">"</span><span class="va">${1##</span>*.<span class="va">}</span><span class="st">"</span></span> |
788 | <span id="cb2-3"><a href="#cb2-3"></a></span> | 803 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a></span> |
789 | <span id="cb2-4"><a href="#cb2-4"></a><span class="co"># generate a random file name</span></span> | 804 | <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a><span class="co"># generate a random file name</span></span> |
790 | <span id="cb2-5"><a href="#cb2-5"></a><span class="va">id=$(</span> <span class="fu">cat</span> /dev/urandom <span class="kw">|</span> <span class="fu">tr</span> -dc <span class="st">'a-zA-Z0-9'</span> <span class="kw">|</span> <span class="ex">fold</span> -w 2 <span class="kw">|</span> <span class="fu">head</span> -n 1 <span class="va">)</span></span> | 805 | <span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a><span class="va">id=$(</span> <span class="fu">cat</span> /dev/urandom <span class="kw">|</span> <span class="fu">tr</span> -dc <span class="st">'a-zA-Z0-9'</span> <span class="kw">|</span> <span class="ex">fold</span> -w 2 <span class="kw">|</span> <span class="fu">head</span> -n 1 <span class="va">)</span></span> |
791 | <span id="cb2-6"><a href="#cb2-6"></a><span class="va">id=</span><span class="st">"</span><span class="va">$id</span><span class="st">.</span><span class="va">$ext</span><span class="st">"</span></span> | 806 | <span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a><span class="va">id=</span><span class="st">"</span><span class="va">$id</span><span class="st">.</span><span class="va">$ext</span><span class="st">"</span></span> |
792 | <span id="cb2-7"><a href="#cb2-7"></a></span> | 807 | <span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a></span> |
793 | <span id="cb2-8"><a href="#cb2-8"></a><span class="co"># copy to my file host</span></span> | 808 | <span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a><span class="co"># copy to my file host</span></span> |
794 | <span id="cb2-9"><a href="#cb2-9"></a><span class="fu">scp</span> -P 443 <span class="st">"</span><span class="va">$1</span><span class="st">"</span> emerald:files/<span class="st">"</span><span class="va">$id</span><span class="st">"</span> </span> | 809 | <span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a><span class="fu">scp</span> -P 443 <span class="st">"</span><span class="va">$1</span><span class="st">"</span> emerald:files/<span class="st">"</span><span class="va">$id</span><span class="st">"</span> </span> |
795 | <span id="cb2-10"><a href="#cb2-10"></a><span class="bu">echo</span> <span class="st">"https://u.peppe.rs/</span><span class="va">$id</span><span class="st">"</span></span></code></pre></div> | 810 | <span id="cb2-10"><a href="#cb2-10" aria-hidden="true"></a><span class="bu">echo</span> <span class="st">"https://u.peppe.rs/</span><span class="va">$id</span><span class="st">"</span></span></code></pre></div> |
796 | <h3 id="templating">Templating</h3> | 811 | <h3 id="templating">Templating</h3> |
797 | <p><a href="https://github.com/NerdyPepper/site/blob/master/generate.sh"><code>generate.sh</code></a> brings the above bits and pieces together (with some extra cruft to avoid javascript). It uses <code>sed</code> to produce nice titles from the file names (removes underscores, title-case), and <code>date(1)</code> to add the date to each post listing!</p></description> | 812 | <p><a href="https://github.com/NerdyPepper/site/blob/master/generate.sh"><code>generate.sh</code></a> brings the above bits and pieces together (with some extra cruft to avoid javascript). It uses <code>sed</code> to produce nice titles from the file names (removes underscores, title-case), and <code>date(1)</code> to add the date to each post listing!</p></description> |
798 | <link>https://peppe.rs/posts/static_sites_with_bash/</link> | 813 | <link>https://peppe.rs/posts/static_sites_with_bash/</link> |
diff --git a/docs/posts/auto-currying_rust_functions/index.html b/docs/posts/auto-currying_rust_functions/index.html index db70890..a86667c 100644 --- a/docs/posts/auto-currying_rust_functions/index.html +++ b/docs/posts/auto-currying_rust_functions/index.html | |||
@@ -94,17 +94,17 @@ h(x)(y)(z) = g(y)(z) = k(z) = v</code></pre> | |||
94 | <p>We will be using Attribute macros to convert a Rust function into a curried Rust function, which we should be able to call via: <code>function(arg1)(arg2)</code>.</p> | 94 | <p>We will be using Attribute macros to convert a Rust function into a curried Rust function, which we should be able to call via: <code>function(arg1)(arg2)</code>.</p> |
95 | <h3 id="definitions">Definitions</h3> | 95 | <h3 id="definitions">Definitions</h3> |
96 | <p>Being respectable programmers, we define the input to and the output from our proc-macro. Here’s a good non-trivial function to start out with:</p> | 96 | <p>Being respectable programmers, we define the input to and the output from our proc-macro. Here’s a good non-trivial function to start out with:</p> |
97 | <div class="sourceCode" id="cb4"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb4-1"><a href="#cb4-1"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 97 | <div class="sourceCode" id="cb4"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
98 | <span id="cb4-2"><a href="#cb4-2"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 98 | <span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
99 | <span id="cb4-3"><a href="#cb4-3"></a><span class="op">}</span></span></code></pre></div> | 99 | <span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
100 | <p>Hmm, what would our output look like? What should our proc-macro generate ideally? Well, if we understood currying correctly, we should accept an argument and return a function that accepts an argument and returns … you get the point. Something like this should do:</p> | 100 | <p>Hmm, what would our output look like? What should our proc-macro generate ideally? Well, if we understood currying correctly, we should accept an argument and return a function that accepts an argument and returns … you get the point. Something like this should do:</p> |
101 | <div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1"></a><span class="kw">fn</span> add_curried1(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> | 101 | <div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true"></a><span class="kw">fn</span> add_curried1(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> |
102 | <span id="cb5-2"><a href="#cb5-2"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried2 (y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> | 102 | <span id="cb5-2"><a href="#cb5-2" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried2 (y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="op">?</span> <span class="op">{</span></span> |
103 | <span id="cb5-3"><a href="#cb5-3"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried3 (z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 103 | <span id="cb5-3"><a href="#cb5-3" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">fn</span> add_curried3 (z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
104 | <span id="cb5-4"><a href="#cb5-4"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 104 | <span id="cb5-4"><a href="#cb5-4" aria-hidden="true"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
105 | <span id="cb5-5"><a href="#cb5-5"></a> <span class="op">}</span></span> | 105 | <span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a> <span class="op">}</span></span> |
106 | <span id="cb5-6"><a href="#cb5-6"></a> <span class="op">}</span></span> | 106 | <span id="cb5-6"><a href="#cb5-6" aria-hidden="true"></a> <span class="op">}</span></span> |
107 | <span id="cb5-7"><a href="#cb5-7"></a><span class="op">}</span></span></code></pre></div> | 107 | <span id="cb5-7"><a href="#cb5-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
108 | <p>A couple of things to note:</p> | 108 | <p>A couple of things to note:</p> |
109 | <p><strong>Return types</strong><br /> | 109 | <p><strong>Return types</strong><br /> |
110 | We have placed <code>?</code>s in place of return types. Let’s try to fix that. <code>add_curried3</code> returns the ‘value’, so <code>u32</code> is accurate. <code>add_curried2</code> returns <code>add_curried3</code>. What is the type of <code>add_curried3</code>? It is a function that takes in a <code>u32</code> and returns a <code>u32</code>. So a <code>fn(u32) -> u32</code> will do right? No, I’ll explain why in the next point, but for now, we will make use of the <code>Fn</code> trait, our return type is <code>impl Fn(u32) -> u32</code>. This basically tells the compiler that we will be returning something function-like, a.k.a, behaves like a <code>Fn</code>. Cool!</p> | 110 | We have placed <code>?</code>s in place of return types. Let’s try to fix that. <code>add_curried3</code> returns the ‘value’, so <code>u32</code> is accurate. <code>add_curried2</code> returns <code>add_curried3</code>. What is the type of <code>add_curried3</code>? It is a function that takes in a <code>u32</code> and returns a <code>u32</code>. So a <code>fn(u32) -> u32</code> will do right? No, I’ll explain why in the next point, but for now, we will make use of the <code>Fn</code> trait, our return type is <code>impl Fn(u32) -> u32</code>. This basically tells the compiler that we will be returning something function-like, a.k.a, behaves like a <code>Fn</code>. Cool!</p> |
@@ -117,9 +117,9 @@ We have placed <code>?</code>s in place of return types. Let’s try to fix that | |||
117 | A function cannot access it’s environment. Our solution will not work. <code>add_curried3</code> attempts to access <code>x</code>, which is not allowed! A closure<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> however, can. If we are returning a closure, our return type must be <code>impl Fn</code>, and not <code>fn</code>. The difference between the <code>Fn</code> trait and function pointers is beyond the scope of this post.</p> | 117 | A function cannot access it’s environment. Our solution will not work. <code>add_curried3</code> attempts to access <code>x</code>, which is not allowed! A closure<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> however, can. If we are returning a closure, our return type must be <code>impl Fn</code>, and not <code>fn</code>. The difference between the <code>Fn</code> trait and function pointers is beyond the scope of this post.</p> |
118 | <h3 id="refinement">Refinement</h3> | 118 | <h3 id="refinement">Refinement</h3> |
119 | <p>Armed with knowledge, we refine our expected output, this time, employing closures:</p> | 119 | <p>Armed with knowledge, we refine our expected output, this time, employing closures:</p> |
120 | <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 120 | <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
121 | <span id="cb8-2"><a href="#cb8-2"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 121 | <span id="cb8-2"><a href="#cb8-2" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
122 | <span id="cb8-3"><a href="#cb8-3"></a><span class="op">}</span></span></code></pre></div> | 122 | <span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
123 | <p>Alas, that does not compile either! It errors out with the following message:</p> | 123 | <p>Alas, that does not compile either! It errors out with the following message:</p> |
124 | <pre><code>error[E0562]: `impl Trait` not allowed outside of function | 124 | <pre><code>error[E0562]: `impl Trait` not allowed outside of function |
125 | and inherent method return types | 125 | and inherent method return types |
@@ -130,15 +130,15 @@ and inherent method return types | |||
130 | </code></pre> | 130 | </code></pre> |
131 | <p>You are allowed to return an <code>impl Fn</code> only inside a function. We are currently returning it from another return! Or at least, that was the most I could make out of the error message.</p> | 131 | <p>You are allowed to return an <code>impl Fn</code> only inside a function. We are currently returning it from another return! Or at least, that was the most I could make out of the error message.</p> |
132 | <p>We are going to have to cheat a bit to fix this issue; with type aliases and a convenient nightly feature <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a>:</p> | 132 | <p>We are going to have to cheat a bit to fix this issue; with type aliases and a convenient nightly feature <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a>:</p> |
133 | <div class="sourceCode" id="cb10"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb10-1"><a href="#cb10-1"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span> <span class="co">// allows us to use `impl Fn` in type aliases!</span></span> | 133 | <div class="sourceCode" id="cb10"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span> <span class="co">// allows us to use `impl Fn` in type aliases!</span></span> |
134 | <span id="cb10-2"><a href="#cb10-2"></a></span> | 134 | <span id="cb10-2"><a href="#cb10-2" aria-hidden="true"></a></span> |
135 | <span id="cb10-3"><a href="#cb10-3"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span> <span class="co">// the return value when zero args are to be applied</span></span> | 135 | <span id="cb10-3"><a href="#cb10-3" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span> <span class="co">// the return value when zero args are to be applied</span></span> |
136 | <span id="cb10-4"><a href="#cb10-4"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span> <span class="co">// the return value when one arg is to be applied</span></span> | 136 | <span id="cb10-4"><a href="#cb10-4" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span> <span class="co">// the return value when one arg is to be applied</span></span> |
137 | <span id="cb10-5"><a href="#cb10-5"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span> <span class="co">// the return value when two args are to be applied</span></span> | 137 | <span id="cb10-5"><a href="#cb10-5" aria-hidden="true"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span> <span class="co">// the return value when two args are to be applied</span></span> |
138 | <span id="cb10-6"><a href="#cb10-6"></a></span> | 138 | <span id="cb10-6"><a href="#cb10-6" aria-hidden="true"></a></span> |
139 | <span id="cb10-7"><a href="#cb10-7"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> T2 <span class="op">{</span></span> | 139 | <span id="cb10-7"><a href="#cb10-7" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> T2 <span class="op">{</span></span> |
140 | <span id="cb10-8"><a href="#cb10-8"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 140 | <span id="cb10-8"><a href="#cb10-8" aria-hidden="true"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
141 | <span id="cb10-9"><a href="#cb10-9"></a><span class="op">}</span></span></code></pre></div> | 141 | <span id="cb10-9"><a href="#cb10-9" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
142 | <p>Drop that into a cargo project, call <code>add(4)(5)(6)</code>, cross your fingers, and run <code>cargo +nightly run</code>. You should see a 15 unless you forgot to print it!</p> | 142 | <p>Drop that into a cargo project, call <code>add(4)(5)(6)</code>, cross your fingers, and run <code>cargo +nightly run</code>. You should see a 15 unless you forgot to print it!</p> |
143 | <h3 id="the-in-betweens">The In-Betweens</h3> | 143 | <h3 id="the-in-betweens">The In-Betweens</h3> |
144 | <p>Let us write the magical bits that take us from function to curried function.</p> | 144 | <p>Let us write the magical bits that take us from function to curried function.</p> |
@@ -172,19 +172,19 @@ proc-macro = true # this is important!</code></pre> | |||
172 | <p>We will be using an external <code>proc-macro2</code> crate as well as an internal <code>proc-macro</code> crate. Not confusing at all!</p> | 172 | <p>We will be using an external <code>proc-macro2</code> crate as well as an internal <code>proc-macro</code> crate. Not confusing at all!</p> |
173 | <h4 id="the-attribute-macro">The attribute macro</h4> | 173 | <h4 id="the-attribute-macro">The attribute macro</h4> |
174 | <p>Drop this into <code>src/lib.rs</code>, to get the ball rolling.</p> | 174 | <p>Drop this into <code>src/lib.rs</code>, to get the ball rolling.</p> |
175 | <div class="sourceCode" id="cb13"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb13-1"><a href="#cb13-1"></a><span class="co">// src/lib.rs</span></span> | 175 | <div class="sourceCode" id="cb13"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
176 | <span id="cb13-2"><a href="#cb13-2"></a></span> | 176 | <span id="cb13-2"><a href="#cb13-2" aria-hidden="true"></a></span> |
177 | <span id="cb13-3"><a href="#cb13-3"></a><span class="kw">use</span> <span class="pp">proc_macro::</span>TokenStream<span class="op">;</span> <span class="co">// 1</span></span> | 177 | <span id="cb13-3"><a href="#cb13-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">proc_macro::</span>TokenStream<span class="op">;</span> <span class="co">// 1</span></span> |
178 | <span id="cb13-4"><a href="#cb13-4"></a><span class="kw">use</span> <span class="pp">quote::</span>quote<span class="op">;</span></span> | 178 | <span id="cb13-4"><a href="#cb13-4" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">quote::</span>quote<span class="op">;</span></span> |
179 | <span id="cb13-5"><a href="#cb13-5"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> ItemFn<span class="op">};</span></span> | 179 | <span id="cb13-5"><a href="#cb13-5" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> ItemFn<span class="op">};</span></span> |
180 | <span id="cb13-6"><a href="#cb13-6"></a></span> | 180 | <span id="cb13-6"><a href="#cb13-6" aria-hidden="true"></a></span> |
181 | <span id="cb13-7"><a href="#cb13-7"></a><span class="at">#[</span>proc_macro_attribute<span class="at">]</span> <span class="co">// 2</span></span> | 181 | <span id="cb13-7"><a href="#cb13-7" aria-hidden="true"></a><span class="at">#[</span>proc_macro_attribute<span class="at">]</span> <span class="co">// 2</span></span> |
182 | <span id="cb13-8"><a href="#cb13-8"></a><span class="kw">pub</span> <span class="kw">fn</span> curry(_attr<span class="op">:</span> TokenStream<span class="op">,</span> item<span class="op">:</span> TokenStream) <span class="op">-></span> TokenStream <span class="op">{</span></span> | 182 | <span id="cb13-8"><a href="#cb13-8" aria-hidden="true"></a><span class="kw">pub</span> <span class="kw">fn</span> curry(_attr<span class="op">:</span> TokenStream<span class="op">,</span> item<span class="op">:</span> TokenStream) <span class="op">-></span> TokenStream <span class="op">{</span></span> |
183 | <span id="cb13-9"><a href="#cb13-9"></a> <span class="kw">let</span> parsed <span class="op">=</span> <span class="pp">parse_macro_input!</span>(item <span class="kw">as</span> ItemFn)<span class="op">;</span> <span class="co">// 3</span></span> | 183 | <span id="cb13-9"><a href="#cb13-9" aria-hidden="true"></a> <span class="kw">let</span> parsed <span class="op">=</span> <span class="pp">parse_macro_input!</span>(item <span class="kw">as</span> ItemFn)<span class="op">;</span> <span class="co">// 3</span></span> |
184 | <span id="cb13-10"><a href="#cb13-10"></a> generate_curry(parsed)<span class="op">.</span>into() <span class="co">// 4</span></span> | 184 | <span id="cb13-10"><a href="#cb13-10" aria-hidden="true"></a> generate_curry(parsed)<span class="op">.</span>into() <span class="co">// 4</span></span> |
185 | <span id="cb13-11"><a href="#cb13-11"></a><span class="op">}</span></span> | 185 | <span id="cb13-11"><a href="#cb13-11" aria-hidden="true"></a><span class="op">}</span></span> |
186 | <span id="cb13-12"><a href="#cb13-12"></a></span> | 186 | <span id="cb13-12"><a href="#cb13-12" aria-hidden="true"></a></span> |
187 | <span id="cb13-13"><a href="#cb13-13"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{}</span></span></code></pre></div> | 187 | <span id="cb13-13"><a href="#cb13-13" aria-hidden="true"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{}</span></span></code></pre></div> |
188 | <p><strong>1. Imports</strong></p> | 188 | <p><strong>1. Imports</strong></p> |
189 | <p>A <code>Tokenstream</code> holds (hopefully valid) Rust code, this is the type of our input and output. Note that we are importing this type from <code>proc_macro</code> and not <code>proc_macro2</code>.</p> | 189 | <p>A <code>Tokenstream</code> holds (hopefully valid) Rust code, this is the type of our input and output. Note that we are importing this type from <code>proc_macro</code> and not <code>proc_macro2</code>.</p> |
190 | <p><code>quote!</code> from the <code>quote</code> crate is a macro that allows us to quickly produce <code>TokenStream</code>s. Much like the LISP <code>quote</code> procedure, you can use the <code>quote!</code> macro for symbolic transformations.</p> | 190 | <p><code>quote!</code> from the <code>quote</code> crate is a macro that allows us to quickly produce <code>TokenStream</code>s. Much like the LISP <code>quote</code> procedure, you can use the <code>quote!</code> macro for symbolic transformations.</p> |
@@ -196,16 +196,16 @@ proc-macro = true # this is important!</code></pre> | |||
196 | <p><strong>4. Returning <code>TokenStream</code>s </strong></p> | 196 | <p><strong>4. Returning <code>TokenStream</code>s </strong></p> |
197 | <p>We haven’t filled in <code>generate_curry</code> yet, but we can see that it returns a <code>proc_macro2::TokenStream</code> and not a <code>proc_macro::TokenStream</code>, so drop a <code>.into()</code> to convert it.</p> | 197 | <p>We haven’t filled in <code>generate_curry</code> yet, but we can see that it returns a <code>proc_macro2::TokenStream</code> and not a <code>proc_macro::TokenStream</code>, so drop a <code>.into()</code> to convert it.</p> |
198 | <p>Lets move on, and fill in <code>generate_curry</code>, I would suggest keeping the documentation for <a href="https://docs.rs/syn/1.0.19/syn/struct.ItemFn.html"><code>syn::ItemFn</code></a> and <a href="https://docs.rs/syn/1.0.19/syn/struct.Signature.html"><code>syn::Signature</code></a> open.</p> | 198 | <p>Lets move on, and fill in <code>generate_curry</code>, I would suggest keeping the documentation for <a href="https://docs.rs/syn/1.0.19/syn/struct.ItemFn.html"><code>syn::ItemFn</code></a> and <a href="https://docs.rs/syn/1.0.19/syn/struct.Signature.html"><code>syn::Signature</code></a> open.</p> |
199 | <div class="sourceCode" id="cb14"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb14-1"><a href="#cb14-1"></a><span class="co">// src/lib.rs</span></span> | 199 | <div class="sourceCode" id="cb14"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
200 | <span id="cb14-2"><a href="#cb14-2"></a></span> | 200 | <span id="cb14-2"><a href="#cb14-2" aria-hidden="true"></a></span> |
201 | <span id="cb14-3"><a href="#cb14-3"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> | 201 | <span id="cb14-3"><a href="#cb14-3" aria-hidden="true"></a><span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> |
202 | <span id="cb14-4"><a href="#cb14-4"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span> <span class="co">// function body</span></span> | 202 | <span id="cb14-4"><a href="#cb14-4" aria-hidden="true"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span> <span class="co">// function body</span></span> |
203 | <span id="cb14-5"><a href="#cb14-5"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span> <span class="co">// function signature</span></span> | 203 | <span id="cb14-5"><a href="#cb14-5" aria-hidden="true"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span> <span class="co">// function signature</span></span> |
204 | <span id="cb14-6"><a href="#cb14-6"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span> <span class="co">// visibility, pub or not</span></span> | 204 | <span id="cb14-6"><a href="#cb14-6" aria-hidden="true"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span> <span class="co">// visibility, pub or not</span></span> |
205 | <span id="cb14-7"><a href="#cb14-7"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span> <span class="co">// function name/identifier</span></span> | 205 | <span id="cb14-7"><a href="#cb14-7" aria-hidden="true"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span> <span class="co">// function name/identifier</span></span> |
206 | <span id="cb14-8"><a href="#cb14-8"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span> <span class="co">// comma separated args</span></span> | 206 | <span id="cb14-8"><a href="#cb14-8" aria-hidden="true"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span> <span class="co">// comma separated args</span></span> |
207 | <span id="cb14-9"><a href="#cb14-9"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span> <span class="co">// return type</span></span> | 207 | <span id="cb14-9"><a href="#cb14-9" aria-hidden="true"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span> <span class="co">// return type</span></span> |
208 | <span id="cb14-10"><a href="#cb14-10"></a><span class="op">}</span></span></code></pre></div> | 208 | <span id="cb14-10"><a href="#cb14-10" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
209 | <p>We are simply extracting the bits of the function, we will be reusing the original function’s visibility and name. Take a look at what <code>syn::Signature</code> can tell us about a function:</p> | 209 | <p>We are simply extracting the bits of the function, we will be reusing the original function’s visibility and name. Take a look at what <code>syn::Signature</code> can tell us about a function:</p> |
210 | <pre><code> .-- syn::Ident (ident) | 210 | <pre><code> .-- syn::Ident (ident) |
211 | / | 211 | / |
@@ -217,220 +217,220 @@ syn::token::Fn --' / \ (output) | |||
217 | <p>Enough analysis, lets produce our first bit of Rust code.</p> | 217 | <p>Enough analysis, lets produce our first bit of Rust code.</p> |
218 | <h4 id="function-body">Function Body</h4> | 218 | <h4 id="function-body">Function Body</h4> |
219 | <p>Recall that the body of a curried <code>add</code> should look like this:</p> | 219 | <p>Recall that the body of a curried <code>add</code> should look like this:</p> |
220 | <div class="sourceCode" id="cb16"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb16-1"><a href="#cb16-1"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span></code></pre></div> | 220 | <div class="sourceCode" id="cb16"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span></code></pre></div> |
221 | <p>And in general:</p> | 221 | <p>And in general:</p> |
222 | <div class="sourceCode" id="cb17"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb17-1"><a href="#cb17-1"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>arg2<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>arg3<span class="op">|</span> <span class="op">...</span> <span class="op">|</span>argN<span class="op">|</span> <span class="op"><</span>function body here<span class="op">></span></span></code></pre></div> | 222 | <div class="sourceCode" id="cb17"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>arg2<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>arg3<span class="op">|</span> <span class="op">...</span> <span class="op">|</span>argN<span class="op">|</span> <span class="op"><</span>function body here<span class="op">></span></span></code></pre></div> |
223 | <p>We already have the function’s body, provided by <code>fn_body</code>, in our <code>generate_curry</code> function. All that’s left to add is the <code>move |arg2| move |arg3| ...</code> stuff, for which we need to extract the argument identifiers (doc: <a href="https://docs.rs/syn/1.0.18/syn/punctuated/struct.Punctuated.html">Punctuated</a>, <a href="https://docs.rs/syn/1.0.18/syn/enum.FnArg.html">FnArg</a>, <a href="https://docs.rs/syn/1.0.18/syn/struct.PatType.html">PatType</a>):</p> | 223 | <p>We already have the function’s body, provided by <code>fn_body</code>, in our <code>generate_curry</code> function. All that’s left to add is the <code>move |arg2| move |arg3| ...</code> stuff, for which we need to extract the argument identifiers (doc: <a href="https://docs.rs/syn/1.0.18/syn/punctuated/struct.Punctuated.html">Punctuated</a>, <a href="https://docs.rs/syn/1.0.18/syn/enum.FnArg.html">FnArg</a>, <a href="https://docs.rs/syn/1.0.18/syn/struct.PatType.html">PatType</a>):</p> |
224 | <div class="sourceCode" id="cb18"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb18-1"><a href="#cb18-1"></a><span class="co">// src/lib.rs</span></span> | 224 | <div class="sourceCode" id="cb18"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
225 | <span id="cb18-2"><a href="#cb18-2"></a><span class="kw">use</span> <span class="pp">syn::punctuated::</span>Punctuated<span class="op">;</span></span> | 225 | <span id="cb18-2"><a href="#cb18-2" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::punctuated::</span>Punctuated<span class="op">;</span></span> |
226 | <span id="cb18-3"><a href="#cb18-3"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> FnArg<span class="op">,</span> Pat<span class="op">,</span> ItemFn<span class="op">,</span> Block<span class="op">};</span></span> | 226 | <span id="cb18-3"><a href="#cb18-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> FnArg<span class="op">,</span> Pat<span class="op">,</span> ItemFn<span class="op">,</span> Block<span class="op">};</span></span> |
227 | <span id="cb18-4"><a href="#cb18-4"></a></span> | 227 | <span id="cb18-4"><a href="#cb18-4" aria-hidden="true"></a></span> |
228 | <span id="cb18-5"><a href="#cb18-5"></a><span class="kw">fn</span> extract_arg_idents(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Pat<span class="op">>></span> <span class="op">{</span> </span> | 228 | <span id="cb18-5"><a href="#cb18-5" aria-hidden="true"></a><span class="kw">fn</span> extract_arg_idents(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Pat<span class="op">>></span> <span class="op">{</span> </span> |
229 | <span id="cb18-6"><a href="#cb18-6"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_arg_pat)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> | 229 | <span id="cb18-6"><a href="#cb18-6" aria-hidden="true"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_arg_pat)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> |
230 | <span id="cb18-7"><a href="#cb18-7"></a><span class="op">}</span></span></code></pre></div> | 230 | <span id="cb18-7"><a href="#cb18-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
231 | <p>Alright, so we are iterating over function args (<code>Punctuated</code> is a collection that you can iterate over) and mapping an <code>extract_arg_pat</code> to every item. What’s <code>extract_arg_pat</code>?</p> | 231 | <p>Alright, so we are iterating over function args (<code>Punctuated</code> is a collection that you can iterate over) and mapping an <code>extract_arg_pat</code> to every item. What’s <code>extract_arg_pat</code>?</p> |
232 | <div class="sourceCode" id="cb19"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb19-1"><a href="#cb19-1"></a><span class="co">// src/lib.rs</span></span> | 232 | <div class="sourceCode" id="cb19"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
233 | <span id="cb19-2"><a href="#cb19-2"></a></span> | 233 | <span id="cb19-2"><a href="#cb19-2" aria-hidden="true"></a></span> |
234 | <span id="cb19-3"><a href="#cb19-3"></a><span class="kw">fn</span> extract_arg_pat(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span> <span class="op">{</span></span> | 234 | <span id="cb19-3"><a href="#cb19-3" aria-hidden="true"></a><span class="kw">fn</span> extract_arg_pat(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span> <span class="op">{</span></span> |
235 | <span id="cb19-4"><a href="#cb19-4"></a> <span class="kw">match</span> a <span class="op">{</span></span> | 235 | <span id="cb19-4"><a href="#cb19-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> |
236 | <span id="cb19-5"><a href="#cb19-5"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>pat<span class="op">,</span></span> | 236 | <span id="cb19-5"><a href="#cb19-5" aria-hidden="true"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>pat<span class="op">,</span></span> |
237 | <span id="cb19-6"><a href="#cb19-6"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> | 237 | <span id="cb19-6"><a href="#cb19-6" aria-hidden="true"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> |
238 | <span id="cb19-7"><a href="#cb19-7"></a> <span class="op">}</span></span> | 238 | <span id="cb19-7"><a href="#cb19-7" aria-hidden="true"></a> <span class="op">}</span></span> |
239 | <span id="cb19-8"><a href="#cb19-8"></a><span class="op">}</span></span></code></pre></div> | 239 | <span id="cb19-8"><a href="#cb19-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
240 | <p><code>FnArg</code> is an enum type as you might have guessed. The <code>Typed</code> variant encompasses args that are written as <code>name: type</code> and the other variant, <code>Reciever</code> refers to <code>self</code> types. Ignore those for now, keep it simple.</p> | 240 | <p><code>FnArg</code> is an enum type as you might have guessed. The <code>Typed</code> variant encompasses args that are written as <code>name: type</code> and the other variant, <code>Reciever</code> refers to <code>self</code> types. Ignore those for now, keep it simple.</p> |
241 | <p>Every <code>FnArg::Typed</code> value contains a <code>pat</code>, which is in essence, the name of the argument. The type of the arg is accessible via <code>p.ty</code> (we will be using this later).</p> | 241 | <p>Every <code>FnArg::Typed</code> value contains a <code>pat</code>, which is in essence, the name of the argument. The type of the arg is accessible via <code>p.ty</code> (we will be using this later).</p> |
242 | <p>With that done, we should be able to write the codegen for the function body:</p> | 242 | <p>With that done, we should be able to write the codegen for the function body:</p> |
243 | <div class="sourceCode" id="cb20"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb20-1"><a href="#cb20-1"></a><span class="co">// src/lib.rs</span></span> | 243 | <div class="sourceCode" id="cb20"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
244 | <span id="cb20-2"><a href="#cb20-2"></a></span> | 244 | <span id="cb20-2"><a href="#cb20-2" aria-hidden="true"></a></span> |
245 | <span id="cb20-3"><a href="#cb20-3"></a><span class="kw">fn</span> generate_body(fn_args<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span>]<span class="op">,</span> body<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Block<span class="op">></span>) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> | 245 | <span id="cb20-3"><a href="#cb20-3" aria-hidden="true"></a><span class="kw">fn</span> generate_body(fn_args<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Pat<span class="op">></span>]<span class="op">,</span> body<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Block<span class="op">></span>) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> |
246 | <span id="cb20-4"><a href="#cb20-4"></a> <span class="pp">quote!</span> <span class="op">{</span></span> | 246 | <span id="cb20-4"><a href="#cb20-4" aria-hidden="true"></a> <span class="pp">quote!</span> <span class="op">{</span></span> |
247 | <span id="cb20-5"><a href="#cb20-5"></a> <span class="kw">return</span> #( <span class="kw">move</span> <span class="op">|</span>#fn_args<span class="op">|</span> )<span class="op">*</span> #body</span> | 247 | <span id="cb20-5"><a href="#cb20-5" aria-hidden="true"></a> <span class="kw">return</span> #( <span class="kw">move</span> <span class="op">|</span>#fn_args<span class="op">|</span> )<span class="op">*</span> #body</span> |
248 | <span id="cb20-6"><a href="#cb20-6"></a> <span class="op">}</span></span> | 248 | <span id="cb20-6"><a href="#cb20-6" aria-hidden="true"></a> <span class="op">}</span></span> |
249 | <span id="cb20-7"><a href="#cb20-7"></a><span class="op">}</span></span></code></pre></div> | 249 | <span id="cb20-7"><a href="#cb20-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
250 | <p>That is some scary looking syntax! Allow me to explain. The <code>quote!{ ... }</code> returns a <code>proc_macro2::TokenStream</code>, if we wrote <code>quote!{ let x = 1 + 2; }</code>, it wouldn’t create a new variable <code>x</code> with value 3, it would literally produce a stream of tokens with that expression.</p> | 250 | <p>That is some scary looking syntax! Allow me to explain. The <code>quote!{ ... }</code> returns a <code>proc_macro2::TokenStream</code>, if we wrote <code>quote!{ let x = 1 + 2; }</code>, it wouldn’t create a new variable <code>x</code> with value 3, it would literally produce a stream of tokens with that expression.</p> |
251 | <p>The <code>#</code> enables variable interpolation. <code>#body</code> will look for <code>body</code> in the current scope, take its value, and insert it in the returned <code>TokenStream</code>. Kinda like quasi quoting in LISPs, you have written one.</p> | 251 | <p>The <code>#</code> enables variable interpolation. <code>#body</code> will look for <code>body</code> in the current scope, take its value, and insert it in the returned <code>TokenStream</code>. Kinda like quasi quoting in LISPs, you have written one.</p> |
252 | <p>What about <code>#( move |#fn_args| )*</code>? That is repetition. <code>quote</code> iterates through <code>fn_args</code>, and drops a <code>move</code> behind each one, it then places pipes (<code>|</code>), around it.</p> | 252 | <p>What about <code>#( move |#fn_args| )*</code>? That is repetition. <code>quote</code> iterates through <code>fn_args</code>, and drops a <code>move</code> behind each one, it then places pipes (<code>|</code>), around it.</p> |
253 | <p>Let us test our first bit of codegen! Modify <code>generate_curry</code> like so:</p> | 253 | <p>Let us test our first bit of codegen! Modify <code>generate_curry</code> like so:</p> |
254 | <div class="sourceCode" id="cb21"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb21-1"><a href="#cb21-1"></a><span class="co">// src/lib.rs</span></span> | 254 | <div class="sourceCode" id="cb21"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
255 | <span id="cb21-2"><a href="#cb21-2"></a></span> | 255 | <span id="cb21-2"><a href="#cb21-2" aria-hidden="true"></a></span> |
256 | <span id="cb21-3"><a href="#cb21-3"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> TokenStream <span class="op">{</span></span> | 256 | <span id="cb21-3"><a href="#cb21-3" aria-hidden="true"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> TokenStream <span class="op">{</span></span> |
257 | <span id="cb21-4"><a href="#cb21-4"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> | 257 | <span id="cb21-4"><a href="#cb21-4" aria-hidden="true"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> |
258 | <span id="cb21-5"><a href="#cb21-5"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> | 258 | <span id="cb21-5"><a href="#cb21-5" aria-hidden="true"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> |
259 | <span id="cb21-6"><a href="#cb21-6"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> | 259 | <span id="cb21-6"><a href="#cb21-6" aria-hidden="true"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> |
260 | <span id="cb21-7"><a href="#cb21-7"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> | 260 | <span id="cb21-7"><a href="#cb21-7" aria-hidden="true"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> |
261 | <span id="cb21-8"><a href="#cb21-8"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> | 261 | <span id="cb21-8"><a href="#cb21-8" aria-hidden="true"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> |
262 | <span id="cb21-9"><a href="#cb21-9"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> | 262 | <span id="cb21-9"><a href="#cb21-9" aria-hidden="true"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> |
263 | <span id="cb21-10"><a href="#cb21-10"></a></span> | 263 | <span id="cb21-10"><a href="#cb21-10" aria-hidden="true"></a></span> |
264 | <span id="cb21-11"><a href="#cb21-11"></a><span class="op">+</span> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> | 264 | <span id="cb21-11"><a href="#cb21-11" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> |
265 | <span id="cb21-12"><a href="#cb21-12"></a><span class="op">+</span> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> | 265 | <span id="cb21-12"><a href="#cb21-12" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> |
266 | <span id="cb21-13"><a href="#cb21-13"></a></span> | 266 | <span id="cb21-13"><a href="#cb21-13" aria-hidden="true"></a></span> |
267 | <span id="cb21-14"><a href="#cb21-14"></a><span class="op">+</span> <span class="co">// remember, our curried body starts with the second argument!</span></span> | 267 | <span id="cb21-14"><a href="#cb21-14" aria-hidden="true"></a><span class="op">+</span> <span class="co">// remember, our curried body starts with the second argument!</span></span> |
268 | <span id="cb21-15"><a href="#cb21-15"></a><span class="op">+</span> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> | 268 | <span id="cb21-15"><a href="#cb21-15" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> |
269 | <span id="cb21-16"><a href="#cb21-16"></a><span class="op">+</span> <span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> curried_body)<span class="op">;</span></span> | 269 | <span id="cb21-16"><a href="#cb21-16" aria-hidden="true"></a><span class="op">+</span> <span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> curried_body)<span class="op">;</span></span> |
270 | <span id="cb21-17"><a href="#cb21-17"></a></span> | 270 | <span id="cb21-17"><a href="#cb21-17" aria-hidden="true"></a></span> |
271 | <span id="cb21-18"><a href="#cb21-18"></a> <span class="kw">return</span> <span class="pp">TokenStream::</span>new()<span class="op">;</span></span> | 271 | <span id="cb21-18"><a href="#cb21-18" aria-hidden="true"></a> <span class="kw">return</span> <span class="pp">TokenStream::</span>new()<span class="op">;</span></span> |
272 | <span id="cb21-19"><a href="#cb21-19"></a> <span class="op">}</span></span></code></pre></div> | 272 | <span id="cb21-19"><a href="#cb21-19" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> |
273 | <p>Add a little test to <code>tests/</code>:</p> | 273 | <p>Add a little test to <code>tests/</code>:</p> |
274 | <div class="sourceCode" id="cb22"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb22-1"><a href="#cb22-1"></a><span class="co">// tests/smoke.rs</span></span> | 274 | <div class="sourceCode" id="cb22"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true"></a><span class="co">// tests/smoke.rs</span></span> |
275 | <span id="cb22-2"><a href="#cb22-2"></a></span> | 275 | <span id="cb22-2"><a href="#cb22-2" aria-hidden="true"></a></span> |
276 | <span id="cb22-3"><a href="#cb22-3"></a><span class="at">#[</span><span class="pp">currying::</span>curry<span class="at">]</span></span> | 276 | <span id="cb22-3"><a href="#cb22-3" aria-hidden="true"></a><span class="at">#[</span><span class="pp">currying::</span>curry<span class="at">]</span></span> |
277 | <span id="cb22-4"><a href="#cb22-4"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 277 | <span id="cb22-4"><a href="#cb22-4" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
278 | <span id="cb22-5"><a href="#cb22-5"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> | 278 | <span id="cb22-5"><a href="#cb22-5" aria-hidden="true"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> |
279 | <span id="cb22-6"><a href="#cb22-6"></a><span class="op">}</span></span> | 279 | <span id="cb22-6"><a href="#cb22-6" aria-hidden="true"></a><span class="op">}</span></span> |
280 | <span id="cb22-7"><a href="#cb22-7"></a></span> | 280 | <span id="cb22-7"><a href="#cb22-7" aria-hidden="true"></a></span> |
281 | <span id="cb22-8"><a href="#cb22-8"></a><span class="at">#[</span>test<span class="at">]</span></span> | 281 | <span id="cb22-8"><a href="#cb22-8" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> |
282 | <span id="cb22-9"><a href="#cb22-9"></a><span class="kw">fn</span> works() <span class="op">{</span></span> | 282 | <span id="cb22-9"><a href="#cb22-9" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> |
283 | <span id="cb22-10"><a href="#cb22-10"></a> <span class="pp">assert!</span>(<span class="cn">true</span>)<span class="op">;</span></span> | 283 | <span id="cb22-10"><a href="#cb22-10" aria-hidden="true"></a> <span class="pp">assert!</span>(<span class="cn">true</span>)<span class="op">;</span></span> |
284 | <span id="cb22-11"><a href="#cb22-11"></a><span class="op">}</span></span></code></pre></div> | 284 | <span id="cb22-11"><a href="#cb22-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
285 | <p>You should find something like this in the output of <code>cargo test</code>:</p> | 285 | <p>You should find something like this in the output of <code>cargo test</code>:</p> |
286 | <pre><code>return move | y | move | z | { x + y + z }</code></pre> | 286 | <pre><code>return move | y | move | z | { x + y + z }</code></pre> |
287 | <p>Glorious <code>println!</code> debugging!</p> | 287 | <p>Glorious <code>println!</code> debugging!</p> |
288 | <h4 id="function-signature">Function signature</h4> | 288 | <h4 id="function-signature">Function signature</h4> |
289 | <p>This section gets into the more complicated bits of the macro, generating type aliases and the function signature. By the end of this section, we should have a full working auto-currying macro!</p> | 289 | <p>This section gets into the more complicated bits of the macro, generating type aliases and the function signature. By the end of this section, we should have a full working auto-currying macro!</p> |
290 | <p>Recall what our generated type aliases should look like, for our <code>add</code> function:</p> | 290 | <p>Recall what our generated type aliases should look like, for our <code>add</code> function:</p> |
291 | <div class="sourceCode" id="cb24"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb24-1"><a href="#cb24-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 291 | <div class="sourceCode" id="cb24"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
292 | <span id="cb24-2"><a href="#cb24-2"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> | 292 | <span id="cb24-2"><a href="#cb24-2" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> |
293 | <span id="cb24-3"><a href="#cb24-3"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span></span></code></pre></div> | 293 | <span id="cb24-3"><a href="#cb24-3" aria-hidden="true"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T1<span class="op">;</span></span></code></pre></div> |
294 | <p>In general:</p> | 294 | <p>In general:</p> |
295 | <div class="sourceCode" id="cb25"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb25-1"><a href="#cb25-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op"><</span><span class="kw">return</span> <span class="kw">type</span>><span class="op">;</span></span> | 295 | <div class="sourceCode" id="cb25"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op"><</span><span class="kw">return</span> <span class="kw">type</span>><span class="op">;</span></span> |
296 | <span id="cb25-2"><a href="#cb25-2"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N>) -> T0<span class="op">;</span></span> | 296 | <span id="cb25-2"><a href="#cb25-2" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N>) -> T0<span class="op">;</span></span> |
297 | <span id="cb25-3"><a href="#cb25-3"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N - 1>) -> T1<span class="op">;</span></span> | 297 | <span id="cb25-3"><a href="#cb25-3" aria-hidden="true"></a><span class="kw">type</span> T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg N - 1>) -> T1<span class="op">;</span></span> |
298 | <span id="cb25-4"><a href="#cb25-4"></a><span class="op">.</span></span> | 298 | <span id="cb25-4"><a href="#cb25-4" aria-hidden="true"></a><span class="op">.</span></span> |
299 | <span id="cb25-5"><a href="#cb25-5"></a><span class="op">.</span></span> | 299 | <span id="cb25-5"><a href="#cb25-5" aria-hidden="true"></a><span class="op">.</span></span> |
300 | <span id="cb25-6"><a href="#cb25-6"></a><span class="op">.</span></span> | 300 | <span id="cb25-6"><a href="#cb25-6" aria-hidden="true"></a><span class="op">.</span></span> |
301 | <span id="cb25-7"><a href="#cb25-7"></a><span class="kw">type</span> T(N-1) <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg 2>) -> T(N-2)<span class="op">;</span></span></code></pre></div> | 301 | <span id="cb25-7"><a href="#cb25-7" aria-hidden="true"></a><span class="kw">type</span> T(N-1) <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="op"><</span><span class="kw">type</span> of arg 2>) -> T(N-2)<span class="op">;</span></span></code></pre></div> |
302 | <p>To codegen that, we need the types of:</p> | 302 | <p>To codegen that, we need the types of:</p> |
303 | <ul> | 303 | <ul> |
304 | <li>all our inputs (arguments)</li> | 304 | <li>all our inputs (arguments)</li> |
305 | <li>the output (the return type)</li> | 305 | <li>the output (the return type)</li> |
306 | </ul> | 306 | </ul> |
307 | <p>To fetch the types of all our inputs, we can simply reuse the bits we wrote to fetch the names of all our inputs! (doc: <a href="https://docs.rs/syn/1.0.18/syn/enum.Type.html">Type</a>)</p> | 307 | <p>To fetch the types of all our inputs, we can simply reuse the bits we wrote to fetch the names of all our inputs! (doc: <a href="https://docs.rs/syn/1.0.18/syn/enum.Type.html">Type</a>)</p> |
308 | <div class="sourceCode" id="cb26"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb26-1"><a href="#cb26-1"></a><span class="co">// src/lib.rs</span></span> | 308 | <div class="sourceCode" id="cb26"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
309 | <span id="cb26-2"><a href="#cb26-2"></a></span> | 309 | <span id="cb26-2"><a href="#cb26-2" aria-hidden="true"></a></span> |
310 | <span id="cb26-3"><a href="#cb26-3"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> Block<span class="op">,</span> FnArg<span class="op">,</span> ItemFn<span class="op">,</span> Pat<span class="op">,</span> ReturnType<span class="op">,</span> Type<span class="op">};</span></span> | 310 | <span id="cb26-3"><a href="#cb26-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">syn::</span><span class="op">{</span>parse_macro_input<span class="op">,</span> Block<span class="op">,</span> FnArg<span class="op">,</span> ItemFn<span class="op">,</span> Pat<span class="op">,</span> ReturnType<span class="op">,</span> Type<span class="op">};</span></span> |
311 | <span id="cb26-4"><a href="#cb26-4"></a></span> | 311 | <span id="cb26-4"><a href="#cb26-4" aria-hidden="true"></a></span> |
312 | <span id="cb26-5"><a href="#cb26-5"></a><span class="kw">fn</span> extract_type(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> | 312 | <span id="cb26-5"><a href="#cb26-5" aria-hidden="true"></a><span class="kw">fn</span> extract_type(a<span class="op">:</span> FnArg) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> |
313 | <span id="cb26-6"><a href="#cb26-6"></a> <span class="kw">match</span> a <span class="op">{</span></span> | 313 | <span id="cb26-6"><a href="#cb26-6" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> |
314 | <span id="cb26-7"><a href="#cb26-7"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>ty<span class="op">,</span> <span class="co">// notice `ty` instead of `pat`</span></span> | 314 | <span id="cb26-7"><a href="#cb26-7" aria-hidden="true"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=></span> p<span class="op">.</span>ty<span class="op">,</span> <span class="co">// notice `ty` instead of `pat`</span></span> |
315 | <span id="cb26-8"><a href="#cb26-8"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> | 315 | <span id="cb26-8"><a href="#cb26-8" aria-hidden="true"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on types with `self`!"</span>)<span class="op">,</span></span> |
316 | <span id="cb26-9"><a href="#cb26-9"></a> <span class="op">}</span></span> | 316 | <span id="cb26-9"><a href="#cb26-9" aria-hidden="true"></a> <span class="op">}</span></span> |
317 | <span id="cb26-10"><a href="#cb26-10"></a><span class="op">}</span></span> | 317 | <span id="cb26-10"><a href="#cb26-10" aria-hidden="true"></a><span class="op">}</span></span> |
318 | <span id="cb26-11"><a href="#cb26-11"></a></span> | 318 | <span id="cb26-11"><a href="#cb26-11" aria-hidden="true"></a></span> |
319 | <span id="cb26-12"><a href="#cb26-12"></a><span class="kw">fn</span> extract_arg_types(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Type<span class="op">>></span> <span class="op">{</span></span> | 319 | <span id="cb26-12"><a href="#cb26-12" aria-hidden="true"></a><span class="kw">fn</span> extract_arg_types(fn_args<span class="op">:</span> Punctuated<span class="op"><</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">></span>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Box</span><span class="op"><</span>Type<span class="op">>></span> <span class="op">{</span></span> |
320 | <span id="cb26-13"><a href="#cb26-13"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_type)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> | 320 | <span id="cb26-13"><a href="#cb26-13" aria-hidden="true"></a> <span class="kw">return</span> fn_args<span class="op">.</span>into_iter()<span class="op">.</span>map(extract_type)<span class="op">.</span><span class="pp">collect::</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span>_<span class="op">>></span>()<span class="op">;</span></span> |
321 | <span id="cb26-14"><a href="#cb26-14"></a></span> | 321 | <span id="cb26-14"><a href="#cb26-14" aria-hidden="true"></a></span> |
322 | <span id="cb26-15"><a href="#cb26-15"></a><span class="op">}</span></span></code></pre></div> | 322 | <span id="cb26-15"><a href="#cb26-15" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
323 | <p>A good reader would have looked at the docs for output member of the <code>syn::Signature</code> struct. It has the type <code>syn::ReturnType</code>. So there is no extraction to do here right? There are actually a couple of things we have to ensure here:</p> | 323 | <p>A good reader would have looked at the docs for output member of the <code>syn::Signature</code> struct. It has the type <code>syn::ReturnType</code>. So there is no extraction to do here right? There are actually a couple of things we have to ensure here:</p> |
324 | <ol type="1"> | 324 | <ol type="1"> |
325 | <li><p>We need to ensure that the function returns! A function that does not return is pointless in this case, and I will tell you why, in the <a href="#notes">Notes</a> section.</p></li> | 325 | <li><p>We need to ensure that the function returns! A function that does not return is pointless in this case, and I will tell you why, in the <a href="#notes">Notes</a> section.</p></li> |
326 | <li><p>A <code>ReturnType</code> encloses the arrow of the return as well, we need to get rid of that. Recall:</p> | 326 | <li><p>A <code>ReturnType</code> encloses the arrow of the return as well, we need to get rid of that. Recall:</p> |
327 | <div class="sourceCode" id="cb27"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb27-1"><a href="#cb27-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span></span> | 327 | <div class="sourceCode" id="cb27"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span></span> |
328 | <span id="cb27-2"><a href="#cb27-2"></a><span class="co">// and not</span></span> | 328 | <span id="cb27-2"><a href="#cb27-2" aria-hidden="true"></a><span class="co">// and not</span></span> |
329 | <span id="cb27-3"><a href="#cb27-3"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op">-></span> <span class="dt">u32</span></span></code></pre></div></li> | 329 | <span id="cb27-3"><a href="#cb27-3" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="op">-></span> <span class="dt">u32</span></span></code></pre></div></li> |
330 | </ol> | 330 | </ol> |
331 | <p>Here is the snippet that handles extraction of the return type (doc: <a href="https://docs.rs/syn/1.0.19/syn/enum.ReturnType.html">syn::ReturnType</a>):</p> | 331 | <p>Here is the snippet that handles extraction of the return type (doc: <a href="https://docs.rs/syn/1.0.19/syn/enum.ReturnType.html">syn::ReturnType</a>):</p> |
332 | <div class="sourceCode" id="cb28"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb28-1"><a href="#cb28-1"></a><span class="co">// src/lib.rs</span></span> | 332 | <div class="sourceCode" id="cb28"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
333 | <span id="cb28-2"><a href="#cb28-2"></a></span> | 333 | <span id="cb28-2"><a href="#cb28-2" aria-hidden="true"></a></span> |
334 | <span id="cb28-3"><a href="#cb28-3"></a><span class="kw">fn</span> extract_return_type(a<span class="op">:</span> ReturnType) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> | 334 | <span id="cb28-3"><a href="#cb28-3" aria-hidden="true"></a><span class="kw">fn</span> extract_return_type(a<span class="op">:</span> ReturnType) <span class="op">-></span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span> <span class="op">{</span></span> |
335 | <span id="cb28-4"><a href="#cb28-4"></a> <span class="kw">match</span> a <span class="op">{</span></span> | 335 | <span id="cb28-4"><a href="#cb28-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> |
336 | <span id="cb28-5"><a href="#cb28-5"></a> <span class="pp">ReturnType::</span>Type(_<span class="op">,</span> p) <span class="op">=></span> p<span class="op">,</span></span> | 336 | <span id="cb28-5"><a href="#cb28-5" aria-hidden="true"></a> <span class="pp">ReturnType::</span>Type(_<span class="op">,</span> p) <span class="op">=></span> p<span class="op">,</span></span> |
337 | <span id="cb28-6"><a href="#cb28-6"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on functions without return types!"</span>)<span class="op">,</span></span> | 337 | <span id="cb28-6"><a href="#cb28-6" aria-hidden="true"></a> _ <span class="op">=></span> <span class="pp">panic!</span>(<span class="st">"Not supported on functions without return types!"</span>)<span class="op">,</span></span> |
338 | <span id="cb28-7"><a href="#cb28-7"></a> <span class="op">}</span></span> | 338 | <span id="cb28-7"><a href="#cb28-7" aria-hidden="true"></a> <span class="op">}</span></span> |
339 | <span id="cb28-8"><a href="#cb28-8"></a><span class="op">}</span></span></code></pre></div> | 339 | <span id="cb28-8"><a href="#cb28-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
340 | <p>You might notice that we are making extensive use of the <code>panic!</code> macro. Well, that is because it is a good idea to quit on receiving an unsatisfactory <code>TokenStream</code>.</p> | 340 | <p>You might notice that we are making extensive use of the <code>panic!</code> macro. Well, that is because it is a good idea to quit on receiving an unsatisfactory <code>TokenStream</code>.</p> |
341 | <p>With all our types ready, we can get on with generating type aliases:</p> | 341 | <p>With all our types ready, we can get on with generating type aliases:</p> |
342 | <div class="sourceCode" id="cb29"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb29-1"><a href="#cb29-1"></a><span class="co">// src/lib.rs</span></span> | 342 | <div class="sourceCode" id="cb29"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
343 | <span id="cb29-2"><a href="#cb29-2"></a></span> | 343 | <span id="cb29-2"><a href="#cb29-2" aria-hidden="true"></a></span> |
344 | <span id="cb29-3"><a href="#cb29-3"></a><span class="kw">use</span> <span class="pp">quote::</span><span class="op">{</span>quote<span class="op">,</span> format_ident<span class="op">};</span></span> | 344 | <span id="cb29-3"><a href="#cb29-3" aria-hidden="true"></a><span class="kw">use</span> <span class="pp">quote::</span><span class="op">{</span>quote<span class="op">,</span> format_ident<span class="op">};</span></span> |
345 | <span id="cb29-4"><a href="#cb29-4"></a></span> | 345 | <span id="cb29-4"><a href="#cb29-4" aria-hidden="true"></a></span> |
346 | <span id="cb29-5"><a href="#cb29-5"></a><span class="kw">fn</span> generate_type_aliases(</span> | 346 | <span id="cb29-5"><a href="#cb29-5" aria-hidden="true"></a><span class="kw">fn</span> generate_type_aliases(</span> |
347 | <span id="cb29-6"><a href="#cb29-6"></a> fn_arg_types<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span>]<span class="op">,</span></span> | 347 | <span id="cb29-6"><a href="#cb29-6" aria-hidden="true"></a> fn_arg_types<span class="op">:</span> <span class="op">&</span>[<span class="dt">Box</span><span class="op"><</span>Type<span class="op">></span>]<span class="op">,</span></span> |
348 | <span id="cb29-7"><a href="#cb29-7"></a> fn_return_type<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">>,</span></span> | 348 | <span id="cb29-7"><a href="#cb29-7" aria-hidden="true"></a> fn_return_type<span class="op">:</span> <span class="dt">Box</span><span class="op"><</span>Type<span class="op">>,</span></span> |
349 | <span id="cb29-8"><a href="#cb29-8"></a> fn_name<span class="op">:</span> <span class="op">&</span><span class="pp">syn::</span>Ident<span class="op">,</span></span> | 349 | <span id="cb29-8"><a href="#cb29-8" aria-hidden="true"></a> fn_name<span class="op">:</span> <span class="op">&</span><span class="pp">syn::</span>Ident<span class="op">,</span></span> |
350 | <span id="cb29-9"><a href="#cb29-9"></a>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="pp">proc_macro2::</span>TokenStream<span class="op">></span> <span class="op">{</span> <span class="co">// 1</span></span> | 350 | <span id="cb29-9"><a href="#cb29-9" aria-hidden="true"></a>) <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="pp">proc_macro2::</span>TokenStream<span class="op">></span> <span class="op">{</span> <span class="co">// 1</span></span> |
351 | <span id="cb29-10"><a href="#cb29-10"></a></span> | 351 | <span id="cb29-10"><a href="#cb29-10" aria-hidden="true"></a></span> |
352 | <span id="cb29-11"><a href="#cb29-11"></a> <span class="kw">let</span> type_t0 <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_T0"</span><span class="op">,</span> fn_name)<span class="op">;</span> <span class="co">// 2</span></span> | 352 | <span id="cb29-11"><a href="#cb29-11" aria-hidden="true"></a> <span class="kw">let</span> type_t0 <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_T0"</span><span class="op">,</span> fn_name)<span class="op">;</span> <span class="co">// 2</span></span> |
353 | <span id="cb29-12"><a href="#cb29-12"></a> <span class="kw">let</span> <span class="kw">mut</span> type_aliases <span class="op">=</span> <span class="pp">vec!</span>[<span class="pp">quote!</span> <span class="op">{</span> <span class="kw">type</span> #type_t0 <span class="op">=</span> #fn_return_type <span class="op">}</span>]<span class="op">;</span></span> | 353 | <span id="cb29-12"><a href="#cb29-12" aria-hidden="true"></a> <span class="kw">let</span> <span class="kw">mut</span> type_aliases <span class="op">=</span> <span class="pp">vec!</span>[<span class="pp">quote!</span> <span class="op">{</span> <span class="kw">type</span> #type_t0 <span class="op">=</span> #fn_return_type <span class="op">}</span>]<span class="op">;</span></span> |
354 | <span id="cb29-13"><a href="#cb29-13"></a></span> | 354 | <span id="cb29-13"><a href="#cb29-13" aria-hidden="true"></a></span> |
355 | <span id="cb29-14"><a href="#cb29-14"></a> <span class="co">// 3</span></span> | 355 | <span id="cb29-14"><a href="#cb29-14" aria-hidden="true"></a> <span class="co">// 3</span></span> |
356 | <span id="cb29-15"><a href="#cb29-15"></a> <span class="kw">for</span> (i<span class="op">,</span> t) <span class="kw">in</span> (<span class="dv">1</span><span class="op">..</span>)<span class="op">.</span>zip(fn_arg_types<span class="op">.</span>into_iter()<span class="op">.</span>rev()) <span class="op">{</span></span> | 356 | <span id="cb29-15"><a href="#cb29-15" aria-hidden="true"></a> <span class="kw">for</span> (i<span class="op">,</span> t) <span class="kw">in</span> (<span class="dv">1</span><span class="op">..</span>)<span class="op">.</span>zip(fn_arg_types<span class="op">.</span>into_iter()<span class="op">.</span>rev()) <span class="op">{</span></span> |
357 | <span id="cb29-16"><a href="#cb29-16"></a> <span class="kw">let</span> p <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> | 357 | <span id="cb29-16"><a href="#cb29-16" aria-hidden="true"></a> <span class="kw">let</span> p <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> |
358 | <span id="cb29-17"><a href="#cb29-17"></a> <span class="kw">let</span> n <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i))<span class="op">;</span></span> | 358 | <span id="cb29-17"><a href="#cb29-17" aria-hidden="true"></a> <span class="kw">let</span> n <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> i))<span class="op">;</span></span> |
359 | <span id="cb29-18"><a href="#cb29-18"></a></span> | 359 | <span id="cb29-18"><a href="#cb29-18" aria-hidden="true"></a></span> |
360 | <span id="cb29-19"><a href="#cb29-19"></a> type_aliases<span class="op">.</span>push(<span class="pp">quote!</span> <span class="op">{</span></span> | 360 | <span id="cb29-19"><a href="#cb29-19" aria-hidden="true"></a> type_aliases<span class="op">.</span>push(<span class="pp">quote!</span> <span class="op">{</span></span> |
361 | <span id="cb29-20"><a href="#cb29-20"></a> <span class="kw">type</span> #n <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(#t) <span class="op">-></span> #p</span> | 361 | <span id="cb29-20"><a href="#cb29-20" aria-hidden="true"></a> <span class="kw">type</span> #n <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(#t) <span class="op">-></span> #p</span> |
362 | <span id="cb29-21"><a href="#cb29-21"></a> <span class="op">}</span>)<span class="op">;</span></span> | 362 | <span id="cb29-21"><a href="#cb29-21" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> |
363 | <span id="cb29-22"><a href="#cb29-22"></a> <span class="op">}</span></span> | 363 | <span id="cb29-22"><a href="#cb29-22" aria-hidden="true"></a> <span class="op">}</span></span> |
364 | <span id="cb29-23"><a href="#cb29-23"></a></span> | 364 | <span id="cb29-23"><a href="#cb29-23" aria-hidden="true"></a></span> |
365 | <span id="cb29-24"><a href="#cb29-24"></a> <span class="kw">return</span> type_aliases<span class="op">;</span></span> | 365 | <span id="cb29-24"><a href="#cb29-24" aria-hidden="true"></a> <span class="kw">return</span> type_aliases<span class="op">;</span></span> |
366 | <span id="cb29-25"><a href="#cb29-25"></a><span class="op">}</span></span></code></pre></div> | 366 | <span id="cb29-25"><a href="#cb29-25" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
367 | <p><strong>1. The return value</strong><br /> | 367 | <p><strong>1. The return value</strong><br /> |
368 | We are returning a <code>Vec<proc_macro2::TokenStream></code>, i. e., a list of <code>TokenStream</code>s, where each item is a type alias.</p> | 368 | We are returning a <code>Vec<proc_macro2::TokenStream></code>, i. e., a list of <code>TokenStream</code>s, where each item is a type alias.</p> |
369 | <p><strong>2. Format identifier?</strong><br /> | 369 | <p><strong>2. Format identifier?</strong><br /> |
370 | I’ve got some explanation to do on this line. Clearly, we are trying to write the first type alias, and initialize our <code>TokenStream</code> vector with <code>T0</code>, because it is different from the others:</p> | 370 | I’ve got some explanation to do on this line. Clearly, we are trying to write the first type alias, and initialize our <code>TokenStream</code> vector with <code>T0</code>, because it is different from the others:</p> |
371 | <div class="sourceCode" id="cb30"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb30-1"><a href="#cb30-1"></a><span class="kw">type</span> T0 <span class="op">=</span> something</span> | 371 | <div class="sourceCode" id="cb30"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> something</span> |
372 | <span id="cb30-2"><a href="#cb30-2"></a><span class="co">// the others are of the form</span></span> | 372 | <span id="cb30-2"><a href="#cb30-2" aria-hidden="true"></a><span class="co">// the others are of the form</span></span> |
373 | <span id="cb30-3"><a href="#cb30-3"></a><span class="kw">type</span> Tr <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(something) <span class="op">-></span> something</span></code></pre></div> | 373 | <span id="cb30-3"><a href="#cb30-3" aria-hidden="true"></a><span class="kw">type</span> Tr <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(something) <span class="op">-></span> something</span></code></pre></div> |
374 | <p><code>format_ident!</code> is similar to <code>format!</code>. Instead of returning a formatted string, it returns a <code>syn::Ident</code>. Therefore, <code>type_t0</code> is actually an identifier for, in the case of our <code>add</code> function, <code>_add_T0</code>. Why is this formatting important? Namespacing.</p> | 374 | <p><code>format_ident!</code> is similar to <code>format!</code>. Instead of returning a formatted string, it returns a <code>syn::Ident</code>. Therefore, <code>type_t0</code> is actually an identifier for, in the case of our <code>add</code> function, <code>_add_T0</code>. Why is this formatting important? Namespacing.</p> |
375 | <p>Picture this, we have two functions, <code>add</code> and <code>subtract</code>, that we wish to curry with our macro:</p> | 375 | <p>Picture this, we have two functions, <code>add</code> and <code>subtract</code>, that we wish to curry with our macro:</p> |
376 | <div class="sourceCode" id="cb31"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb31-1"><a href="#cb31-1"></a><span class="at">#[</span>curry<span class="at">]</span></span> | 376 | <div class="sourceCode" id="cb31"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> |
377 | <span id="cb31-2"><a href="#cb31-2"></a><span class="kw">fn</span> add(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> | 377 | <span id="cb31-2"><a href="#cb31-2" aria-hidden="true"></a><span class="kw">fn</span> add(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> |
378 | <span id="cb31-3"><a href="#cb31-3"></a></span> | 378 | <span id="cb31-3"><a href="#cb31-3" aria-hidden="true"></a></span> |
379 | <span id="cb31-4"><a href="#cb31-4"></a><span class="at">#[</span>curry<span class="at">]</span></span> | 379 | <span id="cb31-4"><a href="#cb31-4" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> |
380 | <span id="cb31-5"><a href="#cb31-5"></a><span class="kw">fn</span> sub(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> | 380 | <span id="cb31-5"><a href="#cb31-5" aria-hidden="true"></a><span class="kw">fn</span> sub(<span class="op">...</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> |
381 | <p>Here is the same but with macros expanded:</p> | 381 | <p>Here is the same but with macros expanded:</p> |
382 | <div class="sourceCode" id="cb32"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb32-1"><a href="#cb32-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 382 | <div class="sourceCode" id="cb32"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
383 | <span id="cb32-2"><a href="#cb32-2"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> | 383 | <span id="cb32-2"><a href="#cb32-2" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> |
384 | <span id="cb32-3"><a href="#cb32-3"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> | 384 | <span id="cb32-3"><a href="#cb32-3" aria-hidden="true"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> |
385 | <span id="cb32-4"><a href="#cb32-4"></a></span> | 385 | <span id="cb32-4"><a href="#cb32-4" aria-hidden="true"></a></span> |
386 | <span id="cb32-5"><a href="#cb32-5"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 386 | <span id="cb32-5"><a href="#cb32-5" aria-hidden="true"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
387 | <span id="cb32-6"><a href="#cb32-6"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> | 387 | <span id="cb32-6"><a href="#cb32-6" aria-hidden="true"></a><span class="kw">type</span> T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> T0<span class="op">;</span></span> |
388 | <span id="cb32-7"><a href="#cb32-7"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> | 388 | <span id="cb32-7"><a href="#cb32-7" aria-hidden="true"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> |
389 | <p>We end up with two definitions of <code>T0</code>! Now, if we do the little <code>format_ident!</code> dance we did up there:</p> | 389 | <p>We end up with two definitions of <code>T0</code>! Now, if we do the little <code>format_ident!</code> dance we did up there:</p> |
390 | <div class="sourceCode" id="cb33"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb33-1"><a href="#cb33-1"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 390 | <div class="sourceCode" id="cb33"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
391 | <span id="cb33-2"><a href="#cb33-2"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> | 391 | <span id="cb33-2"><a href="#cb33-2" aria-hidden="true"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> |
392 | <span id="cb33-3"><a href="#cb33-3"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> _add_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> | 392 | <span id="cb33-3"><a href="#cb33-3" aria-hidden="true"></a><span class="kw">fn</span> add( <span class="op">...</span> ) <span class="op">-></span> _add_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> |
393 | <span id="cb33-4"><a href="#cb33-4"></a></span> | 393 | <span id="cb33-4"><a href="#cb33-4" aria-hidden="true"></a></span> |
394 | <span id="cb33-5"><a href="#cb33-5"></a><span class="kw">type</span> _sub_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 394 | <span id="cb33-5"><a href="#cb33-5" aria-hidden="true"></a><span class="kw">type</span> _sub_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
395 | <span id="cb33-6"><a href="#cb33-6"></a><span class="kw">type</span> _sub_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _sub_T0<span class="op">;</span></span> | 395 | <span id="cb33-6"><a href="#cb33-6" aria-hidden="true"></a><span class="kw">type</span> _sub_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _sub_T0<span class="op">;</span></span> |
396 | <span id="cb33-7"><a href="#cb33-7"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> _sub_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> | 396 | <span id="cb33-7"><a href="#cb33-7" aria-hidden="true"></a><span class="kw">fn</span> sub( <span class="op">...</span> ) <span class="op">-></span> _sub_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> |
397 | <p>Voilà! The type aliases don’t tread on each other. Remember to import <code>format_ident</code> from the <code>quote</code> crate.</p> | 397 | <p>Voilà! The type aliases don’t tread on each other. Remember to import <code>format_ident</code> from the <code>quote</code> crate.</p> |
398 | <p><strong>3. The TokenStream Vector</strong></p> | 398 | <p><strong>3. The TokenStream Vector</strong></p> |
399 | <p>We iterate over our types in reverse order (<code>T0</code> is the last return, <code>T1</code> is the second last, so on), assign a number to each iteration with <code>zip</code>, generate type names with <code>format_ident</code>, push a <code>TokenStream</code> with the help of <code>quote</code> and variable interpolation.</p> | 399 | <p>We iterate over our types in reverse order (<code>T0</code> is the last return, <code>T1</code> is the second last, so on), assign a number to each iteration with <code>zip</code>, generate type names with <code>format_ident</code>, push a <code>TokenStream</code> with the help of <code>quote</code> and variable interpolation.</p> |
400 | <p>If you are wondering why we used <code>(1..).zip()</code> instead of <code>.enumerate()</code>, it’s because we wanted to start counting from 1 instead of 0 (we are already done with <code>T0</code>!).</p> | 400 | <p>If you are wondering why we used <code>(1..).zip()</code> instead of <code>.enumerate()</code>, it’s because we wanted to start counting from 1 instead of 0 (we are already done with <code>T0</code>!).</p> |
401 | <h4 id="getting-it-together">Getting it together</h4> | 401 | <h4 id="getting-it-together">Getting it together</h4> |
402 | <p>I promised we’d have a fully working macro by the end of last section. I lied, we have to tie everything together in our <code>generate_curry</code> function:</p> | 402 | <p>I promised we’d have a fully working macro by the end of last section. I lied, we have to tie everything together in our <code>generate_curry</code> function:</p> |
403 | <div class="sourceCode" id="cb34"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb34-1"><a href="#cb34-1"></a><span class="co">// src/lib.rs</span></span> | 403 | <div class="sourceCode" id="cb34"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true"></a><span class="co">// src/lib.rs</span></span> |
404 | <span id="cb34-2"><a href="#cb34-2"></a></span> | 404 | <span id="cb34-2"><a href="#cb34-2" aria-hidden="true"></a></span> |
405 | <span id="cb34-3"><a href="#cb34-3"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> | 405 | <span id="cb34-3"><a href="#cb34-3" aria-hidden="true"></a> <span class="kw">fn</span> generate_curry(parsed<span class="op">:</span> ItemFn) <span class="op">-></span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> |
406 | <span id="cb34-4"><a href="#cb34-4"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> | 406 | <span id="cb34-4"><a href="#cb34-4" aria-hidden="true"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> |
407 | <span id="cb34-5"><a href="#cb34-5"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> | 407 | <span id="cb34-5"><a href="#cb34-5" aria-hidden="true"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> |
408 | <span id="cb34-6"><a href="#cb34-6"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> | 408 | <span id="cb34-6"><a href="#cb34-6" aria-hidden="true"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> |
409 | <span id="cb34-7"><a href="#cb34-7"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> | 409 | <span id="cb34-7"><a href="#cb34-7" aria-hidden="true"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> |
410 | <span id="cb34-8"><a href="#cb34-8"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> | 410 | <span id="cb34-8"><a href="#cb34-8" aria-hidden="true"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> |
411 | <span id="cb34-9"><a href="#cb34-9"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> | 411 | <span id="cb34-9"><a href="#cb34-9" aria-hidden="true"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> |
412 | <span id="cb34-10"><a href="#cb34-10"></a></span> | 412 | <span id="cb34-10"><a href="#cb34-10" aria-hidden="true"></a></span> |
413 | <span id="cb34-11"><a href="#cb34-11"></a> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> | 413 | <span id="cb34-11"><a href="#cb34-11" aria-hidden="true"></a> <span class="kw">let</span> arg_idents <span class="op">=</span> extract_arg_idents(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> |
414 | <span id="cb34-12"><a href="#cb34-12"></a> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> | 414 | <span id="cb34-12"><a href="#cb34-12" aria-hidden="true"></a> <span class="kw">let</span> first_ident <span class="op">=</span> <span class="op">&</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> |
415 | <span id="cb34-13"><a href="#cb34-13"></a> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> | 415 | <span id="cb34-13"><a href="#cb34-13" aria-hidden="true"></a> <span class="kw">let</span> curried_body <span class="op">=</span> generate_body(<span class="op">&</span>arg_idents[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span> fn_body<span class="op">.</span>clone())<span class="op">;</span></span> |
416 | <span id="cb34-14"><a href="#cb34-14"></a></span> | 416 | <span id="cb34-14"><a href="#cb34-14" aria-hidden="true"></a></span> |
417 | <span id="cb34-15"><a href="#cb34-15"></a><span class="op">+</span> <span class="kw">let</span> arg_types <span class="op">=</span> extract_arg_types(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> | 417 | <span id="cb34-15"><a href="#cb34-15" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> arg_types <span class="op">=</span> extract_arg_types(fn_args<span class="op">.</span>clone())<span class="op">;</span></span> |
418 | <span id="cb34-16"><a href="#cb34-16"></a><span class="op">+</span> <span class="kw">let</span> first_type <span class="op">=</span> <span class="op">&</span>arg_types<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> | 418 | <span id="cb34-16"><a href="#cb34-16" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> first_type <span class="op">=</span> <span class="op">&</span>arg_types<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> |
419 | <span id="cb34-17"><a href="#cb34-17"></a><span class="op">+</span> <span class="kw">let</span> type_aliases <span class="op">=</span> generate_type_aliases(</span> | 419 | <span id="cb34-17"><a href="#cb34-17" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> type_aliases <span class="op">=</span> generate_type_aliases(</span> |
420 | <span id="cb34-18"><a href="#cb34-18"></a><span class="op">+</span> <span class="op">&</span>arg_types[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span></span> | 420 | <span id="cb34-18"><a href="#cb34-18" aria-hidden="true"></a><span class="op">+</span> <span class="op">&</span>arg_types[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span></span> |
421 | <span id="cb34-19"><a href="#cb34-19"></a><span class="op">+</span> extract_return_type(fn_return_type)<span class="op">,</span></span> | 421 | <span id="cb34-19"><a href="#cb34-19" aria-hidden="true"></a><span class="op">+</span> extract_return_type(fn_return_type)<span class="op">,</span></span> |
422 | <span id="cb34-20"><a href="#cb34-20"></a><span class="op">+</span> <span class="op">&</span>fn_name<span class="op">,</span></span> | 422 | <span id="cb34-20"><a href="#cb34-20" aria-hidden="true"></a><span class="op">+</span> <span class="op">&</span>fn_name<span class="op">,</span></span> |
423 | <span id="cb34-21"><a href="#cb34-21"></a><span class="op">+</span> )<span class="op">;</span></span> | 423 | <span id="cb34-21"><a href="#cb34-21" aria-hidden="true"></a><span class="op">+</span> )<span class="op">;</span></span> |
424 | <span id="cb34-22"><a href="#cb34-22"></a></span> | 424 | <span id="cb34-22"><a href="#cb34-22" aria-hidden="true"></a></span> |
425 | <span id="cb34-23"><a href="#cb34-23"></a><span class="op">+</span> <span class="kw">let</span> return_type <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> <span class="op">&</span>fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> type_aliases<span class="op">.</span>len() <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> | 425 | <span id="cb34-23"><a href="#cb34-23" aria-hidden="true"></a><span class="op">+</span> <span class="kw">let</span> return_type <span class="op">=</span> <span class="pp">format_ident!</span>(<span class="st">"_{}_{}"</span><span class="op">,</span> <span class="op">&</span>fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">"T{}"</span><span class="op">,</span> type_aliases<span class="op">.</span>len() <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> |
426 | <span id="cb34-24"><a href="#cb34-24"></a></span> | 426 | <span id="cb34-24"><a href="#cb34-24" aria-hidden="true"></a></span> |
427 | <span id="cb34-25"><a href="#cb34-25"></a><span class="op">+</span> <span class="kw">return</span> <span class="pp">quote!</span> <span class="op">{</span></span> | 427 | <span id="cb34-25"><a href="#cb34-25" aria-hidden="true"></a><span class="op">+</span> <span class="kw">return</span> <span class="pp">quote!</span> <span class="op">{</span></span> |
428 | <span id="cb34-26"><a href="#cb34-26"></a><span class="op">+</span> #(#type_aliases)<span class="op">;*</span> <span class="op">;</span></span> | 428 | <span id="cb34-26"><a href="#cb34-26" aria-hidden="true"></a><span class="op">+</span> #(#type_aliases)<span class="op">;*</span> <span class="op">;</span></span> |
429 | <span id="cb34-27"><a href="#cb34-27"></a><span class="op">+</span> #vis <span class="kw">fn</span> #fn_name (#first_ident<span class="op">:</span> #first_type) <span class="op">-></span> #return_type <span class="op">{</span></span> | 429 | <span id="cb34-27"><a href="#cb34-27" aria-hidden="true"></a><span class="op">+</span> #vis <span class="kw">fn</span> #fn_name (#first_ident<span class="op">:</span> #first_type) <span class="op">-></span> #return_type <span class="op">{</span></span> |
430 | <span id="cb34-28"><a href="#cb34-28"></a><span class="op">+</span> #curried_body <span class="op">;</span></span> | 430 | <span id="cb34-28"><a href="#cb34-28" aria-hidden="true"></a><span class="op">+</span> #curried_body <span class="op">;</span></span> |
431 | <span id="cb34-29"><a href="#cb34-29"></a><span class="op">+</span> <span class="op">}</span></span> | 431 | <span id="cb34-29"><a href="#cb34-29" aria-hidden="true"></a><span class="op">+</span> <span class="op">}</span></span> |
432 | <span id="cb34-30"><a href="#cb34-30"></a><span class="op">+</span> <span class="op">};</span></span> | 432 | <span id="cb34-30"><a href="#cb34-30" aria-hidden="true"></a><span class="op">+</span> <span class="op">};</span></span> |
433 | <span id="cb34-31"><a href="#cb34-31"></a> <span class="op">}</span></span></code></pre></div> | 433 | <span id="cb34-31"><a href="#cb34-31" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> |
434 | <p>Most of the additions are self explanatory, I’ll go through the return statement with you. We are returning a <code>quote!{ ... }</code>, so a <code>proc_macro2::TokenStream</code>. We are iterating through the <code>type_aliases</code> variable, which you might recall, is a <code>Vec<TokenStream></code>. You might notice the sneaky semicolon before the <code>*</code>. This basically tells <code>quote</code>, to insert an item, then a semicolon, and then the next one, another semicolon, and so on. The semicolon is a separator. We need to manually insert another semicolon at the end of it all, <code>quote</code> doesn’t insert a separator at the end of the iteration.</p> | 434 | <p>Most of the additions are self explanatory, I’ll go through the return statement with you. We are returning a <code>quote!{ ... }</code>, so a <code>proc_macro2::TokenStream</code>. We are iterating through the <code>type_aliases</code> variable, which you might recall, is a <code>Vec<TokenStream></code>. You might notice the sneaky semicolon before the <code>*</code>. This basically tells <code>quote</code>, to insert an item, then a semicolon, and then the next one, another semicolon, and so on. The semicolon is a separator. We need to manually insert another semicolon at the end of it all, <code>quote</code> doesn’t insert a separator at the end of the iteration.</p> |
435 | <p>We retain the visibility and name of our original function. Our curried function takes as args, just the first argument of our original function. The return type of our curried function is actually, the last type alias we create. If you think back to our manually curried <code>add</code> function, we returned <code>T2</code>, which was in fact, the last type alias we created.</p> | 435 | <p>We retain the visibility and name of our original function. Our curried function takes as args, just the first argument of our original function. The return type of our curried function is actually, the last type alias we create. If you think back to our manually curried <code>add</code> function, we returned <code>T2</code>, which was in fact, the last type alias we created.</p> |
436 | <p>I am sure, at this point, you are itching to test this out, but before that, let me introduce you to some good methods of debugging proc-macro code.</p> | 436 | <p>I am sure, at this point, you are itching to test this out, but before that, let me introduce you to some good methods of debugging proc-macro code.</p> |
@@ -462,57 +462,57 @@ fn main() { | |||
462 | <p>Writing proc-macros without <code>cargo-expand</code> is tantamount to driving a vehicle without rear view mirrors! Keep an eye on what is going on behind your back.</p> | 462 | <p>Writing proc-macros without <code>cargo-expand</code> is tantamount to driving a vehicle without rear view mirrors! Keep an eye on what is going on behind your back.</p> |
463 | <p>Now, your macro won’t always compile, you might just recieve the bee movie script as an error. <code>cargo-expand</code> will not work in such cases. I would suggest printing out your variables to inspect them. <code>TokenStream</code> implements <code>Display</code> as well as <code>Debug</code>. We don’t always have to be respectable programmers. Just print it.</p> | 463 | <p>Now, your macro won’t always compile, you might just recieve the bee movie script as an error. <code>cargo-expand</code> will not work in such cases. I would suggest printing out your variables to inspect them. <code>TokenStream</code> implements <code>Display</code> as well as <code>Debug</code>. We don’t always have to be respectable programmers. Just print it.</p> |
464 | <p>Enough of that, lets get testing:</p> | 464 | <p>Enough of that, lets get testing:</p> |
465 | <div class="sourceCode" id="cb37"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb37-1"><a href="#cb37-1"></a><span class="co">// tests/smoke.rs</span></span> | 465 | <div class="sourceCode" id="cb37"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true"></a><span class="co">// tests/smoke.rs</span></span> |
466 | <span id="cb37-2"><a href="#cb37-2"></a></span> | 466 | <span id="cb37-2"><a href="#cb37-2" aria-hidden="true"></a></span> |
467 | <span id="cb37-3"><a href="#cb37-3"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span></span> | 467 | <span id="cb37-3"><a href="#cb37-3" aria-hidden="true"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span></span> |
468 | <span id="cb37-4"><a href="#cb37-4"></a></span> | 468 | <span id="cb37-4"><a href="#cb37-4" aria-hidden="true"></a></span> |
469 | <span id="cb37-5"><a href="#cb37-5"></a><span class="at">#[</span><span class="pp">crate_name::</span>curry<span class="at">]</span></span> | 469 | <span id="cb37-5"><a href="#cb37-5" aria-hidden="true"></a><span class="at">#[</span><span class="pp">crate_name::</span>curry<span class="at">]</span></span> |
470 | <span id="cb37-6"><a href="#cb37-6"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 470 | <span id="cb37-6"><a href="#cb37-6" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
471 | <span id="cb37-7"><a href="#cb37-7"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> | 471 | <span id="cb37-7"><a href="#cb37-7" aria-hidden="true"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> |
472 | <span id="cb37-8"><a href="#cb37-8"></a><span class="op">}</span></span> | 472 | <span id="cb37-8"><a href="#cb37-8" aria-hidden="true"></a><span class="op">}</span></span> |
473 | <span id="cb37-9"><a href="#cb37-9"></a></span> | 473 | <span id="cb37-9"><a href="#cb37-9" aria-hidden="true"></a></span> |
474 | <span id="cb37-10"><a href="#cb37-10"></a><span class="at">#[</span>test<span class="at">]</span></span> | 474 | <span id="cb37-10"><a href="#cb37-10" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> |
475 | <span id="cb37-11"><a href="#cb37-11"></a><span class="kw">fn</span> works() <span class="op">{</span></span> | 475 | <span id="cb37-11"><a href="#cb37-11" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> |
476 | <span id="cb37-12"><a href="#cb37-12"></a> <span class="pp">assert_eq!</span>(<span class="dv">15</span><span class="op">,</span> add(<span class="dv">4</span>)(<span class="dv">5</span>)(<span class="dv">6</span>))<span class="op">;</span></span> | 476 | <span id="cb37-12"><a href="#cb37-12" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(<span class="dv">15</span><span class="op">,</span> add(<span class="dv">4</span>)(<span class="dv">5</span>)(<span class="dv">6</span>))<span class="op">;</span></span> |
477 | <span id="cb37-13"><a href="#cb37-13"></a><span class="op">}</span></span></code></pre></div> | 477 | <span id="cb37-13"><a href="#cb37-13" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
478 | <p>Run <code>cargo +nightly test</code>. You should see a pleasing message:</p> | 478 | <p>Run <code>cargo +nightly test</code>. You should see a pleasing message:</p> |
479 | <pre><code>running 1 test | 479 | <pre><code>running 1 test |
480 | test tests::works ... ok</code></pre> | 480 | test tests::works ... ok</code></pre> |
481 | <p>Take a look at the expansion for our curry macro, via <code>cargo +nightly expand --tests smoke</code>:</p> | 481 | <p>Take a look at the expansion for our curry macro, via <code>cargo +nightly expand --tests smoke</code>:</p> |
482 | <div class="sourceCode" id="cb39"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb39-1"><a href="#cb39-1"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> | 482 | <div class="sourceCode" id="cb39"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> |
483 | <span id="cb39-2"><a href="#cb39-2"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> | 483 | <span id="cb39-2"><a href="#cb39-2" aria-hidden="true"></a><span class="kw">type</span> _add_T1 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T0<span class="op">;</span></span> |
484 | <span id="cb39-3"><a href="#cb39-3"></a><span class="kw">type</span> _add_T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T1<span class="op">;</span></span> | 484 | <span id="cb39-3"><a href="#cb39-3" aria-hidden="true"></a><span class="kw">type</span> _add_T2 <span class="op">=</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-></span> _add_T1<span class="op">;</span></span> |
485 | <span id="cb39-4"><a href="#cb39-4"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> _add_T2 <span class="op">{</span></span> | 485 | <span id="cb39-4"><a href="#cb39-4" aria-hidden="true"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> _add_T2 <span class="op">{</span></span> |
486 | <span id="cb39-5"><a href="#cb39-5"></a> <span class="kw">return</span> (<span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="op">{</span></span> | 486 | <span id="cb39-5"><a href="#cb39-5" aria-hidden="true"></a> <span class="kw">return</span> (<span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="op">{</span></span> |
487 | <span id="cb39-6"><a href="#cb39-6"></a> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> <span class="op">{</span></span> | 487 | <span id="cb39-6"><a href="#cb39-6" aria-hidden="true"></a> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> <span class="op">{</span></span> |
488 | <span id="cb39-7"><a href="#cb39-7"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> | 488 | <span id="cb39-7"><a href="#cb39-7" aria-hidden="true"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> |
489 | <span id="cb39-8"><a href="#cb39-8"></a> <span class="op">}</span></span> | 489 | <span id="cb39-8"><a href="#cb39-8" aria-hidden="true"></a> <span class="op">}</span></span> |
490 | <span id="cb39-9"><a href="#cb39-9"></a> <span class="op">}</span>)<span class="op">;</span></span> | 490 | <span id="cb39-9"><a href="#cb39-9" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> |
491 | <span id="cb39-10"><a href="#cb39-10"></a><span class="op">}</span></span> | 491 | <span id="cb39-10"><a href="#cb39-10" aria-hidden="true"></a><span class="op">}</span></span> |
492 | <span id="cb39-11"><a href="#cb39-11"></a></span> | 492 | <span id="cb39-11"><a href="#cb39-11" aria-hidden="true"></a></span> |
493 | <span id="cb39-12"><a href="#cb39-12"></a><span class="co">// a bunch of other stuff generated by #[test] and assert_eq!</span></span></code></pre></div> | 493 | <span id="cb39-12"><a href="#cb39-12" aria-hidden="true"></a><span class="co">// a bunch of other stuff generated by #[test] and assert_eq!</span></span></code></pre></div> |
494 | <p>A sight for sore eyes.</p> | 494 | <p>A sight for sore eyes.</p> |
495 | <p>Here is a more complex example that generates ten multiples of the first ten natural numbers:</p> | 495 | <p>Here is a more complex example that generates ten multiples of the first ten natural numbers:</p> |
496 | <div class="sourceCode" id="cb40"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb40-1"><a href="#cb40-1"></a><span class="at">#[</span>curry<span class="at">]</span></span> | 496 | <div class="sourceCode" id="cb40"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> |
497 | <span id="cb40-2"><a href="#cb40-2"></a><span class="kw">fn</span> product(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> | 497 | <span id="cb40-2"><a href="#cb40-2" aria-hidden="true"></a><span class="kw">fn</span> product(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-></span> <span class="dt">u32</span> <span class="op">{</span></span> |
498 | <span id="cb40-3"><a href="#cb40-3"></a> x <span class="op">*</span> y</span> | 498 | <span id="cb40-3"><a href="#cb40-3" aria-hidden="true"></a> x <span class="op">*</span> y</span> |
499 | <span id="cb40-4"><a href="#cb40-4"></a><span class="op">}</span></span> | 499 | <span id="cb40-4"><a href="#cb40-4" aria-hidden="true"></a><span class="op">}</span></span> |
500 | <span id="cb40-5"><a href="#cb40-5"></a></span> | 500 | <span id="cb40-5"><a href="#cb40-5" aria-hidden="true"></a></span> |
501 | <span id="cb40-6"><a href="#cb40-6"></a><span class="kw">fn</span> multiples() <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span><span class="dt">u32</span><span class="op">>>{</span></span> | 501 | <span id="cb40-6"><a href="#cb40-6" aria-hidden="true"></a><span class="kw">fn</span> multiples() <span class="op">-></span> <span class="dt">Vec</span><span class="op"><</span><span class="dt">Vec</span><span class="op"><</span><span class="dt">u32</span><span class="op">>>{</span></span> |
502 | <span id="cb40-7"><a href="#cb40-7"></a> <span class="kw">let</span> v <span class="op">=</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)<span class="op">.</span>map(product)<span class="op">;</span></span> | 502 | <span id="cb40-7"><a href="#cb40-7" aria-hidden="true"></a> <span class="kw">let</span> v <span class="op">=</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)<span class="op">.</span>map(product)<span class="op">;</span></span> |
503 | <span id="cb40-8"><a href="#cb40-8"></a> <span class="kw">return</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)</span> | 503 | <span id="cb40-8"><a href="#cb40-8" aria-hidden="true"></a> <span class="kw">return</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)</span> |
504 | <span id="cb40-9"><a href="#cb40-9"></a> <span class="op">.</span>map(<span class="op">|</span>x<span class="op">|</span> v<span class="op">.</span>clone()<span class="op">.</span>map(<span class="op">|</span>f<span class="op">|</span> f(x))<span class="op">.</span>collect())</span> | 504 | <span id="cb40-9"><a href="#cb40-9" aria-hidden="true"></a> <span class="op">.</span>map(<span class="op">|</span>x<span class="op">|</span> v<span class="op">.</span>clone()<span class="op">.</span>map(<span class="op">|</span>f<span class="op">|</span> f(x))<span class="op">.</span>collect())</span> |
505 | <span id="cb40-10"><a href="#cb40-10"></a> <span class="op">.</span>collect()<span class="op">;</span></span> | 505 | <span id="cb40-10"><a href="#cb40-10" aria-hidden="true"></a> <span class="op">.</span>collect()<span class="op">;</span></span> |
506 | <span id="cb40-11"><a href="#cb40-11"></a><span class="op">}</span></span></code></pre></div> | 506 | <span id="cb40-11"><a href="#cb40-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> |
507 | <h3 id="notes">Notes</h3> | 507 | <h3 id="notes">Notes</h3> |
508 | <p>I didn’t quite explain why we use <code>move |arg|</code> in our closure. This is because we want to take ownership of the variable supplied to us. Take a look at this example:</p> | 508 | <p>I didn’t quite explain why we use <code>move |arg|</code> in our closure. This is because we want to take ownership of the variable supplied to us. Take a look at this example:</p> |
509 | <div class="sourceCode" id="cb41"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb41-1"><a href="#cb41-1"></a><span class="kw">let</span> v <span class="op">=</span> add(<span class="dv">5</span>)<span class="op">;</span></span> | 509 | <div class="sourceCode" id="cb41"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true"></a><span class="kw">let</span> v <span class="op">=</span> add(<span class="dv">5</span>)<span class="op">;</span></span> |
510 | <span id="cb41-2"><a href="#cb41-2"></a><span class="kw">let</span> g<span class="op">;</span></span> | 510 | <span id="cb41-2"><a href="#cb41-2" aria-hidden="true"></a><span class="kw">let</span> g<span class="op">;</span></span> |
511 | <span id="cb41-3"><a href="#cb41-3"></a><span class="op">{</span></span> | 511 | <span id="cb41-3"><a href="#cb41-3" aria-hidden="true"></a><span class="op">{</span></span> |
512 | <span id="cb41-4"><a href="#cb41-4"></a> <span class="kw">let</span> x <span class="op">=</span> <span class="dv">5</span><span class="op">;</span></span> | 512 | <span id="cb41-4"><a href="#cb41-4" aria-hidden="true"></a> <span class="kw">let</span> x <span class="op">=</span> <span class="dv">5</span><span class="op">;</span></span> |
513 | <span id="cb41-5"><a href="#cb41-5"></a> g <span class="op">=</span> v(x)<span class="op">;</span></span> | 513 | <span id="cb41-5"><a href="#cb41-5" aria-hidden="true"></a> g <span class="op">=</span> v(x)<span class="op">;</span></span> |
514 | <span id="cb41-6"><a href="#cb41-6"></a><span class="op">}</span></span> | 514 | <span id="cb41-6"><a href="#cb41-6" aria-hidden="true"></a><span class="op">}</span></span> |
515 | <span id="cb41-7"><a href="#cb41-7"></a><span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> g(<span class="dv">2</span>))<span class="op">;</span></span></code></pre></div> | 515 | <span id="cb41-7"><a href="#cb41-7" aria-hidden="true"></a><span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> g(<span class="dv">2</span>))<span class="op">;</span></span></code></pre></div> |
516 | <p>Variable <code>x</code> goes out of scope before <code>g</code> can return a concrete value. If we take ownership of <code>x</code> by <code>move</code>ing it into our closure, we can expect this to work reliably. In fact, rustc understands this, and forces you to use <code>move</code>.</p> | 516 | <p>Variable <code>x</code> goes out of scope before <code>g</code> can return a concrete value. If we take ownership of <code>x</code> by <code>move</code>ing it into our closure, we can expect this to work reliably. In fact, rustc understands this, and forces you to use <code>move</code>.</p> |
517 | <p>This usage of <code>move</code> is exactly why <strong>a curried function without a return is useless</strong>. Every variable we pass to our curried function gets moved into its local scope. Playing with these variables cannot cause a change outside this scope. Returning is our only method of interaction with anything beyond this function.</p> | 517 | <p>This usage of <code>move</code> is exactly why <strong>a curried function without a return is useless</strong>. Every variable we pass to our curried function gets moved into its local scope. Playing with these variables cannot cause a change outside this scope. Returning is our only method of interaction with anything beyond this function.</p> |
518 | <h3 id="conclusion">Conclusion</h3> | 518 | <h3 id="conclusion">Conclusion</h3> |
diff --git a/docs/posts/rapid_refactoring_with_vim/index.html b/docs/posts/rapid_refactoring_with_vim/index.html index c5bc188..ee05dc0 100644 --- a/docs/posts/rapid_refactoring_with_vim/index.html +++ b/docs/posts/rapid_refactoring_with_vim/index.html | |||
@@ -40,34 +40,34 @@ | |||
40 | <div class="post-text"> | 40 | <div class="post-text"> |
41 | <p>Last weekend, I was tasked with refactoring the 96 unit tests on <a href="https://github.com/ruma/ruma-events/pull/70">ruma-events</a> to use strictly typed json objects using <code>serde_json::json!</code> instead of raw strings. It was rather painless thanks to vim :)</p> | 41 | <p>Last weekend, I was tasked with refactoring the 96 unit tests on <a href="https://github.com/ruma/ruma-events/pull/70">ruma-events</a> to use strictly typed json objects using <code>serde_json::json!</code> instead of raw strings. It was rather painless thanks to vim :)</p> |
42 | <p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> | 42 | <p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> |
43 | <div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_str<span class="op">};</span></span> | 43 | <div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_str<span class="op">};</span></span> |
44 | <span id="cb1-2"><a href="#cb1-2"></a> </span> | 44 | <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a> </span> |
45 | <span id="cb1-3"><a href="#cb1-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> | 45 | <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> |
46 | <span id="cb1-4"><a href="#cb1-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> | 46 | <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> |
47 | <span id="cb1-5"><a href="#cb1-5"></a> <span class="pp">assert_eq!</span>(</span> | 47 | <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> |
48 | <span id="cb1-6"><a href="#cb1-6"></a>→ <span class="pp">from_str::</span><span class="op"><</span>Action<span class="op">></span>(<span class="st">r#"{"set_tweak": "highlight"}"#</span>)<span class="op">,</span></span> | 48 | <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a>→ <span class="pp">from_str::</span><span class="op"><</span>Action<span class="op">></span>(<span class="st">r#"{"set_tweak": "highlight"}"#),</span></span> |
49 | <span id="cb1-7"><a href="#cb1-7"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> | 49 | <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a><span class="st"> Action::SetTweak(Tweak::Highlight { value: true })</span></span> |
50 | <span id="cb1-8"><a href="#cb1-8"></a> )<span class="op">;</span></span> | 50 | <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="st"> );</span></span> |
51 | <span id="cb1-9"><a href="#cb1-9"></a> <span class="op">}</span></span></code></pre></div> | 51 | <span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a><span class="st"> }</span></span></code></pre></div> |
52 | <p>had to be converted to:</p> | 52 | <p>had to be converted to:</p> |
53 | <div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_value<span class="op">};</span></span> | 53 | <div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_value<span class="op">};</span></span> |
54 | <span id="cb2-2"><a href="#cb2-2"></a> </span> | 54 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> </span> |
55 | <span id="cb2-3"><a href="#cb2-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> | 55 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> |
56 | <span id="cb2-4"><a href="#cb2-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> | 56 | <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> |
57 | <span id="cb2-5"><a href="#cb2-5"></a> <span class="pp">assert_eq!</span>(</span> | 57 | <span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> |
58 | <span id="cb2-6"><a href="#cb2-6"></a>→ <span class="pp">from_value::</span><span class="op"><</span>Action<span class="op">></span>(<span class="pp">json!</span>(<span class="op">{</span><span class="st">"set_tweak"</span><span class="op">:</span> <span class="st">"highlight"</span><span class="op">}</span>))<span class="op">,</span></span> | 58 | <span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a>→ <span class="pp">from_value::</span><span class="op"><</span>Action<span class="op">></span>(<span class="pp">json!</span>(<span class="op">{</span><span class="st">"set_tweak"</span><span class="op">:</span> <span class="st">"highlight"</span><span class="op">}</span>))<span class="op">,</span></span> |
59 | <span id="cb2-7"><a href="#cb2-7"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> | 59 | <span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> |
60 | <span id="cb2-8"><a href="#cb2-8"></a> )<span class="op">;</span></span> | 60 | <span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a> )<span class="op">;</span></span> |
61 | <span id="cb2-9"><a href="#cb2-9"></a> <span class="op">}</span></span></code></pre></div> | 61 | <span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> |
62 | <h3 id="the-arglist">The arglist</h3> | 62 | <h3 id="the-arglist">The arglist</h3> |
63 | <p>For the initial pass, I decided to handle imports, this was a simple find and replace operation, done to all the files containing tests. Luckily, modules (and therefore files) containing tests in Rust are annotated with the <code>#[cfg(test)]</code> attribute. I opened all such files:</p> | 63 | <p>For the initial pass, I decided to handle imports, this was a simple find and replace operation, done to all the files containing tests. Luckily, modules (and therefore files) containing tests in Rust are annotated with the <code>#[cfg(test)]</code> attribute. I opened all such files:</p> |
64 | <div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1"></a><span class="co"># `grep -l pattern files` lists all the files</span></span> | 64 | <div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="co"># `grep -l pattern files` lists all the files</span></span> |
65 | <span id="cb3-2"><a href="#cb3-2"></a><span class="co"># matching the pattern</span></span> | 65 | <span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a><span class="co"># matching the pattern</span></span> |
66 | <span id="cb3-3"><a href="#cb3-3"></a></span> | 66 | <span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a></span> |
67 | <span id="cb3-4"><a href="#cb3-4"></a><span class="ex">vim</span> <span class="va">$(</span><span class="fu">grep</span> -l <span class="st">'cfg\(test\)'</span> ./**/*.rs<span class="va">)</span></span> | 67 | <span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a><span class="ex">vim</span> <span class="va">$(</span><span class="fu">grep</span> -l <span class="st">'cfg\(test\)'</span> ./**/*.rs<span class="va">)</span></span> |
68 | <span id="cb3-5"><a href="#cb3-5"></a></span> | 68 | <span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a></span> |
69 | <span id="cb3-6"><a href="#cb3-6"></a><span class="co"># expands to something like:</span></span> | 69 | <span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a><span class="co"># expands to something like:</span></span> |
70 | <span id="cb3-7"><a href="#cb3-7"></a><span class="ex">vim</span> push_rules.rs room/member.rs key/verification/lib.rs</span></code></pre></div> | 70 | <span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a><span class="ex">vim</span> push_rules.rs room/member.rs key/verification/lib.rs</span></code></pre></div> |
71 | <p>Starting vim with more than one file at the shell prompt populates the arglist. Hit <code>:args</code> to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with <code>:next</code> and <code>:prev</code>. I use tpope’s vim-unimpaired <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>, which adds <code>]a</code> and <code>[a</code>, mapped to <code>:next</code> and <code>:prev</code>.</p> | 71 | <p>Starting vim with more than one file at the shell prompt populates the arglist. Hit <code>:args</code> to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with <code>:next</code> and <code>:prev</code>. I use tpope’s vim-unimpaired <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>, which adds <code>]a</code> and <code>[a</code>, mapped to <code>:next</code> and <code>:prev</code>.</p> |
72 | <p>All that’s left to do is the find and replace, for which we will be using vim’s <code>argdo</code>, applying a substitution to every file in the arglist:</p> | 72 | <p>All that’s left to do is the find and replace, for which we will be using vim’s <code>argdo</code>, applying a substitution to every file in the arglist:</p> |
73 | <pre><code>:argdo s/from_str/from_value/g</code></pre> | 73 | <pre><code>:argdo s/from_str/from_value/g</code></pre> |
diff --git a/docs/posts/static_sites_with_bash/index.html b/docs/posts/static_sites_with_bash/index.html index 67eaf12..b2a90a7 100644 --- a/docs/posts/static_sites_with_bash/index.html +++ b/docs/posts/static_sites_with_bash/index.html | |||
@@ -43,26 +43,26 @@ | |||
43 | <p>I chose to write in markdown, and convert to html with <a href="https://kristaps.bsd.lv/lowdown/">lowdown</a>.</p> | 43 | <p>I chose to write in markdown, and convert to html with <a href="https://kristaps.bsd.lv/lowdown/">lowdown</a>.</p> |
44 | <h3 id="directory-structure">Directory structure</h3> | 44 | <h3 id="directory-structure">Directory structure</h3> |
45 | <p>I host my site on GitHub pages, so <code>docs/</code> has to be the entry point. Markdown formatted posts go into <code>posts/</code>, get converted into html, and end up in <code>docs/index.html</code>, something like this:</p> | 45 | <p>I host my site on GitHub pages, so <code>docs/</code> has to be the entry point. Markdown formatted posts go into <code>posts/</code>, get converted into html, and end up in <code>docs/index.html</code>, something like this:</p> |
46 | <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1"></a><span class="va">posts=$(</span><span class="fu">ls</span> -t ./posts<span class="va">)</span> <span class="co"># chronological order!</span></span> | 46 | <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="va">posts=$(</span><span class="fu">ls</span> -t ./posts<span class="va">)</span> <span class="co"># chronological order!</span></span> |
47 | <span id="cb1-2"><a href="#cb1-2"></a><span class="kw">for</span> <span class="ex">f</span> in <span class="va">$posts</span><span class="kw">;</span> <span class="kw">do</span></span> | 47 | <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a><span class="kw">for</span> <span class="ex">f</span> in <span class="va">$posts</span><span class="kw">;</span> <span class="kw">do</span></span> |
48 | <span id="cb1-3"><a href="#cb1-3"></a> <span class="va">file=</span><span class="st">"./posts/"</span><span class="va">$f</span> <span class="co"># `ls` mangled our file paths</span></span> | 48 | <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="va">file=</span><span class="st">"./posts/"</span><span class="va">$f</span> <span class="co"># `ls` mangled our file paths</span></span> |
49 | <span id="cb1-4"><a href="#cb1-4"></a> <span class="bu">echo</span> <span class="st">"generating post </span><span class="va">$file</span><span class="st">"</span></span> | 49 | <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="bu">echo</span> <span class="st">"generating post </span><span class="va">$file</span><span class="st">"</span></span> |
50 | <span id="cb1-5"><a href="#cb1-5"></a></span> | 50 | <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a></span> |
51 | <span id="cb1-6"><a href="#cb1-6"></a> <span class="va">html=$(</span><span class="ex">lowdown</span> <span class="st">"</span><span class="va">$file</span><span class="st">"</span><span class="va">)</span></span> | 51 | <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a> <span class="va">html=$(</span><span class="ex">lowdown</span> <span class="st">"</span><span class="va">$file</span><span class="st">"</span><span class="va">)</span></span> |
52 | <span id="cb1-7"><a href="#cb1-7"></a> <span class="bu">echo</span> -e <span class="st">"html"</span> <span class="op">>></span> docs/index.html</span> | 52 | <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a> <span class="bu">echo</span> -e <span class="st">"html"</span> <span class="op">>></span> docs/index.html</span> |
53 | <span id="cb1-8"><a href="#cb1-8"></a><span class="kw">done</span></span></code></pre></div> | 53 | <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="kw">done</span></span></code></pre></div> |
54 | <h3 id="assets">Assets</h3> | 54 | <h3 id="assets">Assets</h3> |
55 | <p>Most static site generators recommend dropping image assets into the site source itself. That does have it’s merits, but I prefer hosting images separately:</p> | 55 | <p>Most static site generators recommend dropping image assets into the site source itself. That does have it’s merits, but I prefer hosting images separately:</p> |
56 | <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1"></a><span class="co"># strip file extension</span></span> | 56 | <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a><span class="co"># strip file extension</span></span> |
57 | <span id="cb2-2"><a href="#cb2-2"></a><span class="va">ext=</span><span class="st">"</span><span class="va">${1##</span>*.<span class="va">}</span><span class="st">"</span></span> | 57 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a><span class="va">ext=</span><span class="st">"</span><span class="va">${1##</span>*.<span class="va">}</span><span class="st">"</span></span> |
58 | <span id="cb2-3"><a href="#cb2-3"></a></span> | 58 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a></span> |
59 | <span id="cb2-4"><a href="#cb2-4"></a><span class="co"># generate a random file name</span></span> | 59 | <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a><span class="co"># generate a random file name</span></span> |
60 | <span id="cb2-5"><a href="#cb2-5"></a><span class="va">id=$(</span> <span class="fu">cat</span> /dev/urandom <span class="kw">|</span> <span class="fu">tr</span> -dc <span class="st">'a-zA-Z0-9'</span> <span class="kw">|</span> <span class="ex">fold</span> -w 2 <span class="kw">|</span> <span class="fu">head</span> -n 1 <span class="va">)</span></span> | 60 | <span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a><span class="va">id=$(</span> <span class="fu">cat</span> /dev/urandom <span class="kw">|</span> <span class="fu">tr</span> -dc <span class="st">'a-zA-Z0-9'</span> <span class="kw">|</span> <span class="ex">fold</span> -w 2 <span class="kw">|</span> <span class="fu">head</span> -n 1 <span class="va">)</span></span> |
61 | <span id="cb2-6"><a href="#cb2-6"></a><span class="va">id=</span><span class="st">"</span><span class="va">$id</span><span class="st">.</span><span class="va">$ext</span><span class="st">"</span></span> | 61 | <span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a><span class="va">id=</span><span class="st">"</span><span class="va">$id</span><span class="st">.</span><span class="va">$ext</span><span class="st">"</span></span> |
62 | <span id="cb2-7"><a href="#cb2-7"></a></span> | 62 | <span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a></span> |
63 | <span id="cb2-8"><a href="#cb2-8"></a><span class="co"># copy to my file host</span></span> | 63 | <span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a><span class="co"># copy to my file host</span></span> |
64 | <span id="cb2-9"><a href="#cb2-9"></a><span class="fu">scp</span> -P 443 <span class="st">"</span><span class="va">$1</span><span class="st">"</span> emerald:files/<span class="st">"</span><span class="va">$id</span><span class="st">"</span> </span> | 64 | <span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a><span class="fu">scp</span> -P 443 <span class="st">"</span><span class="va">$1</span><span class="st">"</span> emerald:files/<span class="st">"</span><span class="va">$id</span><span class="st">"</span> </span> |
65 | <span id="cb2-10"><a href="#cb2-10"></a><span class="bu">echo</span> <span class="st">"https://u.peppe.rs/</span><span class="va">$id</span><span class="st">"</span></span></code></pre></div> | 65 | <span id="cb2-10"><a href="#cb2-10" aria-hidden="true"></a><span class="bu">echo</span> <span class="st">"https://u.peppe.rs/</span><span class="va">$id</span><span class="st">"</span></span></code></pre></div> |
66 | <h3 id="templating">Templating</h3> | 66 | <h3 id="templating">Templating</h3> |
67 | <p><a href="https://github.com/NerdyPepper/site/blob/master/generate.sh"><code>generate.sh</code></a> brings the above bits and pieces together (with some extra cruft to avoid javascript). It uses <code>sed</code> to produce nice titles from the file names (removes underscores, title-case), and <code>date(1)</code> to add the date to each post listing!</p> | 67 | <p><a href="https://github.com/NerdyPepper/site/blob/master/generate.sh"><code>generate.sh</code></a> brings the above bits and pieces together (with some extra cruft to avoid javascript). It uses <code>sed</code> to produce nice titles from the file names (removes underscores, title-case), and <code>date(1)</code> to add the date to each post listing!</p> |
68 | 68 | ||
diff --git a/docs/posts/turing_complete_type_systems/index.html b/docs/posts/turing_complete_type_systems/index.html new file mode 100644 index 0000000..4a7e2b1 --- /dev/null +++ b/docs/posts/turing_complete_type_systems/index.html | |||
@@ -0,0 +1,73 @@ | |||
1 | <!DOCTYPE html> | ||
2 | <html lang="en"> | ||
3 | <head> | ||
4 | <link rel="stylesheet" href="/style.css"> | ||
5 | <link rel="stylesheet" href="/syntax.css"> | ||
6 | <meta charset="UTF-8"> | ||
7 | <meta name="viewport" content="initial-scale=1"> | ||
8 | <meta content="#ffffff" name="theme-color"> | ||
9 | <meta name="HandheldFriendly" content="true"> | ||
10 | <meta property="og:title" content="Turing Complete Type Systems"> | ||
11 | <meta property="og:type" content="website"> | ||
12 | <meta property="og:description" content="a static site {for, by, about} me "> | ||
13 | <meta property="og:url" content="https://peppe.rs"> | ||
14 | <link rel="icon" type="image/x-icon" href="/favicon.png"> | ||
15 | <title>Turing Complete Type Systems · peppe.rs</title> | ||
16 | <body> | ||
17 | <div class="posts"> | ||
18 | <div class="post"> | ||
19 | <a href="/" class="post-end-link">⟵ Back</a> | ||
20 | <a class="stats post-end-link" href="https://raw.githubusercontent.com/nerdypepper/site/master/posts/turing_complete_type_systems.md | ||
21 | ">View Raw</a> | ||
22 | <div class="separator"></div> | ||
23 | <div class="date"> | ||
24 | 18/06 — 2020 | ||
25 | <div class="stats"> | ||
26 | <span class="stats-number"> | ||
27 | 8.78 | ||
28 | </span> | ||
29 | <span class="stats-unit">cm</span> | ||
30 |   | ||
31 | <span class="stats-number"> | ||
32 | 0.8 | ||
33 | </span> | ||
34 | <span class="stats-unit">min</span> | ||
35 | </div> | ||
36 | </div> | ||
37 | <h1> | ||
38 | Turing Complete Type Systems | ||
39 | </h1> | ||
40 | <div class="post-text"> | ||
41 | <p>Rust’s type system is Turing complete:</p> | ||
42 | <ul> | ||
43 | <li><a href="https://github.com/doctorn/trait-eval/">FizzBuzz with Rust Traits</a></li> | ||
44 | <li><a href="https://github.com/Ashymad/fortraith">A Forth implementation with Rust Traits</a></li> | ||
45 | </ul> | ||
46 | <p>It is impossible to determine if a program written in a generally Turing complete system will ever stop. That is, it is impossible to write a program <code>f</code> that determines if a program <code>g</code>, where <code>g</code> is written in a Turing complete programming language, will ever halt. The <a href="https://en.wikipedia.org/wiki/Halting_problem">Halting Problem</a> is in fact, an <a href="https://en.wikipedia.org/wiki/Undecidable_problem">undecidable problem</a>.</p> | ||
47 | <p><em>How is any of this relevant?</em></p> | ||
48 | <p>Rust performs compile-time type inference. The type checker, in turn, compiles and infers types, I would describe it as a compiler inside a compiler. It is possible that <code>rustc</code> may never finish compiling your Rust program!</p> | ||
49 | <p>I understand that this post lacks content.</p> | ||
50 | |||
51 | </div> | ||
52 | |||
53 | <div class=intro> | ||
54 | Hi. | ||
55 | <div class=hot-links> | ||
56 | <a href=https://peppe.rs/index.xml class=feed-button>Subscribe</a> | ||
57 | <a href=https://liberapay.com/nerdypepper/donate class=donate-button>Donate</a> | ||
58 | </div> | ||
59 | <p>I'm Akshay, I go by nerd or nerdypepper on the internet.</p> | ||
60 | <p> | ||
61 | I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. | ||
62 | I write open-source stuff to pass time. I also design fonts: scientifica, curie. | ||
63 | </p> | ||
64 | <p>Send me a mail at [email protected] or a message at [email protected].</p> | ||
65 | </div> | ||
66 | |||
67 | <a href="/" class="post-end-link">⟵ Back</a> | ||
68 | <a class="stats post-end-link" href="https://raw.githubusercontent.com/nerdypepper/site/master/posts/turing_complete_type_systems.md | ||
69 | ">View Raw</a> | ||
70 | </div> | ||
71 | </div> | ||
72 | </body> | ||
73 | </html> | ||
diff --git a/posts/turing_complete_type_systems.md b/posts/turing_complete_type_systems.md new file mode 100644 index 0000000..b6ec7bc --- /dev/null +++ b/posts/turing_complete_type_systems.md | |||
@@ -0,0 +1,22 @@ | |||
1 | Rust's type system is Turing complete: | ||
2 | |||
3 | - [FizzBuzz with Rust Traits](https://github.com/doctorn/trait-eval/) | ||
4 | - [A Forth implementation with Rust Traits](https://github.com/Ashymad/fortraith) | ||
5 | |||
6 | It is impossible to determine if a program written in a | ||
7 | generally Turing complete system will ever stop. That is, it | ||
8 | is impossible to write a program `f` that determines if a | ||
9 | program `g`, where `g` is written in a Turing complete | ||
10 | programming language, will ever halt. The [Halting | ||
11 | Problem](https://en.wikipedia.org/wiki/Halting_problem) is | ||
12 | in fact, an [undecidable | ||
13 | problem](https://en.wikipedia.org/wiki/Undecidable_problem). | ||
14 | |||
15 | *How is any of this relevant?* | ||
16 | |||
17 | Rust performs compile-time type inference. The type checker, | ||
18 | in turn, compiles and infers types, I would describe it as a | ||
19 | compiler inside a compiler. It is possible that `rustc` may | ||
20 | never finish compiling your Rust program! | ||
21 | |||
22 | I understand that this post lacks content. | ||