aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-03 13:57:58 +0100
committerAkshay <[email protected]>2020-07-03 13:57:58 +0100
commitcc72d07ae82386e9f087517ccf2653e17c76714a (patch)
tree6d325c93d88ca90e3afbb09136015b5d8243fa68
parent2f2feaa73878c2ae4f5402a3946525c0752fd14a (diff)
escape html tags in rss feed
-rw-r--r--docs/index.xml1556
1 files changed, 778 insertions, 778 deletions
diff --git a/docs/index.xml b/docs/index.xml
index edf43f5..628c111 100644
--- a/docs/index.xml
+++ b/docs/index.xml
@@ -13,422 +13,422 @@
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> 15<title>Turing Complete Type Systems</title>
16<description><p>Rust’s type system is Turing complete:</p> 16<description>&lt;p&gt;Rust’s type system is Turing complete:&lt;/p&gt;
17<ul> 17&lt;ul&gt;
18<li><a href="https://github.com/doctorn/trait-eval/">FizzBuzz with Rust Traits</a></li> 18&lt;li&gt;&lt;a href="https://github.com/doctorn/trait-eval/"&gt;FizzBuzz with Rust Traits&lt;/a&gt;&lt;/li&gt;
19<li><a href="https://github.com/Ashymad/fortraith">A Forth implementation with Rust Traits</a></li> 19&lt;li&gt;&lt;a href="https://github.com/Ashymad/fortraith"&gt;A Forth implementation with Rust Traits&lt;/a&gt;&lt;/li&gt;
20</ul> 20&lt;/ul&gt;
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> 21&lt;p&gt;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 &lt;code&gt;f&lt;/code&gt; that determines if a program &lt;code&gt;g&lt;/code&gt;, where &lt;code&gt;g&lt;/code&gt; is written in a Turing complete programming language, will ever halt. The &lt;a href="https://en.wikipedia.org/wiki/Halting_problem"&gt;Halting Problem&lt;/a&gt; is in fact, an &lt;a href="https://en.wikipedia.org/wiki/Undecidable_problem"&gt;undecidable problem&lt;/a&gt;.&lt;/p&gt;
22<p><em>How is any of this relevant?</em></p> 22&lt;p&gt;&lt;em&gt;How is any of this relevant?&lt;/em&gt;&lt;/p&gt;
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! I lied, <code>rustc</code> stops after a while, after hitting the recursion limit.</p> 23&lt;p&gt;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 &lt;code&gt;rustc&lt;/code&gt; may never finish compiling your Rust program! I lied, &lt;code&gt;rustc&lt;/code&gt; stops after a while, after hitting the recursion limit.&lt;/p&gt;
24<p>I understand that this post lacks content.</p></description> 24&lt;p&gt;I understand that this post lacks content.&lt;/p&gt;</description>
25<link>https://peppe.rs/posts/turing_complete_type_systems/</link> 25<link>https://peppe.rs/posts/turing_complete_type_systems/</link>
26<pubDate>Thu, 18 Jun 2020 05:18:00 +0000</pubDate> 26<pubDate>Thu, 18 Jun 2020 05:18:00 +0000</pubDate>
27<guid>https://peppe.rs/posts/turing_complete_type_systems/</guid> 27<guid>https://peppe.rs/posts/turing_complete_type_systems/</guid>
28</item> 28</item>
29<item> 29<item>
30<title>Auto-currying Rust Functions</title> 30<title>Auto-currying Rust Functions</title>
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> 31<description>&lt;p&gt;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 &lt;a href="https://github.com/nerdypepper/cutlass"&gt;here&lt;/a&gt;. It is also available on &lt;a href="https://crates.io/crates/cutlass"&gt;crates.io&lt;/a&gt;.&lt;/p&gt;
32<p>The following links might prove to be useful before getting started:</p> 32&lt;p&gt;The following links might prove to be useful before getting started:&lt;/p&gt;
33<ul> 33&lt;ul&gt;
34<li><a href="https://doc.rust-lang.org/reference/procedural-macros.html">Procedural Macros</a></li> 34&lt;li&gt;&lt;a href="https://doc.rust-lang.org/reference/procedural-macros.html"&gt;Procedural Macros&lt;/a&gt;&lt;/li&gt;
35<li><a href="https://en.wikipedia.org/wiki/Currying">Currying</a></li> 35&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Currying"&gt;Currying&lt;/a&gt;&lt;/li&gt;
36</ul> 36&lt;/ul&gt;
37<p>Or you can pretend you read them, because I have included a primer here :)</p> 37&lt;p&gt;Or you can pretend you read them, because I have included a primer here :)&lt;/p&gt;
38<h3 id="contents">Contents</h3> 38&lt;h3 id="contents"&gt;Contents&lt;/h3&gt;
39<ol type="1"> 39&lt;ol type="1"&gt;
40<li><a href="#currying">Currying</a><br /> 40&lt;li&gt;&lt;a href="#currying"&gt;Currying&lt;/a&gt;&lt;br /&gt;
41</li> 41&lt;/li&gt;
42<li><a href="#procedural-macros">Procedural Macros</a><br /> 42&lt;li&gt;&lt;a href="#procedural-macros"&gt;Procedural Macros&lt;/a&gt;&lt;br /&gt;
43</li> 43&lt;/li&gt;
44<li><a href="#definitions">Definitions</a><br /> 44&lt;li&gt;&lt;a href="#definitions"&gt;Definitions&lt;/a&gt;&lt;br /&gt;
45</li> 45&lt;/li&gt;
46<li><a href="#refinement">Refinement</a><br /> 46&lt;li&gt;&lt;a href="#refinement"&gt;Refinement&lt;/a&gt;&lt;br /&gt;
47</li> 47&lt;/li&gt;
48<li><a href="#the-in-betweens">The In-betweens</a><br /> 48&lt;li&gt;&lt;a href="#the-in-betweens"&gt;The In-betweens&lt;/a&gt;&lt;br /&gt;
49     5.1 <a href="#dependencies">Dependencies</a><br /> 49     5.1 &lt;a href="#dependencies"&gt;Dependencies&lt;/a&gt;&lt;br /&gt;
50     5.2 <a href="#the-attribute-macro">The attribute macro</a><br /> 50     5.2 &lt;a href="#the-attribute-macro"&gt;The attribute macro&lt;/a&gt;&lt;br /&gt;
51     5.3 <a href="#function-body">Function Body</a><br /> 51     5.3 &lt;a href="#function-body"&gt;Function Body&lt;/a&gt;&lt;br /&gt;
52     5.4 <a href="#function-signature">Function Signature</a><br /> 52     5.4 &lt;a href="#function-signature"&gt;Function Signature&lt;/a&gt;&lt;br /&gt;
53     5.5 <a href="#getting-it-together">Getting it together</a><br /> 53     5.5 &lt;a href="#getting-it-together"&gt;Getting it together&lt;/a&gt;&lt;br /&gt;
54</li> 54&lt;/li&gt;
55<li><a href="#debugging-and-testing">Debugging and Testing</a><br /> 55&lt;li&gt;&lt;a href="#debugging-and-testing"&gt;Debugging and Testing&lt;/a&gt;&lt;br /&gt;
56</li> 56&lt;/li&gt;
57<li><a href="#notes">Notes</a><br /> 57&lt;li&gt;&lt;a href="#notes"&gt;Notes&lt;/a&gt;&lt;br /&gt;
58</li> 58&lt;/li&gt;
59<li><a href="#conclusion">Conclusion</a></li> 59&lt;li&gt;&lt;a href="#conclusion"&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
60</ol> 60&lt;/ol&gt;
61<h3 id="currying">Currying</h3> 61&lt;h3 id="currying"&gt;Currying&lt;/h3&gt;
62<p>Currying is the process of transformation of a function call like <code>f(a, b, c)</code> to <code>f(a)(b)(c)</code>. A curried function returns a concrete value only when it receives all its arguments! If it does recieve an insufficient amount of arguments, say 1 of 3, it returns a <em>curried function</em>, that returns after receiving 2 arguments.</p> 62&lt;p&gt;Currying is the process of transformation of a function call like &lt;code&gt;f(a, b, c)&lt;/code&gt; to &lt;code&gt;f(a)(b)(c)&lt;/code&gt;. A curried function returns a concrete value only when it receives all its arguments! If it does recieve an insufficient amount of arguments, say 1 of 3, it returns a &lt;em&gt;curried function&lt;/em&gt;, that returns after receiving 2 arguments.&lt;/p&gt;
63<pre><code>curry(f(a, b, c)) = h(a)(b)(c) 63&lt;pre&gt;&lt;code&gt;curry(f(a, b, c)) = h(a)(b)(c)
64 64
65h(x) = g &lt;- curried function that takes upto 2 args (g) 65h(x) = g &amp;lt;- curried function that takes upto 2 args (g)
66g(y) = k &lt;- curried function that takes upto 1 arg (k) 66g(y) = k &amp;lt;- curried function that takes upto 1 arg (k)
67k(z) = v &lt;- a value (v) 67k(z) = v &amp;lt;- a value (v)
68 68
69Keen readers will conclude the following, 69Keen readers will conclude the following,
70h(x)(y)(z) = g(y)(z) = k(z) = v</code></pre> 70h(x)(y)(z) = g(y)(z) = k(z) = v&lt;/code&gt;&lt;/pre&gt;
71<p>Mathematically, if <code>f</code> is a function that takes two arguments <code>x</code> and <code>y</code>, such that <code>x ϵ X</code>, and <code>y ϵ Y</code> , we write it as:</p> 71&lt;p&gt;Mathematically, if &lt;code&gt;f&lt;/code&gt; is a function that takes two arguments &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, such that &lt;code&gt;x ϵ X&lt;/code&gt;, and &lt;code&gt;y ϵ Y&lt;/code&gt; , we write it as:&lt;/p&gt;
72<pre><code>f: (X × Y) -&gt; Z</code></pre> 72&lt;pre&gt;&lt;code&gt;f: (X × Y) -&amp;gt; Z&lt;/code&gt;&lt;/pre&gt;
73<p>where <code>×</code> denotes the Cartesian product of set <code>X</code> and <code>Y</code>, and curried <code>f</code> (denoted by <code>h</code> here) is written as:</p> 73&lt;p&gt;where &lt;code&gt;×&lt;/code&gt; denotes the Cartesian product of set &lt;code&gt;X&lt;/code&gt; and &lt;code&gt;Y&lt;/code&gt;, and curried &lt;code&gt;f&lt;/code&gt; (denoted by &lt;code&gt;h&lt;/code&gt; here) is written as:&lt;/p&gt;
74<pre><code>h: X -&gt; (Y -&gt; Z)</code></pre> 74&lt;pre&gt;&lt;code&gt;h: X -&amp;gt; (Y -&amp;gt; Z)&lt;/code&gt;&lt;/pre&gt;
75<h3 id="procedural-macros">Procedural Macros</h3> 75&lt;h3 id="procedural-macros"&gt;Procedural Macros&lt;/h3&gt;
76<p>These are functions that take code as input and spit out modified code as output. Powerful stuff. Rust has three kinds of proc-macros:</p> 76&lt;p&gt;These are functions that take code as input and spit out modified code as output. Powerful stuff. Rust has three kinds of proc-macros:&lt;/p&gt;
77<ul> 77&lt;ul&gt;
78<li>Function like macros<br /> 78&lt;li&gt;Function like macros&lt;br /&gt;
79</li> 79&lt;/li&gt;
80<li>Derive macros: <code>#[derive(...)]</code>, used to automatically implement traits for structs/enums<br /> 80&lt;li&gt;Derive macros: &lt;code&gt;#[derive(...)]&lt;/code&gt;, used to automatically implement traits for structs/enums&lt;br /&gt;
81</li> 81&lt;/li&gt;
82<li>and Attribute macros: <code>#[test]</code>, usually slapped onto functions</li> 82&lt;li&gt;and Attribute macros: &lt;code&gt;#[test]&lt;/code&gt;, usually slapped onto functions&lt;/li&gt;
83</ul> 83&lt;/ul&gt;
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> 84&lt;p&gt;We will be using Attribute macros to convert a Rust function into a curried Rust function, which we should be able to call via: &lt;code&gt;function(arg1)(arg2)&lt;/code&gt;.&lt;/p&gt;
85<h3 id="definitions">Definitions</h3> 85&lt;h3 id="definitions"&gt;Definitions&lt;/h3&gt;
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> 86&lt;p&gt;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:&lt;/p&gt;
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">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 87&lt;div class="sourceCode" id="cb4"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb4-1"&gt;&lt;a href="#cb4-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; y&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; z&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 88&lt;span id="cb4-2"&gt;&lt;a href="#cb4-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
89<span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 89&lt;span id="cb4-3"&gt;&lt;a href="#cb4-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 90&lt;p&gt;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:&lt;/p&gt;
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">-&gt;</span> <span class="op">?</span> <span class="op">{</span></span> 91&lt;div class="sourceCode" id="cb5"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb5-1"&gt;&lt;a href="#cb5-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add_curried1(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;?&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="op">?</span> <span class="op">{</span></span> 92&lt;span id="cb5-2"&gt;&lt;a href="#cb5-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;fn&lt;/span&gt; add_curried2 (y&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;?&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 93&lt;span id="cb5-3"&gt;&lt;a href="#cb5-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;fn&lt;/span&gt; add_curried3 (z&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 94&lt;span id="cb5-4"&gt;&lt;a href="#cb5-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
95<span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a> <span class="op">}</span></span> 95&lt;span id="cb5-5"&gt;&lt;a href="#cb5-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
96<span id="cb5-6"><a href="#cb5-6" aria-hidden="true"></a> <span class="op">}</span></span> 96&lt;span id="cb5-6"&gt;&lt;a href="#cb5-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
97<span id="cb5-7"><a href="#cb5-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 97&lt;span id="cb5-7"&gt;&lt;a href="#cb5-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
98<p>A couple of things to note:</p> 98&lt;p&gt;A couple of things to note:&lt;/p&gt;
99<p><strong>Return types</strong><br /> 99&lt;p&gt;&lt;strong&gt;Return types&lt;/strong&gt;&lt;br /&gt;
100We 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) -&gt; 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) -&gt; 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> 100We have placed &lt;code&gt;?&lt;/code&gt;s in place of return types. Let’s try to fix that. &lt;code&gt;add_curried3&lt;/code&gt; returns the ‘value’, so &lt;code&gt;u32&lt;/code&gt; is accurate. &lt;code&gt;add_curried2&lt;/code&gt; returns &lt;code&gt;add_curried3&lt;/code&gt;. What is the type of &lt;code&gt;add_curried3&lt;/code&gt;? It is a function that takes in a &lt;code&gt;u32&lt;/code&gt; and returns a &lt;code&gt;u32&lt;/code&gt;. So a &lt;code&gt;fn(u32) -&amp;gt; u32&lt;/code&gt; will do right? No, I’ll explain why in the next point, but for now, we will make use of the &lt;code&gt;Fn&lt;/code&gt; trait, our return type is &lt;code&gt;impl Fn(u32) -&amp;gt; u32&lt;/code&gt;. This basically tells the compiler that we will be returning something function-like, a.k.a, behaves like a &lt;code&gt;Fn&lt;/code&gt;. Cool!&lt;/p&gt;
101<p>If you have been following along, you should be able to tell that the return type of <code>add_curried1</code> is:</p> 101&lt;p&gt;If you have been following along, you should be able to tell that the return type of &lt;code&gt;add_curried1&lt;/code&gt; is:&lt;/p&gt;
102<pre><code>impl Fn(u32) -&gt; (impl Fn(u32) -&gt; u32)</code></pre> 102&lt;pre&gt;&lt;code&gt;impl Fn(u32) -&amp;gt; (impl Fn(u32) -&amp;gt; u32)&lt;/code&gt;&lt;/pre&gt;
103<p>We can drop the parentheses because <code>-&gt;</code> is right associative:</p> 103&lt;p&gt;We can drop the parentheses because &lt;code&gt;-&amp;gt;&lt;/code&gt; is right associative:&lt;/p&gt;
104<pre><code>impl Fn(u32) -&gt; impl Fn(u32) -&gt; u32 104&lt;pre&gt;&lt;code&gt;impl Fn(u32) -&amp;gt; impl Fn(u32) -&amp;gt; u32
105</code></pre> 105&lt;/code&gt;&lt;/pre&gt;
106<p><strong>Accessing environment</strong><br /> 106&lt;p&gt;&lt;strong&gt;Accessing environment&lt;/strong&gt;&lt;br /&gt;
107A 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> 107A function cannot access it’s environment. Our solution will not work. &lt;code&gt;add_curried3&lt;/code&gt; attempts to access &lt;code&gt;x&lt;/code&gt;, which is not allowed! A closure&lt;a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; however, can. If we are returning a closure, our return type must be &lt;code&gt;impl Fn&lt;/code&gt;, and not &lt;code&gt;fn&lt;/code&gt;. The difference between the &lt;code&gt;Fn&lt;/code&gt; trait and function pointers is beyond the scope of this post.&lt;/p&gt;
108<h3 id="refinement">Refinement</h3> 108&lt;h3 id="refinement"&gt;Refinement&lt;/h3&gt;
109<p>Armed with knowledge, we refine our expected output, this time, employing closures:</p> 109&lt;p&gt;Armed with knowledge, we refine our expected output, this time, employing closures:&lt;/p&gt;
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">-&gt;</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="kw">impl</span> <span class="bu">Fn</span>(<span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 110&lt;div class="sourceCode" id="cb8"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb8-1"&gt;&lt;a href="#cb8-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 111&lt;span id="cb8-2"&gt;&lt;a href="#cb8-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;y&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;z&lt;span class="op"&gt;|&lt;/span&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
112<span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 112&lt;span id="cb8-3"&gt;&lt;a href="#cb8-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
113<p>Alas, that does not compile either! It errors out with the following message:</p> 113&lt;p&gt;Alas, that does not compile either! It errors out with the following message:&lt;/p&gt;
114<pre><code>error[E0562]: `impl Trait` not allowed outside of function 114&lt;pre&gt;&lt;code&gt;error[E0562]: `impl Trait` not allowed outside of function
115and inherent method return types 115and inherent method return types
116 --&gt; src/main.rs:17:37 116 --&amp;gt; src/main.rs:17:37
117 | 117 |
118 | fn add(x: u32) -&gt; impl Fn(u32) -&gt; impl Fn(u32) -&gt; u32 118 | fn add(x: u32) -&amp;gt; impl Fn(u32) -&amp;gt; impl Fn(u32) -&amp;gt; u32
119 | ^^^^^^^^^^^^^^^^^^^ 119 | ^^^^^^^^^^^^^^^^^^^
120</code></pre> 120&lt;/code&gt;&lt;/pre&gt;
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> 121&lt;p&gt;You are allowed to return an &lt;code&gt;impl Fn&lt;/code&gt; 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.&lt;/p&gt;
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> 122&lt;p&gt;We are going to have to cheat a bit to fix this issue; with type aliases and a convenient nightly feature &lt;a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;:&lt;/p&gt;
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> 123&lt;div class="sourceCode" id="cb10"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb10-1"&gt;&lt;a href="#cb10-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#![&lt;/span&gt;feature&lt;span class="at"&gt;(&lt;/span&gt;type_alias_impl_trait&lt;span class="at"&gt;)]&lt;/span&gt; &lt;span class="co"&gt;// allows us to use `impl Fn` in type aliases!&lt;/span&gt;&lt;/span&gt;
124<span id="cb10-2"><a href="#cb10-2" aria-hidden="true"></a></span> 124&lt;span id="cb10-2"&gt;&lt;a href="#cb10-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 125&lt;span id="cb10-3"&gt;&lt;a href="#cb10-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// the return value when zero args are to be applied&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T0<span class="op">;</span> <span class="co">// the return value when one arg is to be applied</span></span> 126&lt;span id="cb10-4"&gt;&lt;a href="#cb10-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T0&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// the return value when one arg is to be applied&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T1<span class="op">;</span> <span class="co">// the return value when two args are to be applied</span></span> 127&lt;span id="cb10-5"&gt;&lt;a href="#cb10-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T2 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T1&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// the return value when two args are to be applied&lt;/span&gt;&lt;/span&gt;
128<span id="cb10-6"><a href="#cb10-6" aria-hidden="true"></a></span> 128&lt;span id="cb10-6"&gt;&lt;a href="#cb10-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> T2 <span class="op">{</span></span> 129&lt;span id="cb10-7"&gt;&lt;a href="#cb10-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T2 &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 130&lt;span id="cb10-8"&gt;&lt;a href="#cb10-8" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;y&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;z&lt;span class="op"&gt;|&lt;/span&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
131<span id="cb10-9"><a href="#cb10-9" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 131&lt;span id="cb10-9"&gt;&lt;a href="#cb10-9" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 132&lt;p&gt;Drop that into a cargo project, call &lt;code&gt;add(4)(5)(6)&lt;/code&gt;, cross your fingers, and run &lt;code&gt;cargo +nightly run&lt;/code&gt;. You should see a 15 unless you forgot to print it!&lt;/p&gt;
133<h3 id="the-in-betweens">The In-Betweens</h3> 133&lt;h3 id="the-in-betweens"&gt;The In-Betweens&lt;/h3&gt;
134<p>Let us write the magical bits that take us from function to curried function.</p> 134&lt;p&gt;Let us write the magical bits that take us from function to curried function.&lt;/p&gt;
135<p>Initialize your workspace with <code>cargo new --lib currying</code>. Proc-macro crates are libraries with exactly one export, the macro itself. Add a <code>tests</code> directory to your crate root. Your directory should look something like this:</p> 135&lt;p&gt;Initialize your workspace with &lt;code&gt;cargo new --lib currying&lt;/code&gt;. Proc-macro crates are libraries with exactly one export, the macro itself. Add a &lt;code&gt;tests&lt;/code&gt; directory to your crate root. Your directory should look something like this:&lt;/p&gt;
136<pre><code>. 136&lt;pre&gt;&lt;code&gt;.
137├── Cargo.toml 137├── Cargo.toml
138├── src 138├── src
139│   └── lib.rs 139│   └── lib.rs
140└── tests 140└── tests
141 └── smoke.rs</code></pre> 141 └── smoke.rs&lt;/code&gt;&lt;/pre&gt;
142<h4 id="dependencies">Dependencies</h4> 142&lt;h4 id="dependencies"&gt;Dependencies&lt;/h4&gt;
143<p>We will be using a total of 3 external crates:</p> 143&lt;p&gt;We will be using a total of 3 external crates:&lt;/p&gt;
144<ul> 144&lt;ul&gt;
145<li><a href="https://docs.rs/proc-macro2/1.0.12/proc_macro2/">proc_macro2</a></li> 145&lt;li&gt;&lt;a href="https://docs.rs/proc-macro2/1.0.12/proc_macro2/"&gt;proc_macro2&lt;/a&gt;&lt;/li&gt;
146<li><a href="https://docs.rs/syn/1.0.18/syn/index.html">syn</a></li> 146&lt;li&gt;&lt;a href="https://docs.rs/syn/1.0.18/syn/index.html"&gt;syn&lt;/a&gt;&lt;/li&gt;
147<li><a href="https://docs.rs/quote/1.0.4/quote/index.html">quote</a></li> 147&lt;li&gt;&lt;a href="https://docs.rs/quote/1.0.4/quote/index.html"&gt;quote&lt;/a&gt;&lt;/li&gt;
148</ul> 148&lt;/ul&gt;
149<p>Here’s a sample <code>Cargo.toml</code>:</p> 149&lt;p&gt;Here’s a sample &lt;code&gt;Cargo.toml&lt;/code&gt;:&lt;/p&gt;
150<pre><code># Cargo.toml 150&lt;pre&gt;&lt;code&gt;# Cargo.toml
151 151
152[dependencies] 152[dependencies]
153proc-macro2 = &quot;1.0.9&quot; 153proc-macro2 = &amp;quot;1.0.9&amp;quot;
154quote = &quot;1.0&quot; 154quote = &amp;quot;1.0&amp;quot;
155 155
156[dependencies.syn] 156[dependencies.syn]
157version = &quot;1.0&quot; 157version = &amp;quot;1.0&amp;quot;
158features = [&quot;full&quot;] 158features = [&amp;quot;full&amp;quot;]
159 159
160[lib] 160[lib]
161proc-macro = true # this is important!</code></pre> 161proc-macro = true # this is important!&lt;/code&gt;&lt;/pre&gt;
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> 162&lt;p&gt;We will be using an external &lt;code&gt;proc-macro2&lt;/code&gt; crate as well as an internal &lt;code&gt;proc-macro&lt;/code&gt; crate. Not confusing at all!&lt;/p&gt;
163<h4 id="the-attribute-macro">The attribute macro</h4> 163&lt;h4 id="the-attribute-macro"&gt;The attribute macro&lt;/h4&gt;
164<p>Drop this into <code>src/lib.rs</code>, to get the ball rolling.</p> 164&lt;p&gt;Drop this into &lt;code&gt;src/lib.rs&lt;/code&gt;, to get the ball rolling.&lt;/p&gt;
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> 165&lt;div class="sourceCode" id="cb13"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb13-1"&gt;&lt;a href="#cb13-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
166<span id="cb13-2"><a href="#cb13-2" aria-hidden="true"></a></span> 166&lt;span id="cb13-2"&gt;&lt;a href="#cb13-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 167&lt;span id="cb13-3"&gt;&lt;a href="#cb13-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;proc_macro::&lt;/span&gt;TokenStream&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// 1&lt;/span&gt;&lt;/span&gt;
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> 168&lt;span id="cb13-4"&gt;&lt;a href="#cb13-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;quote::&lt;/span&gt;quote&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 169&lt;span id="cb13-5"&gt;&lt;a href="#cb13-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;syn::&lt;/span&gt;&lt;span class="op"&gt;{&lt;/span&gt;parse_macro_input&lt;span class="op"&gt;,&lt;/span&gt; ItemFn&lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;
170<span id="cb13-6"><a href="#cb13-6" aria-hidden="true"></a></span> 170&lt;span id="cb13-6"&gt;&lt;a href="#cb13-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 171&lt;span id="cb13-7"&gt;&lt;a href="#cb13-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;proc_macro_attribute&lt;span class="at"&gt;]&lt;/span&gt; &lt;span class="co"&gt;// 2&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> TokenStream <span class="op">{</span></span> 172&lt;span id="cb13-8"&gt;&lt;a href="#cb13-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;pub&lt;/span&gt; &lt;span class="kw"&gt;fn&lt;/span&gt; curry(_attr&lt;span class="op"&gt;:&lt;/span&gt; TokenStream&lt;span class="op"&gt;,&lt;/span&gt; item&lt;span class="op"&gt;:&lt;/span&gt; TokenStream) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; TokenStream &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 173&lt;span id="cb13-9"&gt;&lt;a href="#cb13-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; parsed &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;parse_macro_input!&lt;/span&gt;(item &lt;span class="kw"&gt;as&lt;/span&gt; ItemFn)&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// 3&lt;/span&gt;&lt;/span&gt;
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> 174&lt;span id="cb13-10"&gt;&lt;a href="#cb13-10" aria-hidden="true"&gt;&lt;/a&gt; generate_curry(parsed)&lt;span class="op"&gt;.&lt;/span&gt;into() &lt;span class="co"&gt;// 4&lt;/span&gt;&lt;/span&gt;
175<span id="cb13-11"><a href="#cb13-11" aria-hidden="true"></a><span class="op">}</span></span> 175&lt;span id="cb13-11"&gt;&lt;a href="#cb13-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
176<span id="cb13-12"><a href="#cb13-12" aria-hidden="true"></a></span> 176&lt;span id="cb13-12"&gt;&lt;a href="#cb13-12" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{}</span></span></code></pre></div> 177&lt;span id="cb13-13"&gt;&lt;a href="#cb13-13" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; generate_curry(parsed&lt;span class="op"&gt;:&lt;/span&gt; ItemFn) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;proc_macro2::&lt;/span&gt;TokenStream &lt;span class="op"&gt;{}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
178<p><strong>1. Imports</strong></p> 178&lt;p&gt;&lt;strong&gt;1. Imports&lt;/strong&gt;&lt;/p&gt;
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> 179&lt;p&gt;A &lt;code&gt;Tokenstream&lt;/code&gt; holds (hopefully valid) Rust code, this is the type of our input and output. Note that we are importing this type from &lt;code&gt;proc_macro&lt;/code&gt; and not &lt;code&gt;proc_macro2&lt;/code&gt;.&lt;/p&gt;
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> 180&lt;p&gt;&lt;code&gt;quote!&lt;/code&gt; from the &lt;code&gt;quote&lt;/code&gt; crate is a macro that allows us to quickly produce &lt;code&gt;TokenStream&lt;/code&gt;s. Much like the LISP &lt;code&gt;quote&lt;/code&gt; procedure, you can use the &lt;code&gt;quote!&lt;/code&gt; macro for symbolic transformations.&lt;/p&gt;
181<p><code>ItemFn</code> from the <code>syn</code> crate holds the parsed <code>TokenStream</code> of a Rust function. <code>parse_macro_input!</code> is a helper macro provided by <code>syn</code>.</p> 181&lt;p&gt;&lt;code&gt;ItemFn&lt;/code&gt; from the &lt;code&gt;syn&lt;/code&gt; crate holds the parsed &lt;code&gt;TokenStream&lt;/code&gt; of a Rust function. &lt;code&gt;parse_macro_input!&lt;/code&gt; is a helper macro provided by &lt;code&gt;syn&lt;/code&gt;.&lt;/p&gt;
182<p><strong>2. The lone export</strong></p> 182&lt;p&gt;&lt;strong&gt;2. The lone export&lt;/strong&gt;&lt;/p&gt;
183<p>Annotate the only <code>pub</code> of our crate with <code>#[proc_macro_attribute]</code>. This tells rustc that <code>curry</code> is a procedural macro, and allows us to use it as <code>#[crate_name::curry]</code> in other crates. Note the signature of the <code>curry</code> function. <code>_attr</code> is the <code>TokenStream</code> representing the attribute itself, <code>item</code> refers to the thing we slapped our macro into, in this case a function (like <code>add</code>). The return value is a modified <code>TokenStream</code>, this will contain our curried version of <code>add</code>.</p> 183&lt;p&gt;Annotate the only &lt;code&gt;pub&lt;/code&gt; of our crate with &lt;code&gt;#[proc_macro_attribute]&lt;/code&gt;. This tells rustc that &lt;code&gt;curry&lt;/code&gt; is a procedural macro, and allows us to use it as &lt;code&gt;#[crate_name::curry]&lt;/code&gt; in other crates. Note the signature of the &lt;code&gt;curry&lt;/code&gt; function. &lt;code&gt;_attr&lt;/code&gt; is the &lt;code&gt;TokenStream&lt;/code&gt; representing the attribute itself, &lt;code&gt;item&lt;/code&gt; refers to the thing we slapped our macro into, in this case a function (like &lt;code&gt;add&lt;/code&gt;). The return value is a modified &lt;code&gt;TokenStream&lt;/code&gt;, this will contain our curried version of &lt;code&gt;add&lt;/code&gt;.&lt;/p&gt;
184<p><strong>3. The helper macro</strong></p> 184&lt;p&gt;&lt;strong&gt;3. The helper macro&lt;/strong&gt;&lt;/p&gt;
185<p>A <code>TokenStream</code> is a little hard to work with, which is why we have the <code>syn</code> crate, which provides types to represent Rust tokens. An <code>RArrow</code> struct to represent the return arrow on a function and so on. One of those types is <code>ItemFn</code>, that represents an entire Rust function. The <code>parse_macro_input!</code> automatically puts the input to our macro into an <code>ItemFn</code>. What a gentleman!</p> 185&lt;p&gt;A &lt;code&gt;TokenStream&lt;/code&gt; is a little hard to work with, which is why we have the &lt;code&gt;syn&lt;/code&gt; crate, which provides types to represent Rust tokens. An &lt;code&gt;RArrow&lt;/code&gt; struct to represent the return arrow on a function and so on. One of those types is &lt;code&gt;ItemFn&lt;/code&gt;, that represents an entire Rust function. The &lt;code&gt;parse_macro_input!&lt;/code&gt; automatically puts the input to our macro into an &lt;code&gt;ItemFn&lt;/code&gt;. What a gentleman!&lt;/p&gt;
186<p><strong>4. Returning <code>TokenStream</code>s </strong></p> 186&lt;p&gt;&lt;strong&gt;4. Returning &lt;code&gt;TokenStream&lt;/code&gt;s &lt;/strong&gt;&lt;/p&gt;
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> 187&lt;p&gt;We haven’t filled in &lt;code&gt;generate_curry&lt;/code&gt; yet, but we can see that it returns a &lt;code&gt;proc_macro2::TokenStream&lt;/code&gt; and not a &lt;code&gt;proc_macro::TokenStream&lt;/code&gt;, so drop a &lt;code&gt;.into()&lt;/code&gt; to convert it.&lt;/p&gt;
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> 188&lt;p&gt;Lets move on, and fill in &lt;code&gt;generate_curry&lt;/code&gt;, I would suggest keeping the documentation for &lt;a href="https://docs.rs/syn/1.0.19/syn/struct.ItemFn.html"&gt;&lt;code&gt;syn::ItemFn&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://docs.rs/syn/1.0.19/syn/struct.Signature.html"&gt;&lt;code&gt;syn::Signature&lt;/code&gt;&lt;/a&gt; open.&lt;/p&gt;
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> 189&lt;div class="sourceCode" id="cb14"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb14-1"&gt;&lt;a href="#cb14-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
190<span id="cb14-2"><a href="#cb14-2" aria-hidden="true"></a></span> 190&lt;span id="cb14-2"&gt;&lt;a href="#cb14-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> 191&lt;span id="cb14-3"&gt;&lt;a href="#cb14-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; generate_curry(parsed&lt;span class="op"&gt;:&lt;/span&gt; ItemFn) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;proc_macro2::&lt;/span&gt;TokenStream &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 192&lt;span id="cb14-4"&gt;&lt;a href="#cb14-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_body &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;block&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// function body&lt;/span&gt;&lt;/span&gt;
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> 193&lt;span id="cb14-5"&gt;&lt;a href="#cb14-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; sig &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;sig&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// function signature&lt;/span&gt;&lt;/span&gt;
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> 194&lt;span id="cb14-6"&gt;&lt;a href="#cb14-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; vis &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;vis&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// visibility, pub or not&lt;/span&gt;&lt;/span&gt;
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> 195&lt;span id="cb14-7"&gt;&lt;a href="#cb14-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_name &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;ident&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// function name/identifier&lt;/span&gt;&lt;/span&gt;
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> 196&lt;span id="cb14-8"&gt;&lt;a href="#cb14-8" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_args &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;inputs&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// comma separated args&lt;/span&gt;&lt;/span&gt;
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> 197&lt;span id="cb14-9"&gt;&lt;a href="#cb14-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_return_type &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;output&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// return type&lt;/span&gt;&lt;/span&gt;
198<span id="cb14-10"><a href="#cb14-10" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 198&lt;span id="cb14-10"&gt;&lt;a href="#cb14-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 199&lt;p&gt;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 &lt;code&gt;syn::Signature&lt;/code&gt; can tell us about a function:&lt;/p&gt;
200<pre><code> .-- syn::Ident (ident) 200&lt;pre&gt;&lt;code&gt; .-- syn::Ident (ident)
201 / 201 /
202 fn add(x: u32, y: u32) -&gt; u32 202 fn add(x: u32, y: u32) -&amp;gt; u32
203 (fn_token) / ~~~~~~~,~~~~~~ ~~~~~~ 203 (fn_token) / ~~~~~~~,~~~~~~ ~~~~~~
204syn::token::Fn --&#39; / \ (output) 204syn::token::Fn --&amp;#39; / \ (output)
205 &#39; `- syn::ReturnType 205 &amp;#39; `- syn::ReturnType
206 Punctuated&lt;FnArg, Comma&gt; (inputs)</code></pre> 206 Punctuated&amp;lt;FnArg, Comma&amp;gt; (inputs)&lt;/code&gt;&lt;/pre&gt;
207<p>Enough analysis, lets produce our first bit of Rust code.</p> 207&lt;p&gt;Enough analysis, lets produce our first bit of Rust code.&lt;/p&gt;
208<h4 id="function-body">Function Body</h4> 208&lt;h4 id="function-body"&gt;Function Body&lt;/h4&gt;
209<p>Recall that the body of a curried <code>add</code> should look like this:</p> 209&lt;p&gt;Recall that the body of a curried &lt;code&gt;add&lt;/code&gt; should look like this:&lt;/p&gt;
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> 210&lt;div class="sourceCode" id="cb16"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb16-1"&gt;&lt;a href="#cb16-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;y&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;z&lt;span class="op"&gt;|&lt;/span&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
211<p>And in general:</p> 211&lt;p&gt;And in general:&lt;/p&gt;
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">&lt;</span>function body here<span class="op">&gt;</span></span></code></pre></div> 212&lt;div class="sourceCode" id="cb17"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb17-1"&gt;&lt;a href="#cb17-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;arg2&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;arg3&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="op"&gt;...&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;argN&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="op"&gt;&amp;lt;&lt;/span&gt;function body here&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 213&lt;p&gt;We already have the function’s body, provided by &lt;code&gt;fn_body&lt;/code&gt;, in our &lt;code&gt;generate_curry&lt;/code&gt; function. All that’s left to add is the &lt;code&gt;move |arg2| move |arg3| ...&lt;/code&gt; stuff, for which we need to extract the argument identifiers (doc: &lt;a href="https://docs.rs/syn/1.0.18/syn/punctuated/struct.Punctuated.html"&gt;Punctuated&lt;/a&gt;, &lt;a href="https://docs.rs/syn/1.0.18/syn/enum.FnArg.html"&gt;FnArg&lt;/a&gt;, &lt;a href="https://docs.rs/syn/1.0.18/syn/struct.PatType.html"&gt;PatType&lt;/a&gt;):&lt;/p&gt;
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> 214&lt;div class="sourceCode" id="cb18"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb18-1"&gt;&lt;a href="#cb18-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
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> 215&lt;span id="cb18-2"&gt;&lt;a href="#cb18-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;syn::punctuated::&lt;/span&gt;Punctuated&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 216&lt;span id="cb18-3"&gt;&lt;a href="#cb18-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;syn::&lt;/span&gt;&lt;span class="op"&gt;{&lt;/span&gt;parse_macro_input&lt;span class="op"&gt;,&lt;/span&gt; FnArg&lt;span class="op"&gt;,&lt;/span&gt; Pat&lt;span class="op"&gt;,&lt;/span&gt; ItemFn&lt;span class="op"&gt;,&lt;/span&gt; Block&lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;
217<span id="cb18-4"><a href="#cb18-4" aria-hidden="true"></a></span> 217&lt;span id="cb18-4"&gt;&lt;a href="#cb18-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">&lt;</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">&gt;</span>) <span class="op">-&gt;</span> <span class="dt">Vec</span><span class="op">&lt;</span><span class="dt">Box</span><span class="op">&lt;</span>Pat<span class="op">&gt;&gt;</span> <span class="op">{</span> </span> 218&lt;span id="cb18-5"&gt;&lt;a href="#cb18-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; extract_arg_idents(fn_args&lt;span class="op"&gt;:&lt;/span&gt; Punctuated&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;FnArg&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;syn::token::&lt;/span&gt;Comma&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Vec&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Pat&lt;span class="op"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt; &lt;/span&gt;
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">&lt;</span><span class="dt">Vec</span><span class="op">&lt;</span>_<span class="op">&gt;&gt;</span>()<span class="op">;</span></span> 219&lt;span id="cb18-6"&gt;&lt;a href="#cb18-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; fn_args&lt;span class="op"&gt;.&lt;/span&gt;into_iter()&lt;span class="op"&gt;.&lt;/span&gt;map(extract_arg_pat)&lt;span class="op"&gt;.&lt;/span&gt;&lt;span class="pp"&gt;collect::&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;Vec&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;_&lt;span class="op"&gt;&amp;gt;&amp;gt;&lt;/span&gt;()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
220<span id="cb18-7"><a href="#cb18-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 220&lt;span id="cb18-7"&gt;&lt;a href="#cb18-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 221&lt;p&gt;Alright, so we are iterating over function args (&lt;code&gt;Punctuated&lt;/code&gt; is a collection that you can iterate over) and mapping an &lt;code&gt;extract_arg_pat&lt;/code&gt; to every item. What’s &lt;code&gt;extract_arg_pat&lt;/code&gt;?&lt;/p&gt;
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> 222&lt;div class="sourceCode" id="cb19"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb19-1"&gt;&lt;a href="#cb19-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
223<span id="cb19-2"><a href="#cb19-2" aria-hidden="true"></a></span> 223&lt;span id="cb19-2"&gt;&lt;a href="#cb19-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">Box</span><span class="op">&lt;</span>Pat<span class="op">&gt;</span> <span class="op">{</span></span> 224&lt;span id="cb19-3"&gt;&lt;a href="#cb19-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; extract_arg_pat(a&lt;span class="op"&gt;:&lt;/span&gt; FnArg) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Pat&lt;span class="op"&gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
225<span id="cb19-4"><a href="#cb19-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> 225&lt;span id="cb19-4"&gt;&lt;a href="#cb19-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;match&lt;/span&gt; a &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
226<span id="cb19-5"><a href="#cb19-5" aria-hidden="true"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=&gt;</span> p<span class="op">.</span>pat<span class="op">,</span></span> 226&lt;span id="cb19-5"&gt;&lt;a href="#cb19-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;FnArg::&lt;/span&gt;Typed(p) &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; p&lt;span class="op"&gt;.&lt;/span&gt;pat&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
227<span id="cb19-6"><a href="#cb19-6" aria-hidden="true"></a> _ <span class="op">=&gt;</span> <span class="pp">panic!</span>(<span class="st">&quot;Not supported on types with `self`!&quot;</span>)<span class="op">,</span></span> 227&lt;span id="cb19-6"&gt;&lt;a href="#cb19-6" aria-hidden="true"&gt;&lt;/a&gt; _ &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;panic!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;Not supported on types with `self`!&amp;quot;&lt;/span&gt;)&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
228<span id="cb19-7"><a href="#cb19-7" aria-hidden="true"></a> <span class="op">}</span></span> 228&lt;span id="cb19-7"&gt;&lt;a href="#cb19-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
229<span id="cb19-8"><a href="#cb19-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 229&lt;span id="cb19-8"&gt;&lt;a href="#cb19-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 230&lt;p&gt;&lt;code&gt;FnArg&lt;/code&gt; is an enum type as you might have guessed. The &lt;code&gt;Typed&lt;/code&gt; variant encompasses args that are written as &lt;code&gt;name: type&lt;/code&gt; and the other variant, &lt;code&gt;Reciever&lt;/code&gt; refers to &lt;code&gt;self&lt;/code&gt; types. Ignore those for now, keep it simple.&lt;/p&gt;
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> 231&lt;p&gt;Every &lt;code&gt;FnArg::Typed&lt;/code&gt; value contains a &lt;code&gt;pat&lt;/code&gt;, which is in essence, the name of the argument. The type of the arg is accessible via &lt;code&gt;p.ty&lt;/code&gt; (we will be using this later).&lt;/p&gt;
232<p>With that done, we should be able to write the codegen for the function body:</p> 232&lt;p&gt;With that done, we should be able to write the codegen for the function body:&lt;/p&gt;
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> 233&lt;div class="sourceCode" id="cb20"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb20-1"&gt;&lt;a href="#cb20-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
234<span id="cb20-2"><a href="#cb20-2" aria-hidden="true"></a></span> 234&lt;span id="cb20-2"&gt;&lt;a href="#cb20-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">&amp;</span>[<span class="dt">Box</span><span class="op">&lt;</span>Pat<span class="op">&gt;</span>]<span class="op">,</span> body<span class="op">:</span> <span class="dt">Box</span><span class="op">&lt;</span>Block<span class="op">&gt;</span>) <span class="op">-&gt;</span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> 235&lt;span id="cb20-3"&gt;&lt;a href="#cb20-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; generate_body(fn_args&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;[&lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Pat&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;]&lt;span class="op"&gt;,&lt;/span&gt; body&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Block&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;proc_macro2::&lt;/span&gt;TokenStream &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
236<span id="cb20-4"><a href="#cb20-4" aria-hidden="true"></a> <span class="pp">quote!</span> <span class="op">{</span></span> 236&lt;span id="cb20-4"&gt;&lt;a href="#cb20-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;quote!&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 237&lt;span id="cb20-5"&gt;&lt;a href="#cb20-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; #( &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;#fn_args&lt;span class="op"&gt;|&lt;/span&gt; )&lt;span class="op"&gt;*&lt;/span&gt; #body&lt;/span&gt;
238<span id="cb20-6"><a href="#cb20-6" aria-hidden="true"></a> <span class="op">}</span></span> 238&lt;span id="cb20-6"&gt;&lt;a href="#cb20-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
239<span id="cb20-7"><a href="#cb20-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 239&lt;span id="cb20-7"&gt;&lt;a href="#cb20-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 240&lt;p&gt;That is some scary looking syntax! Allow me to explain. The &lt;code&gt;quote!{ ... }&lt;/code&gt; returns a &lt;code&gt;proc_macro2::TokenStream&lt;/code&gt;, if we wrote &lt;code&gt;quote!{ let x = 1 + 2; }&lt;/code&gt;, it wouldn’t create a new variable &lt;code&gt;x&lt;/code&gt; with value 3, it would literally produce a stream of tokens with that expression.&lt;/p&gt;
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> 241&lt;p&gt;The &lt;code&gt;#&lt;/code&gt; enables variable interpolation. &lt;code&gt;#body&lt;/code&gt; will look for &lt;code&gt;body&lt;/code&gt; in the current scope, take its value, and insert it in the returned &lt;code&gt;TokenStream&lt;/code&gt;. Kinda like quasi quoting in LISPs, you have written one.&lt;/p&gt;
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> 242&lt;p&gt;What about &lt;code&gt;#( move |#fn_args| )*&lt;/code&gt;? That is repetition. &lt;code&gt;quote&lt;/code&gt; iterates through &lt;code&gt;fn_args&lt;/code&gt;, and drops a &lt;code&gt;move&lt;/code&gt; behind each one, it then places pipes (&lt;code&gt;|&lt;/code&gt;), around it.&lt;/p&gt;
243<p>Let us test our first bit of codegen! Modify <code>generate_curry</code> like so:</p> 243&lt;p&gt;Let us test our first bit of codegen! Modify &lt;code&gt;generate_curry&lt;/code&gt; like so:&lt;/p&gt;
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> 244&lt;div class="sourceCode" id="cb21"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb21-1"&gt;&lt;a href="#cb21-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
245<span id="cb21-2"><a href="#cb21-2" aria-hidden="true"></a></span> 245&lt;span id="cb21-2"&gt;&lt;a href="#cb21-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> TokenStream <span class="op">{</span></span> 246&lt;span id="cb21-3"&gt;&lt;a href="#cb21-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;fn&lt;/span&gt; generate_curry(parsed&lt;span class="op"&gt;:&lt;/span&gt; ItemFn) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; TokenStream &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 247&lt;span id="cb21-4"&gt;&lt;a href="#cb21-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_body &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;block&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 248&lt;span id="cb21-5"&gt;&lt;a href="#cb21-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; sig &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;sig&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 249&lt;span id="cb21-6"&gt;&lt;a href="#cb21-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; vis &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;vis&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 250&lt;span id="cb21-7"&gt;&lt;a href="#cb21-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_name &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;ident&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 251&lt;span id="cb21-8"&gt;&lt;a href="#cb21-8" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_args &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;inputs&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 252&lt;span id="cb21-9"&gt;&lt;a href="#cb21-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_return_type &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;output&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
253<span id="cb21-10"><a href="#cb21-10" aria-hidden="true"></a></span> 253&lt;span id="cb21-10"&gt;&lt;a href="#cb21-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 254&lt;span id="cb21-11"&gt;&lt;a href="#cb21-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;let&lt;/span&gt; arg_idents &lt;span class="op"&gt;=&lt;/span&gt; extract_arg_idents(fn_args&lt;span class="op"&gt;.&lt;/span&gt;clone())&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&amp;</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> 255&lt;span id="cb21-12"&gt;&lt;a href="#cb21-12" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;let&lt;/span&gt; first_ident &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;arg_idents&lt;span class="op"&gt;.&lt;/span&gt;first()&lt;span class="op"&gt;.&lt;/span&gt;unwrap()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
256<span id="cb21-13"><a href="#cb21-13" aria-hidden="true"></a></span> 256&lt;span id="cb21-13"&gt;&lt;a href="#cb21-13" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 257&lt;span id="cb21-14"&gt;&lt;a href="#cb21-14" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="co"&gt;// remember, our curried body starts with the second argument!&lt;/span&gt;&lt;/span&gt;
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">&amp;</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&lt;span id="cb21-15"&gt;&lt;a href="#cb21-15" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;let&lt;/span&gt; curried_body &lt;span class="op"&gt;=&lt;/span&gt; generate_body(&lt;span class="op"&gt;&amp;amp;&lt;/span&gt;arg_idents[&lt;span class="dv"&gt;1&lt;/span&gt;&lt;span class="op"&gt;..&lt;/span&gt;]&lt;span class="op"&gt;,&lt;/span&gt; fn_body&lt;span class="op"&gt;.&lt;/span&gt;clone())&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&quot;{}&quot;</span><span class="op">,</span> curried_body)<span class="op">;</span></span> 259&lt;span id="cb21-16"&gt;&lt;a href="#cb21-16" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="pp"&gt;println!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; curried_body)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
260<span id="cb21-17"><a href="#cb21-17" aria-hidden="true"></a></span> 260&lt;span id="cb21-17"&gt;&lt;a href="#cb21-17" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 261&lt;span id="cb21-18"&gt;&lt;a href="#cb21-18" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="pp"&gt;TokenStream::&lt;/span&gt;new()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
262<span id="cb21-19"><a href="#cb21-19" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> 262&lt;span id="cb21-19"&gt;&lt;a href="#cb21-19" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
263<p>Add a little test to <code>tests/</code>:</p> 263&lt;p&gt;Add a little test to &lt;code&gt;tests/&lt;/code&gt;:&lt;/p&gt;
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> 264&lt;div class="sourceCode" id="cb22"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb22-1"&gt;&lt;a href="#cb22-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// tests/smoke.rs&lt;/span&gt;&lt;/span&gt;
265<span id="cb22-2"><a href="#cb22-2" aria-hidden="true"></a></span> 265&lt;span id="cb22-2"&gt;&lt;a href="#cb22-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 266&lt;span id="cb22-3"&gt;&lt;a href="#cb22-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;&lt;span class="pp"&gt;currying::&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 267&lt;span id="cb22-4"&gt;&lt;a href="#cb22-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; y&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; z&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 268&lt;span id="cb22-5"&gt;&lt;a href="#cb22-5" aria-hidden="true"&gt;&lt;/a&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;/span&gt;
269<span id="cb22-6"><a href="#cb22-6" aria-hidden="true"></a><span class="op">}</span></span> 269&lt;span id="cb22-6"&gt;&lt;a href="#cb22-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
270<span id="cb22-7"><a href="#cb22-7" aria-hidden="true"></a></span> 270&lt;span id="cb22-7"&gt;&lt;a href="#cb22-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
271<span id="cb22-8"><a href="#cb22-8" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> 271&lt;span id="cb22-8"&gt;&lt;a href="#cb22-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
272<span id="cb22-9"><a href="#cb22-9" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> 272&lt;span id="cb22-9"&gt;&lt;a href="#cb22-9" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; works() &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 273&lt;span id="cb22-10"&gt;&lt;a href="#cb22-10" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;assert!&lt;/span&gt;(&lt;span class="cn"&gt;true&lt;/span&gt;)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
274<span id="cb22-11"><a href="#cb22-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 274&lt;span id="cb22-11"&gt;&lt;a href="#cb22-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
275<p>You should find something like this in the output of <code>cargo test</code>:</p> 275&lt;p&gt;You should find something like this in the output of &lt;code&gt;cargo test&lt;/code&gt;:&lt;/p&gt;
276<pre><code>return move | y | move | z | { x + y + z }</code></pre> 276&lt;pre&gt;&lt;code&gt;return move | y | move | z | { x + y + z }&lt;/code&gt;&lt;/pre&gt;
277<p>Glorious <code>println!</code> debugging!</p> 277&lt;p&gt;Glorious &lt;code&gt;println!&lt;/code&gt; debugging!&lt;/p&gt;
278<h4 id="function-signature">Function signature</h4> 278&lt;h4 id="function-signature"&gt;Function signature&lt;/h4&gt;
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> 279&lt;p&gt;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!&lt;/p&gt;
280<p>Recall what our generated type aliases should look like, for our <code>add</code> function:</p> 280&lt;p&gt;Recall what our generated type aliases should look like, for our &lt;code&gt;add&lt;/code&gt; function:&lt;/p&gt;
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> 281&lt;div class="sourceCode" id="cb24"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb24-1"&gt;&lt;a href="#cb24-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T0<span class="op">;</span></span> 282&lt;span id="cb24-2"&gt;&lt;a href="#cb24-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T0&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T1<span class="op">;</span></span></code></pre></div> 283&lt;span id="cb24-3"&gt;&lt;a href="#cb24-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T2 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T1&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
284<p>In general:</p> 284&lt;p&gt;In general:&lt;/p&gt;
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">&lt;</span><span class="kw">return</span> <span class="kw">type</span>&gt;<span class="op">;</span></span> 285&lt;div class="sourceCode" id="cb25"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb25-1"&gt;&lt;a href="#cb25-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;type&lt;/span&gt;&amp;gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&lt;</span><span class="kw">type</span> of arg N&gt;) -&gt; T0<span class="op">;</span></span> 286&lt;span id="cb25-2"&gt;&lt;a href="#cb25-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kw"&gt;type&lt;/span&gt; of arg N&amp;gt;) -&amp;gt; T0&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&lt;</span><span class="kw">type</span> of arg N - 1&gt;) -&gt; T1<span class="op">;</span></span> 287&lt;span id="cb25-3"&gt;&lt;a href="#cb25-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T2 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kw"&gt;type&lt;/span&gt; of arg N - 1&amp;gt;) -&amp;gt; T1&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
288<span id="cb25-4"><a href="#cb25-4" aria-hidden="true"></a><span class="op">.</span></span> 288&lt;span id="cb25-4"&gt;&lt;a href="#cb25-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;.&lt;/span&gt;&lt;/span&gt;
289<span id="cb25-5"><a href="#cb25-5" aria-hidden="true"></a><span class="op">.</span></span> 289&lt;span id="cb25-5"&gt;&lt;a href="#cb25-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;.&lt;/span&gt;&lt;/span&gt;
290<span id="cb25-6"><a href="#cb25-6" aria-hidden="true"></a><span class="op">.</span></span> 290&lt;span id="cb25-6"&gt;&lt;a href="#cb25-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;.&lt;/span&gt;&lt;/span&gt;
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">&lt;</span><span class="kw">type</span> of arg 2&gt;) -&gt; T(N-2)<span class="op">;</span></span></code></pre></div> 291&lt;span id="cb25-7"&gt;&lt;a href="#cb25-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T(N-1) &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kw"&gt;type&lt;/span&gt; of arg 2&amp;gt;) -&amp;gt; T(N-2)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
292<p>To codegen that, we need the types of:</p> 292&lt;p&gt;To codegen that, we need the types of:&lt;/p&gt;
293<ul> 293&lt;ul&gt;
294<li>all our inputs (arguments)</li> 294&lt;li&gt;all our inputs (arguments)&lt;/li&gt;
295<li>the output (the return type)</li> 295&lt;li&gt;the output (the return type)&lt;/li&gt;
296</ul> 296&lt;/ul&gt;
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> 297&lt;p&gt;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: &lt;a href="https://docs.rs/syn/1.0.18/syn/enum.Type.html"&gt;Type&lt;/a&gt;)&lt;/p&gt;
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> 298&lt;div class="sourceCode" id="cb26"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb26-1"&gt;&lt;a href="#cb26-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
299<span id="cb26-2"><a href="#cb26-2" aria-hidden="true"></a></span> 299&lt;span id="cb26-2"&gt;&lt;a href="#cb26-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 300&lt;span id="cb26-3"&gt;&lt;a href="#cb26-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;syn::&lt;/span&gt;&lt;span class="op"&gt;{&lt;/span&gt;parse_macro_input&lt;span class="op"&gt;,&lt;/span&gt; Block&lt;span class="op"&gt;,&lt;/span&gt; FnArg&lt;span class="op"&gt;,&lt;/span&gt; ItemFn&lt;span class="op"&gt;,&lt;/span&gt; Pat&lt;span class="op"&gt;,&lt;/span&gt; ReturnType&lt;span class="op"&gt;,&lt;/span&gt; Type&lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;
301<span id="cb26-4"><a href="#cb26-4" aria-hidden="true"></a></span> 301&lt;span id="cb26-4"&gt;&lt;a href="#cb26-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">Box</span><span class="op">&lt;</span>Type<span class="op">&gt;</span> <span class="op">{</span></span> 302&lt;span id="cb26-5"&gt;&lt;a href="#cb26-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; extract_type(a&lt;span class="op"&gt;:&lt;/span&gt; FnArg) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Type&lt;span class="op"&gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
303<span id="cb26-6"><a href="#cb26-6" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> 303&lt;span id="cb26-6"&gt;&lt;a href="#cb26-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;match&lt;/span&gt; a &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
304<span id="cb26-7"><a href="#cb26-7" aria-hidden="true"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=&gt;</span> p<span class="op">.</span>ty<span class="op">,</span> <span class="co">// notice `ty` instead of `pat`</span></span> 304&lt;span id="cb26-7"&gt;&lt;a href="#cb26-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;FnArg::&lt;/span&gt;Typed(p) &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; p&lt;span class="op"&gt;.&lt;/span&gt;ty&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="co"&gt;// notice `ty` instead of `pat`&lt;/span&gt;&lt;/span&gt;
305<span id="cb26-8"><a href="#cb26-8" aria-hidden="true"></a> _ <span class="op">=&gt;</span> <span class="pp">panic!</span>(<span class="st">&quot;Not supported on types with `self`!&quot;</span>)<span class="op">,</span></span> 305&lt;span id="cb26-8"&gt;&lt;a href="#cb26-8" aria-hidden="true"&gt;&lt;/a&gt; _ &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;panic!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;Not supported on types with `self`!&amp;quot;&lt;/span&gt;)&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
306<span id="cb26-9"><a href="#cb26-9" aria-hidden="true"></a> <span class="op">}</span></span> 306&lt;span id="cb26-9"&gt;&lt;a href="#cb26-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
307<span id="cb26-10"><a href="#cb26-10" aria-hidden="true"></a><span class="op">}</span></span> 307&lt;span id="cb26-10"&gt;&lt;a href="#cb26-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
308<span id="cb26-11"><a href="#cb26-11" aria-hidden="true"></a></span> 308&lt;span id="cb26-11"&gt;&lt;a href="#cb26-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">&lt;</span>FnArg<span class="op">,</span> <span class="pp">syn::token::</span>Comma<span class="op">&gt;</span>) <span class="op">-&gt;</span> <span class="dt">Vec</span><span class="op">&lt;</span><span class="dt">Box</span><span class="op">&lt;</span>Type<span class="op">&gt;&gt;</span> <span class="op">{</span></span> 309&lt;span id="cb26-12"&gt;&lt;a href="#cb26-12" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; extract_arg_types(fn_args&lt;span class="op"&gt;:&lt;/span&gt; Punctuated&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;FnArg&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;syn::token::&lt;/span&gt;Comma&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Vec&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Type&lt;span class="op"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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">&lt;</span><span class="dt">Vec</span><span class="op">&lt;</span>_<span class="op">&gt;&gt;</span>()<span class="op">;</span></span> 310&lt;span id="cb26-13"&gt;&lt;a href="#cb26-13" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; fn_args&lt;span class="op"&gt;.&lt;/span&gt;into_iter()&lt;span class="op"&gt;.&lt;/span&gt;map(extract_type)&lt;span class="op"&gt;.&lt;/span&gt;&lt;span class="pp"&gt;collect::&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;Vec&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;_&lt;span class="op"&gt;&amp;gt;&amp;gt;&lt;/span&gt;()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
311<span id="cb26-14"><a href="#cb26-14" aria-hidden="true"></a></span> 311&lt;span id="cb26-14"&gt;&lt;a href="#cb26-14" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
312<span id="cb26-15"><a href="#cb26-15" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 312&lt;span id="cb26-15"&gt;&lt;a href="#cb26-15" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 313&lt;p&gt;A good reader would have looked at the docs for output member of the &lt;code&gt;syn::Signature&lt;/code&gt; struct. It has the type &lt;code&gt;syn::ReturnType&lt;/code&gt;. So there is no extraction to do here right? There are actually a couple of things we have to ensure here:&lt;/p&gt;
314<ol type="1"> 314&lt;ol type="1"&gt;
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> 315&lt;li&gt;&lt;p&gt;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 &lt;a href="#notes"&gt;Notes&lt;/a&gt; section.&lt;/p&gt;&lt;/li&gt;
316<li><p>A <code>ReturnType</code> encloses the arrow of the return as well, we need to get rid of that. Recall:</p> 316&lt;li&gt;&lt;p&gt;A &lt;code&gt;ReturnType&lt;/code&gt; encloses the arrow of the return as well, we need to get rid of that. Recall:&lt;/p&gt;
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> 317&lt;div class="sourceCode" id="cb27"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb27-1"&gt;&lt;a href="#cb27-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;/span&gt;
318<span id="cb27-2"><a href="#cb27-2" aria-hidden="true"></a><span class="co">// and not</span></span> 318&lt;span id="cb27-2"&gt;&lt;a href="#cb27-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// and not&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">u32</span></span></code></pre></div></li> 319&lt;span id="cb27-3"&gt;&lt;a href="#cb27-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
320</ol> 320&lt;/ol&gt;
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> 321&lt;p&gt;Here is the snippet that handles extraction of the return type (doc: &lt;a href="https://docs.rs/syn/1.0.19/syn/enum.ReturnType.html"&gt;syn::ReturnType&lt;/a&gt;):&lt;/p&gt;
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> 322&lt;div class="sourceCode" id="cb28"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb28-1"&gt;&lt;a href="#cb28-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
323<span id="cb28-2"><a href="#cb28-2" aria-hidden="true"></a></span> 323&lt;span id="cb28-2"&gt;&lt;a href="#cb28-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">Box</span><span class="op">&lt;</span>Type<span class="op">&gt;</span> <span class="op">{</span></span> 324&lt;span id="cb28-3"&gt;&lt;a href="#cb28-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; extract_return_type(a&lt;span class="op"&gt;:&lt;/span&gt; ReturnType) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Type&lt;span class="op"&gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
325<span id="cb28-4"><a href="#cb28-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> 325&lt;span id="cb28-4"&gt;&lt;a href="#cb28-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;match&lt;/span&gt; a &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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">=&gt;</span> p<span class="op">,</span></span> 326&lt;span id="cb28-5"&gt;&lt;a href="#cb28-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;ReturnType::&lt;/span&gt;Type(_&lt;span class="op"&gt;,&lt;/span&gt; p) &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; p&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
327<span id="cb28-6"><a href="#cb28-6" aria-hidden="true"></a> _ <span class="op">=&gt;</span> <span class="pp">panic!</span>(<span class="st">&quot;Not supported on functions without return types!&quot;</span>)<span class="op">,</span></span> 327&lt;span id="cb28-6"&gt;&lt;a href="#cb28-6" aria-hidden="true"&gt;&lt;/a&gt; _ &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;panic!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;Not supported on functions without return types!&amp;quot;&lt;/span&gt;)&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
328<span id="cb28-7"><a href="#cb28-7" aria-hidden="true"></a> <span class="op">}</span></span> 328&lt;span id="cb28-7"&gt;&lt;a href="#cb28-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
329<span id="cb28-8"><a href="#cb28-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 329&lt;span id="cb28-8"&gt;&lt;a href="#cb28-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 330&lt;p&gt;You might notice that we are making extensive use of the &lt;code&gt;panic!&lt;/code&gt; macro. Well, that is because it is a good idea to quit on receiving an unsatisfactory &lt;code&gt;TokenStream&lt;/code&gt;.&lt;/p&gt;
331<p>With all our types ready, we can get on with generating type aliases:</p> 331&lt;p&gt;With all our types ready, we can get on with generating type aliases:&lt;/p&gt;
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> 332&lt;div class="sourceCode" id="cb29"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb29-1"&gt;&lt;a href="#cb29-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
333<span id="cb29-2"><a href="#cb29-2" aria-hidden="true"></a></span> 333&lt;span id="cb29-2"&gt;&lt;a href="#cb29-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 334&lt;span id="cb29-3"&gt;&lt;a href="#cb29-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;quote::&lt;/span&gt;&lt;span class="op"&gt;{&lt;/span&gt;quote&lt;span class="op"&gt;,&lt;/span&gt; format_ident&lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;
335<span id="cb29-4"><a href="#cb29-4" aria-hidden="true"></a></span> 335&lt;span id="cb29-4"&gt;&lt;a href="#cb29-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
336<span id="cb29-5"><a href="#cb29-5" aria-hidden="true"></a><span class="kw">fn</span> generate_type_aliases(</span> 336&lt;span id="cb29-5"&gt;&lt;a href="#cb29-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; generate_type_aliases(&lt;/span&gt;
337<span id="cb29-6"><a href="#cb29-6" aria-hidden="true"></a> fn_arg_types<span class="op">:</span> <span class="op">&amp;</span>[<span class="dt">Box</span><span class="op">&lt;</span>Type<span class="op">&gt;</span>]<span class="op">,</span></span> 337&lt;span id="cb29-6"&gt;&lt;a href="#cb29-6" aria-hidden="true"&gt;&lt;/a&gt; fn_arg_types&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;[&lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Type&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;]&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
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">&lt;</span>Type<span class="op">&gt;,</span></span> 338&lt;span id="cb29-7"&gt;&lt;a href="#cb29-7" aria-hidden="true"&gt;&lt;/a&gt; fn_return_type&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;Box&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Type&lt;span class="op"&gt;&amp;gt;,&lt;/span&gt;&lt;/span&gt;
339<span id="cb29-8"><a href="#cb29-8" aria-hidden="true"></a> fn_name<span class="op">:</span> <span class="op">&amp;</span><span class="pp">syn::</span>Ident<span class="op">,</span></span> 339&lt;span id="cb29-8"&gt;&lt;a href="#cb29-8" aria-hidden="true"&gt;&lt;/a&gt; fn_name&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;&lt;span class="pp"&gt;syn::&lt;/span&gt;Ident&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
340<span id="cb29-9"><a href="#cb29-9" aria-hidden="true"></a>) <span class="op">-&gt;</span> <span class="dt">Vec</span><span class="op">&lt;</span><span class="pp">proc_macro2::</span>TokenStream<span class="op">&gt;</span> <span class="op">{</span> <span class="co">// 1</span></span> 340&lt;span id="cb29-9"&gt;&lt;a href="#cb29-9" aria-hidden="true"&gt;&lt;/a&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Vec&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="pp"&gt;proc_macro2::&lt;/span&gt;TokenStream&lt;span class="op"&gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="co"&gt;// 1&lt;/span&gt;&lt;/span&gt;
341<span id="cb29-10"><a href="#cb29-10" aria-hidden="true"></a></span> 341&lt;span id="cb29-10"&gt;&lt;a href="#cb29-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">&quot;_{}_T0&quot;</span><span class="op">,</span> fn_name)<span class="op">;</span> <span class="co">// 2</span></span> 342&lt;span id="cb29-11"&gt;&lt;a href="#cb29-11" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; type_t0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;format_ident!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;_{}_T0&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; fn_name)&lt;span class="op"&gt;;&lt;/span&gt; &lt;span class="co"&gt;// 2&lt;/span&gt;&lt;/span&gt;
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> 343&lt;span id="cb29-12"&gt;&lt;a href="#cb29-12" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; &lt;span class="kw"&gt;mut&lt;/span&gt; type_aliases &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;vec!&lt;/span&gt;[&lt;span class="pp"&gt;quote!&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="kw"&gt;type&lt;/span&gt; #type_t0 &lt;span class="op"&gt;=&lt;/span&gt; #fn_return_type &lt;span class="op"&gt;}&lt;/span&gt;]&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
344<span id="cb29-13"><a href="#cb29-13" aria-hidden="true"></a></span> 344&lt;span id="cb29-13"&gt;&lt;a href="#cb29-13" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
345<span id="cb29-14"><a href="#cb29-14" aria-hidden="true"></a> <span class="co">// 3</span></span> 345&lt;span id="cb29-14"&gt;&lt;a href="#cb29-14" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="co"&gt;// 3&lt;/span&gt;&lt;/span&gt;
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> 346&lt;span id="cb29-15"&gt;&lt;a href="#cb29-15" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;for&lt;/span&gt; (i&lt;span class="op"&gt;,&lt;/span&gt; t) &lt;span class="kw"&gt;in&lt;/span&gt; (&lt;span class="dv"&gt;1&lt;/span&gt;&lt;span class="op"&gt;..&lt;/span&gt;)&lt;span class="op"&gt;.&lt;/span&gt;zip(fn_arg_types&lt;span class="op"&gt;.&lt;/span&gt;into_iter()&lt;span class="op"&gt;.&lt;/span&gt;rev()) &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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">&quot;_{}_{}&quot;</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">&quot;T{}&quot;</span><span class="op">,</span> i <span class="op">-</span> <span class="dv">1</span>))<span class="op">;</span></span> 347&lt;span id="cb29-16"&gt;&lt;a href="#cb29-16" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; p &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;format_ident!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;_{}_{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; fn_name&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;format!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;T{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; i &lt;span class="op"&gt;-&lt;/span&gt; &lt;span class="dv"&gt;1&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&quot;_{}_{}&quot;</span><span class="op">,</span> fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">&quot;T{}&quot;</span><span class="op">,</span> i))<span class="op">;</span></span> 348&lt;span id="cb29-17"&gt;&lt;a href="#cb29-17" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; n &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;format_ident!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;_{}_{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; fn_name&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;format!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;T{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; i))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
349<span id="cb29-18"><a href="#cb29-18" aria-hidden="true"></a></span> 349&lt;span id="cb29-18"&gt;&lt;a href="#cb29-18" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 350&lt;span id="cb29-19"&gt;&lt;a href="#cb29-19" aria-hidden="true"&gt;&lt;/a&gt; type_aliases&lt;span class="op"&gt;.&lt;/span&gt;push(&lt;span class="pp"&gt;quote!&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> #p</span> 351&lt;span id="cb29-20"&gt;&lt;a href="#cb29-20" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;type&lt;/span&gt; #n &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(#t) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; #p&lt;/span&gt;
352<span id="cb29-21"><a href="#cb29-21" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> 352&lt;span id="cb29-21"&gt;&lt;a href="#cb29-21" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
353<span id="cb29-22"><a href="#cb29-22" aria-hidden="true"></a> <span class="op">}</span></span> 353&lt;span id="cb29-22"&gt;&lt;a href="#cb29-22" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
354<span id="cb29-23"><a href="#cb29-23" aria-hidden="true"></a></span> 354&lt;span id="cb29-23"&gt;&lt;a href="#cb29-23" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 355&lt;span id="cb29-24"&gt;&lt;a href="#cb29-24" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; type_aliases&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
356<span id="cb29-25"><a href="#cb29-25" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 356&lt;span id="cb29-25"&gt;&lt;a href="#cb29-25" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
357<p><strong>1. The return value</strong><br /> 357&lt;p&gt;&lt;strong&gt;1. The return value&lt;/strong&gt;&lt;br /&gt;
358We are returning a <code>Vec&lt;proc_macro2::TokenStream&gt;</code>, i. e., a list of <code>TokenStream</code>s, where each item is a type alias.</p> 358We are returning a &lt;code&gt;Vec&amp;lt;proc_macro2::TokenStream&amp;gt;&lt;/code&gt;, i. e., a list of &lt;code&gt;TokenStream&lt;/code&gt;s, where each item is a type alias.&lt;/p&gt;
359<p><strong>2. Format identifier?</strong><br /> 359&lt;p&gt;&lt;strong&gt;2. Format identifier?&lt;/strong&gt;&lt;br /&gt;
360I’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> 360I’ve got some explanation to do on this line. Clearly, we are trying to write the first type alias, and initialize our &lt;code&gt;TokenStream&lt;/code&gt; vector with &lt;code&gt;T0&lt;/code&gt;, because it is different from the others:&lt;/p&gt;
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> 361&lt;div class="sourceCode" id="cb30"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb30-1"&gt;&lt;a href="#cb30-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; something&lt;/span&gt;
362<span id="cb30-2"><a href="#cb30-2" aria-hidden="true"></a><span class="co">// the others are of the form</span></span> 362&lt;span id="cb30-2"&gt;&lt;a href="#cb30-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// the others are of the form&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> something</span></code></pre></div> 363&lt;span id="cb30-3"&gt;&lt;a href="#cb30-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; Tr &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(something) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; something&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 364&lt;p&gt;&lt;code&gt;format_ident!&lt;/code&gt; is similar to &lt;code&gt;format!&lt;/code&gt;. Instead of returning a formatted string, it returns a &lt;code&gt;syn::Ident&lt;/code&gt;. Therefore, &lt;code&gt;type_t0&lt;/code&gt; is actually an identifier for, in the case of our &lt;code&gt;add&lt;/code&gt; function, &lt;code&gt;_add_T0&lt;/code&gt;. Why is this formatting important? Namespacing.&lt;/p&gt;
365<p>Picture this, we have two functions, <code>add</code> and <code>subtract</code>, that we wish to curry with our macro:</p> 365&lt;p&gt;Picture this, we have two functions, &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;subtract&lt;/code&gt;, that we wish to curry with our macro:&lt;/p&gt;
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> 366&lt;div class="sourceCode" id="cb31"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb31-1"&gt;&lt;a href="#cb31-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> 367&lt;span id="cb31-2"&gt;&lt;a href="#cb31-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add(&lt;span class="op"&gt;...&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="op"&gt;...&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
368<span id="cb31-3"><a href="#cb31-3" aria-hidden="true"></a></span> 368&lt;span id="cb31-3"&gt;&lt;a href="#cb31-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
369<span id="cb31-4"><a href="#cb31-4" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> 369&lt;span id="cb31-4"&gt;&lt;a href="#cb31-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> 370&lt;span id="cb31-5"&gt;&lt;a href="#cb31-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; sub(&lt;span class="op"&gt;...&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="op"&gt;...&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
371<p>Here is the same but with macros expanded:</p> 371&lt;p&gt;Here is the same but with macros expanded:&lt;/p&gt;
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> 372&lt;div class="sourceCode" id="cb32"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb32-1"&gt;&lt;a href="#cb32-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T0<span class="op">;</span></span> 373&lt;span id="cb32-2"&gt;&lt;a href="#cb32-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T0&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> 374&lt;span id="cb32-3"&gt;&lt;a href="#cb32-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add( &lt;span class="op"&gt;...&lt;/span&gt; ) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T1 &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="op"&gt;...&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
375<span id="cb32-4"><a href="#cb32-4" aria-hidden="true"></a></span> 375&lt;span id="cb32-4"&gt;&lt;a href="#cb32-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 376&lt;span id="cb32-5"&gt;&lt;a href="#cb32-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T0<span class="op">;</span></span> 377&lt;span id="cb32-6"&gt;&lt;a href="#cb32-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T0&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> 378&lt;span id="cb32-7"&gt;&lt;a href="#cb32-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; sub( &lt;span class="op"&gt;...&lt;/span&gt; ) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; T1 &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="op"&gt;...&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 379&lt;p&gt;We end up with two definitions of &lt;code&gt;T0&lt;/code&gt;! Now, if we do the little &lt;code&gt;format_ident!&lt;/code&gt; dance we did up there:&lt;/p&gt;
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> 380&lt;div class="sourceCode" id="cb33"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb33-1"&gt;&lt;a href="#cb33-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; _add_T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> _add_T0<span class="op">;</span></span> 381&lt;span id="cb33-2"&gt;&lt;a href="#cb33-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; _add_T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; _add_T0&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> _add_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span> 382&lt;span id="cb33-3"&gt;&lt;a href="#cb33-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add( &lt;span class="op"&gt;...&lt;/span&gt; ) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; _add_T1 &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="op"&gt;...&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
383<span id="cb33-4"><a href="#cb33-4" aria-hidden="true"></a></span> 383&lt;span id="cb33-4"&gt;&lt;a href="#cb33-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 384&lt;span id="cb33-5"&gt;&lt;a href="#cb33-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; _sub_T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> _sub_T0<span class="op">;</span></span> 385&lt;span id="cb33-6"&gt;&lt;a href="#cb33-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; _sub_T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; _sub_T0&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> _sub_T1 <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> 386&lt;span id="cb33-7"&gt;&lt;a href="#cb33-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; sub( &lt;span class="op"&gt;...&lt;/span&gt; ) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; _sub_T1 &lt;span class="op"&gt;{&lt;/span&gt; &lt;span class="op"&gt;...&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 387&lt;p&gt;Voilà! The type aliases don’t tread on each other. Remember to import &lt;code&gt;format_ident&lt;/code&gt; from the &lt;code&gt;quote&lt;/code&gt; crate.&lt;/p&gt;
388<p><strong>3. The TokenStream Vector</strong></p> 388&lt;p&gt;&lt;strong&gt;3. The TokenStream Vector&lt;/strong&gt;&lt;/p&gt;
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> 389&lt;p&gt;We iterate over our types in reverse order (&lt;code&gt;T0&lt;/code&gt; is the last return, &lt;code&gt;T1&lt;/code&gt; is the second last, so on), assign a number to each iteration with &lt;code&gt;zip&lt;/code&gt;, generate type names with &lt;code&gt;format_ident&lt;/code&gt;, push a &lt;code&gt;TokenStream&lt;/code&gt; with the help of &lt;code&gt;quote&lt;/code&gt; and variable interpolation.&lt;/p&gt;
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> 390&lt;p&gt;If you are wondering why we used &lt;code&gt;(1..).zip()&lt;/code&gt; instead of &lt;code&gt;.enumerate()&lt;/code&gt;, it’s because we wanted to start counting from 1 instead of 0 (we are already done with &lt;code&gt;T0&lt;/code&gt;!).&lt;/p&gt;
391<h4 id="getting-it-together">Getting it together</h4> 391&lt;h4 id="getting-it-together"&gt;Getting it together&lt;/h4&gt;
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> 392&lt;p&gt;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 &lt;code&gt;generate_curry&lt;/code&gt; function:&lt;/p&gt;
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> 393&lt;div class="sourceCode" id="cb34"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb34-1"&gt;&lt;a href="#cb34-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt;
394<span id="cb34-2"><a href="#cb34-2" aria-hidden="true"></a></span> 394&lt;span id="cb34-2"&gt;&lt;a href="#cb34-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">-&gt;</span> <span class="pp">proc_macro2::</span>TokenStream <span class="op">{</span></span> 395&lt;span id="cb34-3"&gt;&lt;a href="#cb34-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;fn&lt;/span&gt; generate_curry(parsed&lt;span class="op"&gt;:&lt;/span&gt; ItemFn) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;proc_macro2::&lt;/span&gt;TokenStream &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 396&lt;span id="cb34-4"&gt;&lt;a href="#cb34-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_body &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;block&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 397&lt;span id="cb34-5"&gt;&lt;a href="#cb34-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; sig &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;sig&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 398&lt;span id="cb34-6"&gt;&lt;a href="#cb34-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; vis &lt;span class="op"&gt;=&lt;/span&gt; parsed&lt;span class="op"&gt;.&lt;/span&gt;vis&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 399&lt;span id="cb34-7"&gt;&lt;a href="#cb34-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_name &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;ident&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 400&lt;span id="cb34-8"&gt;&lt;a href="#cb34-8" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_args &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;inputs&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 401&lt;span id="cb34-9"&gt;&lt;a href="#cb34-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; fn_return_type &lt;span class="op"&gt;=&lt;/span&gt; sig&lt;span class="op"&gt;.&lt;/span&gt;output&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
402<span id="cb34-10"><a href="#cb34-10" aria-hidden="true"></a></span> 402&lt;span id="cb34-10"&gt;&lt;a href="#cb34-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 403&lt;span id="cb34-11"&gt;&lt;a href="#cb34-11" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; arg_idents &lt;span class="op"&gt;=&lt;/span&gt; extract_arg_idents(fn_args&lt;span class="op"&gt;.&lt;/span&gt;clone())&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&amp;</span>arg_idents<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> 404&lt;span id="cb34-12"&gt;&lt;a href="#cb34-12" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; first_ident &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;arg_idents&lt;span class="op"&gt;.&lt;/span&gt;first()&lt;span class="op"&gt;.&lt;/span&gt;unwrap()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&amp;</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&lt;span id="cb34-13"&gt;&lt;a href="#cb34-13" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; curried_body &lt;span class="op"&gt;=&lt;/span&gt; generate_body(&lt;span class="op"&gt;&amp;amp;&lt;/span&gt;arg_idents[&lt;span class="dv"&gt;1&lt;/span&gt;&lt;span class="op"&gt;..&lt;/span&gt;]&lt;span class="op"&gt;,&lt;/span&gt; fn_body&lt;span class="op"&gt;.&lt;/span&gt;clone())&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
406<span id="cb34-14"><a href="#cb34-14" aria-hidden="true"></a></span> 406&lt;span id="cb34-14"&gt;&lt;a href="#cb34-14" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 407&lt;span id="cb34-15"&gt;&lt;a href="#cb34-15" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;let&lt;/span&gt; arg_types &lt;span class="op"&gt;=&lt;/span&gt; extract_arg_types(fn_args&lt;span class="op"&gt;.&lt;/span&gt;clone())&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">&amp;</span>arg_types<span class="op">.</span>first()<span class="op">.</span>unwrap()<span class="op">;</span></span> 408&lt;span id="cb34-16"&gt;&lt;a href="#cb34-16" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;let&lt;/span&gt; first_type &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;arg_types&lt;span class="op"&gt;.&lt;/span&gt;first()&lt;span class="op"&gt;.&lt;/span&gt;unwrap()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 409&lt;span id="cb34-17"&gt;&lt;a href="#cb34-17" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;let&lt;/span&gt; type_aliases &lt;span class="op"&gt;=&lt;/span&gt; generate_type_aliases(&lt;/span&gt;
410<span id="cb34-18"><a href="#cb34-18" aria-hidden="true"></a><span class="op">+</span> <span class="op">&amp;</span>arg_types[<span class="dv">1</span><span class="op">..</span>]<span class="op">,</span></span> 410&lt;span id="cb34-18"&gt;&lt;a href="#cb34-18" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;arg_types[&lt;span class="dv"&gt;1&lt;/span&gt;&lt;span class="op"&gt;..&lt;/span&gt;]&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
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> 411&lt;span id="cb34-19"&gt;&lt;a href="#cb34-19" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; extract_return_type(fn_return_type)&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
412<span id="cb34-20"><a href="#cb34-20" aria-hidden="true"></a><span class="op">+</span> <span class="op">&amp;</span>fn_name<span class="op">,</span></span> 412&lt;span id="cb34-20"&gt;&lt;a href="#cb34-20" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;fn_name&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
413<span id="cb34-21"><a href="#cb34-21" aria-hidden="true"></a><span class="op">+</span> )<span class="op">;</span></span> 413&lt;span id="cb34-21"&gt;&lt;a href="#cb34-21" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; )&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
414<span id="cb34-22"><a href="#cb34-22" aria-hidden="true"></a></span> 414&lt;span id="cb34-22"&gt;&lt;a href="#cb34-22" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">&quot;_{}_{}&quot;</span><span class="op">,</span> <span class="op">&amp;</span>fn_name<span class="op">,</span> <span class="pp">format!</span>(<span class="st">&quot;T{}&quot;</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&lt;span id="cb34-23"&gt;&lt;a href="#cb34-23" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;let&lt;/span&gt; return_type &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;format_ident!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;_{}_{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;fn_name&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;format!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;T{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; type_aliases&lt;span class="op"&gt;.&lt;/span&gt;len() &lt;span class="op"&gt;-&lt;/span&gt; &lt;span class="dv"&gt;1&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
416<span id="cb34-24"><a href="#cb34-24" aria-hidden="true"></a></span> 416&lt;span id="cb34-24"&gt;&lt;a href="#cb34-24" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 417&lt;span id="cb34-25"&gt;&lt;a href="#cb34-25" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="pp"&gt;quote!&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 418&lt;span id="cb34-26"&gt;&lt;a href="#cb34-26" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; #(#type_aliases)&lt;span class="op"&gt;;*&lt;/span&gt; &lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> #return_type <span class="op">{</span></span> 419&lt;span id="cb34-27"&gt;&lt;a href="#cb34-27" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; #vis &lt;span class="kw"&gt;fn&lt;/span&gt; #fn_name (#first_ident&lt;span class="op"&gt;:&lt;/span&gt; #first_type) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; #return_type &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
420<span id="cb34-28"><a href="#cb34-28" aria-hidden="true"></a><span class="op">+</span> #curried_body <span class="op">;</span></span> 420&lt;span id="cb34-28"&gt;&lt;a href="#cb34-28" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; #curried_body &lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
421<span id="cb34-29"><a href="#cb34-29" aria-hidden="true"></a><span class="op">+</span> <span class="op">}</span></span> 421&lt;span id="cb34-29"&gt;&lt;a href="#cb34-29" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
422<span id="cb34-30"><a href="#cb34-30" aria-hidden="true"></a><span class="op">+</span> <span class="op">};</span></span> 422&lt;span id="cb34-30"&gt;&lt;a href="#cb34-30" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;
423<span id="cb34-31"><a href="#cb34-31" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> 423&lt;span id="cb34-31"&gt;&lt;a href="#cb34-31" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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&lt;TokenStream&gt;</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&lt;p&gt;Most of the additions are self explanatory, I’ll go through the return statement with you. We are returning a &lt;code&gt;quote!{ ... }&lt;/code&gt;, so a &lt;code&gt;proc_macro2::TokenStream&lt;/code&gt;. We are iterating through the &lt;code&gt;type_aliases&lt;/code&gt; variable, which you might recall, is a &lt;code&gt;Vec&amp;lt;TokenStream&amp;gt;&lt;/code&gt;. You might notice the sneaky semicolon before the &lt;code&gt;*&lt;/code&gt;. This basically tells &lt;code&gt;quote&lt;/code&gt;, 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, &lt;code&gt;quote&lt;/code&gt; doesn’t insert a separator at the end of the iteration.&lt;/p&gt;
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> 425&lt;p&gt;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 &lt;code&gt;add&lt;/code&gt; function, we returned &lt;code&gt;T2&lt;/code&gt;, which was in fact, the last type alias we created.&lt;/p&gt;
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> 426&lt;p&gt;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.&lt;/p&gt;
427<h3 id="debugging-and-testing">Debugging and Testing</h3> 427&lt;h3 id="debugging-and-testing"&gt;Debugging and Testing&lt;/h3&gt;
428<p>Install <code>cargo-expand</code> via:</p> 428&lt;p&gt;Install &lt;code&gt;cargo-expand&lt;/code&gt; via:&lt;/p&gt;
429<pre><code>cargo install cargo-expand</code></pre> 429&lt;pre&gt;&lt;code&gt;cargo install cargo-expand&lt;/code&gt;&lt;/pre&gt;
430<p><code>cargo-expand</code> is a neat little tool that expands your macro in places where it is used, and lets you view the generated code! For example:</p> 430&lt;p&gt;&lt;code&gt;cargo-expand&lt;/code&gt; is a neat little tool that expands your macro in places where it is used, and lets you view the generated code! For example:&lt;/p&gt;
431<pre class="shell"><code># create a bin package hello 431&lt;pre class="shell"&gt;&lt;code&gt;# create a bin package hello
432$ cargo new hello 432$ cargo new hello
433 433
434# view the expansion of the println! macro 434# view the expansion of the println! macro
@@ -442,516 +442,516 @@ extern crate std;
442fn main() { 442fn main() {
443 { 443 {
444 ::std::io::_print(::core::fmt::Arguments::new_v1( 444 ::std::io::_print(::core::fmt::Arguments::new_v1(
445 &amp;[&quot;Hello, world!\n&quot;], 445 &amp;amp;[&amp;quot;Hello, world!\n&amp;quot;],
446 &amp;match () { 446 &amp;amp;match () {
447 () =&gt; [], 447 () =&amp;gt; [],
448 }, 448 },
449 )); 449 ));
450 }; 450 };
451}</code></pre> 451}&lt;/code&gt;&lt;/pre&gt;
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> 452&lt;p&gt;Writing proc-macros without &lt;code&gt;cargo-expand&lt;/code&gt; is tantamount to driving a vehicle without rear view mirrors! Keep an eye on what is going on behind your back.&lt;/p&gt;
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> 453&lt;p&gt;Now, your macro won’t always compile, you might just recieve the bee movie script as an error. &lt;code&gt;cargo-expand&lt;/code&gt; will not work in such cases. I would suggest printing out your variables to inspect them. &lt;code&gt;TokenStream&lt;/code&gt; implements &lt;code&gt;Display&lt;/code&gt; as well as &lt;code&gt;Debug&lt;/code&gt;. We don’t always have to be respectable programmers. Just print it.&lt;/p&gt;
454<p>Enough of that, lets get testing:</p> 454&lt;p&gt;Enough of that, lets get testing:&lt;/p&gt;
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> 455&lt;div class="sourceCode" id="cb37"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb37-1"&gt;&lt;a href="#cb37-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// tests/smoke.rs&lt;/span&gt;&lt;/span&gt;
456<span id="cb37-2"><a href="#cb37-2" aria-hidden="true"></a></span> 456&lt;span id="cb37-2"&gt;&lt;a href="#cb37-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 457&lt;span id="cb37-3"&gt;&lt;a href="#cb37-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#![&lt;/span&gt;feature&lt;span class="at"&gt;(&lt;/span&gt;type_alias_impl_trait&lt;span class="at"&gt;)]&lt;/span&gt;&lt;/span&gt;
458<span id="cb37-4"><a href="#cb37-4" aria-hidden="true"></a></span> 458&lt;span id="cb37-4"&gt;&lt;a href="#cb37-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 459&lt;span id="cb37-5"&gt;&lt;a href="#cb37-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;&lt;span class="pp"&gt;crate_name::&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 460&lt;span id="cb37-6"&gt;&lt;a href="#cb37-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; y&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; z&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 461&lt;span id="cb37-7"&gt;&lt;a href="#cb37-7" aria-hidden="true"&gt;&lt;/a&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;/span&gt;
462<span id="cb37-8"><a href="#cb37-8" aria-hidden="true"></a><span class="op">}</span></span> 462&lt;span id="cb37-8"&gt;&lt;a href="#cb37-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
463<span id="cb37-9"><a href="#cb37-9" aria-hidden="true"></a></span> 463&lt;span id="cb37-9"&gt;&lt;a href="#cb37-9" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
464<span id="cb37-10"><a href="#cb37-10" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> 464&lt;span id="cb37-10"&gt;&lt;a href="#cb37-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
465<span id="cb37-11"><a href="#cb37-11" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> 465&lt;span id="cb37-11"&gt;&lt;a href="#cb37-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; works() &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 466&lt;span id="cb37-12"&gt;&lt;a href="#cb37-12" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;assert_eq!&lt;/span&gt;(&lt;span class="dv"&gt;15&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; add(&lt;span class="dv"&gt;4&lt;/span&gt;)(&lt;span class="dv"&gt;5&lt;/span&gt;)(&lt;span class="dv"&gt;6&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
467<span id="cb37-13"><a href="#cb37-13" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 467&lt;span id="cb37-13"&gt;&lt;a href="#cb37-13" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
468<p>Run <code>cargo +nightly test</code>. You should see a pleasing message:</p> 468&lt;p&gt;Run &lt;code&gt;cargo +nightly test&lt;/code&gt;. You should see a pleasing message:&lt;/p&gt;
469<pre><code>running 1 test 469&lt;pre&gt;&lt;code&gt;running 1 test
470test tests::works ... ok</code></pre> 470test tests::works ... ok&lt;/code&gt;&lt;/pre&gt;
471<p>Take a look at the expansion for our curry macro, via <code>cargo +nightly expand --tests smoke</code>:</p> 471&lt;p&gt;Take a look at the expansion for our curry macro, via &lt;code&gt;cargo +nightly expand --tests smoke&lt;/code&gt;:&lt;/p&gt;
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> 472&lt;div class="sourceCode" id="cb39"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb39-1"&gt;&lt;a href="#cb39-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; _add_T0 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> _add_T0<span class="op">;</span></span> 473&lt;span id="cb39-2"&gt;&lt;a href="#cb39-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; _add_T1 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; _add_T0&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> _add_T1<span class="op">;</span></span> 474&lt;span id="cb39-3"&gt;&lt;a href="#cb39-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; _add_T2 &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;impl&lt;/span&gt; &lt;span class="bu"&gt;Fn&lt;/span&gt;(&lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; _add_T1&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> _add_T2 <span class="op">{</span></span> 475&lt;span id="cb39-4"&gt;&lt;a href="#cb39-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; add(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; _add_T2 &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 476&lt;span id="cb39-5"&gt;&lt;a href="#cb39-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; (&lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;y&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 477&lt;span id="cb39-6"&gt;&lt;a href="#cb39-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;move&lt;/span&gt; &lt;span class="op"&gt;|&lt;/span&gt;z&lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 478&lt;span id="cb39-7"&gt;&lt;a href="#cb39-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; x &lt;span class="op"&gt;+&lt;/span&gt; y &lt;span class="op"&gt;+&lt;/span&gt; z&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
479<span id="cb39-8"><a href="#cb39-8" aria-hidden="true"></a> <span class="op">}</span></span> 479&lt;span id="cb39-8"&gt;&lt;a href="#cb39-8" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
480<span id="cb39-9"><a href="#cb39-9" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> 480&lt;span id="cb39-9"&gt;&lt;a href="#cb39-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
481<span id="cb39-10"><a href="#cb39-10" aria-hidden="true"></a><span class="op">}</span></span> 481&lt;span id="cb39-10"&gt;&lt;a href="#cb39-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
482<span id="cb39-11"><a href="#cb39-11" aria-hidden="true"></a></span> 482&lt;span id="cb39-11"&gt;&lt;a href="#cb39-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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> 483&lt;span id="cb39-12"&gt;&lt;a href="#cb39-12" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// a bunch of other stuff generated by #[test] and assert_eq!&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
484<p>A sight for sore eyes.</p> 484&lt;p&gt;A sight for sore eyes.&lt;/p&gt;
485<p>Here is a more complex example that generates ten multiples of the first ten natural numbers:</p> 485&lt;p&gt;Here is a more complex example that generates ten multiples of the first ten natural numbers:&lt;/p&gt;
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> 486&lt;div class="sourceCode" id="cb40"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb40-1"&gt;&lt;a href="#cb40-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
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">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 487&lt;span id="cb40-2"&gt;&lt;a href="#cb40-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; product(x&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; y&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt;) &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;u32&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
488<span id="cb40-3"><a href="#cb40-3" aria-hidden="true"></a> x <span class="op">*</span> y</span> 488&lt;span id="cb40-3"&gt;&lt;a href="#cb40-3" aria-hidden="true"&gt;&lt;/a&gt; x &lt;span class="op"&gt;*&lt;/span&gt; y&lt;/span&gt;
489<span id="cb40-4"><a href="#cb40-4" aria-hidden="true"></a><span class="op">}</span></span> 489&lt;span id="cb40-4"&gt;&lt;a href="#cb40-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
490<span id="cb40-5"><a href="#cb40-5" aria-hidden="true"></a></span> 490&lt;span id="cb40-5"&gt;&lt;a href="#cb40-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
491<span id="cb40-6"><a href="#cb40-6" aria-hidden="true"></a><span class="kw">fn</span> multiples() <span class="op">-&gt;</span> <span class="dt">Vec</span><span class="op">&lt;</span><span class="dt">Vec</span><span class="op">&lt;</span><span class="dt">u32</span><span class="op">&gt;&gt;{</span></span> 491&lt;span id="cb40-6"&gt;&lt;a href="#cb40-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; multiples() &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Vec&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;Vec&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;u32&lt;/span&gt;&lt;span class="op"&gt;&amp;gt;&amp;gt;{&lt;/span&gt;&lt;/span&gt;
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> 492&lt;span id="cb40-7"&gt;&lt;a href="#cb40-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; v &lt;span class="op"&gt;=&lt;/span&gt; (&lt;span class="dv"&gt;1&lt;/span&gt;&lt;span class="op"&gt;..=&lt;/span&gt;&lt;span class="dv"&gt;10&lt;/span&gt;)&lt;span class="op"&gt;.&lt;/span&gt;map(product)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 493&lt;span id="cb40-8"&gt;&lt;a href="#cb40-8" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;return&lt;/span&gt; (&lt;span class="dv"&gt;1&lt;/span&gt;&lt;span class="op"&gt;..=&lt;/span&gt;&lt;span class="dv"&gt;10&lt;/span&gt;)&lt;/span&gt;
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> 494&lt;span id="cb40-9"&gt;&lt;a href="#cb40-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;map(&lt;span class="op"&gt;|&lt;/span&gt;x&lt;span class="op"&gt;|&lt;/span&gt; v&lt;span class="op"&gt;.&lt;/span&gt;clone()&lt;span class="op"&gt;.&lt;/span&gt;map(&lt;span class="op"&gt;|&lt;/span&gt;f&lt;span class="op"&gt;|&lt;/span&gt; f(x))&lt;span class="op"&gt;.&lt;/span&gt;collect())&lt;/span&gt;
495<span id="cb40-10"><a href="#cb40-10" aria-hidden="true"></a> <span class="op">.</span>collect()<span class="op">;</span></span> 495&lt;span id="cb40-10"&gt;&lt;a href="#cb40-10" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;collect()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
496<span id="cb40-11"><a href="#cb40-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 496&lt;span id="cb40-11"&gt;&lt;a href="#cb40-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
497<h3 id="notes">Notes</h3> 497&lt;h3 id="notes"&gt;Notes&lt;/h3&gt;
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> 498&lt;p&gt;I didn’t quite explain why we use &lt;code&gt;move |arg|&lt;/code&gt; in our closure. This is because we want to take ownership of the variable supplied to us. Take a look at this example:&lt;/p&gt;
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> 499&lt;div class="sourceCode" id="cb41"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb41-1"&gt;&lt;a href="#cb41-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; v &lt;span class="op"&gt;=&lt;/span&gt; add(&lt;span class="dv"&gt;5&lt;/span&gt;)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
500<span id="cb41-2"><a href="#cb41-2" aria-hidden="true"></a><span class="kw">let</span> g<span class="op">;</span></span> 500&lt;span id="cb41-2"&gt;&lt;a href="#cb41-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; g&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
501<span id="cb41-3"><a href="#cb41-3" aria-hidden="true"></a><span class="op">{</span></span> 501&lt;span id="cb41-3"&gt;&lt;a href="#cb41-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
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> 502&lt;span id="cb41-4"&gt;&lt;a href="#cb41-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; x &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dv"&gt;5&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
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> 503&lt;span id="cb41-5"&gt;&lt;a href="#cb41-5" aria-hidden="true"&gt;&lt;/a&gt; g &lt;span class="op"&gt;=&lt;/span&gt; v(x)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
504<span id="cb41-6"><a href="#cb41-6" aria-hidden="true"></a><span class="op">}</span></span> 504&lt;span id="cb41-6"&gt;&lt;a href="#cb41-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;
505<span id="cb41-7"><a href="#cb41-7" aria-hidden="true"></a><span class="pp">println!</span>(<span class="st">&quot;{}&quot;</span><span class="op">,</span> g(<span class="dv">2</span>))<span class="op">;</span></span></code></pre></div> 505&lt;span id="cb41-7"&gt;&lt;a href="#cb41-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="pp"&gt;println!&lt;/span&gt;(&lt;span class="st"&gt;&amp;quot;{}&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; g(&lt;span class="dv"&gt;2&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 506&lt;p&gt;Variable &lt;code&gt;x&lt;/code&gt; goes out of scope before &lt;code&gt;g&lt;/code&gt; can return a concrete value. If we take ownership of &lt;code&gt;x&lt;/code&gt; by &lt;code&gt;move&lt;/code&gt;ing it into our closure, we can expect this to work reliably. In fact, rustc understands this, and forces you to use &lt;code&gt;move&lt;/code&gt;.&lt;/p&gt;
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> 507&lt;p&gt;This usage of &lt;code&gt;move&lt;/code&gt; is exactly why &lt;strong&gt;a curried function without a return is useless&lt;/strong&gt;. 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.&lt;/p&gt;
508<h3 id="conclusion">Conclusion</h3> 508&lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt;
509<p>Currying may not seem to be all that useful. Curried functions are unwieldy in Rust because the standard library is not built around currying. If you enjoy the possibilities posed by currying, consider taking a look at Haskell or Scheme.</p> 509&lt;p&gt;Currying may not seem to be all that useful. Curried functions are unwieldy in Rust because the standard library is not built around currying. If you enjoy the possibilities posed by currying, consider taking a look at Haskell or Scheme.&lt;/p&gt;
510<p>My original intention with <a href="https://peppe.rs">peppe.rs</a> was to post condensed articles, a micro blog, but this one turned out extra long.</p> 510&lt;p&gt;My original intention with &lt;a href="https://peppe.rs"&gt;peppe.rs&lt;/a&gt; was to post condensed articles, a micro blog, but this one turned out extra long.&lt;/p&gt;
511<p>Perhaps I should call it a ‘macro’ blog :)</p> 511&lt;p&gt;Perhaps I should call it a ‘macro’ blog :)&lt;/p&gt;
512<section class="footnotes" role="doc-endnotes"> 512&lt;section class="footnotes" role="doc-endnotes"&gt;
513<hr /> 513&lt;hr /&gt;
514<ol> 514&lt;ol&gt;
515<li id="fn1" role="doc-endnote"><p><a href="https://doc.rust-lang.org/book/ch13-01-closures.html">https://doc.rust-lang.org/book/ch13-01-closures.html</a><a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 515&lt;li id="fn1" role="doc-endnote"&gt;&lt;p&gt;&lt;a href="https://doc.rust-lang.org/book/ch13-01-closures.html"&gt;https://doc.rust-lang.org/book/ch13-01-closures.html&lt;/a&gt;&lt;a href="#fnref1" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
516<li id="fn2" role="doc-endnote"><p><a href="https://caniuse.rs">caniuse.rs</a> contains an indexed list of features and their status.<a href="#fnref2" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 516&lt;li id="fn2" role="doc-endnote"&gt;&lt;p&gt;&lt;a href="https://caniuse.rs"&gt;caniuse.rs&lt;/a&gt; contains an indexed list of features and their status.&lt;a href="#fnref2" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
517</ol> 517&lt;/ol&gt;
518</section></description> 518&lt;/section&gt;</description>
519<link>https://peppe.rs/posts/auto-currying_rust_functions/</link> 519<link>https://peppe.rs/posts/auto-currying_rust_functions/</link>
520<pubDate>Sat, 09 May 2020 06:12:00 +0000</pubDate> 520<pubDate>Sat, 09 May 2020 06:12:00 +0000</pubDate>
521<guid>https://peppe.rs/posts/auto-currying_rust_functions/</guid> 521<guid>https://peppe.rs/posts/auto-currying_rust_functions/</guid>
522</item> 522</item>
523<item> 523<item>
524<title>Pixel Art In GIMP</title> 524<title>Pixel Art In GIMP</title>
525<description><p>I’ve always been an admirer of pixel art, because of it’s simplicity and it’s resemblance to bitmap font design. Recently, I decided to take the dive and make some art of my own.</p> 525<description>&lt;p&gt;I’ve always been an admirer of pixel art, because of it’s simplicity and it’s resemblance to bitmap font design. Recently, I decided to take the dive and make some art of my own.&lt;/p&gt;
526<p>I used GIMP because I am fairly familiar with it. Aseprite seems to be the editor of choice for animated pixel art though.</p> 526&lt;p&gt;I used GIMP because I am fairly familiar with it. Aseprite seems to be the editor of choice for animated pixel art though.&lt;/p&gt;
527<h3 id="setting-up-the-canvas">Setting up the canvas</h3> 527&lt;h3 id="setting-up-the-canvas"&gt;Setting up the canvas&lt;/h3&gt;
528<p>Picking a canvas size is daunting. Too small, and you won’t be able to fit in enough detail to make a legible piece. Too big and you’ve got too many pixels to work with!</p> 528&lt;p&gt;Picking a canvas size is daunting. Too small, and you won’t be able to fit in enough detail to make a legible piece. Too big and you’ve got too many pixels to work with!&lt;/p&gt;
529<p>I would suggest starting out with anywhere between 100x100 and 200x200. <a href="https://u.peppe.rs/u9.png">Here’s</a> a sample configuration.</p> 529&lt;p&gt;I would suggest starting out with anywhere between 100x100 and 200x200. &lt;a href="https://u.peppe.rs/u9.png"&gt;Here’s&lt;/a&gt; a sample configuration.&lt;/p&gt;
530<p>Sometimes I use a 10x10 grid, <code>View &gt; Show Grid</code> and <code>Edit &gt; Preferences &gt; Default Grid &gt; Spacing</code>, but that can get jarring, so I throw down a couple of guides, drag right or down from the left or top gutters for vertical and horizontal guides respectively.</p> 530&lt;p&gt;Sometimes I use a 10x10 grid, &lt;code&gt;View &amp;gt; Show Grid&lt;/code&gt; and &lt;code&gt;Edit &amp;gt; Preferences &amp;gt; Default Grid &amp;gt; Spacing&lt;/code&gt;, but that can get jarring, so I throw down a couple of guides, drag right or down from the left or top gutters for vertical and horizontal guides respectively.&lt;/p&gt;
531<h3 id="choosing-a-brush">Choosing a Brush</h3> 531&lt;h3 id="choosing-a-brush"&gt;Choosing a Brush&lt;/h3&gt;
532<p>The most important part of our setup is the brush. Use the Pencil Tool (<code>n</code> on the keyboard) for hard edge drawings. Here’s a small comparison if you don’t know the difference between a hard edge and a soft edge:</p> 532&lt;p&gt;The most important part of our setup is the brush. Use the Pencil Tool (&lt;code&gt;n&lt;/code&gt; on the keyboard) for hard edge drawings. Here’s a small comparison if you don’t know the difference between a hard edge and a soft edge:&lt;/p&gt;
533<figure> 533&lt;figure&gt;
534<img src="https://u.peppe.rs/kz.png" alt="" /><figcaption>Hard edge vs Soft Edge</figcaption> 534&lt;img src="https://u.peppe.rs/kz.png" alt="" /&gt;&lt;figcaption&gt;Hard edge vs Soft Edge&lt;/figcaption&gt;
535</figure> 535&lt;/figure&gt;
536<p>I turn the size down all the way to 1 (<code>[</code> on the keyboard). Set <code>Dynamics</code> off. <a href="https://u.peppe.rs/Fs.png">Here’s</a> a sample brush configuration.</p> 536&lt;p&gt;I turn the size down all the way to 1 (&lt;code&gt;[&lt;/code&gt; on the keyboard). Set &lt;code&gt;Dynamics&lt;/code&gt; off. &lt;a href="https://u.peppe.rs/Fs.png"&gt;Here’s&lt;/a&gt; a sample brush configuration.&lt;/p&gt;
537<h3 id="laying-down-the-pixels">Laying down the pixels!</h3> 537&lt;h3 id="laying-down-the-pixels"&gt;Laying down the pixels!&lt;/h3&gt;
538<p>With the boring stuff out of the way, we can start with our piece. I usually follow a three step process:</p> 538&lt;p&gt;With the boring stuff out of the way, we can start with our piece. I usually follow a three step process:&lt;/p&gt;
539<ul> 539&lt;ul&gt;
540<li>draw a rough outline</li> 540&lt;li&gt;draw a rough outline&lt;/li&gt;
541<li>fill in the shadows</li> 541&lt;li&gt;fill in the shadows&lt;/li&gt;
542<li>add highlights</li> 542&lt;li&gt;add highlights&lt;/li&gt;
543</ul> 543&lt;/ul&gt;
544<p>But this process is better explained with an example: an onigiri. Let us start off with a 100x100 canvas.</p> 544&lt;p&gt;But this process is better explained with an example: an onigiri. Let us start off with a 100x100 canvas.&lt;/p&gt;
545<h4 id="drawing-the-outline">Drawing the outline</h4> 545&lt;h4 id="drawing-the-outline"&gt;Drawing the outline&lt;/h4&gt;
546<p>For the most part, our figure will be symmetric. If you are on GIMP 2.10+, you can take advantage of the Symmetry Painting feature. Go ahead and enable vertical symmetry, <code>Window &gt; Dockable Dialogs &gt; Symmetry Painting</code> and <code>Symmetry Painting &gt; Symmetry &gt; Mirror &gt; Vertical</code>.</p> 546&lt;p&gt;For the most part, our figure will be symmetric. If you are on GIMP 2.10+, you can take advantage of the Symmetry Painting feature. Go ahead and enable vertical symmetry, &lt;code&gt;Window &amp;gt; Dockable Dialogs &amp;gt; Symmetry Painting&lt;/code&gt; and &lt;code&gt;Symmetry Painting &amp;gt; Symmetry &amp;gt; Mirror &amp;gt; Vertical&lt;/code&gt;.&lt;/p&gt;
547<p>If you are running an older version of GIMP, draw in the left side, duplicate the layer, flip it horizontally, and merge it with the original.</p> 547&lt;p&gt;If you are running an older version of GIMP, draw in the left side, duplicate the layer, flip it horizontally, and merge it with the original.&lt;/p&gt;
548<p>Your outline might look something like this:</p> 548&lt;p&gt;Your outline might look something like this:&lt;/p&gt;
549<p><img src="https://u.peppe.rs/mn.png" /></p> 549&lt;p&gt;&lt;img src="https://u.peppe.rs/mn.png" /&gt;&lt;/p&gt;
550<p>Go ahead and fill it in with the fill tool (<code>Shift + b</code> on the keyboard), add in some seaweed as well, preferably on a different layer. You can toggle symmetry on and off to save yourself some time.</p> 550&lt;p&gt;Go ahead and fill it in with the fill tool (&lt;code&gt;Shift + b&lt;/code&gt; on the keyboard), add in some seaweed as well, preferably on a different layer. You can toggle symmetry on and off to save yourself some time.&lt;/p&gt;
551<p><img src="https://u.peppe.rs/xu.png" /></p> 551&lt;p&gt;&lt;img src="https://u.peppe.rs/xu.png" /&gt;&lt;/p&gt;
552<h4 id="shadows">Shadows</h4> 552&lt;h4 id="shadows"&gt;Shadows&lt;/h4&gt;
553<p>For now, let us focus on the shadows on the object itself, we’ll come back to the shadows cast by the object on the surface later.</p> 553&lt;p&gt;For now, let us focus on the shadows on the object itself, we’ll come back to the shadows cast by the object on the surface later.&lt;/p&gt;
554<p>Shadows on any surface always follow the shape of the surface. A spherical onigiri would have a circular shadow:</p> 554&lt;p&gt;Shadows on any surface always follow the shape of the surface. A spherical onigiri would have a circular shadow:&lt;/p&gt;
555<p><img src="https://u.peppe.rs/FU.png" /></p> 555&lt;p&gt;&lt;img src="https://u.peppe.rs/FU.png" /&gt;&lt;/p&gt;
556<p>A couple of noticeable changes:</p> 556&lt;p&gt;A couple of noticeable changes:&lt;/p&gt;
557<p><strong>Layers</strong>: The layer containing the seaweed has been hidden.<br /> 557&lt;p&gt;&lt;strong&gt;Layers&lt;/strong&gt;: The layer containing the seaweed has been hidden.&lt;br /&gt;
558<strong>Color</strong>: The color of the shadow is just a slightly lighter version of the original object (reduce the Value on the HSV scale).<br /> 558&lt;strong&gt;Color&lt;/strong&gt;: The color of the shadow is just a slightly lighter version of the original object (reduce the Value on the HSV scale).&lt;br /&gt;
559<strong>Area</strong>: The shadow does not go all the way (notice the bottom edges).</p> 559&lt;strong&gt;Area&lt;/strong&gt;: The shadow does not go all the way (notice the bottom edges).&lt;/p&gt;
560<p>The shadow does not go all the way because we will be filling in that area with another, darker shadow! An image might explain better:</p> 560&lt;p&gt;The shadow does not go all the way because we will be filling in that area with another, darker shadow! An image might explain better:&lt;/p&gt;
561<p><img src="https://u.peppe.rs/Br.png" /></p> 561&lt;p&gt;&lt;img src="https://u.peppe.rs/Br.png" /&gt;&lt;/p&gt;
562<p>To emulate soft lights, reduce the value by 2 to 3 points every iteration. Notice how area <code>1</code> is much larger than area <code>4</code>. This is because an onigiri resembles a bottom heavy oblate spheroid, a sphere that is slightly fatter around the lower bottom, and areas <code>1</code> and <code>2</code> catch more light than areas <code>3</code> and <code>4</code>.</p> 562&lt;p&gt;To emulate soft lights, reduce the value by 2 to 3 points every iteration. Notice how area &lt;code&gt;1&lt;/code&gt; is much larger than area &lt;code&gt;4&lt;/code&gt;. This is because an onigiri resembles a bottom heavy oblate spheroid, a sphere that is slightly fatter around the lower bottom, and areas &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; catch more light than areas &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;
563<p>Do the same with the seaweed. The seaweed, being a smaller, flatter object, doesn’t cast much of a shadow, so stop with 1 or 2 iterations of the gradient:</p> 563&lt;p&gt;Do the same with the seaweed. The seaweed, being a smaller, flatter object, doesn’t cast much of a shadow, so stop with 1 or 2 iterations of the gradient:&lt;/p&gt;
564<p><img src="https://u.peppe.rs/T3.png" /></p> 564&lt;p&gt;&lt;img src="https://u.peppe.rs/T3.png" /&gt;&lt;/p&gt;
565<p>We’re getting there!</p> 565&lt;p&gt;We’re getting there!&lt;/p&gt;
566<h4 id="highlights">Highlights</h4> 566&lt;h4 id="highlights"&gt;Highlights&lt;/h4&gt;
567<p>This step handles the details on the strongly illuminated portions of the object. Seaweed is a bit glossy, lighten the edges to make it seem shiny. The rice is not as shiny, but it does form an uneven surface. Add in some shadows to promote the idea of rice grains. Here is the finished result:</p> 567&lt;p&gt;This step handles the details on the strongly illuminated portions of the object. Seaweed is a bit glossy, lighten the edges to make it seem shiny. The rice is not as shiny, but it does form an uneven surface. Add in some shadows to promote the idea of rice grains. Here is the finished result:&lt;/p&gt;
568<p><img src="https://u.peppe.rs/VE.png" /></p> 568&lt;p&gt;&lt;img src="https://u.peppe.rs/VE.png" /&gt;&lt;/p&gt;
569<h3 id="finishing-touches">Finishing Touches</h3> 569&lt;h3 id="finishing-touches"&gt;Finishing Touches&lt;/h3&gt;
570<p>Some color correction and <code>a e s t h e t i c</code> Japanese text later, our piece is complete!</p> 570&lt;p&gt;Some color correction and &lt;code&gt;a e s t h e t i c&lt;/code&gt; Japanese text later, our piece is complete!&lt;/p&gt;
571<p><img src="https://u.peppe.rs/cn.png" /></p> 571&lt;p&gt;&lt;img src="https://u.peppe.rs/cn.png" /&gt;&lt;/p&gt;
572<p>Hold on, why is it so tiny? Well, that’s because our canvas was 100x100, head over to <code>Image &gt; Scale Image</code>, set <code>Quality &gt; Interpolation</code> to <code>None</code> and scale it up to 700x700, et voilà!</p> 572&lt;p&gt;Hold on, why is it so tiny? Well, that’s because our canvas was 100x100, head over to &lt;code&gt;Image &amp;gt; Scale Image&lt;/code&gt;, set &lt;code&gt;Quality &amp;gt; Interpolation&lt;/code&gt; to &lt;code&gt;None&lt;/code&gt; and scale it up to 700x700, et voilà!&lt;/p&gt;
573<p><img src="https://u.peppe.rs/CH.png" /></p></description> 573&lt;p&gt;&lt;img src="https://u.peppe.rs/CH.png" /&gt;&lt;/p&gt;</description>
574<link>https://peppe.rs/posts/pixel_art_in_GIMP/</link> 574<link>https://peppe.rs/posts/pixel_art_in_GIMP/</link>
575<pubDate>Thu, 09 Apr 2020 16:28:00 +0000</pubDate> 575<pubDate>Thu, 09 Apr 2020 16:28:00 +0000</pubDate>
576<guid>https://peppe.rs/posts/pixel_art_in_GIMP/</guid> 576<guid>https://peppe.rs/posts/pixel_art_in_GIMP/</guid>
577</item> 577</item>
578<item> 578<item>
579<title>Rapid Refactoring With Vim</title> 579<title>Rapid Refactoring With Vim</title>
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> 580<description>&lt;p&gt;Last weekend, I was tasked with refactoring the 96 unit tests on &lt;a href="https://github.com/ruma/ruma-events/pull/70"&gt;ruma-events&lt;/a&gt; to use strictly typed json objects using &lt;code&gt;serde_json::json!&lt;/code&gt; instead of raw strings. It was rather painless thanks to vim :)&lt;/p&gt;
581<p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> 581&lt;p&gt;Here’s a small sample of what had to be done (note the lines prefixed with the arrow):&lt;/p&gt;
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> 582&lt;div class="sourceCode" id="cb1"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb1-1"&gt;&lt;a href="#cb1-1" aria-hidden="true"&gt;&lt;/a&gt;→ &lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;serde_json::&lt;/span&gt;&lt;span class="op"&gt;{&lt;/span&gt;from_str&lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;
583<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a> </span> 583&lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;/span&gt;
584<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> 584&lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
585<span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 585&lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;fn&lt;/span&gt; deserialize() &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
586<span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> 586&lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;assert_eq!&lt;/span&gt;(&lt;/span&gt;
587<span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a>→ <span class="pp">from_str::</span><span class="op">&lt;</span>Action<span class="op">&gt;</span>(<span class="st">r#&quot;{&quot;set_tweak&quot;: &quot;highlight&quot;}&quot;#),</span></span> 587&lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true"&gt;&lt;/a&gt;→ &lt;span class="pp"&gt;from_str::&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Action&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;(&lt;span class="st"&gt;r#&amp;quot;{&amp;quot;set_tweak&amp;quot;: &amp;quot;highlight&amp;quot;}&amp;quot;#),&lt;/span&gt;&lt;/span&gt;
588<span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a><span class="st"> Action::SetTweak(Tweak::Highlight { value: true })</span></span> 588&lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="st"&gt; Action::SetTweak(Tweak::Highlight { value: true })&lt;/span&gt;&lt;/span&gt;
589<span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="st"> );</span></span> 589&lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="st"&gt; );&lt;/span&gt;&lt;/span&gt;
590<span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a><span class="st"> }</span></span></code></pre></div> 590&lt;span id="cb1-9"&gt;&lt;a href="#cb1-9" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="st"&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
591<p>had to be converted to:</p> 591&lt;p&gt;had to be converted to:&lt;/p&gt;
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> 592&lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb2-1"&gt;&lt;a href="#cb2-1" aria-hidden="true"&gt;&lt;/a&gt;→ &lt;span class="kw"&gt;use&lt;/span&gt; &lt;span class="pp"&gt;serde_json::&lt;/span&gt;&lt;span class="op"&gt;{&lt;/span&gt;from_value&lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;
593<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> </span> 593&lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;/span&gt;
594<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> 594&lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt;
595<span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 595&lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;fn&lt;/span&gt; deserialize() &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt;
596<span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> 596&lt;span id="cb2-5"&gt;&lt;a href="#cb2-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;assert_eq!&lt;/span&gt;(&lt;/span&gt;
597<span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a>→ <span class="pp">from_value::</span><span class="op">&lt;</span>Action<span class="op">&gt;</span>(<span class="pp">json!</span>(<span class="op">{</span><span class="st">&quot;set_tweak&quot;</span><span class="op">:</span> <span class="st">&quot;highlight&quot;</span><span class="op">}</span>))<span class="op">,</span></span> 597&lt;span id="cb2-6"&gt;&lt;a href="#cb2-6" aria-hidden="true"&gt;&lt;/a&gt;→ &lt;span class="pp"&gt;from_value::&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;Action&lt;span class="op"&gt;&amp;gt;&lt;/span&gt;(&lt;span class="pp"&gt;json!&lt;/span&gt;(&lt;span class="op"&gt;{&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;set_tweak&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;highlight&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;}&lt;/span&gt;))&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt;
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> 598&lt;span id="cb2-7"&gt;&lt;a href="#cb2-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;Action::&lt;/span&gt;SetTweak(&lt;span class="pp"&gt;Tweak::&lt;/span&gt;Highlight &lt;span class="op"&gt;{&lt;/span&gt; value&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="cn"&gt;true&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;)&lt;/span&gt;
599<span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a> )<span class="op">;</span></span> 599&lt;span id="cb2-8"&gt;&lt;a href="#cb2-8" aria-hidden="true"&gt;&lt;/a&gt; )&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;
600<span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> 600&lt;span id="cb2-9"&gt;&lt;a href="#cb2-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
601<h3 id="the-arglist">The arglist</h3> 601&lt;h3 id="the-arglist"&gt;The arglist&lt;/h3&gt;
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> 602&lt;p&gt;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 &lt;code&gt;#[cfg(test)]&lt;/code&gt; attribute. I opened all such files:&lt;/p&gt;
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> 603&lt;div class="sourceCode" id="cb3"&gt;&lt;pre class="sourceCode bash"&gt;&lt;code class="sourceCode bash"&gt;&lt;span id="cb3-1"&gt;&lt;a href="#cb3-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# `grep -l pattern files` lists all the files&lt;/span&gt;&lt;/span&gt;
604<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a><span class="co"># matching the pattern</span></span> 604&lt;span id="cb3-2"&gt;&lt;a href="#cb3-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# matching the pattern&lt;/span&gt;&lt;/span&gt;
605<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a></span> 605&lt;span id="cb3-3"&gt;&lt;a href="#cb3-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">&#39;cfg\(test\)&#39;</span> ./**/*.rs<span class="va">)</span></span> 606&lt;span id="cb3-4"&gt;&lt;a href="#cb3-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="ex"&gt;vim&lt;/span&gt; &lt;span class="va"&gt;$(&lt;/span&gt;&lt;span class="fu"&gt;grep&lt;/span&gt; -l &lt;span class="st"&gt;&amp;#39;cfg\(test\)&amp;#39;&lt;/span&gt; ./**/*.rs&lt;span class="va"&gt;)&lt;/span&gt;&lt;/span&gt;
607<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a></span> 607&lt;span id="cb3-5"&gt;&lt;a href="#cb3-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
608<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a><span class="co"># expands to something like:</span></span> 608&lt;span id="cb3-6"&gt;&lt;a href="#cb3-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# expands to something like:&lt;/span&gt;&lt;/span&gt;
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> 609&lt;span id="cb3-7"&gt;&lt;a href="#cb3-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="ex"&gt;vim&lt;/span&gt; push_rules.rs room/member.rs key/verification/lib.rs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
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> 610&lt;p&gt;Starting vim with more than one file at the shell prompt populates the arglist. Hit &lt;code&gt;:args&lt;/code&gt; to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with &lt;code&gt;:next&lt;/code&gt; and &lt;code&gt;:prev&lt;/code&gt;. I use tpope’s vim-unimpaired &lt;a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;, which adds &lt;code&gt;]a&lt;/code&gt; and &lt;code&gt;[a&lt;/code&gt;, mapped to &lt;code&gt;:next&lt;/code&gt; and &lt;code&gt;:prev&lt;/code&gt;.&lt;/p&gt;
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> 611&lt;p&gt;All that’s left to do is the find and replace, for which we will be using vim’s &lt;code&gt;argdo&lt;/code&gt;, applying a substitution to every file in the arglist:&lt;/p&gt;
612<pre><code>:argdo s/from_str/from_value/g</code></pre> 612&lt;pre&gt;&lt;code&gt;:argdo s/from_str/from_value/g&lt;/code&gt;&lt;/pre&gt;
613<h3 id="the-quickfix-list">The quickfix list</h3> 613&lt;h3 id="the-quickfix-list"&gt;The quickfix list&lt;/h3&gt;
614<p>Next up, replacing <code>r#" ... "#</code> with <code>json!( ... )</code>. I couldn’t search and replace that trivially, so I went with a macro call <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> instead, starting with the cursor on ‘r’, represented by the caret, in my attempt to breakdown the process:</p> 614&lt;p&gt;Next up, replacing &lt;code&gt;r#" ... "#&lt;/code&gt; with &lt;code&gt;json!( ... )&lt;/code&gt;. I couldn’t search and replace that trivially, so I went with a macro call &lt;a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; instead, starting with the cursor on ‘r’, represented by the caret, in my attempt to breakdown the process:&lt;/p&gt;
615<pre><code>BUFFER: r#&quot; ... &quot;#; 615&lt;pre&gt;&lt;code&gt;BUFFER: r#&amp;quot; ... &amp;quot;#;
616 ^ 616 ^
617 617
618ACTION: vllsjson!( 618ACTION: vllsjson!(
619 619
620BUFFER json!( ... &quot;#; 620BUFFER json!( ... &amp;quot;#;
621 ^ 621 ^
622 622
623ACTION: &lt;esc&gt;$F# 623ACTION: &amp;lt;esc&amp;gt;$F#
624 624
625BUFFER: json!( ... &quot;#; 625BUFFER: json!( ... &amp;quot;#;
626 ^ 626 ^
627 627
628ACTION: vhs)&lt;esc&gt; 628ACTION: vhs)&amp;lt;esc&amp;gt;
629 629
630BUFFER: json!( ... );</code></pre> 630BUFFER: json!( ... );&lt;/code&gt;&lt;/pre&gt;
631<p>Here’s the recorded <a href="#fn3" class="footnote-ref" id="fnref3" role="doc-noteref"><sup>3</sup></a> macro in all its glory: <code>vllsjson!(&lt;esc&gt;$F#vhs)&lt;esc&gt;</code>.</p> 631&lt;p&gt;Here’s the recorded &lt;a href="#fn3" class="footnote-ref" id="fnref3" role="doc-noteref"&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt; macro in all its glory: &lt;code&gt;vllsjson!(&amp;lt;esc&amp;gt;$F#vhs)&amp;lt;esc&amp;gt;&lt;/code&gt;.&lt;/p&gt;
632<p>Great! So now we just go ahead, find every occurrence of <code>r#</code> and apply the macro right? Unfortunately, there were more than a few occurrences of raw strings that had to stay raw strings. Enter, the quickfix list.</p> 632&lt;p&gt;Great! So now we just go ahead, find every occurrence of &lt;code&gt;r#&lt;/code&gt; and apply the macro right? Unfortunately, there were more than a few occurrences of raw strings that had to stay raw strings. Enter, the quickfix list.&lt;/p&gt;
633<p>The idea behind the quickfix list is to jump from one position in a file to another (maybe in a different file), much like how the arglist lets you jump from one file to another.</p> 633&lt;p&gt;The idea behind the quickfix list is to jump from one position in a file to another (maybe in a different file), much like how the arglist lets you jump from one file to another.&lt;/p&gt;
634<p>One of the easiest ways to populate this list with a bunch of positions is to use <code>vimgrep</code>:</p> 634&lt;p&gt;One of the easiest ways to populate this list with a bunch of positions is to use &lt;code&gt;vimgrep&lt;/code&gt;:&lt;/p&gt;
635<pre><code># basic usage 635&lt;pre&gt;&lt;code&gt;# basic usage
636:vimgrep pattern files 636:vimgrep pattern files
637 637
638# search for raw strings 638# search for raw strings
639:vimgrep &#39;r#&#39; ./**/*.rs</code></pre> 639:vimgrep &amp;#39;r#&amp;#39; ./**/*.rs&lt;/code&gt;&lt;/pre&gt;
640<p>Like <code>:next</code> and <code>:prev</code>, you can navigate the quickfix list with <code>:cnext</code> and <code>:cprev</code>. Every time you move up or down the list, vim indicates your index:</p> 640&lt;p&gt;Like &lt;code&gt;:next&lt;/code&gt; and &lt;code&gt;:prev&lt;/code&gt;, you can navigate the quickfix list with &lt;code&gt;:cnext&lt;/code&gt; and &lt;code&gt;:cprev&lt;/code&gt;. Every time you move up or down the list, vim indicates your index:&lt;/p&gt;
641<pre><code>(1 of 131): r#&quot;{&quot;set_tweak&quot;: &quot;highlight&quot;}&quot;#;</code></pre> 641&lt;pre&gt;&lt;code&gt;(1 of 131): r#&amp;quot;{&amp;quot;set_tweak&amp;quot;: &amp;quot;highlight&amp;quot;}&amp;quot;#;&lt;/code&gt;&lt;/pre&gt;
642<p>And just like <code>argdo</code>, you can <code>cdo</code> to apply commands to <em>every</em> match in the quickfix list:</p> 642&lt;p&gt;And just like &lt;code&gt;argdo&lt;/code&gt;, you can &lt;code&gt;cdo&lt;/code&gt; to apply commands to &lt;em&gt;every&lt;/em&gt; match in the quickfix list:&lt;/p&gt;
643<pre><code>:cdo norm! @q</code></pre> 643&lt;pre&gt;&lt;code&gt;:cdo norm! @q&lt;/code&gt;&lt;/pre&gt;
644<p>But, I had to manually pick out matches, and it involved some button mashing.</p> 644&lt;p&gt;But, I had to manually pick out matches, and it involved some button mashing.&lt;/p&gt;
645<h3 id="external-filtering">External Filtering</h3> 645&lt;h3 id="external-filtering"&gt;External Filtering&lt;/h3&gt;
646<p>Some code reviews later, I was asked to format all the json inside the <code>json!</code> macro. All you have to do is pass a visual selection through a pretty json printer. Select the range to be formatted in visual mode, and hit <code>:</code>, you will notice the command line displaying what seems to be gibberish:</p> 646&lt;p&gt;Some code reviews later, I was asked to format all the json inside the &lt;code&gt;json!&lt;/code&gt; macro. All you have to do is pass a visual selection through a pretty json printer. Select the range to be formatted in visual mode, and hit &lt;code&gt;:&lt;/code&gt;, you will notice the command line displaying what seems to be gibberish:&lt;/p&gt;
647<pre><code>:&#39;&lt;,&#39;&gt;</code></pre> 647&lt;pre&gt;&lt;code&gt;:&amp;#39;&amp;lt;,&amp;#39;&amp;gt;&lt;/code&gt;&lt;/pre&gt;
648<p><code>'&lt;</code> and <code>'&gt;</code> are <em>marks</em> <a href="#fn4" class="footnote-ref" id="fnref4" role="doc-noteref"><sup>4</sup></a>. More specifically, they are marks that vim sets automatically every time you make a visual selection, denoting the start and end of the selection.</p> 648&lt;p&gt;&lt;code&gt;'&amp;lt;&lt;/code&gt; and &lt;code&gt;'&amp;gt;&lt;/code&gt; are &lt;em&gt;marks&lt;/em&gt; &lt;a href="#fn4" class="footnote-ref" id="fnref4" role="doc-noteref"&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;. More specifically, they are marks that vim sets automatically every time you make a visual selection, denoting the start and end of the selection.&lt;/p&gt;
649<p>A range is one or more line specifiers separated by a <code>,</code>:</p> 649&lt;p&gt;A range is one or more line specifiers separated by a &lt;code&gt;,&lt;/code&gt;:&lt;/p&gt;
650<pre><code>:1,7 lines 1 through 7 650&lt;pre&gt;&lt;code&gt;:1,7 lines 1 through 7
651:32 just line 32 651:32 just line 32
652:. the current line 652:. the current line
653:.,$ the current line to the last line 653:.,$ the current line to the last line
654:&#39;a,&#39;b mark &#39;a&#39; to mark &#39;b&#39;</code></pre> 654:&amp;#39;a,&amp;#39;b mark &amp;#39;a&amp;#39; to mark &amp;#39;b&amp;#39;&lt;/code&gt;&lt;/pre&gt;
655<p>Most <code>:</code> commands can be prefixed by ranges. <code>:help usr_10.txt</code> for more on that.</p> 655&lt;p&gt;Most &lt;code&gt;:&lt;/code&gt; commands can be prefixed by ranges. &lt;code&gt;:help usr_10.txt&lt;/code&gt; for more on that.&lt;/p&gt;
656<p>Alright, lets pass json through <code>python -m json.tool</code>, a json formatter that accepts <code>stdin</code> (note the use of <code>!</code> to make use of an external program):</p> 656&lt;p&gt;Alright, lets pass json through &lt;code&gt;python -m json.tool&lt;/code&gt;, a json formatter that accepts &lt;code&gt;stdin&lt;/code&gt; (note the use of &lt;code&gt;!&lt;/code&gt; to make use of an external program):&lt;/p&gt;
657<pre><code>:&#39;&lt;,&#39;&gt;!python -m json.tool</code></pre> 657&lt;pre&gt;&lt;code&gt;:&amp;#39;&amp;lt;,&amp;#39;&amp;gt;!python -m json.tool&lt;/code&gt;&lt;/pre&gt;
658<p>Unfortunately that didn’t quite work for me because the range included some non-json text as well, a mix of regex and macros helped fix that. I think you get the drift.</p> 658&lt;p&gt;Unfortunately that didn’t quite work for me because the range included some non-json text as well, a mix of regex and macros helped fix that. I think you get the drift.&lt;/p&gt;
659<p>Another fun filter I use from time to time is <code>:!sort</code>, to sort css attributes, or <code>:!uniq</code> to remove repeated imports.</p> 659&lt;p&gt;Another fun filter I use from time to time is &lt;code&gt;:!sort&lt;/code&gt;, to sort css attributes, or &lt;code&gt;:!uniq&lt;/code&gt; to remove repeated imports.&lt;/p&gt;
660<section class="footnotes" role="doc-endnotes"> 660&lt;section class="footnotes" role="doc-endnotes"&gt;
661<hr /> 661&lt;hr /&gt;
662<ol> 662&lt;ol&gt;
663<li id="fn1" role="doc-endnote"><p>https://github.com/tpope/vim-unimpaired It also handles various other mappings, <code>]q</code> and <code>[q</code> to navigate the quickfix list for example<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 663&lt;li id="fn1" role="doc-endnote"&gt;&lt;p&gt;https://github.com/tpope/vim-unimpaired It also handles various other mappings, &lt;code&gt;]q&lt;/code&gt; and &lt;code&gt;[q&lt;/code&gt; to navigate the quickfix list for example&lt;a href="#fnref1" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
664<li id="fn2" role="doc-endnote"><p><code>:help recording</code><a href="#fnref2" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 664&lt;li id="fn2" role="doc-endnote"&gt;&lt;p&gt;&lt;code&gt;:help recording&lt;/code&gt;&lt;a href="#fnref2" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
665<li id="fn3" role="doc-endnote"><p>When I’m recording a macro, I prefer starting out by storing it in register <code>q</code>, and then copying it over to another register if it works as intended. I think of <code>qq</code> as ‘quick record’.<a href="#fnref3" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 665&lt;li id="fn3" role="doc-endnote"&gt;&lt;p&gt;When I’m recording a macro, I prefer starting out by storing it in register &lt;code&gt;q&lt;/code&gt;, and then copying it over to another register if it works as intended. I think of &lt;code&gt;qq&lt;/code&gt; as ‘quick record’.&lt;a href="#fnref3" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
666<li id="fn4" role="doc-endnote"><p><code>:help mark-motions</code><a href="#fnref4" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 666&lt;li id="fn4" role="doc-endnote"&gt;&lt;p&gt;&lt;code&gt;:help mark-motions&lt;/code&gt;&lt;a href="#fnref4" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
667</ol> 667&lt;/ol&gt;
668</section></description> 668&lt;/section&gt;</description>
669<link>https://peppe.rs/posts/rapid_refactoring_with_vim/</link> 669<link>https://peppe.rs/posts/rapid_refactoring_with_vim/</link>
670<pubDate>Wed, 01 Apr 2020 06:29:00 +0000</pubDate> 670<pubDate>Wed, 01 Apr 2020 06:29:00 +0000</pubDate>
671<guid>https://peppe.rs/posts/rapid_refactoring_with_vim/</guid> 671<guid>https://peppe.rs/posts/rapid_refactoring_with_vim/</guid>
672</item> 672</item>
673<item> 673<item>
674<title>Font Size Fallacies</title> 674<title>Font Size Fallacies</title>
675<description><p>I am not an expert with fonts, but I do have some experience <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>, and common sense. This post aims to debunk some misconceptions about font sizes!</p> 675<description>&lt;p&gt;I am not an expert with fonts, but I do have some experience &lt;a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;, and common sense. This post aims to debunk some misconceptions about font sizes!&lt;/p&gt;
676<p>11 px on your display is <em>probably not</em> 11 px on my display. Let’s do some quick math. I have two displays, 1366x768 @ 21" and another with 1920x1080 @ 13", call them <code>A</code> and <code>B</code> for now.</p> 676&lt;p&gt;11 px on your display is &lt;em&gt;probably not&lt;/em&gt; 11 px on my display. Let’s do some quick math. I have two displays, 1366x768 @ 21" and another with 1920x1080 @ 13", call them &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; for now.&lt;/p&gt;
677<p>Display <code>A</code> has 1,049,088 pixels. A pixel is a square, of side say, <code>s</code> cm. The total area covered by my 21" display is about 1,066 cm^2 (41x26). Thus,</p> 677&lt;p&gt;Display &lt;code&gt;A&lt;/code&gt; has 1,049,088 pixels. A pixel is a square, of side say, &lt;code&gt;s&lt;/code&gt; cm. The total area covered by my 21" display is about 1,066 cm^2 (41x26). Thus,&lt;/p&gt;
678<pre><code>Display A 678&lt;pre&gt;&lt;code&gt;Display A
679Dimensions: 1366x768 @ 21&quot; (41x26 sq. cm) 679Dimensions: 1366x768 @ 21&amp;quot; (41x26 sq. cm)
6801,049,088 s^2 = 1066 6801,049,088 s^2 = 1066
681 s = 0.0318 cm (side of a pixel on Display A)</code></pre> 681 s = 0.0318 cm (side of a pixel on Display A)&lt;/code&gt;&lt;/pre&gt;
682<p>Bear with me, as I repeat the number crunching for Display <code>B</code>:</p> 682&lt;p&gt;Bear with me, as I repeat the number crunching for Display &lt;code&gt;B&lt;/code&gt;:&lt;/p&gt;
683<pre><code>Display B 683&lt;pre&gt;&lt;code&gt;Display B
684Dimensions: 1920x1080 @ 13&quot; (29.5x16.5 sq. cm) 684Dimensions: 1920x1080 @ 13&amp;quot; (29.5x16.5 sq. cm)
6852,073,600 s^2 = 486.75 6852,073,600 s^2 = 486.75
686 s = 0.0153 cm (side of a pixel on Display B)</code></pre> 686 s = 0.0153 cm (side of a pixel on Display B)&lt;/code&gt;&lt;/pre&gt;
687<p>The width of a pixel on Display <code>A</code> is <em>double</em> the width of a pixel on Display <code>B</code>. The area occupied by a pixel on Display <code>A</code> is <em>4 times</em> the area occupied by a pixel on Display <code>B</code>.</p> 687&lt;p&gt;The width of a pixel on Display &lt;code&gt;A&lt;/code&gt; is &lt;em&gt;double&lt;/em&gt; the width of a pixel on Display &lt;code&gt;B&lt;/code&gt;. The area occupied by a pixel on Display &lt;code&gt;A&lt;/code&gt; is &lt;em&gt;4 times&lt;/em&gt; the area occupied by a pixel on Display &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;
688<p><em>The size of a pixel varies from display to display!</em></p> 688&lt;p&gt;&lt;em&gt;The size of a pixel varies from display to display!&lt;/em&gt;&lt;/p&gt;
689<p>A 5x11 bitmap font on Display <code>A</code> would be around 4 mm tall whereas the same bitmap font on Display <code>B</code> would be around 1.9 mm tall. A 11 px tall character on <code>B</code> is visually equivalent to a 5 px character on <code>A</code>. When you view a screenshot of Display <code>A</code> on Display <code>B</code>, the contents are shrunk down by a factor of 2!</p> 689&lt;p&gt;A 5x11 bitmap font on Display &lt;code&gt;A&lt;/code&gt; would be around 4 mm tall whereas the same bitmap font on Display &lt;code&gt;B&lt;/code&gt; would be around 1.9 mm tall. A 11 px tall character on &lt;code&gt;B&lt;/code&gt; is visually equivalent to a 5 px character on &lt;code&gt;A&lt;/code&gt;. When you view a screenshot of Display &lt;code&gt;A&lt;/code&gt; on Display &lt;code&gt;B&lt;/code&gt;, the contents are shrunk down by a factor of 2!&lt;/p&gt;
690<p>So screen resolution is not enough, how else do we measure size? Pixel Density! Keen readers will realize that the 5^th grade math problem we solved up there showcases pixel density, or, pixels per cm (PPCM). Usually we deal with pixels per inch (PPI).</p> 690&lt;p&gt;So screen resolution is not enough, how else do we measure size? Pixel Density! Keen readers will realize that the 5^th grade math problem we solved up there showcases pixel density, or, pixels per cm (PPCM). Usually we deal with pixels per inch (PPI).&lt;/p&gt;
691<p><strong>Note:</strong> PPI is not to be confused with DPI <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> (dots per inch). DPI is defined for printers.</p> 691&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; PPI is not to be confused with DPI &lt;a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; (dots per inch). DPI is defined for printers.&lt;/p&gt;
692<p>In our example, <code>A</code> is a 75 ppi display and <code>B</code> is around 165 ppi <a href="#fn3" class="footnote-ref" id="fnref3" role="doc-noteref"><sup>3</sup></a>. A low ppi display appears to be ‘pixelated’, because the pixels are more prominent, much like Display <code>A</code>. A higher ppi usually means you can view larger images and render crispier fonts. The average desktop display can stuff 100-200 pixels per inch. Smart phones usually fall into the 400-600 ppi (XXXHDPI) category. The human eye fails to differentiate detail past 300 ppi.</p> 692&lt;p&gt;In our example, &lt;code&gt;A&lt;/code&gt; is a 75 ppi display and &lt;code&gt;B&lt;/code&gt; is around 165 ppi &lt;a href="#fn3" class="footnote-ref" id="fnref3" role="doc-noteref"&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;. A low ppi display appears to be ‘pixelated’, because the pixels are more prominent, much like Display &lt;code&gt;A&lt;/code&gt;. A higher ppi usually means you can view larger images and render crispier fonts. The average desktop display can stuff 100-200 pixels per inch. Smart phones usually fall into the 400-600 ppi (XXXHDPI) category. The human eye fails to differentiate detail past 300 ppi.&lt;/p&gt;
693<p><em>So … streaming an 8K video on a 60" TV provides the same clarity as a HD video on a smart phone?</em></p> 693&lt;p&gt;&lt;em&gt;So … streaming an 8K video on a 60" TV provides the same clarity as a HD video on a smart phone?&lt;/em&gt;&lt;/p&gt;
694<p>Absolutely. Well, clarity is subjective, but the amount of detail you can discern on mobile displays has always been limited. Salty consumers of the Xperia 1 <a href="#fn4" class="footnote-ref" id="fnref4" role="doc-noteref"><sup>4</sup></a> will say otherwise.</p> 694&lt;p&gt;Absolutely. Well, clarity is subjective, but the amount of detail you can discern on mobile displays has always been limited. Salty consumers of the Xperia 1 &lt;a href="#fn4" class="footnote-ref" id="fnref4" role="doc-noteref"&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt; will say otherwise.&lt;/p&gt;
695<p>Maybe I will talk about font rendering in another post, but thats all for now. Don’t judge a font size by its screenshot.</p> 695&lt;p&gt;Maybe I will talk about font rendering in another post, but thats all for now. Don’t judge a font size by its screenshot.&lt;/p&gt;
696<section class="footnotes" role="doc-endnotes"> 696&lt;section class="footnotes" role="doc-endnotes"&gt;
697<hr /> 697&lt;hr /&gt;
698<ol> 698&lt;ol&gt;
699<li id="fn1" role="doc-endnote"><p>https://github.com/nerdypepper/scientifica<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 699&lt;li id="fn1" role="doc-endnote"&gt;&lt;p&gt;https://github.com/nerdypepper/scientifica&lt;a href="#fnref1" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
700<li id="fn2" role="doc-endnote"><p>https://en.wikipedia.org/wiki/Dots_per_inch<a href="#fnref2" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 700&lt;li id="fn2" role="doc-endnote"&gt;&lt;p&gt;https://en.wikipedia.org/wiki/Dots_per_inch&lt;a href="#fnref2" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
701<li id="fn3" role="doc-endnote"><p>https://www.sven.de/dpi/<a href="#fnref3" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 701&lt;li id="fn3" role="doc-endnote"&gt;&lt;p&gt;https://www.sven.de/dpi/&lt;a href="#fnref3" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
702<li id="fn4" role="doc-endnote"><p>https://en.wikipedia.org/wiki/Sony_Xperia_1<a href="#fnref4" class="footnote-back" role="doc-backlink">↩︎</a></p></li> 702&lt;li id="fn4" role="doc-endnote"&gt;&lt;p&gt;https://en.wikipedia.org/wiki/Sony_Xperia_1&lt;a href="#fnref4" class="footnote-back" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
703</ol> 703&lt;/ol&gt;
704</section></description> 704&lt;/section&gt;</description>
705<link>https://peppe.rs/posts/font_size_fallacies/</link> 705<link>https://peppe.rs/posts/font_size_fallacies/</link>
706<pubDate>Tue, 17 Mar 2020 16:52:00 +0000</pubDate> 706<pubDate>Tue, 17 Mar 2020 16:52:00 +0000</pubDate>
707<guid>https://peppe.rs/posts/font_size_fallacies/</guid> 707<guid>https://peppe.rs/posts/font_size_fallacies/</guid>
708</item> 708</item>
709<item> 709<item>
710<title>Termux Tandem</title> 710<title>Termux Tandem</title>
711<description><p>I learnt about <code>termux</code> from a friend on IRC recently. It looked super gimmicky to me at first, but it eventually proved to be useful. Here’s what I use it for:</p> 711<description>&lt;p&gt;I learnt about &lt;code&gt;termux&lt;/code&gt; from a friend on IRC recently. It looked super gimmicky to me at first, but it eventually proved to be useful. Here’s what I use it for:&lt;/p&gt;
712<h3 id="rsync">rsync</h3> 712&lt;h3 id="rsync"&gt;rsync&lt;/h3&gt;
713<p>Ever since I degoogled my android device, syncing files between my phone and my PC has always been a pain. I’m looking at you MTP. But, with <code>termux</code> and <code>sshd</code> all set up, it’s as simple as:</p> 713&lt;p&gt;Ever since I degoogled my android device, syncing files between my phone and my PC has always been a pain. I’m looking at you MTP. But, with &lt;code&gt;termux&lt;/code&gt; and &lt;code&gt;sshd&lt;/code&gt; all set up, it’s as simple as:&lt;/p&gt;
714<pre><code>$ arp 714&lt;pre&gt;&lt;code&gt;$ arp
715Address HWtype HWad ... 715Address HWtype HWad ...
716192.168.43.187 ether d0:0 ... 716192.168.43.187 ether d0:0 ...
717 717
718$ rsync -avz 192.168.43.187:~/frogs ~/pics/frogs</code></pre> 718$ rsync -avz 192.168.43.187:~/frogs ~/pics/frogs&lt;/code&gt;&lt;/pre&gt;
719<h3 id="ssh-tmux">ssh &amp; tmux</h3> 719&lt;h3 id="ssh-tmux"&gt;ssh &amp;amp; tmux&lt;/h3&gt;
720<p>My phone doubles as a secondary view into my main machine with <code>ssh</code> and <code>tmux</code>. When I am away from my PC (read: sitting across the room), I check build status and IRC messages by <code>ssh</code>ing into a tmux session running the said build or weechat.</p> 720&lt;p&gt;My phone doubles as a secondary view into my main machine with &lt;code&gt;ssh&lt;/code&gt; and &lt;code&gt;tmux&lt;/code&gt;. When I am away from my PC (read: sitting across the room), I check build status and IRC messages by &lt;code&gt;ssh&lt;/code&gt;ing into a tmux session running the said build or weechat.&lt;/p&gt;
721<h3 id="file-uploads">file uploads</h3> 721&lt;h3 id="file-uploads"&gt;file uploads&lt;/h3&gt;
722<p>Not being able to access my (ssh-only) file host was crippling. With a <code>bash</code> instance on my phone, I just copied over my ssh keys, and popped in a file upload script (a glorified <code>scp</code>). Now I just have to figure out a way to clean up these file names …</p> 722&lt;p&gt;Not being able to access my (ssh-only) file host was crippling. With a &lt;code&gt;bash&lt;/code&gt; instance on my phone, I just copied over my ssh keys, and popped in a file upload script (a glorified &lt;code&gt;scp&lt;/code&gt;). Now I just have to figure out a way to clean up these file names …&lt;/p&gt;
723<pre><code>~/storage/pictures/ $ ls 723&lt;pre&gt;&lt;code&gt;~/storage/pictures/ $ ls
72402muf5g7b2i41.jpg 7alt3cwg77841.jpg cl4bsrge7id11.png 72402muf5g7b2i41.jpg 7alt3cwg77841.jpg cl4bsrge7id11.png
725mtZabXG.jpg p8d5c584f2841.jpg vjUxGjq.jpg</code></pre> 725mtZabXG.jpg p8d5c584f2841.jpg vjUxGjq.jpg&lt;/code&gt;&lt;/pre&gt;
726<h3 id="cmus">cmus</h3> 726&lt;h3 id="cmus"&gt;cmus&lt;/h3&gt;
727<p>Alright, I don’t really listen to music via <code>cmus</code>, but I did use it a couple times when my default music player was acting up. <code>cmus</code> is a viable option:</p> 727&lt;p&gt;Alright, I don’t really listen to music via &lt;code&gt;cmus&lt;/code&gt;, but I did use it a couple times when my default music player was acting up. &lt;code&gt;cmus&lt;/code&gt; is a viable option:&lt;/p&gt;
728<p><a href="https://u.peppe.rs/CP.jpg"><img src="https://u.peppe.rs/CP.jpg" /></a></p></description> 728&lt;p&gt;&lt;a href="https://u.peppe.rs/CP.jpg"&gt;&lt;img src="https://u.peppe.rs/CP.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;</description>
729<link>https://peppe.rs/posts/termux_tandem/</link> 729<link>https://peppe.rs/posts/termux_tandem/</link>
730<pubDate>Sun, 08 Mar 2020 16:47:00 +0000</pubDate> 730<pubDate>Sun, 08 Mar 2020 16:47:00 +0000</pubDate>
731<guid>https://peppe.rs/posts/termux_tandem/</guid> 731<guid>https://peppe.rs/posts/termux_tandem/</guid>
732</item> 732</item>
733<item> 733<item>
734<title>Call To ARMs</title> 734<title>Call To ARMs</title>
735<description><p>My 4th semester involves ARM programming. And proprietary tooling (Keil C). But we don’t do that here.</p> 735<description>&lt;p&gt;My 4th semester involves ARM programming. And proprietary tooling (Keil C). But we don’t do that here.&lt;/p&gt;
736<h3 id="building">Building</h3> 736&lt;h3 id="building"&gt;Building&lt;/h3&gt;
737<p>Assembling and linking ARM binaries on non-ARM architecture devices is fairly trivial. I went along with the GNU cross bare metal toolchain binutils, which provides <code>arm-as</code> and <code>arm-ld</code> (among a bunch of other utils that I don’t care about for now).</p> 737&lt;p&gt;Assembling and linking ARM binaries on non-ARM architecture devices is fairly trivial. I went along with the GNU cross bare metal toolchain binutils, which provides &lt;code&gt;arm-as&lt;/code&gt; and &lt;code&gt;arm-ld&lt;/code&gt; (among a bunch of other utils that I don’t care about for now).&lt;/p&gt;
738<p>Assemble <code>.s</code> files with:</p> 738&lt;p&gt;Assemble &lt;code&gt;.s&lt;/code&gt; files with:&lt;/p&gt;
739<pre class="shell"><code>arm-none-eabi-as main.s -g -march=armv8.1-a -o main.out</code></pre> 739&lt;pre class="shell"&gt;&lt;code&gt;arm-none-eabi-as main.s -g -march=armv8.1-a -o main.out&lt;/code&gt;&lt;/pre&gt;
740<p>The <code>-g</code> flag generates extra debugging information that <code>gdb</code> picks up. The <code>-march</code> option establishes target architecture.</p> 740&lt;p&gt;The &lt;code&gt;-g&lt;/code&gt; flag generates extra debugging information that &lt;code&gt;gdb&lt;/code&gt; picks up. The &lt;code&gt;-march&lt;/code&gt; option establishes target architecture.&lt;/p&gt;
741<p>Link <code>.o</code> files with:</p> 741&lt;p&gt;Link &lt;code&gt;.o&lt;/code&gt; files with:&lt;/p&gt;
742<pre class="shell"><code>arm-none-eabi-ld main.out -o main</code></pre> 742&lt;pre class="shell"&gt;&lt;code&gt;arm-none-eabi-ld main.out -o main&lt;/code&gt;&lt;/pre&gt;
743<h3 id="running-and-debugging">Running (and Debugging)</h3> 743&lt;h3 id="running-and-debugging"&gt;Running (and Debugging)&lt;/h3&gt;
744<p>Things get interesting here. <code>gdb</code> on your x86 machine cannot read nor execute binaries compiled for ARM. So, we simulate an ARM processor using <code>qemu</code>. Now qemu allows you to run <code>gdbserver</code> on startup. Connecting our local <code>gdb</code> instance to <code>gdbserver</code> gives us a view into the program’s execution. Easy!</p> 744&lt;p&gt;Things get interesting here. &lt;code&gt;gdb&lt;/code&gt; on your x86 machine cannot read nor execute binaries compiled for ARM. So, we simulate an ARM processor using &lt;code&gt;qemu&lt;/code&gt;. Now qemu allows you to run &lt;code&gt;gdbserver&lt;/code&gt; on startup. Connecting our local &lt;code&gt;gdb&lt;/code&gt; instance to &lt;code&gt;gdbserver&lt;/code&gt; gives us a view into the program’s execution. Easy!&lt;/p&gt;
745<p>Run <code>qemu</code>, with <code>gdbserver</code> on port <code>1234</code>, with our ARM binary, <code>main</code>:</p> 745&lt;p&gt;Run &lt;code&gt;qemu&lt;/code&gt;, with &lt;code&gt;gdbserver&lt;/code&gt; on port &lt;code&gt;1234&lt;/code&gt;, with our ARM binary, &lt;code&gt;main&lt;/code&gt;:&lt;/p&gt;
746<pre class="shell"><code>qemu-arm -singlestep -g 1234 main</code></pre> 746&lt;pre class="shell"&gt;&lt;code&gt;qemu-arm -singlestep -g 1234 main&lt;/code&gt;&lt;/pre&gt;
747<p>Start up <code>gdb</code> on your machine, and connect to <code>qemu</code>’s <code>gdbserver</code>:</p> 747&lt;p&gt;Start up &lt;code&gt;gdb&lt;/code&gt; on your machine, and connect to &lt;code&gt;qemu&lt;/code&gt;’s &lt;code&gt;gdbserver&lt;/code&gt;:&lt;/p&gt;
748<pre><code>(gdb) set architecture armv8-a 748&lt;pre&gt;&lt;code&gt;(gdb) set architecture armv8-a
749(gdb) target remote localhost:1234 749(gdb) target remote localhost:1234
750(gdb) file main 750(gdb) file main
751Reading symbols from main... # yay!</code></pre> 751Reading symbols from main... # yay!&lt;/code&gt;&lt;/pre&gt;
752<h3 id="gdb-enhanced">GDB Enhanced</h3> 752&lt;h3 id="gdb-enhanced"&gt;GDB Enhanced&lt;/h3&gt;
753<p><code>gdb</code> is cool, but it’s not nearly as comfortable as well fleshed out emulators/IDEs like Keil. Watching registers, CPSR and memory chunks update <em>is</em> pretty fun.</p> 753&lt;p&gt;&lt;code&gt;gdb&lt;/code&gt; is cool, but it’s not nearly as comfortable as well fleshed out emulators/IDEs like Keil. Watching registers, CPSR and memory chunks update &lt;em&gt;is&lt;/em&gt; pretty fun.&lt;/p&gt;
754<p>I came across <code>gdb</code>’s TUI mode (hit <code>C-x C-a</code> or type <code>tui enable</code> at the prompt). TUI mode is a godsend. It highlights the current line of execution, shows you disassembly outputs, updated registers, active breakpoints and more.</p> 754&lt;p&gt;I came across &lt;code&gt;gdb&lt;/code&gt;’s TUI mode (hit &lt;code&gt;C-x C-a&lt;/code&gt; or type &lt;code&gt;tui enable&lt;/code&gt; at the prompt). TUI mode is a godsend. It highlights the current line of execution, shows you disassembly outputs, updated registers, active breakpoints and more.&lt;/p&gt;
755<p><em>But</em>, it is an absolute eyesore.</p> 755&lt;p&gt;&lt;em&gt;But&lt;/em&gt;, it is an absolute eyesore.&lt;/p&gt;
756<p>Say hello to <a href="https://github.com/hugsy/gef">GEF</a>! “GDB Enhanced Features” teaches our old dog some cool new tricks. Here are some additions that made my ARM debugging experience loads better:</p> 756&lt;p&gt;Say hello to &lt;a href="https://github.com/hugsy/gef"&gt;GEF&lt;/a&gt;! “GDB Enhanced Features” teaches our old dog some cool new tricks. Here are some additions that made my ARM debugging experience loads better:&lt;/p&gt;
757<ul> 757&lt;ul&gt;
758<li>Memory watches</li> 758&lt;li&gt;Memory watches&lt;/li&gt;
759<li>Register watches, with up to 7 levels of deref (overkill, I agree)</li> 759&lt;li&gt;Register watches, with up to 7 levels of deref (overkill, I agree)&lt;/li&gt;
760<li>Stack tracing</li> 760&lt;li&gt;Stack tracing&lt;/li&gt;
761</ul> 761&lt;/ul&gt;
762<p>And it’s pretty! See for yourself:</p> 762&lt;p&gt;And it’s pretty! See for yourself:&lt;/p&gt;
763<p><a href="https://u.peppe.rs/wq.png"><img src="https://u.peppe.rs/wq.png" /></a></p> 763&lt;p&gt;&lt;a href="https://u.peppe.rs/wq.png"&gt;&lt;img src="https://u.peppe.rs/wq.png" /&gt;&lt;/a&gt;&lt;/p&gt;
764<h3 id="editing">Editing</h3> 764&lt;h3 id="editing"&gt;Editing&lt;/h3&gt;
765<p>Vim, with <code>syntax off</code> because it dosen’t handle GNU ARM syntax too well.</p></description> 765&lt;p&gt;Vim, with &lt;code&gt;syntax off&lt;/code&gt; because it dosen’t handle GNU ARM syntax too well.&lt;/p&gt;</description>
766<link>https://peppe.rs/posts/call_to_ARMs/</link> 766<link>https://peppe.rs/posts/call_to_ARMs/</link>
767<pubDate>Fri, 07 Feb 2020 18:30:00 +0000</pubDate> 767<pubDate>Fri, 07 Feb 2020 18:30:00 +0000</pubDate>
768<guid>https://peppe.rs/posts/call_to_ARMs/</guid> 768<guid>https://peppe.rs/posts/call_to_ARMs/</guid>
769</item> 769</item>
770<item> 770<item>
771<title>Color Conundrum</title> 771<title>Color Conundrum</title>
772<description><p>This piece aims to highlight (pun intended) some of the reasons behind my <a href="https://u.peppe.rs/bF.png">color free</a> editor setup.</p> 772<description>&lt;p&gt;This piece aims to highlight (pun intended) some of the reasons behind my &lt;a href="https://u.peppe.rs/bF.png"&gt;color free&lt;/a&gt; editor setup.&lt;/p&gt;
773<p>Imagine highlighting an entire book because <em>all</em> of it is important. That is exactly what (most) syntax highlighting does. It is difficult for the human eye to filter out noise in rainbow barf. Use color to draw attention, not diverge it.</p> 773&lt;p&gt;Imagine highlighting an entire book because &lt;em&gt;all&lt;/em&gt; of it is important. That is exactly what (most) syntax highlighting does. It is difficult for the human eye to filter out noise in rainbow barf. Use color to draw attention, not diverge it.&lt;/p&gt;
774<p>At the same time, a book devoid of color is <em>boring!</em> What is the takeaway from this 10 line paragraph? What are the technical terms used?</p> 774&lt;p&gt;At the same time, a book devoid of color is &lt;em&gt;boring!&lt;/em&gt; What is the takeaway from this 10 line paragraph? What are the technical terms used?&lt;/p&gt;
775<p>Prose and code are certainly different, but the fickle minded human eye is the same. The eye constantly looks for a frame of reference, a focal point. It grows tired when it can’t find one.</p> 775&lt;p&gt;Prose and code are certainly different, but the fickle minded human eye is the same. The eye constantly looks for a frame of reference, a focal point. It grows tired when it can’t find one.&lt;/p&gt;
776<p>The following comparison does a better job of explaining (none, ample and over-the-top highlighting, from left to right):</p> 776&lt;p&gt;The following comparison does a better job of explaining (none, ample and over-the-top highlighting, from left to right):&lt;/p&gt;
777<p><a href="https://u.peppe.rs/lt.png"><img src="https://u.peppe.rs/lt.png" /></a></p> 777&lt;p&gt;&lt;a href="https://u.peppe.rs/lt.png"&gt;&lt;img src="https://u.peppe.rs/lt.png" /&gt;&lt;/a&gt;&lt;/p&gt;
778<p>Without highlighting (far left), it is hard to differentiate between comments and code! The florid color scheme (far right) is no good either, it contains too many attention grabbers. The center sample is a healthy balance of both. Function calls and constants stand out, and repetitive keywords and other noise (<code>let</code>, <code>as</code>) are mildly dimmed out. Comments and non-code text (sign column, status text) are dimmed further.</p> 778&lt;p&gt;Without highlighting (far left), it is hard to differentiate between comments and code! The florid color scheme (far right) is no good either, it contains too many attention grabbers. The center sample is a healthy balance of both. Function calls and constants stand out, and repetitive keywords and other noise (&lt;code&gt;let&lt;/code&gt;, &lt;code&gt;as&lt;/code&gt;) are mildly dimmed out. Comments and non-code text (sign column, status text) are dimmed further.&lt;/p&gt;
779<p>I’ll stop myself before I rant about color contrast and combinations.</p></description> 779&lt;p&gt;I’ll stop myself before I rant about color contrast and combinations.&lt;/p&gt;</description>
780<link>https://peppe.rs/posts/color_conundrum/</link> 780<link>https://peppe.rs/posts/color_conundrum/</link>
781<pubDate>Mon, 30 Dec 2019 18:30:00 +0000</pubDate> 781<pubDate>Mon, 30 Dec 2019 18:30:00 +0000</pubDate>
782<guid>https://peppe.rs/posts/color_conundrum/</guid> 782<guid>https://peppe.rs/posts/color_conundrum/</guid>
783</item> 783</item>
784<item> 784<item>
785<title>Static Sites With Bash</title> 785<title>Static Sites With Bash</title>
786<description><p>After going through a bunch of static site generators (<a href="https://blog.getpelican.com/">pelican</a>, <a href="https://gohugo.io">hugo</a>, <a href="https://github.com/icyphox/vite">vite</a>), I decided to roll my own. If you are more of the ‘show me the code’ kinda guy, <a href="https://github.com/nerdypepper/site">here</a> you go.</p> 786<description>&lt;p&gt;After going through a bunch of static site generators (&lt;a href="https://blog.getpelican.com/"&gt;pelican&lt;/a&gt;, &lt;a href="https://gohugo.io"&gt;hugo&lt;/a&gt;, &lt;a href="https://github.com/icyphox/vite"&gt;vite&lt;/a&gt;), I decided to roll my own. If you are more of the ‘show me the code’ kinda guy, &lt;a href="https://github.com/nerdypepper/site"&gt;here&lt;/a&gt; you go.&lt;/p&gt;
787<h3 id="text-formatting">Text formatting</h3> 787&lt;h3 id="text-formatting"&gt;Text formatting&lt;/h3&gt;
788<p>I chose to write in markdown, and convert to html with <a href="https://kristaps.bsd.lv/lowdown/">lowdown</a>.</p> 788&lt;p&gt;I chose to write in markdown, and convert to html with &lt;a href="https://kristaps.bsd.lv/lowdown/"&gt;lowdown&lt;/a&gt;.&lt;/p&gt;
789<h3 id="directory-structure">Directory structure</h3> 789&lt;h3 id="directory-structure"&gt;Directory structure&lt;/h3&gt;
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> 790&lt;p&gt;I host my site on GitHub pages, so &lt;code&gt;docs/&lt;/code&gt; has to be the entry point. Markdown formatted posts go into &lt;code&gt;posts/&lt;/code&gt;, get converted into html, and end up in &lt;code&gt;docs/index.html&lt;/code&gt;, something like this:&lt;/p&gt;
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> 791&lt;div class="sourceCode" id="cb1"&gt;&lt;pre class="sourceCode bash"&gt;&lt;code class="sourceCode bash"&gt;&lt;span id="cb1-1"&gt;&lt;a href="#cb1-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="va"&gt;posts=$(&lt;/span&gt;&lt;span class="fu"&gt;ls&lt;/span&gt; -t ./posts&lt;span class="va"&gt;)&lt;/span&gt; &lt;span class="co"&gt;# chronological order!&lt;/span&gt;&lt;/span&gt;
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> 792&lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;for&lt;/span&gt; &lt;span class="ex"&gt;f&lt;/span&gt; in &lt;span class="va"&gt;$posts&lt;/span&gt;&lt;span class="kw"&gt;;&lt;/span&gt; &lt;span class="kw"&gt;do&lt;/span&gt;&lt;/span&gt;
793<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="va">file=</span><span class="st">&quot;./posts/&quot;</span><span class="va">$f</span> <span class="co"># `ls` mangled our file paths</span></span> 793&lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="va"&gt;file=&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;./posts/&amp;quot;&lt;/span&gt;&lt;span class="va"&gt;$f&lt;/span&gt; &lt;span class="co"&gt;# `ls` mangled our file paths&lt;/span&gt;&lt;/span&gt;
794<span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="bu">echo</span> <span class="st">&quot;generating post </span><span class="va">$file</span><span class="st">&quot;</span></span> 794&lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="bu"&gt;echo&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;generating post &lt;/span&gt;&lt;span class="va"&gt;$file&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
795<span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a></span> 795&lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
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">&quot;</span><span class="va">$file</span><span class="st">&quot;</span><span class="va">)</span></span> 796&lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="va"&gt;html=$(&lt;/span&gt;&lt;span class="ex"&gt;lowdown&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;span class="va"&gt;$file&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;span class="va"&gt;)&lt;/span&gt;&lt;/span&gt;
797<span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a> <span class="bu">echo</span> -e <span class="st">&quot;html&quot;</span> <span class="op">&gt;&gt;</span> docs/index.html</span> 797&lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="bu"&gt;echo&lt;/span&gt; -e &lt;span class="st"&gt;&amp;quot;html&amp;quot;&lt;/span&gt; &lt;span class="op"&gt;&amp;gt;&amp;gt;&lt;/span&gt; docs/index.html&lt;/span&gt;
798<span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="kw">done</span></span></code></pre></div> 798&lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;done&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
799<h3 id="assets">Assets</h3> 799&lt;h3 id="assets"&gt;Assets&lt;/h3&gt;
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> 800&lt;p&gt;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:&lt;/p&gt;
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> 801&lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode bash"&gt;&lt;code class="sourceCode bash"&gt;&lt;span id="cb2-1"&gt;&lt;a href="#cb2-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# strip file extension&lt;/span&gt;&lt;/span&gt;
802<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a><span class="va">ext=</span><span class="st">&quot;</span><span class="va">${1##</span>*.<span class="va">}</span><span class="st">&quot;</span></span> 802&lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="va"&gt;ext=&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;span class="va"&gt;${1##&lt;/span&gt;*.&lt;span class="va"&gt;}&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
803<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a></span> 803&lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
804<span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a><span class="co"># generate a random file name</span></span> 804&lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# generate a random file name&lt;/span&gt;&lt;/span&gt;
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">&#39;a-zA-Z0-9&#39;</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&lt;span id="cb2-5"&gt;&lt;a href="#cb2-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="va"&gt;id=$(&lt;/span&gt; &lt;span class="fu"&gt;cat&lt;/span&gt; /dev/urandom &lt;span class="kw"&gt;|&lt;/span&gt; &lt;span class="fu"&gt;tr&lt;/span&gt; -dc &lt;span class="st"&gt;&amp;#39;a-zA-Z0-9&amp;#39;&lt;/span&gt; &lt;span class="kw"&gt;|&lt;/span&gt; &lt;span class="ex"&gt;fold&lt;/span&gt; -w 2 &lt;span class="kw"&gt;|&lt;/span&gt; &lt;span class="fu"&gt;head&lt;/span&gt; -n 1 &lt;span class="va"&gt;)&lt;/span&gt;&lt;/span&gt;
806<span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a><span class="va">id=</span><span class="st">&quot;</span><span class="va">$id</span><span class="st">.</span><span class="va">$ext</span><span class="st">&quot;</span></span> 806&lt;span id="cb2-6"&gt;&lt;a href="#cb2-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="va"&gt;id=&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;span class="va"&gt;$id&lt;/span&gt;&lt;span class="st"&gt;.&lt;/span&gt;&lt;span class="va"&gt;$ext&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
807<span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a></span> 807&lt;span id="cb2-7"&gt;&lt;a href="#cb2-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt;
808<span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a><span class="co"># copy to my file host</span></span> 808&lt;span id="cb2-8"&gt;&lt;a href="#cb2-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# copy to my file host&lt;/span&gt;&lt;/span&gt;
809<span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a><span class="fu">scp</span> -P 443 <span class="st">&quot;</span><span class="va">$1</span><span class="st">&quot;</span> emerald:files/<span class="st">&quot;</span><span class="va">$id</span><span class="st">&quot;</span> </span> 809&lt;span id="cb2-9"&gt;&lt;a href="#cb2-9" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;scp&lt;/span&gt; -P 443 &lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;span class="va"&gt;$1&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt; emerald:files/&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;span class="va"&gt;$id&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt; &lt;/span&gt;
810<span id="cb2-10"><a href="#cb2-10" aria-hidden="true"></a><span class="bu">echo</span> <span class="st">&quot;https://u.peppe.rs/</span><span class="va">$id</span><span class="st">&quot;</span></span></code></pre></div> 810&lt;span id="cb2-10"&gt;&lt;a href="#cb2-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="bu"&gt;echo&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;https://u.peppe.rs/&lt;/span&gt;&lt;span class="va"&gt;$id&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
811<h3 id="templating">Templating</h3> 811&lt;h3 id="templating"&gt;Templating&lt;/h3&gt;
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> 812&lt;p&gt;&lt;a href="https://github.com/NerdyPepper/site/blob/master/generate.sh"&gt;&lt;code&gt;generate.sh&lt;/code&gt;&lt;/a&gt; brings the above bits and pieces together (with some extra cruft to avoid javascript). It uses &lt;code&gt;sed&lt;/code&gt; to produce nice titles from the file names (removes underscores, title-case), and &lt;code&gt;date(1)&lt;/code&gt; to add the date to each post listing!&lt;/p&gt;</description>
813<link>https://peppe.rs/posts/static_sites_with_bash/</link> 813<link>https://peppe.rs/posts/static_sites_with_bash/</link>
814<pubDate>Fri, 22 Nov 2019 18:30:00 +0000</pubDate> 814<pubDate>Fri, 22 Nov 2019 18:30:00 +0000</pubDate>
815<guid>https://peppe.rs/posts/static_sites_with_bash/</guid> 815<guid>https://peppe.rs/posts/static_sites_with_bash/</guid>
816</item> 816</item>
817<item> 817<item>
818<title>My Setup</title> 818<title>My Setup</title>
819<description><p>Decided to do one of these because everyone does one of these.</p> 819<description>&lt;p&gt;Decided to do one of these because everyone does one of these.&lt;/p&gt;
820<p><img src="https://u.peppe.rs/Hb.png" /></p> 820&lt;p&gt;&lt;img src="https://u.peppe.rs/Hb.png" /&gt;&lt;/p&gt;
821<p>My entire setup is managed with GNU <code>stow</code>, making it easier to replicate on fresh installations. You can find my configuration files on <a href="https://github.com/nerdypepper">GitHub</a>.</p> 821&lt;p&gt;My entire setup is managed with GNU &lt;code&gt;stow&lt;/code&gt;, making it easier to replicate on fresh installations. You can find my configuration files on &lt;a href="https://github.com/nerdypepper"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
822<p>I run Void Linux (glibc) on my <a href="https://store.hp.com/us/en/mdp/laptops/envy-13">HP Envy 13" (2018)</a>. To keep things simple, I run a raw X session with <code>2bwm</code> as my window manager, along with <code>dunst</code> (notification daemon) and Sam’s <a href="https://github.com/sdhand/compton"><code>compton</code></a> (compositor) fork.</p> 822&lt;p&gt;I run Void Linux (glibc) on my &lt;a href="https://store.hp.com/us/en/mdp/laptops/envy-13"&gt;HP Envy 13" (2018)&lt;/a&gt;. To keep things simple, I run a raw X session with &lt;code&gt;2bwm&lt;/code&gt; as my window manager, along with &lt;code&gt;dunst&lt;/code&gt; (notification daemon) and Sam’s &lt;a href="https://github.com/sdhand/compton"&gt;&lt;code&gt;compton&lt;/code&gt;&lt;/a&gt; (compositor) fork.&lt;/p&gt;
823<p>I am a fan of GNU tools, so I use <code>bash</code> as my shell, and <code>coreutils</code> to manage files, archives, strings, paths etc. I edit files with <code>vim</code>, chat with <code>weechat</code>, listen to music with <code>cmus</code>, monitor processes with <code>htop</code>, manage sessions with <code>tmux</code>, read pdfs in <code>zathura</code>. I rarely ever leave the comfort of my terminal emulator, <code>urxvt</code>.</p> 823&lt;p&gt;I am a fan of GNU tools, so I use &lt;code&gt;bash&lt;/code&gt; as my shell, and &lt;code&gt;coreutils&lt;/code&gt; to manage files, archives, strings, paths etc. I edit files with &lt;code&gt;vim&lt;/code&gt;, chat with &lt;code&gt;weechat&lt;/code&gt;, listen to music with &lt;code&gt;cmus&lt;/code&gt;, monitor processes with &lt;code&gt;htop&lt;/code&gt;, manage sessions with &lt;code&gt;tmux&lt;/code&gt;, read pdfs in &lt;code&gt;zathura&lt;/code&gt;. I rarely ever leave the comfort of my terminal emulator, &lt;code&gt;urxvt&lt;/code&gt;.&lt;/p&gt;
824<p>Most of my academic typesetting is done with TeX, and compiled with <code>xelatex</code>. Other <em>fun</em> documents are made with GIMP :).</p></description> 824&lt;p&gt;Most of my academic typesetting is done with TeX, and compiled with &lt;code&gt;xelatex&lt;/code&gt;. Other &lt;em&gt;fun&lt;/em&gt; documents are made with GIMP :).&lt;/p&gt;</description>
825<link>https://peppe.rs/posts/my_setup/</link> 825<link>https://peppe.rs/posts/my_setup/</link>
826<pubDate>Wed, 06 Nov 2019 18:30:00 +0000</pubDate> 826<pubDate>Wed, 06 Nov 2019 18:30:00 +0000</pubDate>
827<guid>https://peppe.rs/posts/my_setup/</guid> 827<guid>https://peppe.rs/posts/my_setup/</guid>
828</item> 828</item>
829<item> 829<item>
830<title>WPA Woes</title> 830<title>WPA Woes</title>
831<description><p>I finally got around to installing Void GNU/Linux on my main computer. Rolling release, non-systemd, need I say more?</p> 831<description>&lt;p&gt;I finally got around to installing Void GNU/Linux on my main computer. Rolling release, non-systemd, need I say more?&lt;/p&gt;
832<p>As with all GNU/Linux distributions, wireless networks had me in a fix. If you can see this post, it means I’ve managed to get online. It turns out, <code>wpa_supplicant</code> was detecting the wrong interface by default (does it ever select the right one?). Let us fix that:</p> 832&lt;p&gt;As with all GNU/Linux distributions, wireless networks had me in a fix. If you can see this post, it means I’ve managed to get online. It turns out, &lt;code&gt;wpa_supplicant&lt;/code&gt; was detecting the wrong interface by default (does it ever select the right one?). Let us fix that:&lt;/p&gt;
833<pre><code>$ sudo rm -r /var/service/wpa_supplicant 833&lt;pre&gt;&lt;code&gt;$ sudo rm -r /var/service/wpa_supplicant
834$ sudo killall dhcpcd</code></pre> 834$ sudo killall dhcpcd&lt;/code&gt;&lt;/pre&gt;
835<p>What is the right interface though?</p> 835&lt;p&gt;What is the right interface though?&lt;/p&gt;
836<pre><code>$ iw dev 836&lt;pre&gt;&lt;code&gt;$ iw dev
837 ... 837 ...
838 Interface wlp2s0 838 Interface wlp2s0
839 ...</code></pre> 839 ...&lt;/code&gt;&lt;/pre&gt;
840<p>Aha! Let us run <code>wpa_supplicant</code> on that interface, as a background process:</p> 840&lt;p&gt;Aha! Let us run &lt;code&gt;wpa_supplicant&lt;/code&gt; on that interface, as a background process:&lt;/p&gt;
841<pre><code>$ sudo wpa_supplicant -B -i wlp2s0 -c /etc/wpa_supplicant/wpa_supplicant.conf 841&lt;pre&gt;&lt;code&gt;$ sudo wpa_supplicant -B -i wlp2s0 -c /etc/wpa_supplicant/wpa_supplicant.conf
842$ sudo dhcpcd -B wlp2s0 842$ sudo dhcpcd -B wlp2s0
843$ ping google.com 843$ ping google.com
844PING ...</code></pre> 844PING ...&lt;/code&gt;&lt;/pre&gt;
845<p>Yay! Make those changes perpetual by enabling the service:</p> 845&lt;p&gt;Yay! Make those changes perpetual by enabling the service:&lt;/p&gt;
846<pre><code>------------------------------------------------------ 846&lt;pre&gt;&lt;code&gt;------------------------------------------------------
847# Add these to /etc/wpa_supplicant/wpa_supplicant.conf 847# Add these to /etc/wpa_supplicant/wpa_supplicant.conf
848OPTS=&quot;-B&quot; 848OPTS=&amp;quot;-B&amp;quot;
849WPA_INTERFACE=&quot;wlp2s0&quot; 849WPA_INTERFACE=&amp;quot;wlp2s0&amp;quot;
850------------------------------------------------------ 850------------------------------------------------------
851$ sudo ln -s /etc/sv/wpa_supplicant /var/service/ 851$ sudo ln -s /etc/sv/wpa_supplicant /var/service/
852$ sudo ln -s /etc/sv/dhcpcd /var/service/ 852$ sudo ln -s /etc/sv/dhcpcd /var/service/
853$ sudo sv restart wpa_supplicant 853$ sudo sv restart wpa_supplicant
854$ sudo sv restart dhcpcd</code></pre></description> 854$ sudo sv restart dhcpcd&lt;/code&gt;&lt;/pre&gt;</description>
855<link>https://peppe.rs/posts/WPA_woes/</link> 855<link>https://peppe.rs/posts/WPA_woes/</link>
856<pubDate>Sat, 12 Oct 2019 16:18:00 +0000</pubDate> 856<pubDate>Sat, 12 Oct 2019 16:18:00 +0000</pubDate>
857<guid>https://peppe.rs/posts/WPA_woes/</guid> 857<guid>https://peppe.rs/posts/WPA_woes/</guid>
858</item> 858</item>
859<item> 859<item>
860<title>Bye Bye BDFs</title> 860<title>Bye Bye BDFs</title>
861<description><p>Glyph Bitmap Distribution Format is no more, as the creators of <a href="https://pango.org">Pango</a>, one of the most widely used text rendering libraries, <a href="https://blogs.gnome.org/mclasen/2019/05/25/pango-future-directions/">announced</a> their plans for Pango 1.44.</p> 861<description>&lt;p&gt;Glyph Bitmap Distribution Format is no more, as the creators of &lt;a href="https://pango.org"&gt;Pango&lt;/a&gt;, one of the most widely used text rendering libraries, &lt;a href="https://blogs.gnome.org/mclasen/2019/05/25/pango-future-directions/"&gt;announced&lt;/a&gt; their plans for Pango 1.44.&lt;/p&gt;
862<p>Until recently, Pango used FreeType to draw fonts. They will be moving over to <a href="https://harfbuzz.org">Harfbuzz</a>, an evolution of FreeType.</p> 862&lt;p&gt;Until recently, Pango used FreeType to draw fonts. They will be moving over to &lt;a href="https://harfbuzz.org"&gt;Harfbuzz&lt;/a&gt;, an evolution of FreeType.&lt;/p&gt;
863<p><em>Why?</em></p> 863&lt;p&gt;&lt;em&gt;Why?&lt;/em&gt;&lt;/p&gt;
864<p>In short, FreeType was hard to work with. It required complex logic, and provided no advantage over Harfbuzz (other than being able to fetch opentype metrics with ease).</p> 864&lt;p&gt;In short, FreeType was hard to work with. It required complex logic, and provided no advantage over Harfbuzz (other than being able to fetch opentype metrics with ease).&lt;/p&gt;
865<p>Upgrading to Pango v1.44 will break your GTK applications (if you use a <code>bdf</code>/<code>pcf</code> bitmap font). Harfbuzz <em>does</em> support bitmap-only OpenType fonts, <code>otb</code>s. Convert your existing fonts over to <code>otb</code>s using <a href="https://fontforge.github.io">FontForge</a>. It is to be noted that applications such as <code>xterm</code> and <code>rxvt</code> use <code>xft</code> (X FreeType) to render fonts, and will remain unaffected by the update.</p> 865&lt;p&gt;Upgrading to Pango v1.44 will break your GTK applications (if you use a &lt;code&gt;bdf&lt;/code&gt;/&lt;code&gt;pcf&lt;/code&gt; bitmap font). Harfbuzz &lt;em&gt;does&lt;/em&gt; support bitmap-only OpenType fonts, &lt;code&gt;otb&lt;/code&gt;s. Convert your existing fonts over to &lt;code&gt;otb&lt;/code&gt;s using &lt;a href="https://fontforge.github.io"&gt;FontForge&lt;/a&gt;. It is to be noted that applications such as &lt;code&gt;xterm&lt;/code&gt; and &lt;code&gt;rxvt&lt;/code&gt; use &lt;code&gt;xft&lt;/code&gt; (X FreeType) to render fonts, and will remain unaffected by the update.&lt;/p&gt;
866<p>Both <a href="https://github.com/nerdypepper/scientifica">scientifica</a> and <a href="https://github.com/nerdypepper/curie">curie</a> will soon ship with bitmap-only OpenType font formats.</p></description> 866&lt;p&gt;Both &lt;a href="https://github.com/nerdypepper/scientifica"&gt;scientifica&lt;/a&gt; and &lt;a href="https://github.com/nerdypepper/curie"&gt;curie&lt;/a&gt; will soon ship with bitmap-only OpenType font formats.&lt;/p&gt;</description>
867<link>https://peppe.rs/posts/bye_bye_BDFs/</link> 867<link>https://peppe.rs/posts/bye_bye_BDFs/</link>
868<pubDate>Wed, 07 Aug 2019 12:31:00 +0000</pubDate> 868<pubDate>Wed, 07 Aug 2019 12:31:00 +0000</pubDate>
869<guid>https://peppe.rs/posts/bye_bye_BDFs/</guid> 869<guid>https://peppe.rs/posts/bye_bye_BDFs/</guid>
870</item> 870</item>
871<item> 871<item>
872<title>Onivim Sucks</title> 872<title>Onivim Sucks</title>
873<description><p><a href="https://v2.onivim.io">Onivim</a> is a ‘modern modal editor’, combining fancy interface and language features with vim-style modal editing. What’s wrong you ask?</p> 873<description>&lt;p&gt;&lt;a href="https://v2.onivim.io"&gt;Onivim&lt;/a&gt; is a ‘modern modal editor’, combining fancy interface and language features with vim-style modal editing. What’s wrong you ask?&lt;/p&gt;
874<p>Apart from <a href="https://github.com/onivim/oni2/issues/550">buggy syntax highlighting</a>, <a href="https://github.com/onivim/oni2/issues/519">broken scrolling</a> and <a href="https://github.com/onivim/oni2/issues?q=is%3Aissue+label%3A%22daily+editor+blocker%22+is%3Aopen">others</a>, Onivim is <strong>proprietary</strong> software. It is licensed under a commercial <a href="https://github.com/onivim/oni1/blob/master/Outrun-Labs-EULA-v1.1.md">end user agreement license</a>, which prohibits redistribution in both object code and source code formats.</p> 874&lt;p&gt;Apart from &lt;a href="https://github.com/onivim/oni2/issues/550"&gt;buggy syntax highlighting&lt;/a&gt;, &lt;a href="https://github.com/onivim/oni2/issues/519"&gt;broken scrolling&lt;/a&gt; and &lt;a href="https://github.com/onivim/oni2/issues?q=is%3Aissue+label%3A%22daily+editor+blocker%22+is%3Aopen"&gt;others&lt;/a&gt;, Onivim is &lt;strong&gt;proprietary&lt;/strong&gt; software. It is licensed under a commercial &lt;a href="https://github.com/onivim/oni1/blob/master/Outrun-Labs-EULA-v1.1.md"&gt;end user agreement license&lt;/a&gt;, which prohibits redistribution in both object code and source code formats.&lt;/p&gt;
875<p>Onivim’s core editor logic (bits that belong to vim), have been separated from the interface, into <a href="https://github.com/onivim/libvim">libvim</a>. libvim is licensed under MIT, which means, this ‘extension’ of vim is perfectly in adherence to <a href="http://vimdoc.sourceforge.net/htmldoc/uganda.html#license">vim’s license text</a>! Outrun Labs are exploiting this loophole (distributing vim as a library) to commercialize Onivim.</p> 875&lt;p&gt;Onivim’s core editor logic (bits that belong to vim), have been separated from the interface, into &lt;a href="https://github.com/onivim/libvim"&gt;libvim&lt;/a&gt;. libvim is licensed under MIT, which means, this ‘extension’ of vim is perfectly in adherence to &lt;a href="http://vimdoc.sourceforge.net/htmldoc/uganda.html#license"&gt;vim’s license text&lt;/a&gt;! Outrun Labs are exploiting this loophole (distributing vim as a library) to commercialize Onivim.&lt;/p&gt;
876<p>Onivim’s source code is available on <a href="https://github.com/onivim/oni2">GitHub</a>. They do mention that the source code trickles down to the <a href="https://github.com/onivim/oni2-mit">oni2-mit</a> repository, which (not yet) contains MIT-licensed code, <strong>18 months</strong> after each commit to the original repository.</p> 876&lt;p&gt;Onivim’s source code is available on &lt;a href="https://github.com/onivim/oni2"&gt;GitHub&lt;/a&gt;. They do mention that the source code trickles down to the &lt;a href="https://github.com/onivim/oni2-mit"&gt;oni2-mit&lt;/a&gt; repository, which (not yet) contains MIT-licensed code, &lt;strong&gt;18 months&lt;/strong&gt; after each commit to the original repository.&lt;/p&gt;
877<p>Want to contribute to Onivim? Don’t. They make a profit out of your contributions. Currently, Onivim is priced at $19.99, ‘pre-alpha’ pricing which is 80% off the final price! If you are on the lookout for an editor, I would suggest using <a href="https://vim.org">Vim</a>, charity ware that actually works, and costs $100 lesser.</p></description> 877&lt;p&gt;Want to contribute to Onivim? Don’t. They make a profit out of your contributions. Currently, Onivim is priced at $19.99, ‘pre-alpha’ pricing which is 80% off the final price! If you are on the lookout for an editor, I would suggest using &lt;a href="https://vim.org"&gt;Vim&lt;/a&gt;, charity ware that actually works, and costs $100 lesser.&lt;/p&gt;</description>
878<link>https://peppe.rs/posts/onivim_sucks/</link> 878<link>https://peppe.rs/posts/onivim_sucks/</link>
879<pubDate>Fri, 02 Aug 2019 12:31:00 +0000</pubDate> 879<pubDate>Fri, 02 Aug 2019 12:31:00 +0000</pubDate>
880<guid>https://peppe.rs/posts/onivim_sucks/</guid> 880<guid>https://peppe.rs/posts/onivim_sucks/</guid>
881</item> 881</item>
882<item> 882<item>
883<title>Bash Harder With Vim</title> 883<title>Bash Harder With Vim</title>
884<description><p>Bash is tricky, don’t let your editor get in your way. Here’s a couple of neat additions you could make to your <code>vimrc</code> for a better shell programming experience.</p> 884<description>&lt;p&gt;Bash is tricky, don’t let your editor get in your way. Here’s a couple of neat additions you could make to your &lt;code&gt;vimrc&lt;/code&gt; for a better shell programming experience.&lt;/p&gt;
885<h3 id="man-pages-inside-vim">Man pages inside vim</h3> 885&lt;h3 id="man-pages-inside-vim"&gt;Man pages inside vim&lt;/h3&gt;
886<p>Source this script to get started:</p> 886&lt;p&gt;Source this script to get started:&lt;/p&gt;
887<pre><code>runtime ftplugin/man.vim</code></pre> 887&lt;pre&gt;&lt;code&gt;runtime ftplugin/man.vim&lt;/code&gt;&lt;/pre&gt;
888<p>Now, you can open manpages inside vim with <code>:Man</code>! It adds nicer syntax highlighting and the ability to jump around with <code>Ctrl-]</code> and <code>Ctrl-T</code>.</p> 888&lt;p&gt;Now, you can open manpages inside vim with &lt;code&gt;:Man&lt;/code&gt;! It adds nicer syntax highlighting and the ability to jump around with &lt;code&gt;Ctrl-]&lt;/code&gt; and &lt;code&gt;Ctrl-T&lt;/code&gt;.&lt;/p&gt;
889<p>By default, the manpage is opened in a horizontal split, I prefer using a new tab:</p> 889&lt;p&gt;By default, the manpage is opened in a horizontal split, I prefer using a new tab:&lt;/p&gt;
890<pre><code>let g:ft_man_open_mode = &#39;tab&#39;</code></pre> 890&lt;pre&gt;&lt;code&gt;let g:ft_man_open_mode = &amp;#39;tab&amp;#39;&lt;/code&gt;&lt;/pre&gt;
891<h3 id="scratchpad-to-test-your-commands">Scratchpad to test your commands</h3> 891&lt;h3 id="scratchpad-to-test-your-commands"&gt;Scratchpad to test your commands&lt;/h3&gt;
892<p>I often test my <code>sed</code> substitutions, here is a sample from the script used to generate this site:</p> 892&lt;p&gt;I often test my &lt;code&gt;sed&lt;/code&gt; substitutions, here is a sample from the script used to generate this site:&lt;/p&gt;
893<pre><code># a substitution to convert snake_case to Title Case With Spaces 893&lt;pre&gt;&lt;code&gt;# a substitution to convert snake_case to Title Case With Spaces
894echo &quot;$1&quot; | sed -E -e &quot;s/\..+$//g&quot; -e &quot;s/_(.)/ \u\1/g&quot; -e &quot;s/^(.)/\u\1/g&quot;</code></pre> 894echo &amp;quot;$1&amp;quot; | sed -E -e &amp;quot;s/\..+$//g&amp;quot; -e &amp;quot;s/_(.)/ \u\1/g&amp;quot; -e &amp;quot;s/^(.)/\u\1/g&amp;quot;&lt;/code&gt;&lt;/pre&gt;
895<p>Instead of dropping into a new shell, just test it out directly from vim!</p> 895&lt;p&gt;Instead of dropping into a new shell, just test it out directly from vim!&lt;/p&gt;
896<ul> 896&lt;ul&gt;
897<li>Yank the line into a register:</li> 897&lt;li&gt;Yank the line into a register:&lt;/li&gt;
898</ul> 898&lt;/ul&gt;
899<pre><code>yy</code></pre> 899&lt;pre&gt;&lt;code&gt;yy&lt;/code&gt;&lt;/pre&gt;
900<ul> 900&lt;ul&gt;
901<li>Paste it into the command-line window:</li> 901&lt;li&gt;Paste it into the command-line window:&lt;/li&gt;
902</ul> 902&lt;/ul&gt;
903<pre><code>q:p</code></pre> 903&lt;pre&gt;&lt;code&gt;q:p&lt;/code&gt;&lt;/pre&gt;
904<ul> 904&lt;ul&gt;
905<li>Make edits as required:</li> 905&lt;li&gt;Make edits as required:&lt;/li&gt;
906</ul> 906&lt;/ul&gt;
907<pre><code>syntax off # previously run commands 907&lt;pre&gt;&lt;code&gt;syntax off # previously run commands
908edit index.html # in a buffer! 908edit index.html # in a buffer!
909w | so % 909w | so %
910!echo &quot;new_post.md&quot; | sed -E -e &quot;s/\..+$//g&quot; --snip-- 910!echo &amp;quot;new_post.md&amp;quot; | sed -E -e &amp;quot;s/\..+$//g&amp;quot; --snip--
911^--- note the use of &#39;!&#39;</code></pre> 911^--- note the use of &amp;#39;!&amp;#39;&lt;/code&gt;&lt;/pre&gt;
912<ul> 912&lt;ul&gt;
913<li>Hit enter with the cursor on the line containing your command!</li> 913&lt;li&gt;Hit enter with the cursor on the line containing your command!&lt;/li&gt;
914</ul> 914&lt;/ul&gt;
915<pre><code>$ vim 915&lt;pre&gt;&lt;code&gt;$ vim
916New Post # output 916New Post # output
917Press ENTER or type command to continue</code></pre></description> 917Press ENTER or type command to continue&lt;/code&gt;&lt;/pre&gt;</description>
918<link>https://peppe.rs/posts/bash_harder_with_vim/</link> 918<link>https://peppe.rs/posts/bash_harder_with_vim/</link>
919<pubDate>Wed, 31 Jul 2019 06:30:00 +0000</pubDate> 919<pubDate>Wed, 31 Jul 2019 06:30:00 +0000</pubDate>
920<guid>https://peppe.rs/posts/bash_harder_with_vim/</guid> 920<guid>https://peppe.rs/posts/bash_harder_with_vim/</guid>
921</item> 921</item>
922<item> 922<item>
923<title>Hold Position!</title> 923<title>Hold Position!</title>
924<description><p>Often times, when I run a vim command that makes “big” changes to a file (a macro or a <code>:vimgrep</code> command) I lose my original position and feel disoriented.</p> 924<description>&lt;p&gt;Often times, when I run a vim command that makes “big” changes to a file (a macro or a &lt;code&gt;:vimgrep&lt;/code&gt; command) I lose my original position and feel disoriented.&lt;/p&gt;
925<p><em>Save position with <code>winsaveview()</code>!</em></p> 925&lt;p&gt;&lt;em&gt;Save position with &lt;code&gt;winsaveview()&lt;/code&gt;!&lt;/em&gt;&lt;/p&gt;
926<p>The <code>winsaveview()</code> command returns a <code>Dictionary</code> that contains information about the view of the current window. This includes the cursor line number, cursor coloumn, the top most line in the window and a couple of other values, none of which concern us.</p> 926&lt;p&gt;The &lt;code&gt;winsaveview()&lt;/code&gt; command returns a &lt;code&gt;Dictionary&lt;/code&gt; that contains information about the view of the current window. This includes the cursor line number, cursor coloumn, the top most line in the window and a couple of other values, none of which concern us.&lt;/p&gt;
927<p>Before running our command (one that jumps around the buffer, a lot), we save our view, and restore it once its done, with <code>winrestview</code>.</p> 927&lt;p&gt;Before running our command (one that jumps around the buffer, a lot), we save our view, and restore it once its done, with &lt;code&gt;winrestview&lt;/code&gt;.&lt;/p&gt;
928<pre><code>let view = winsaveview() 928&lt;pre&gt;&lt;code&gt;let view = winsaveview()
929s/\s\+$//gc &quot; find and (confirm) replace trailing blanks 929s/\s\+$//gc &amp;quot; find and (confirm) replace trailing blanks
930winrestview(view) &quot; restore our original view!</code></pre> 930winrestview(view) &amp;quot; restore our original view!&lt;/code&gt;&lt;/pre&gt;
931<p>It might seem a little overkill in the above example, just use `` (double backticks) instead, but it comes in handy when you run your file through heavier filtering.</p></description> 931&lt;p&gt;It might seem a little overkill in the above example, just use `` (double backticks) instead, but it comes in handy when you run your file through heavier filtering.&lt;/p&gt;</description>
932<link>https://peppe.rs/posts/hold_position!/</link> 932<link>https://peppe.rs/posts/hold_position!/</link>
933<pubDate>Tue, 30 Jul 2019 12:31:00 +0000</pubDate> 933<pubDate>Tue, 30 Jul 2019 12:31:00 +0000</pubDate>
934<guid>https://peppe.rs/posts/hold_position!/</guid> 934<guid>https://peppe.rs/posts/hold_position!/</guid>
935</item> 935</item>
936<item> 936<item>
937<title>Get Better At Yanking And Putting In Vim</title> 937<title>Get Better At Yanking And Putting In Vim</title>
938<description><p>a couple of nifty tricks to help you copy-paste better:</p> 938<description>&lt;p&gt;a couple of nifty tricks to help you copy-paste better:&lt;/p&gt;
939<ol type="1"> 939&lt;ol type="1"&gt;
940<li><p>reselecting previously selected text (i use this to fix botched selections):</p> 940&lt;li&gt;&lt;p&gt;reselecting previously selected text (i use this to fix botched selections):&lt;/p&gt;
941<pre><code>gv &quot; :h gv for more 941&lt;pre&gt;&lt;code&gt;gv &amp;quot; :h gv for more
942 &quot; you can use `o` in visual mode to go to the `Other` end of the selection 942 &amp;quot; you can use `o` in visual mode to go to the `Other` end of the selection
943 &quot; use a motion to fix the selection</code></pre></li> 943 &amp;quot; use a motion to fix the selection&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
944<li><p>reselecting previously yanked text:</p> 944&lt;li&gt;&lt;p&gt;reselecting previously yanked text:&lt;/p&gt;
945<pre><code>`[v`] 945&lt;pre&gt;&lt;code&gt;`[v`]
946`[ &quot; marks the beginning of the previously yanked text :h `[ 946`[ &amp;quot; marks the beginning of the previously yanked text :h `[
947`] &quot; marks the end :h `] 947`] &amp;quot; marks the end :h `]
948 v &quot; visual select everything in between 948 v &amp;quot; visual select everything in between
949 949
950nnoremap gb `[v`] &quot; &quot;a quick map to perform the above</code></pre></li> 950nnoremap gb `[v`] &amp;quot; &amp;quot;a quick map to perform the above&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
951<li><p>pasting and indenting text (in one go):</p> 951&lt;li&gt;&lt;p&gt;pasting and indenting text (in one go):&lt;/p&gt;
952<pre><code>]p &quot; put (p) and adjust indent to current line 952&lt;pre&gt;&lt;code&gt;]p &amp;quot; put (p) and adjust indent to current line
953]P &quot; put the text before the cursor (P) and adjust indent to current line</code></pre></li> 953]P &amp;quot; put the text before the cursor (P) and adjust indent to current line&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
954</ol></description> 954&lt;/ol&gt;</description>
955<link>https://peppe.rs/posts/get_better_at_yanking_and_putting_in_vim/</link> 955<link>https://peppe.rs/posts/get_better_at_yanking_and_putting_in_vim/</link>
956<pubDate>Mon, 29 Jul 2019 12:31:00 +0000</pubDate> 956<pubDate>Mon, 29 Jul 2019 12:31:00 +0000</pubDate>
957<guid>https://peppe.rs/posts/get_better_at_yanking_and_putting_in_vim/</guid> 957<guid>https://peppe.rs/posts/get_better_at_yanking_and_putting_in_vim/</guid>