the ugly ocaml monstrosity powering my site oppi.li
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

new art: b8

+851 -840
art/b8.png

This is a binary file and will not be displayed.

docs/art/b8.png

This is a binary file and will not be displayed.

+12 -1
docs/art/index.html
··· 21 21 <h1>Art</h1> 22 22 <div class="separator"></div> 23 23 <div id="photos"> 24 - <a href="/art/goat.png"> 24 + <a href="/art/b8.png"> 25 + <div class="photo-container"> 26 + <img src="/art/b8.png"> 27 + <div class="photo-overlay"> 28 + <div class="photo-text"> 29 + <div class="photo-title">B8</div> 30 + <div class="photo-date">04/10 — 2021</div> 31 + </div> 32 + </div> 33 + </div> 34 + </a> 35 + <a href="/art/goat.png"> 25 36 <div class="photo-container"> 26 37 <img src="/art/goat.png"> 27 38 <div class="photo-overlay">
+5 -5
docs/index.html
··· 78 78 <tr> 79 79 <td class=table-post> 80 80 <div class="date"> 81 - 23/08 — 2021 81 + 04/10 — 2021 82 82 </div> 83 - <a href="/art/goat.png" class="post-link"> 84 - <span class="post-link">Goat</span> 83 + <a href="/art/b8.png" class="post-link"> 84 + <span class="post-link">B8</span> 85 85 </a> 86 86 </td> 87 87 <td class="table-stats"> 88 - <a href="/art/goat.png"> 89 - <img src="/art/goat.png" height="50px"> 88 + <a href="/art/b8.png"> 89 + <img src="/art/b8.png" height="50px"> 90 90 </a> 91 91 </td> 92 92 </tr>
+417 -417
docs/index.xml
··· 31 31 &lt;/figure&gt; 32 32 &lt;h3 id="day-17"&gt;Day 17&lt;/h3&gt; 33 33 &lt;p&gt;I decided to give the text-only statusline a touch up, by adding a active color and dither level preview. Aligning the “widget” to the right of statusline involved a lot more than I thought, so I created a ghetto CSS-like rectangle placement system to position containers inside containers:&lt;/p&gt; 34 - &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="co"&gt;// roughly something like this&lt;/span&gt;&lt;/span&gt; 35 - &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; statusline &lt;span class="op"&gt;=&lt;/span&gt; &lt;/span&gt; 36 - &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;Container::&lt;/span&gt;new(&lt;span class="pp"&gt;Offset::&lt;/span&gt;Left(&lt;span class="dv"&gt;0&lt;/span&gt;)&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;Offset::&lt;/span&gt;Bottom(&lt;span class="dv"&gt;40&lt;/span&gt;))&lt;/span&gt; 37 - &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;width(&lt;span class="pp"&gt;Size::&lt;/span&gt;Max)&lt;/span&gt; 38 - &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;height(&lt;span class="pp"&gt;Size::&lt;/span&gt;Absolute(&lt;span class="dv"&gt;20&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 39 - &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true"&gt;&lt;/a&gt; &lt;/span&gt; 40 - &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" 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; primary &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;Container::&lt;/span&gt;uninit()&lt;/span&gt; 41 - &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;width(&lt;span class="pp"&gt;Size::&lt;/span&gt;Absolute(&lt;span class="dv"&gt;16&lt;/span&gt;))&lt;/span&gt; 42 - &lt;span id="cb1-9"&gt;&lt;a href="#cb1-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;height(&lt;span class="pp"&gt;Size::&lt;/span&gt;Absolute(&lt;span class="dv"&gt;16&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 43 - &lt;span id="cb1-10"&gt;&lt;a href="#cb1-10" aria-hidden="true"&gt;&lt;/a&gt; &lt;/span&gt; 44 - &lt;span id="cb1-11"&gt;&lt;a href="#cb1-11" aria-hidden="true"&gt;&lt;/a&gt;container&lt;span class="op"&gt;.&lt;/span&gt;place(&lt;/span&gt; 45 - &lt;span id="cb1-12"&gt;&lt;a href="#cb1-12" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kw"&gt;mut&lt;/span&gt; padding_box&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 46 - &lt;span id="cb1-13"&gt;&lt;a href="#cb1-13" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;HorAlign::&lt;/span&gt;Right&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 47 - &lt;span id="cb1-14"&gt;&lt;a href="#cb1-14" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="pp"&gt;VertAlign::&lt;/span&gt;Center&lt;/span&gt; 48 - &lt;span id="cb1-15"&gt;&lt;a href="#cb1-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; 34 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// roughly something like this&lt;/span&gt;&lt;/span&gt; 35 + &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; statusline &lt;span class="op"&gt;=&lt;/span&gt; &lt;/span&gt; 36 + &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="pp"&gt;Container::&lt;/span&gt;new(&lt;span class="pp"&gt;Offset::&lt;/span&gt;Left(&lt;span class="dv"&gt;0&lt;/span&gt;)&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;Offset::&lt;/span&gt;Bottom(&lt;span class="dv"&gt;40&lt;/span&gt;))&lt;/span&gt; 37 + &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;width(&lt;span class="pp"&gt;Size::&lt;/span&gt;Max)&lt;/span&gt; 38 + &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;height(&lt;span class="pp"&gt;Size::&lt;/span&gt;Absolute(&lt;span class="dv"&gt;20&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 39 + &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;/span&gt; 40 + &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; &lt;span class="kw"&gt;mut&lt;/span&gt; primary &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;Container::&lt;/span&gt;uninit()&lt;/span&gt; 41 + &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;width(&lt;span class="pp"&gt;Size::&lt;/span&gt;Absolute(&lt;span class="dv"&gt;16&lt;/span&gt;))&lt;/span&gt; 42 + &lt;span id="cb1-9"&gt;&lt;a href="#cb1-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;height(&lt;span class="pp"&gt;Size::&lt;/span&gt;Absolute(&lt;span class="dv"&gt;16&lt;/span&gt;))&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 43 + &lt;span id="cb1-10"&gt;&lt;a href="#cb1-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;/span&gt; 44 + &lt;span id="cb1-11"&gt;&lt;a href="#cb1-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;container&lt;span class="op"&gt;.&lt;/span&gt;place(&lt;/span&gt; 45 + &lt;span id="cb1-12"&gt;&lt;a href="#cb1-12" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kw"&gt;mut&lt;/span&gt; padding_box&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 46 + &lt;span id="cb1-13"&gt;&lt;a href="#cb1-13" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="pp"&gt;HorAlign::&lt;/span&gt;Right&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 47 + &lt;span id="cb1-14"&gt;&lt;a href="#cb1-14" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="pp"&gt;VertAlign::&lt;/span&gt;Center&lt;/span&gt; 48 + &lt;span id="cb1-15"&gt;&lt;a href="#cb1-15" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 49 49 &lt;p&gt;The result (brush preview on the bottom right):&lt;/p&gt; 50 50 &lt;figure&gt; 51 51 &lt;video src="https://u.peppe.rs/OtU.mp4" controls=""&gt;&lt;a href="https://u.peppe.rs/OtU.mp4"&gt;Day 17&lt;/a&gt;&lt;/video&gt;&lt;figcaption aria-hidden="true"&gt;Day 17&lt;/figcaption&gt; 52 52 &lt;/figure&gt; 53 53 &lt;h3 id="day-16"&gt;Day 16&lt;/h3&gt; 54 54 &lt;p&gt;The embedded lisp is coming along nicely, users can load a custom &lt;code&gt;rc.lisp&lt;/code&gt;, which is evaluated on startup. To disable to grid on start, for example:&lt;/p&gt; 55 - &lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode scheme"&gt;&lt;code class="sourceCode scheme"&gt;&lt;span id="cb2-1"&gt;&lt;a href="#cb2-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;;;; rc.lisp&lt;/span&gt;&lt;/span&gt; 56 - &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true"&gt;&lt;/a&gt;(toggle-grid)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 55 + &lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode scheme"&gt;&lt;code class="sourceCode scheme"&gt;&lt;span id="cb2-1"&gt;&lt;a href="#cb2-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;;;; rc.lisp&lt;/span&gt;&lt;/span&gt; 56 + &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;(toggle-grid)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 57 57 &lt;p&gt;Some aliases to switch between brushes:&lt;/p&gt; 58 - &lt;div class="sourceCode" id="cb3"&gt;&lt;pre class="sourceCode scheme"&gt;&lt;code class="sourceCode scheme"&gt;&lt;span id="cb3-1"&gt;&lt;a href="#cb3-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;;;; rc.lisp&lt;/span&gt;&lt;/span&gt; 59 - &lt;span id="cb3-2"&gt;&lt;a href="#cb3-2" aria-hidden="true"&gt;&lt;/a&gt;(&lt;span class="ex"&gt;define&lt;/span&gt;&lt;span class="fu"&gt; &lt;/span&gt;(brush kind)&lt;/span&gt; 60 - &lt;span id="cb3-3"&gt;&lt;a href="#cb3-3" aria-hidden="true"&gt;&lt;/a&gt; (&lt;span class="kw"&gt;cond&lt;/span&gt;&lt;/span&gt; 61 - &lt;span id="cb3-4"&gt;&lt;a href="#cb3-4" aria-hidden="true"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;f) (brush-fill))&lt;/span&gt; 62 - &lt;span id="cb3-5"&gt;&lt;a href="#cb3-5" aria-hidden="true"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;c) (brush-circle))&lt;/span&gt; 63 - &lt;span id="cb3-6"&gt;&lt;a href="#cb3-6" aria-hidden="true"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;l) (brush-line))&lt;/span&gt; 64 - &lt;span id="cb3-7"&gt;&lt;a href="#cb3-7" aria-hidden="true"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;l+) (brush-line-extend))&lt;/span&gt; 65 - &lt;span id="cb3-8"&gt;&lt;a href="#cb3-8" aria-hidden="true"&gt;&lt;/a&gt; (&lt;span class="kw"&gt;else&lt;/span&gt; (brush-circle))))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 58 + &lt;div class="sourceCode" id="cb3"&gt;&lt;pre class="sourceCode scheme"&gt;&lt;code class="sourceCode scheme"&gt;&lt;span id="cb3-1"&gt;&lt;a href="#cb3-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;;;; rc.lisp&lt;/span&gt;&lt;/span&gt; 59 + &lt;span id="cb3-2"&gt;&lt;a href="#cb3-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;(&lt;span class="ex"&gt;define&lt;/span&gt;&lt;span class="fu"&gt; &lt;/span&gt;(brush kind)&lt;/span&gt; 60 + &lt;span id="cb3-3"&gt;&lt;a href="#cb3-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; (&lt;span class="kw"&gt;cond&lt;/span&gt;&lt;/span&gt; 61 + &lt;span id="cb3-4"&gt;&lt;a href="#cb3-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;f) (brush-fill))&lt;/span&gt; 62 + &lt;span id="cb3-5"&gt;&lt;a href="#cb3-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;c) (brush-circle))&lt;/span&gt; 63 + &lt;span id="cb3-6"&gt;&lt;a href="#cb3-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;l) (brush-line))&lt;/span&gt; 64 + &lt;span id="cb3-7"&gt;&lt;a href="#cb3-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; ((&lt;span class="kw"&gt;eq?&lt;/span&gt; kind &amp;#39;l+) (brush-line-extend))&lt;/span&gt; 65 + &lt;span id="cb3-8"&gt;&lt;a href="#cb3-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; (&lt;span class="kw"&gt;else&lt;/span&gt; (brush-circle))))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 66 66 &lt;p&gt;The following script draws a straight line along a given axis, at a given distance from the canvas boundary:&lt;/p&gt; 67 67 &lt;figure&gt; 68 68 &lt;video src="https://u.peppe.rs/b3i.mp4" controls=""&gt;&lt;a href="https://u.peppe.rs/b3i.mp4"&gt;Day 16&lt;/a&gt;&lt;/video&gt;&lt;figcaption aria-hidden="true"&gt;Day 16&lt;/figcaption&gt; 69 69 &lt;/figure&gt; 70 70 &lt;h3 id="day-15"&gt;Day 15&lt;/h3&gt; 71 71 &lt;p&gt;I began writing a standard library for the lisp, in lisp. It includes basic list operations: &lt;code&gt;car&lt;/code&gt;, &lt;code&gt;cdr&lt;/code&gt;, &lt;code&gt;null?&lt;/code&gt;, &lt;code&gt;list&lt;/code&gt;, higher order functions: &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;fold&lt;/code&gt;:&lt;/p&gt; 72 - &lt;div class="sourceCode" id="cb4"&gt;&lt;pre class="sourceCode lisp"&gt;&lt;code class="sourceCode commonlisp"&gt;&lt;span id="cb4-1"&gt;&lt;a href="#cb4-1" aria-hidden="true"&gt;&lt;/a&gt;(define (member? item ls)&lt;/span&gt; 73 - &lt;span id="cb4-2"&gt;&lt;a href="#cb4-2" aria-hidden="true"&gt;&lt;/a&gt; (fold &lt;span class="dv"&gt;#f&lt;/span&gt;&lt;/span&gt; 74 - &lt;span id="cb4-3"&gt;&lt;a href="#cb4-3" aria-hidden="true"&gt;&lt;/a&gt; (&lt;span class="kw"&gt;lambda&lt;/span&gt; (acc x) (&lt;span class="kw"&gt;or&lt;/span&gt; acc (eq? item x)))&lt;/span&gt; 75 - &lt;span id="cb4-4"&gt;&lt;a href="#cb4-4" aria-hidden="true"&gt;&lt;/a&gt; ls))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 72 + &lt;div class="sourceCode" id="cb4"&gt;&lt;pre class="sourceCode lisp"&gt;&lt;code class="sourceCode commonlisp"&gt;&lt;span id="cb4-1"&gt;&lt;a href="#cb4-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;(define (member? item ls)&lt;/span&gt; 73 + &lt;span id="cb4-2"&gt;&lt;a href="#cb4-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; (fold &lt;span class="dv"&gt;#f&lt;/span&gt;&lt;/span&gt; 74 + &lt;span id="cb4-3"&gt;&lt;a href="#cb4-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; (&lt;span class="kw"&gt;lambda&lt;/span&gt; (acc x) (&lt;span class="kw"&gt;or&lt;/span&gt; acc (eq? item x)))&lt;/span&gt; 75 + &lt;span id="cb4-4"&gt;&lt;a href="#cb4-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; ls))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 76 76 &lt;h3 id="day-14"&gt;Day 14&lt;/h3&gt; 77 77 &lt;p&gt;I attempted a &lt;a href="https://peppe.rs/art/ramen_noodles.png"&gt;small art piece&lt;/a&gt; using the editor, while it was largely usable, I felt a certain lack of feedback. The brushes just didn’t relay as much info as I’d have liked, for example, the approximate points of the line or the angle made by the line against the x-axis. Unfortunately, the existing infrastructure around brushes and line drawing didn’t easily allow for this either. I went ahead and reimplemented brushes, and added a new flood fill brush too:&lt;/p&gt; 78 78 &lt;figure&gt; ··· 105 105 &lt;/figure&gt; 106 106 &lt;h3 id="day-8"&gt;Day 8&lt;/h3&gt; 107 107 &lt;p&gt;One of my favourite features of GIMP was symmetric editing. I added some coordinate geometry primitives to my pixmap abstraction, allowing for mirroring and reflecting figures about lines or points. The result was an ergonomic function that applies symmetry to any painting operation, (undo/redo works as expected):&lt;/p&gt; 108 - &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;let&lt;/span&gt; line &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;self&lt;/span&gt;&lt;span class="op"&gt;.&lt;/span&gt;pixmap&lt;span class="op"&gt;.&lt;/span&gt;get_line(start&lt;span class="op"&gt;,&lt;/span&gt; end)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 109 - &lt;span id="cb5-2"&gt;&lt;a href="#cb5-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; sym_line &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;self&lt;/span&gt;&lt;span class="op"&gt;.&lt;/span&gt;symmetry&lt;span class="op"&gt;.&lt;/span&gt;apply(&lt;span class="op"&gt;&amp;amp;&lt;/span&gt;line)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 110 - &lt;span id="cb5-3"&gt;&lt;a href="#cb5-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;for&lt;/span&gt; point on line&lt;span class="op"&gt;.&lt;/span&gt;extend(sym_line) &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 111 - &lt;span id="cb5-4"&gt;&lt;a href="#cb5-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="co"&gt;// draw to window&lt;/span&gt;&lt;/span&gt; 112 - &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;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 108 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; line &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;self&lt;/span&gt;&lt;span class="op"&gt;.&lt;/span&gt;pixmap&lt;span class="op"&gt;.&lt;/span&gt;get_line(start&lt;span class="op"&gt;,&lt;/span&gt; end)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 109 + &lt;span id="cb5-2"&gt;&lt;a href="#cb5-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; sym_line &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="kw"&gt;self&lt;/span&gt;&lt;span class="op"&gt;.&lt;/span&gt;symmetry&lt;span class="op"&gt;.&lt;/span&gt;apply(&lt;span class="op"&gt;&amp;amp;&lt;/span&gt;line)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 110 + &lt;span id="cb5-3"&gt;&lt;a href="#cb5-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;for&lt;/span&gt; point on line&lt;span class="op"&gt;.&lt;/span&gt;extend(sym_line) &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 111 + &lt;span id="cb5-4"&gt;&lt;a href="#cb5-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="co"&gt;// draw to window&lt;/span&gt;&lt;/span&gt; 112 + &lt;span id="cb5-5"&gt;&lt;a href="#cb5-5" aria-hidden="true" tabindex="-1"&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 113 &lt;figure&gt; 114 114 &lt;video src="https://u.peppe.rs/B1.mp4" controls=""&gt;&lt;a href="https://u.peppe.rs/B1.mp4"&gt;Day 8&lt;/a&gt;&lt;/video&gt;&lt;figcaption aria-hidden="true"&gt;Day 8&lt;/figcaption&gt; 115 115 &lt;/figure&gt; ··· 159 159 &lt;p&gt;cgit is &lt;em&gt;very&lt;/em&gt; bare bones. It is &lt;a href="https://tools.ietf.org/html/rfc3875"&gt;cgi-based&lt;/a&gt; web interface to git, and nothing more. You may browse repositories, view diffs, commit logs and even clone via http. If you are looking to replace Github with cgit, keep in mind that cgit does not handle issues or pull/merge requests. If people wish to contribute to your work, they would have to send you a patch via email.&lt;/p&gt; 160 160 &lt;h3 id="setup"&gt;Setup&lt;/h3&gt; 161 161 &lt;p&gt;Installing cgit is fairly straightforward, if you would like to compile it from source:&lt;/p&gt; 162 - &lt;div class="sourceCode" id="cb1"&gt;&lt;pre class="sourceCode sh"&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="co"&gt;# fetch&lt;/span&gt;&lt;/span&gt; 163 - &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;git&lt;/span&gt; clone https://git.zx2c4.com &lt;span class="kw"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="bu"&gt;cd&lt;/span&gt; cgit&lt;/span&gt; 164 - &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;git&lt;/span&gt; submodule init&lt;/span&gt; 165 - &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;git&lt;/span&gt; submodule update&lt;/span&gt; 166 - &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 167 - &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# install&lt;/span&gt;&lt;/span&gt; 168 - &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;make&lt;/span&gt; NO_LUA=1&lt;/span&gt; 169 - &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;sudo&lt;/span&gt; make install&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 162 + &lt;div class="sourceCode" id="cb1"&gt;&lt;pre class="sourceCode sh"&gt;&lt;code class="sourceCode bash"&gt;&lt;span id="cb1-1"&gt;&lt;a href="#cb1-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# fetch&lt;/span&gt;&lt;/span&gt; 163 + &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;git&lt;/span&gt; clone https://git.zx2c4.com &lt;span class="kw"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="bu"&gt;cd&lt;/span&gt; cgit&lt;/span&gt; 164 + &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;git&lt;/span&gt; submodule init&lt;/span&gt; 165 + &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;git&lt;/span&gt; submodule update&lt;/span&gt; 166 + &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 167 + &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# install&lt;/span&gt;&lt;/span&gt; 168 + &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;make&lt;/span&gt; NO_LUA=1&lt;/span&gt; 169 + &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;sudo&lt;/span&gt; make install&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 170 170 &lt;p&gt;This would drop the cgit cgi script (and the default css) into &lt;code&gt;/var/www/htdocs/cgit&lt;/code&gt;. You may configure cgit by editing &lt;code&gt;/etc/cgitrc&lt;/code&gt;. I specify the &lt;code&gt;NO_LUA&lt;/code&gt; flag to compile without lua support, exclude that flag if you would like to extend cgit via lua scripts.&lt;/p&gt; 171 171 &lt;h3 id="going-live"&gt;Going live&lt;/h3&gt; 172 172 &lt;p&gt;You might want to use, &lt;a href="https://github.com/gnosek/fcgiwrap"&gt;fcgiwrap&lt;/a&gt;, a &lt;a href="http://www.nongnu.org/fastcgi"&gt;fastcgi&lt;/a&gt; wrapper for &lt;code&gt;cgi&lt;/code&gt; scripts,&lt;/p&gt; 173 - &lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode sh"&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="fu"&gt;sudo&lt;/span&gt; apt install fcgiwrap&lt;/span&gt; 174 - &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;sudo&lt;/span&gt; systemctl start fcgiwrap.socket&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 173 + &lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode sh"&gt;&lt;code class="sourceCode bash"&gt;&lt;span id="cb2-1"&gt;&lt;a href="#cb2-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;sudo&lt;/span&gt; apt install fcgiwrap&lt;/span&gt; 174 + &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;sudo&lt;/span&gt; systemctl start fcgiwrap.socket&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 175 175 &lt;p&gt;Expose the cgit cgi script to the web via &lt;code&gt;nginx&lt;/code&gt;:&lt;/p&gt; 176 176 &lt;pre&gt;&lt;code&gt;# nginx.conf 177 177 server { ··· 298 298 &lt;p&gt;Builds may be generated by specifying a &lt;code&gt;default.nix&lt;/code&gt; file, and running &lt;code&gt;nix-build&lt;/code&gt;. Conventional package managers require you to specify a dependency list, but there is no guarantee that this list is complete. The package will build on your machine even if you forget a dependency. However, with Nix, packages are installed to &lt;code&gt;/nix/store&lt;/code&gt;, and not global paths such as &lt;code&gt;/usr/bin/...&lt;/code&gt;, if your project builds, it means you have included every last one.&lt;/p&gt; 299 299 &lt;p&gt;Issues on most my projects have been “unable to build because &lt;code&gt;libxcb&lt;/code&gt; is missing”, or “this version of &lt;code&gt;openssl&lt;/code&gt; is too old”. Tools like &lt;code&gt;cargo&lt;/code&gt; and &lt;code&gt;pip&lt;/code&gt; are poor package managers. While they &lt;em&gt;can&lt;/em&gt; guarantee that Rust or Python dependencies are met, they make assumptions about the target system.&lt;/p&gt; 300 300 &lt;p&gt;For example, &lt;a href="https://github.com/nerdypepper/site"&gt;this website&lt;/a&gt; is now built using Nix, anyone using Nix may simply, clone the repository and run &lt;code&gt;./generate.sh&lt;/code&gt;, and it would &lt;em&gt;just work&lt;/em&gt;, while keeping your global namespace clean™:&lt;/p&gt; 301 - &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;#! /usr/bin/env nix-shell&lt;/span&gt;&lt;/span&gt; 302 - &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;#! nix-shell -i bash -p eva pandoc esh&lt;/span&gt;&lt;/span&gt; 303 - &lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 304 - &lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;# some bash magic ;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 301 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;#! /usr/bin/env nix-shell&lt;/span&gt;&lt;/span&gt; 302 + &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;#! nix-shell -i bash -p eva pandoc esh&lt;/span&gt;&lt;/span&gt; 303 + &lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 304 + &lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# some bash magic ;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 305 305 &lt;p&gt;Dependencies are included with the &lt;code&gt;-p&lt;/code&gt; flag, the shell script is executed with an interpreter, specified with the &lt;code&gt;-i&lt;/code&gt; flag.&lt;/p&gt; 306 306 &lt;h3 id="impressions"&gt;Impressions&lt;/h3&gt; 307 307 &lt;p&gt;NixOS is by no means, simple. As a newcomer, using Nix was not easy, heck, I had to learn a purely functional, lazy language to just build programs. There is a lot to be desired on the tooling front as well. A well fleshed out LSP plugin would be nice (&lt;a href="https://github.com/nix-community/rnix-lsp"&gt;rnix-lsp looks promising&lt;/a&gt;).&lt;/p&gt; ··· 326 326 &lt;h3 id="lack-of-sum-types"&gt;Lack of Sum types&lt;/h3&gt; 327 327 &lt;p&gt;A “Sum” type is a data type that can hold one of many states at a given time, similar to how a boolean can hold a true or a false, not too different from an &lt;code&gt;enum&lt;/code&gt; type in C. Go lacks &lt;code&gt;enum&lt;/code&gt; types unfortunately, and you are forced to resort to crafting your own substitute.&lt;/p&gt; 328 328 &lt;p&gt;A type to represent gender for example:&lt;/p&gt; 329 - &lt;div class="sourceCode" id="cb1"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb1-1"&gt;&lt;a href="#cb1-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; Gender &lt;span class="dt"&gt;int&lt;/span&gt;&lt;/span&gt; 330 - &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 331 - &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;const&lt;/span&gt; (&lt;/span&gt; 332 - &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true"&gt;&lt;/a&gt; Male Gender = &lt;span class="ot"&gt;iota&lt;/span&gt; &lt;span class="co"&gt;// assigns Male to 0&lt;/span&gt;&lt;/span&gt; 333 - &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true"&gt;&lt;/a&gt; Female &lt;span class="co"&gt;// assigns Female to 1&lt;/span&gt;&lt;/span&gt; 334 - &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true"&gt;&lt;/a&gt; Other &lt;span class="co"&gt;// assigns Other to 2&lt;/span&gt;&lt;/span&gt; 335 - &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true"&gt;&lt;/a&gt;)&lt;/span&gt; 336 - &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 337 - &lt;span id="cb1-9"&gt;&lt;a href="#cb1-9" aria-hidden="true"&gt;&lt;/a&gt;fmt.Println(&lt;span class="st"&gt;&amp;quot;My gender is &amp;quot;&lt;/span&gt;, Male)&lt;/span&gt; 338 - &lt;span id="cb1-10"&gt;&lt;a href="#cb1-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// My gender is 0&lt;/span&gt;&lt;/span&gt; 339 - &lt;span id="cb1-11"&gt;&lt;a href="#cb1-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// Oops! We have to implement String() for Gender ...&lt;/span&gt;&lt;/span&gt; 340 - &lt;span id="cb1-12"&gt;&lt;a href="#cb1-12" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 341 - &lt;span id="cb1-13"&gt;&lt;a href="#cb1-13" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;func&lt;/span&gt; (g Gender) String() &lt;span class="dt"&gt;string&lt;/span&gt; {&lt;/span&gt; 342 - &lt;span id="cb1-14"&gt;&lt;a href="#cb1-14" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;switch&lt;/span&gt; (g) {&lt;/span&gt; 343 - &lt;span id="cb1-15"&gt;&lt;a href="#cb1-15" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;case&lt;/span&gt; &lt;span class="dv"&gt;0&lt;/span&gt;: &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;Male&amp;quot;&lt;/span&gt;&lt;/span&gt; 344 - &lt;span id="cb1-16"&gt;&lt;a href="#cb1-16" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;case&lt;/span&gt; &lt;span class="dv"&gt;1&lt;/span&gt;: &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;Female&amp;quot;&lt;/span&gt;&lt;/span&gt; 345 - &lt;span id="cb1-17"&gt;&lt;a href="#cb1-17" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;default&lt;/span&gt;: &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;Other&amp;quot;&lt;/span&gt;&lt;/span&gt; 346 - &lt;span id="cb1-18"&gt;&lt;a href="#cb1-18" aria-hidden="true"&gt;&lt;/a&gt; }&lt;/span&gt; 347 - &lt;span id="cb1-19"&gt;&lt;a href="#cb1-19" aria-hidden="true"&gt;&lt;/a&gt;}&lt;/span&gt; 348 - &lt;span id="cb1-20"&gt;&lt;a href="#cb1-20" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 349 - &lt;span id="cb1-21"&gt;&lt;a href="#cb1-21" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// You can accidentally do stupid stuff like:&lt;/span&gt;&lt;/span&gt; 350 - &lt;span id="cb1-22"&gt;&lt;a href="#cb1-22" aria-hidden="true"&gt;&lt;/a&gt;gender := Male + &lt;span class="dv"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 329 + &lt;div class="sourceCode" id="cb1"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb1-1"&gt;&lt;a href="#cb1-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;type&lt;/span&gt; Gender &lt;span class="dt"&gt;int&lt;/span&gt;&lt;/span&gt; 330 + &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 331 + &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;const&lt;/span&gt; &lt;span class="op"&gt;(&lt;/span&gt;&lt;/span&gt; 332 + &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; Male Gender &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="ot"&gt;iota&lt;/span&gt; &lt;span class="co"&gt;// assigns Male to 0&lt;/span&gt;&lt;/span&gt; 333 + &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; Female &lt;span class="co"&gt;// assigns Female to 1&lt;/span&gt;&lt;/span&gt; 334 + &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; Other &lt;span class="co"&gt;// assigns Other to 2&lt;/span&gt;&lt;/span&gt; 335 + &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;)&lt;/span&gt;&lt;/span&gt; 336 + &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 337 + &lt;span id="cb1-9"&gt;&lt;a href="#cb1-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;fmt&lt;span class="op"&gt;.&lt;/span&gt;Println&lt;span class="op"&gt;(&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;My gender is &amp;quot;&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; Male&lt;span class="op"&gt;)&lt;/span&gt;&lt;/span&gt; 338 + &lt;span id="cb1-10"&gt;&lt;a href="#cb1-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// My gender is 0&lt;/span&gt;&lt;/span&gt; 339 + &lt;span id="cb1-11"&gt;&lt;a href="#cb1-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// Oops! We have to implement String() for Gender ...&lt;/span&gt;&lt;/span&gt; 340 + &lt;span id="cb1-12"&gt;&lt;a href="#cb1-12" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 341 + &lt;span id="cb1-13"&gt;&lt;a href="#cb1-13" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;func&lt;/span&gt; &lt;span class="op"&gt;(&lt;/span&gt;g Gender&lt;span class="op"&gt;)&lt;/span&gt; String&lt;span class="op"&gt;()&lt;/span&gt; &lt;span class="dt"&gt;string&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 342 + &lt;span id="cb1-14"&gt;&lt;a href="#cb1-14" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;switch&lt;/span&gt; &lt;span class="op"&gt;(&lt;/span&gt;g&lt;span class="op"&gt;)&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 343 + &lt;span id="cb1-15"&gt;&lt;a href="#cb1-15" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;case&lt;/span&gt; &lt;span class="dv"&gt;0&lt;/span&gt;&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;Male&amp;quot;&lt;/span&gt;&lt;/span&gt; 344 + &lt;span id="cb1-16"&gt;&lt;a href="#cb1-16" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;case&lt;/span&gt; &lt;span class="dv"&gt;1&lt;/span&gt;&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;Female&amp;quot;&lt;/span&gt;&lt;/span&gt; 345 + &lt;span id="cb1-17"&gt;&lt;a href="#cb1-17" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;default&lt;/span&gt;&lt;span class="op"&gt;:&lt;/span&gt; &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;Other&amp;quot;&lt;/span&gt;&lt;/span&gt; 346 + &lt;span id="cb1-18"&gt;&lt;a href="#cb1-18" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 347 + &lt;span id="cb1-19"&gt;&lt;a href="#cb1-19" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 348 + &lt;span id="cb1-20"&gt;&lt;a href="#cb1-20" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 349 + &lt;span id="cb1-21"&gt;&lt;a href="#cb1-21" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// You can accidentally do stupid stuff like:&lt;/span&gt;&lt;/span&gt; 350 + &lt;span id="cb1-22"&gt;&lt;a href="#cb1-22" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;gender &lt;span class="op"&gt;:=&lt;/span&gt; Male &lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="dv"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 351 351 &lt;p&gt;The Haskell equivalent of the same:&lt;/p&gt; 352 - &lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode haskell"&gt;&lt;code class="sourceCode haskell"&gt;&lt;span id="cb2-1"&gt;&lt;a href="#cb2-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;data&lt;/span&gt; &lt;span class="dt"&gt;Gender&lt;/span&gt; &lt;span class="ot"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;Male&lt;/span&gt;&lt;/span&gt; 353 - &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="dt"&gt;Female&lt;/span&gt;&lt;/span&gt; 354 - &lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="dt"&gt;Other&lt;/span&gt;&lt;/span&gt; 355 - &lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;deriving&lt;/span&gt; (&lt;span class="dt"&gt;Show&lt;/span&gt;)&lt;/span&gt; 356 - &lt;span id="cb2-5"&gt;&lt;a href="#cb2-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 357 - &lt;span id="cb2-6"&gt;&lt;a href="#cb2-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="fu"&gt;print&lt;/span&gt; &lt;span class="op"&gt;$&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;My gender is &amp;quot;&lt;/span&gt; &lt;span class="op"&gt;++&lt;/span&gt; (&lt;span class="fu"&gt;show&lt;/span&gt; &lt;span class="dt"&gt;Male&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 352 + &lt;div class="sourceCode" id="cb2"&gt;&lt;pre class="sourceCode haskell"&gt;&lt;code class="sourceCode haskell"&gt;&lt;span id="cb2-1"&gt;&lt;a href="#cb2-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;data&lt;/span&gt; &lt;span class="dt"&gt;Gender&lt;/span&gt; &lt;span class="ot"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;Male&lt;/span&gt;&lt;/span&gt; 353 + &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="dt"&gt;Female&lt;/span&gt;&lt;/span&gt; 354 + &lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;|&lt;/span&gt; &lt;span class="dt"&gt;Other&lt;/span&gt;&lt;/span&gt; 355 + &lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;deriving&lt;/span&gt; (&lt;span class="dt"&gt;Show&lt;/span&gt;)&lt;/span&gt; 356 + &lt;span id="cb2-5"&gt;&lt;a href="#cb2-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 357 + &lt;span id="cb2-6"&gt;&lt;a href="#cb2-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;print&lt;/span&gt; &lt;span class="op"&gt;$&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;My gender is &amp;quot;&lt;/span&gt; &lt;span class="op"&gt;++&lt;/span&gt; (&lt;span class="fu"&gt;show&lt;/span&gt; &lt;span class="dt"&gt;Male&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 358 358 &lt;h3 id="type-assertions"&gt;Type Assertions&lt;/h3&gt; 359 359 &lt;p&gt;A downcast with an optional error check? What could go wrong?&lt;/p&gt; 360 360 &lt;p&gt;Type assertions in Go allow you to do:&lt;/p&gt; 361 - &lt;div class="sourceCode" id="cb3"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb3-1"&gt;&lt;a href="#cb3-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;var&lt;/span&gt; x &lt;span class="kw"&gt;interface&lt;/span&gt;{} = &lt;span class="dv"&gt;7&lt;/span&gt;&lt;/span&gt; 362 - &lt;span id="cb3-2"&gt;&lt;a href="#cb3-2" aria-hidden="true"&gt;&lt;/a&gt;y, goodToGo := x.(&lt;span class="dt"&gt;int&lt;/span&gt;)&lt;/span&gt; 363 - &lt;span id="cb3-3"&gt;&lt;a href="#cb3-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;if&lt;/span&gt; goodToGo {&lt;/span&gt; 364 - &lt;span id="cb3-4"&gt;&lt;a href="#cb3-4" aria-hidden="true"&gt;&lt;/a&gt; fmt.Println(y)&lt;/span&gt; 365 - &lt;span id="cb3-5"&gt;&lt;a href="#cb3-5" aria-hidden="true"&gt;&lt;/a&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 361 + &lt;div class="sourceCode" id="cb3"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb3-1"&gt;&lt;a href="#cb3-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;var&lt;/span&gt; x &lt;span class="kw"&gt;interface&lt;/span&gt;&lt;span class="op"&gt;{}&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dv"&gt;7&lt;/span&gt;&lt;/span&gt; 362 + &lt;span id="cb3-2"&gt;&lt;a href="#cb3-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;y&lt;span class="op"&gt;,&lt;/span&gt; goodToGo &lt;span class="op"&gt;:=&lt;/span&gt; x&lt;span class="op"&gt;.(&lt;/span&gt;&lt;span class="dt"&gt;int&lt;/span&gt;&lt;span class="op"&gt;)&lt;/span&gt;&lt;/span&gt; 363 + &lt;span id="cb3-3"&gt;&lt;a href="#cb3-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;if&lt;/span&gt; goodToGo &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 364 + &lt;span id="cb3-4"&gt;&lt;a href="#cb3-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; fmt&lt;span class="op"&gt;.&lt;/span&gt;Println&lt;span class="op"&gt;(&lt;/span&gt;y&lt;span class="op"&gt;)&lt;/span&gt;&lt;/span&gt; 365 + &lt;span id="cb3-5"&gt;&lt;a href="#cb3-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 366 366 &lt;p&gt;The error check however is optional:&lt;/p&gt; 367 - &lt;div class="sourceCode" id="cb4"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb4-1"&gt;&lt;a href="#cb4-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;var&lt;/span&gt; x &lt;span class="kw"&gt;interface&lt;/span&gt;{} = &lt;span class="dv"&gt;7&lt;/span&gt;&lt;/span&gt; 368 - &lt;span id="cb4-2"&gt;&lt;a href="#cb4-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;var&lt;/span&gt; y := x.(&lt;span class="dt"&gt;float64&lt;/span&gt;)&lt;/span&gt; 369 - &lt;span id="cb4-3"&gt;&lt;a href="#cb4-3" aria-hidden="true"&gt;&lt;/a&gt;fmt.Println(y)&lt;/span&gt; 370 - &lt;span id="cb4-4"&gt;&lt;a href="#cb4-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// results in a runtime error:&lt;/span&gt;&lt;/span&gt; 371 - &lt;span id="cb4-5"&gt;&lt;a href="#cb4-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// panic: interface conversion: interface {} is int, not float64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 367 + &lt;div class="sourceCode" id="cb4"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb4-1"&gt;&lt;a href="#cb4-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;var&lt;/span&gt; x &lt;span class="kw"&gt;interface&lt;/span&gt;&lt;span class="op"&gt;{}&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dv"&gt;7&lt;/span&gt;&lt;/span&gt; 368 + &lt;span id="cb4-2"&gt;&lt;a href="#cb4-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;var&lt;/span&gt; y &lt;span class="op"&gt;:=&lt;/span&gt; x&lt;span class="op"&gt;.(&lt;/span&gt;&lt;span class="dt"&gt;float64&lt;/span&gt;&lt;span class="op"&gt;)&lt;/span&gt;&lt;/span&gt; 369 + &lt;span id="cb4-3"&gt;&lt;a href="#cb4-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;fmt&lt;span class="op"&gt;.&lt;/span&gt;Println&lt;span class="op"&gt;(&lt;/span&gt;y&lt;span class="op"&gt;)&lt;/span&gt;&lt;/span&gt; 370 + &lt;span id="cb4-4"&gt;&lt;a href="#cb4-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// results in a runtime error:&lt;/span&gt;&lt;/span&gt; 371 + &lt;span id="cb4-5"&gt;&lt;a href="#cb4-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// panic: interface conversion: interface {} is int, not float64&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 372 372 &lt;h3 id="date-and-time"&gt;Date and Time&lt;/h3&gt; 373 373 &lt;p&gt;Anyone that has written Go previously, will probably already know what I am getting at here. For the uninitiated, parsing and formatting dates in Go requires a “layout”. This “layout” is based on magical reference date:&lt;/p&gt; 374 374 &lt;pre&gt;&lt;code&gt;Mon Jan 2 15:04:05 MST 2006&lt;/code&gt;&lt;/pre&gt; 375 375 &lt;p&gt;Which is the date produced when you write the first seven natural numbers like so:&lt;/p&gt; 376 376 &lt;pre&gt;&lt;code&gt;01/02 03:04:05 &amp;#39;06 -0700&lt;/code&gt;&lt;/pre&gt; 377 377 &lt;p&gt;Parsing a string in &lt;code&gt;YYYY-MM-DD&lt;/code&gt; format would look something like:&lt;/p&gt; 378 - &lt;div class="sourceCode" id="cb7"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb7-1"&gt;&lt;a href="#cb7-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;const&lt;/span&gt; layout = &lt;span class="st"&gt;&amp;quot;2006-01-02&amp;quot;&lt;/span&gt;&lt;/span&gt; 379 - &lt;span id="cb7-2"&gt;&lt;a href="#cb7-2" aria-hidden="true"&gt;&lt;/a&gt;time.Parse(layout, &lt;span class="st"&gt;&amp;quot;2020-08-01&amp;quot;&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 378 + &lt;div class="sourceCode" id="cb7"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb7-1"&gt;&lt;a href="#cb7-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;const&lt;/span&gt; layout &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;2006-01-02&amp;quot;&lt;/span&gt;&lt;/span&gt; 379 + &lt;span id="cb7-2"&gt;&lt;a href="#cb7-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;time&lt;span class="op"&gt;.&lt;/span&gt;Parse&lt;span class="op"&gt;(&lt;/span&gt;layout&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="st"&gt;&amp;quot;2020-08-01&amp;quot;&lt;/span&gt;&lt;span class="op"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 380 380 &lt;p&gt;This so-called “intuitive” method of formatting dates doesn’t allow you to print &lt;code&gt;0000 hrs&lt;/code&gt; as &lt;code&gt;2400 hrs&lt;/code&gt;, it doesn’t allow you to omit the leading zero in 24 hour formats. It is rife with inconveniences, if only there were a &lt;a href="https://man7.org/linux/man-pages/man3/strftime.3.html"&gt;tried and tested&lt;/a&gt; date formatting convention …&lt;/p&gt; 381 381 &lt;h3 id="statements-over-expressions"&gt;Statements over Expressions&lt;/h3&gt; 382 382 &lt;p&gt;Statements have side effects, expressions return values. More often than not, expressions are easier to understand at a glance: evaluate the LHS and assign the same to the RHS.&lt;/p&gt; 383 383 &lt;p&gt;Rust allows you to create local namespaces, and treats blocks (&lt;code&gt;{}&lt;/code&gt;) as expressions:&lt;/p&gt; 384 - &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;let&lt;/span&gt; twenty_seven &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 385 - &lt;span id="cb8-2"&gt;&lt;a href="#cb8-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; three &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;2&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 386 - &lt;span id="cb8-3"&gt;&lt;a href="#cb8-3" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; nine &lt;span class="op"&gt;=&lt;/span&gt; three &lt;span class="op"&gt;*&lt;/span&gt; three&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 387 - &lt;span id="cb8-4"&gt;&lt;a href="#cb8-4" aria-hidden="true"&gt;&lt;/a&gt; nine &lt;span class="op"&gt;*&lt;/span&gt; three&lt;/span&gt; 388 - &lt;span id="cb8-5"&gt;&lt;a href="#cb8-5" 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; 384 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;let&lt;/span&gt; twenty_seven &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 385 + &lt;span id="cb8-2"&gt;&lt;a href="#cb8-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; three &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;2&lt;/span&gt;&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 386 + &lt;span id="cb8-3"&gt;&lt;a href="#cb8-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; nine &lt;span class="op"&gt;=&lt;/span&gt; three &lt;span class="op"&gt;*&lt;/span&gt; three&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 387 + &lt;span id="cb8-4"&gt;&lt;a href="#cb8-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; nine &lt;span class="op"&gt;*&lt;/span&gt; three&lt;/span&gt; 388 + &lt;span id="cb8-5"&gt;&lt;a href="#cb8-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 389 389 &lt;p&gt;The Go equivalent of the same:&lt;/p&gt; 390 - &lt;div class="sourceCode" id="cb9"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb9-1"&gt;&lt;a href="#cb9-1" aria-hidden="true"&gt;&lt;/a&gt;twenty_seven := &lt;span class="ot"&gt;nil&lt;/span&gt;&lt;/span&gt; 391 - &lt;span id="cb9-2"&gt;&lt;a href="#cb9-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 392 - &lt;span id="cb9-3"&gt;&lt;a href="#cb9-3" aria-hidden="true"&gt;&lt;/a&gt;three := &lt;span class="dv"&gt;1&lt;/span&gt; + &lt;span class="dv"&gt;2&lt;/span&gt;&lt;/span&gt; 393 - &lt;span id="cb9-4"&gt;&lt;a href="#cb9-4" aria-hidden="true"&gt;&lt;/a&gt;nine := three * three&lt;/span&gt; 394 - &lt;span id="cb9-5"&gt;&lt;a href="#cb9-5" aria-hidden="true"&gt;&lt;/a&gt;twenty_seven = nine * three&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 390 + &lt;div class="sourceCode" id="cb9"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb9-1"&gt;&lt;a href="#cb9-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;twenty_seven &lt;span class="op"&gt;:=&lt;/span&gt; &lt;span class="ot"&gt;nil&lt;/span&gt;&lt;/span&gt; 391 + &lt;span id="cb9-2"&gt;&lt;a href="#cb9-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 392 + &lt;span id="cb9-3"&gt;&lt;a href="#cb9-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;three &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;2&lt;/span&gt;&lt;/span&gt; 393 + &lt;span id="cb9-4"&gt;&lt;a href="#cb9-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;nine &lt;span class="op"&gt;:=&lt;/span&gt; three &lt;span class="op"&gt;*&lt;/span&gt; three&lt;/span&gt; 394 + &lt;span id="cb9-5"&gt;&lt;a href="#cb9-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;twenty_seven &lt;span class="op"&gt;=&lt;/span&gt; nine &lt;span class="op"&gt;*&lt;/span&gt; three&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 395 395 &lt;h3 id="erroring-out-on-unused-variables"&gt;Erroring out on unused variables&lt;/h3&gt; 396 396 &lt;p&gt;Want to quickly prototype something? Go says no! In all seriousness, a warning would suffice, I don’t want to have to go back and comment each unused import out, only to come back and uncomment them a few seconds later.&lt;/p&gt; 397 397 &lt;h3 id="error-handling"&gt;Error handling&lt;/h3&gt; 398 - &lt;div class="sourceCode" id="cb10"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb10-1"&gt;&lt;a href="#cb10-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;if&lt;/span&gt; err != &lt;span class="ot"&gt;nil&lt;/span&gt; { ... }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 398 + &lt;div class="sourceCode" id="cb10"&gt;&lt;pre class="sourceCode go"&gt;&lt;code class="sourceCode go"&gt;&lt;span id="cb10-1"&gt;&lt;a href="#cb10-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;if&lt;/span&gt; err &lt;span class="op"&gt;!=&lt;/span&gt; &lt;span class="ot"&gt;nil&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; 399 399 &lt;p&gt;Need I say more? I will, for good measure:&lt;/p&gt; 400 400 &lt;ol type="1"&gt; 401 401 &lt;li&gt;Error handling is optional&lt;/li&gt; 402 402 &lt;li&gt;Errors are propagated via a clunky &lt;code&gt;if&lt;/code&gt; + &lt;code&gt;return&lt;/code&gt; statement&lt;/li&gt; 403 403 &lt;/ol&gt; 404 404 &lt;p&gt;I prefer Haskell’s “Monadic” error handling, which is employed by Rust as well:&lt;/p&gt; 405 - &lt;div class="sourceCode" id="cb11"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb11-1"&gt;&lt;a href="#cb11-1" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// 1. error handling is compulsory&lt;/span&gt;&lt;/span&gt; 406 - &lt;span id="cb11-2"&gt;&lt;a href="#cb11-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="co"&gt;// 2. errors are propagated with the `?` operator&lt;/span&gt;&lt;/span&gt; 407 - &lt;span id="cb11-3"&gt;&lt;a href="#cb11-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; foo() &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Result&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;String&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;io::&lt;/span&gt;&lt;span class="bu"&gt;Error&lt;/span&gt;&lt;span class="op"&gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 408 - &lt;span id="cb11-4"&gt;&lt;a href="#cb11-4" 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; f &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;File::&lt;/span&gt;open(&lt;span class="st"&gt;&amp;quot;foo.txt&amp;quot;&lt;/span&gt;)&lt;span class="op"&gt;?;&lt;/span&gt; &lt;span class="co"&gt;// return if error&lt;/span&gt;&lt;/span&gt; 409 - &lt;span id="cb11-5"&gt;&lt;a href="#cb11-5" 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; s &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;String&lt;/span&gt;&lt;span class="pp"&gt;::&lt;/span&gt;new()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 410 - &lt;span id="cb11-6"&gt;&lt;a href="#cb11-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 411 - &lt;span id="cb11-7"&gt;&lt;a href="#cb11-7" aria-hidden="true"&gt;&lt;/a&gt; f&lt;span class="op"&gt;.&lt;/span&gt;read_to_string(&lt;span class="op"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kw"&gt;mut&lt;/span&gt; s)&lt;span class="op"&gt;?;&lt;/span&gt; &lt;span class="co"&gt;// return if error&lt;/span&gt;&lt;/span&gt; 412 - &lt;span id="cb11-8"&gt;&lt;a href="#cb11-8" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 413 - &lt;span id="cb11-9"&gt;&lt;a href="#cb11-9" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="cn"&gt;Ok&lt;/span&gt;(s) &lt;span class="co"&gt;// all good, return a string inside a `Result` context&lt;/span&gt;&lt;/span&gt; 414 - &lt;span id="cb11-10"&gt;&lt;a href="#cb11-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 415 - &lt;span id="cb11-11"&gt;&lt;a href="#cb11-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 416 - &lt;span id="cb11-12"&gt;&lt;a href="#cb11-12" aria-hidden="true"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; main() &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 417 - &lt;span id="cb11-13"&gt;&lt;a href="#cb11-13" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="co"&gt;// `contents` is an enum known as Result:&lt;/span&gt;&lt;/span&gt; 418 - &lt;span id="cb11-14"&gt;&lt;a href="#cb11-14" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; contents &lt;span class="op"&gt;=&lt;/span&gt; foo()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 419 - &lt;span id="cb11-15"&gt;&lt;a href="#cb11-15" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="kw"&gt;match&lt;/span&gt; contents &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 420 - &lt;span id="cb11-16"&gt;&lt;a href="#cb11-16" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="cn"&gt;Ok&lt;/span&gt;(c) &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;println!&lt;/span&gt;(c)&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 421 - &lt;span id="cb11-17"&gt;&lt;a href="#cb11-17" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="cn"&gt;Err&lt;/span&gt;(e) &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;eprintln!&lt;/span&gt;(e)&lt;/span&gt; 422 - &lt;span id="cb11-18"&gt;&lt;a href="#cb11-18" aria-hidden="true"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 423 - &lt;span id="cb11-19"&gt;&lt;a href="#cb11-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; 405 + &lt;div class="sourceCode" id="cb11"&gt;&lt;pre class="sourceCode rust"&gt;&lt;code class="sourceCode rust"&gt;&lt;span id="cb11-1"&gt;&lt;a href="#cb11-1" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// 1. error handling is compulsory&lt;/span&gt;&lt;/span&gt; 406 + &lt;span id="cb11-2"&gt;&lt;a href="#cb11-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// 2. errors are propagated with the `?` operator&lt;/span&gt;&lt;/span&gt; 407 + &lt;span id="cb11-3"&gt;&lt;a href="#cb11-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; foo() &lt;span class="op"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="dt"&gt;Result&lt;/span&gt;&lt;span class="op"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dt"&gt;String&lt;/span&gt;&lt;span class="op"&gt;,&lt;/span&gt; &lt;span class="pp"&gt;io::&lt;/span&gt;&lt;span class="bu"&gt;Error&lt;/span&gt;&lt;span class="op"&gt;&amp;gt;&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 408 + &lt;span id="cb11-4"&gt;&lt;a href="#cb11-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; &lt;span class="kw"&gt;mut&lt;/span&gt; f &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="pp"&gt;File::&lt;/span&gt;open(&lt;span class="st"&gt;&amp;quot;foo.txt&amp;quot;&lt;/span&gt;)&lt;span class="op"&gt;?;&lt;/span&gt; &lt;span class="co"&gt;// return if error&lt;/span&gt;&lt;/span&gt; 409 + &lt;span id="cb11-5"&gt;&lt;a href="#cb11-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; &lt;span class="kw"&gt;mut&lt;/span&gt; s &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="dt"&gt;String&lt;/span&gt;&lt;span class="pp"&gt;::&lt;/span&gt;new()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 410 + &lt;span id="cb11-6"&gt;&lt;a href="#cb11-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 411 + &lt;span id="cb11-7"&gt;&lt;a href="#cb11-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; f&lt;span class="op"&gt;.&lt;/span&gt;read_to_string(&lt;span class="op"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kw"&gt;mut&lt;/span&gt; s)&lt;span class="op"&gt;?;&lt;/span&gt; &lt;span class="co"&gt;// return if error&lt;/span&gt;&lt;/span&gt; 412 + &lt;span id="cb11-8"&gt;&lt;a href="#cb11-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 413 + &lt;span id="cb11-9"&gt;&lt;a href="#cb11-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="cn"&gt;Ok&lt;/span&gt;(s) &lt;span class="co"&gt;// all good, return a string inside a `Result` context&lt;/span&gt;&lt;/span&gt; 414 + &lt;span id="cb11-10"&gt;&lt;a href="#cb11-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 415 + &lt;span id="cb11-11"&gt;&lt;a href="#cb11-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 416 + &lt;span id="cb11-12"&gt;&lt;a href="#cb11-12" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; main() &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 417 + &lt;span id="cb11-13"&gt;&lt;a href="#cb11-13" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="co"&gt;// `contents` is an enum known as Result:&lt;/span&gt;&lt;/span&gt; 418 + &lt;span id="cb11-14"&gt;&lt;a href="#cb11-14" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;let&lt;/span&gt; contents &lt;span class="op"&gt;=&lt;/span&gt; foo()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 419 + &lt;span id="cb11-15"&gt;&lt;a href="#cb11-15" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="kw"&gt;match&lt;/span&gt; contents &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 420 + &lt;span id="cb11-16"&gt;&lt;a href="#cb11-16" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="cn"&gt;Ok&lt;/span&gt;(c) &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;println!&lt;/span&gt;(c)&lt;span class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 421 + &lt;span id="cb11-17"&gt;&lt;a href="#cb11-17" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="cn"&gt;Err&lt;/span&gt;(e) &lt;span class="op"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="pp"&gt;eprintln!&lt;/span&gt;(e)&lt;/span&gt; 422 + &lt;span id="cb11-18"&gt;&lt;a href="#cb11-18" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 423 + &lt;span id="cb11-19"&gt;&lt;a href="#cb11-19" aria-hidden="true" tabindex="-1"&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 424 &lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt; 425 425 &lt;p&gt;I did not want to conclude without talking about stylistic choices, lack of metaprogramming, bizzare export rules, but, I am too busy converting my &lt;code&gt;interface{}&lt;/code&gt; types into actual generic code for Go v2.&lt;/p&gt;</description> 426 426 <link>https://peppe.rs/posts/gripes_with_go/</link> ··· 500 500 &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; 501 501 &lt;h3 id="definitions"&gt;Definitions&lt;/h3&gt; 502 502 &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; 503 - &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; 504 - &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; 505 - &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; 503 + &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" tabindex="-1"&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; 504 + &lt;span id="cb4-2"&gt;&lt;a href="#cb4-2" aria-hidden="true" tabindex="-1"&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; 505 + &lt;span id="cb4-3"&gt;&lt;a href="#cb4-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 506 506 &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; 507 - &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; 508 - &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; 509 - &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; 510 - &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; 511 - &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; 512 - &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; 513 - &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; 507 + &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" tabindex="-1"&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; 508 + &lt;span id="cb5-2"&gt;&lt;a href="#cb5-2" aria-hidden="true" tabindex="-1"&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; 509 + &lt;span id="cb5-3"&gt;&lt;a href="#cb5-3" aria-hidden="true" tabindex="-1"&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; 510 + &lt;span id="cb5-4"&gt;&lt;a href="#cb5-4" aria-hidden="true" tabindex="-1"&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; 511 + &lt;span id="cb5-5"&gt;&lt;a href="#cb5-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 512 + &lt;span id="cb5-6"&gt;&lt;a href="#cb5-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 513 + &lt;span id="cb5-7"&gt;&lt;a href="#cb5-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 514 514 &lt;p&gt;A couple of things to note:&lt;/p&gt; 515 515 &lt;p&gt;&lt;strong&gt;Return types&lt;/strong&gt;&lt;br /&gt; 516 516 We 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; ··· 523 523 A 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; 524 524 &lt;h3 id="refinement"&gt;Refinement&lt;/h3&gt; 525 525 &lt;p&gt;Armed with knowledge, we refine our expected output, this time, employing closures:&lt;/p&gt; 526 - &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; 527 - &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; 528 - &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; 526 + &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" tabindex="-1"&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; 527 + &lt;span id="cb8-2"&gt;&lt;a href="#cb8-2" aria-hidden="true" tabindex="-1"&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; 528 + &lt;span id="cb8-3"&gt;&lt;a href="#cb8-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 529 529 &lt;p&gt;Alas, that does not compile either! It errors out with the following message:&lt;/p&gt; 530 530 &lt;pre&gt;&lt;code&gt;error[E0562]: `impl Trait` not allowed outside of function 531 531 and inherent method return types ··· 536 536 &lt;/code&gt;&lt;/pre&gt; 537 537 &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; 538 538 &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; 539 - &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; 540 - &lt;span id="cb10-2"&gt;&lt;a href="#cb10-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 541 - &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; 542 - &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; 543 - &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; 544 - &lt;span id="cb10-6"&gt;&lt;a href="#cb10-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 545 - &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; 546 - &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; 547 - &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; 539 + &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" tabindex="-1"&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; 540 + &lt;span id="cb10-2"&gt;&lt;a href="#cb10-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 541 + &lt;span id="cb10-3"&gt;&lt;a href="#cb10-3" aria-hidden="true" tabindex="-1"&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; 542 + &lt;span id="cb10-4"&gt;&lt;a href="#cb10-4" aria-hidden="true" tabindex="-1"&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; 543 + &lt;span id="cb10-5"&gt;&lt;a href="#cb10-5" aria-hidden="true" tabindex="-1"&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; 544 + &lt;span id="cb10-6"&gt;&lt;a href="#cb10-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 545 + &lt;span id="cb10-7"&gt;&lt;a href="#cb10-7" aria-hidden="true" tabindex="-1"&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; 546 + &lt;span id="cb10-8"&gt;&lt;a href="#cb10-8" aria-hidden="true" tabindex="-1"&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; 547 + &lt;span id="cb10-9"&gt;&lt;a href="#cb10-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 548 548 &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; 549 549 &lt;h3 id="the-in-betweens"&gt;The In-Betweens&lt;/h3&gt; 550 550 &lt;p&gt;Let us write the magical bits that take us from function to curried function.&lt;/p&gt; ··· 578 578 &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; 579 579 &lt;h4 id="the-attribute-macro"&gt;The attribute macro&lt;/h4&gt; 580 580 &lt;p&gt;Drop this into &lt;code&gt;src/lib.rs&lt;/code&gt;, to get the ball rolling.&lt;/p&gt; 581 - &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; 582 - &lt;span id="cb13-2"&gt;&lt;a href="#cb13-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 583 - &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; 584 - &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; 585 - &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; 586 - &lt;span id="cb13-6"&gt;&lt;a href="#cb13-6" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 587 - &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; 588 - &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; 589 - &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; 590 - &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; 591 - &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; 592 - &lt;span id="cb13-12"&gt;&lt;a href="#cb13-12" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 593 - &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; 581 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 582 + &lt;span id="cb13-2"&gt;&lt;a href="#cb13-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 583 + &lt;span id="cb13-3"&gt;&lt;a href="#cb13-3" aria-hidden="true" tabindex="-1"&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; 584 + &lt;span id="cb13-4"&gt;&lt;a href="#cb13-4" aria-hidden="true" tabindex="-1"&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; 585 + &lt;span id="cb13-5"&gt;&lt;a href="#cb13-5" aria-hidden="true" tabindex="-1"&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; 586 + &lt;span id="cb13-6"&gt;&lt;a href="#cb13-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 587 + &lt;span id="cb13-7"&gt;&lt;a href="#cb13-7" aria-hidden="true" tabindex="-1"&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; 588 + &lt;span id="cb13-8"&gt;&lt;a href="#cb13-8" aria-hidden="true" tabindex="-1"&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; 589 + &lt;span id="cb13-9"&gt;&lt;a href="#cb13-9" aria-hidden="true" tabindex="-1"&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; 590 + &lt;span id="cb13-10"&gt;&lt;a href="#cb13-10" aria-hidden="true" tabindex="-1"&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; 591 + &lt;span id="cb13-11"&gt;&lt;a href="#cb13-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 592 + &lt;span id="cb13-12"&gt;&lt;a href="#cb13-12" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 593 + &lt;span id="cb13-13"&gt;&lt;a href="#cb13-13" aria-hidden="true" tabindex="-1"&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; 594 594 &lt;p&gt;&lt;strong&gt;1. Imports&lt;/strong&gt;&lt;/p&gt; 595 595 &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; 596 596 &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; ··· 602 602 &lt;p&gt;&lt;strong&gt;4. Returning &lt;code&gt;TokenStream&lt;/code&gt;s &lt;/strong&gt;&lt;/p&gt; 603 603 &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; 604 604 &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; 605 - &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; 606 - &lt;span id="cb14-2"&gt;&lt;a href="#cb14-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 607 - &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; 608 - &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; 609 - &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; 610 - &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; 611 - &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; 612 - &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; 613 - &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; 614 - &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; 605 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 606 + &lt;span id="cb14-2"&gt;&lt;a href="#cb14-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 607 + &lt;span id="cb14-3"&gt;&lt;a href="#cb14-3" aria-hidden="true" tabindex="-1"&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; 608 + &lt;span id="cb14-4"&gt;&lt;a href="#cb14-4" aria-hidden="true" tabindex="-1"&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; 609 + &lt;span id="cb14-5"&gt;&lt;a href="#cb14-5" aria-hidden="true" tabindex="-1"&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; 610 + &lt;span id="cb14-6"&gt;&lt;a href="#cb14-6" aria-hidden="true" tabindex="-1"&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; 611 + &lt;span id="cb14-7"&gt;&lt;a href="#cb14-7" aria-hidden="true" tabindex="-1"&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; 612 + &lt;span id="cb14-8"&gt;&lt;a href="#cb14-8" aria-hidden="true" tabindex="-1"&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; 613 + &lt;span id="cb14-9"&gt;&lt;a href="#cb14-9" aria-hidden="true" tabindex="-1"&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; 614 + &lt;span id="cb14-10"&gt;&lt;a href="#cb14-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 615 615 &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; 616 616 &lt;pre&gt;&lt;code&gt; .-- syn::Ident (ident) 617 617 / ··· 623 623 &lt;p&gt;Enough analysis, lets produce our first bit of Rust code.&lt;/p&gt; 624 624 &lt;h4 id="function-body"&gt;Function Body&lt;/h4&gt; 625 625 &lt;p&gt;Recall that the body of a curried &lt;code&gt;add&lt;/code&gt; should look like this:&lt;/p&gt; 626 - &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; 626 + &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" tabindex="-1"&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; 627 627 &lt;p&gt;And in general:&lt;/p&gt; 628 - &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; 628 + &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" tabindex="-1"&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; 629 629 &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; 630 - &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; 631 - &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; 632 - &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; 633 - &lt;span id="cb18-4"&gt;&lt;a href="#cb18-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 634 - &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; 635 - &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; 636 - &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; 630 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 631 + &lt;span id="cb18-2"&gt;&lt;a href="#cb18-2" aria-hidden="true" tabindex="-1"&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; 632 + &lt;span id="cb18-3"&gt;&lt;a href="#cb18-3" aria-hidden="true" tabindex="-1"&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; 633 + &lt;span id="cb18-4"&gt;&lt;a href="#cb18-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 634 + &lt;span id="cb18-5"&gt;&lt;a href="#cb18-5" aria-hidden="true" tabindex="-1"&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; 635 + &lt;span id="cb18-6"&gt;&lt;a href="#cb18-6" aria-hidden="true" tabindex="-1"&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; 636 + &lt;span id="cb18-7"&gt;&lt;a href="#cb18-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 637 637 &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; 638 - &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; 639 - &lt;span id="cb19-2"&gt;&lt;a href="#cb19-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 640 - &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; 641 - &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; 642 - &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; 643 - &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; 644 - &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; 645 - &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; 638 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 639 + &lt;span id="cb19-2"&gt;&lt;a href="#cb19-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 640 + &lt;span id="cb19-3"&gt;&lt;a href="#cb19-3" aria-hidden="true" tabindex="-1"&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; 641 + &lt;span id="cb19-4"&gt;&lt;a href="#cb19-4" aria-hidden="true" tabindex="-1"&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; 642 + &lt;span id="cb19-5"&gt;&lt;a href="#cb19-5" aria-hidden="true" tabindex="-1"&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; 643 + &lt;span id="cb19-6"&gt;&lt;a href="#cb19-6" aria-hidden="true" tabindex="-1"&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; 644 + &lt;span id="cb19-7"&gt;&lt;a href="#cb19-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 645 + &lt;span id="cb19-8"&gt;&lt;a href="#cb19-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 646 646 &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; 647 647 &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; 648 648 &lt;p&gt;With that done, we should be able to write the codegen for the function body:&lt;/p&gt; 649 - &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; 650 - &lt;span id="cb20-2"&gt;&lt;a href="#cb20-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 651 - &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; 652 - &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; 653 - &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; 654 - &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; 655 - &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; 649 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 650 + &lt;span id="cb20-2"&gt;&lt;a href="#cb20-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 651 + &lt;span id="cb20-3"&gt;&lt;a href="#cb20-3" aria-hidden="true" tabindex="-1"&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; 652 + &lt;span id="cb20-4"&gt;&lt;a href="#cb20-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="pp"&gt;quote!&lt;/span&gt; &lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 653 + &lt;span id="cb20-5"&gt;&lt;a href="#cb20-5" aria-hidden="true" tabindex="-1"&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; 654 + &lt;span id="cb20-6"&gt;&lt;a href="#cb20-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 655 + &lt;span id="cb20-7"&gt;&lt;a href="#cb20-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 656 656 &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; 657 657 &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; 658 658 &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; 659 659 &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; 660 - &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; 661 - &lt;span id="cb21-2"&gt;&lt;a href="#cb21-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 662 - &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; 663 - &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; 664 - &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; 665 - &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; 666 - &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; 667 - &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; 668 - &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; 669 - &lt;span id="cb21-10"&gt;&lt;a href="#cb21-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 670 - &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; 671 - &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; 672 - &lt;span id="cb21-13"&gt;&lt;a href="#cb21-13" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 673 - &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; 674 - &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; 675 - &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; 676 - &lt;span id="cb21-17"&gt;&lt;a href="#cb21-17" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 677 - &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; 678 - &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; 660 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 661 + &lt;span id="cb21-2"&gt;&lt;a href="#cb21-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 662 + &lt;span id="cb21-3"&gt;&lt;a href="#cb21-3" aria-hidden="true" tabindex="-1"&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; 663 + &lt;span id="cb21-4"&gt;&lt;a href="#cb21-4" aria-hidden="true" tabindex="-1"&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; 664 + &lt;span id="cb21-5"&gt;&lt;a href="#cb21-5" aria-hidden="true" tabindex="-1"&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; 665 + &lt;span id="cb21-6"&gt;&lt;a href="#cb21-6" aria-hidden="true" tabindex="-1"&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; 666 + &lt;span id="cb21-7"&gt;&lt;a href="#cb21-7" aria-hidden="true" tabindex="-1"&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; 667 + &lt;span id="cb21-8"&gt;&lt;a href="#cb21-8" aria-hidden="true" tabindex="-1"&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; 668 + &lt;span id="cb21-9"&gt;&lt;a href="#cb21-9" aria-hidden="true" tabindex="-1"&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; 669 + &lt;span id="cb21-10"&gt;&lt;a href="#cb21-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 670 + &lt;span id="cb21-11"&gt;&lt;a href="#cb21-11" aria-hidden="true" tabindex="-1"&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; 671 + &lt;span id="cb21-12"&gt;&lt;a href="#cb21-12" aria-hidden="true" tabindex="-1"&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; 672 + &lt;span id="cb21-13"&gt;&lt;a href="#cb21-13" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 673 + &lt;span id="cb21-14"&gt;&lt;a href="#cb21-14" aria-hidden="true" tabindex="-1"&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; 674 + &lt;span id="cb21-15"&gt;&lt;a href="#cb21-15" aria-hidden="true" tabindex="-1"&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; 675 + &lt;span id="cb21-16"&gt;&lt;a href="#cb21-16" aria-hidden="true" tabindex="-1"&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; 676 + &lt;span id="cb21-17"&gt;&lt;a href="#cb21-17" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 677 + &lt;span id="cb21-18"&gt;&lt;a href="#cb21-18" aria-hidden="true" tabindex="-1"&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; 678 + &lt;span id="cb21-19"&gt;&lt;a href="#cb21-19" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 679 679 &lt;p&gt;Add a little test to &lt;code&gt;tests/&lt;/code&gt;:&lt;/p&gt; 680 - &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; 681 - &lt;span id="cb22-2"&gt;&lt;a href="#cb22-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 682 - &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; 683 - &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; 684 - &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; 685 - &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; 686 - &lt;span id="cb22-7"&gt;&lt;a href="#cb22-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 687 - &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; 688 - &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; 689 - &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; 690 - &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; 680 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// tests/smoke.rs&lt;/span&gt;&lt;/span&gt; 681 + &lt;span id="cb22-2"&gt;&lt;a href="#cb22-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 682 + &lt;span id="cb22-3"&gt;&lt;a href="#cb22-3" aria-hidden="true" tabindex="-1"&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; 683 + &lt;span id="cb22-4"&gt;&lt;a href="#cb22-4" aria-hidden="true" tabindex="-1"&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; 684 + &lt;span id="cb22-5"&gt;&lt;a href="#cb22-5" aria-hidden="true" tabindex="-1"&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; 685 + &lt;span id="cb22-6"&gt;&lt;a href="#cb22-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 686 + &lt;span id="cb22-7"&gt;&lt;a href="#cb22-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 687 + &lt;span id="cb22-8"&gt;&lt;a href="#cb22-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt; 688 + &lt;span id="cb22-9"&gt;&lt;a href="#cb22-9" aria-hidden="true" tabindex="-1"&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; 689 + &lt;span id="cb22-10"&gt;&lt;a href="#cb22-10" aria-hidden="true" tabindex="-1"&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; 690 + &lt;span id="cb22-11"&gt;&lt;a href="#cb22-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 691 691 &lt;p&gt;You should find something like this in the output of &lt;code&gt;cargo test&lt;/code&gt;:&lt;/p&gt; 692 692 &lt;pre&gt;&lt;code&gt;return move | y | move | z | { x + y + z }&lt;/code&gt;&lt;/pre&gt; 693 693 &lt;p&gt;Glorious &lt;code&gt;println!&lt;/code&gt; debugging!&lt;/p&gt; 694 694 &lt;h4 id="function-signature"&gt;Function signature&lt;/h4&gt; 695 695 &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; 696 696 &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; 697 - &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; 698 - &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; 699 - &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; 697 + &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" tabindex="-1"&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; 698 + &lt;span id="cb24-2"&gt;&lt;a href="#cb24-2" aria-hidden="true" tabindex="-1"&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; 699 + &lt;span id="cb24-3"&gt;&lt;a href="#cb24-3" aria-hidden="true" tabindex="-1"&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; 700 700 &lt;p&gt;In general:&lt;/p&gt; 701 - &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; 702 - &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; 703 - &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; 704 - &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; 705 - &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; 706 - &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; 707 - &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; 701 + &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" tabindex="-1"&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; 702 + &lt;span id="cb25-2"&gt;&lt;a href="#cb25-2" aria-hidden="true" tabindex="-1"&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; 703 + &lt;span id="cb25-3"&gt;&lt;a href="#cb25-3" aria-hidden="true" tabindex="-1"&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; 704 + &lt;span id="cb25-4"&gt;&lt;a href="#cb25-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;.&lt;/span&gt;&lt;/span&gt; 705 + &lt;span id="cb25-5"&gt;&lt;a href="#cb25-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;.&lt;/span&gt;&lt;/span&gt; 706 + &lt;span id="cb25-6"&gt;&lt;a href="#cb25-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;.&lt;/span&gt;&lt;/span&gt; 707 + &lt;span id="cb25-7"&gt;&lt;a href="#cb25-7" aria-hidden="true" tabindex="-1"&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; 708 708 &lt;p&gt;To codegen that, we need the types of:&lt;/p&gt; 709 709 &lt;ul&gt; 710 710 &lt;li&gt;all our inputs (arguments)&lt;/li&gt; 711 711 &lt;li&gt;the output (the return type)&lt;/li&gt; 712 712 &lt;/ul&gt; 713 713 &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; 714 - &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; 715 - &lt;span id="cb26-2"&gt;&lt;a href="#cb26-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 716 - &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; 717 - &lt;span id="cb26-4"&gt;&lt;a href="#cb26-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 718 - &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; 719 - &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; 720 - &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; 721 - &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; 722 - &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; 723 - &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; 724 - &lt;span id="cb26-11"&gt;&lt;a href="#cb26-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 725 - &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; 726 - &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; 727 - &lt;span id="cb26-14"&gt;&lt;a href="#cb26-14" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 728 - &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; 714 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 715 + &lt;span id="cb26-2"&gt;&lt;a href="#cb26-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 716 + &lt;span id="cb26-3"&gt;&lt;a href="#cb26-3" aria-hidden="true" tabindex="-1"&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; 717 + &lt;span id="cb26-4"&gt;&lt;a href="#cb26-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 718 + &lt;span id="cb26-5"&gt;&lt;a href="#cb26-5" aria-hidden="true" tabindex="-1"&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; 719 + &lt;span id="cb26-6"&gt;&lt;a href="#cb26-6" aria-hidden="true" tabindex="-1"&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; 720 + &lt;span id="cb26-7"&gt;&lt;a href="#cb26-7" aria-hidden="true" tabindex="-1"&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; 721 + &lt;span id="cb26-8"&gt;&lt;a href="#cb26-8" aria-hidden="true" tabindex="-1"&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; 722 + &lt;span id="cb26-9"&gt;&lt;a href="#cb26-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 723 + &lt;span id="cb26-10"&gt;&lt;a href="#cb26-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 724 + &lt;span id="cb26-11"&gt;&lt;a href="#cb26-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 725 + &lt;span id="cb26-12"&gt;&lt;a href="#cb26-12" aria-hidden="true" tabindex="-1"&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; 726 + &lt;span id="cb26-13"&gt;&lt;a href="#cb26-13" aria-hidden="true" tabindex="-1"&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; 727 + &lt;span id="cb26-14"&gt;&lt;a href="#cb26-14" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 728 + &lt;span id="cb26-15"&gt;&lt;a href="#cb26-15" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 729 729 &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; 730 730 &lt;ol type="1"&gt; 731 731 &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; 732 732 &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; 733 - &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; 734 - &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; 735 - &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; 733 + &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" tabindex="-1"&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; 734 + &lt;span id="cb27-2"&gt;&lt;a href="#cb27-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// and not&lt;/span&gt;&lt;/span&gt; 735 + &lt;span id="cb27-3"&gt;&lt;a href="#cb27-3" aria-hidden="true" tabindex="-1"&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; 736 736 &lt;/ol&gt; 737 737 &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; 738 - &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; 739 - &lt;span id="cb28-2"&gt;&lt;a href="#cb28-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 740 - &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; 741 - &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; 742 - &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; 743 - &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; 744 - &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; 745 - &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; 738 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 739 + &lt;span id="cb28-2"&gt;&lt;a href="#cb28-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 740 + &lt;span id="cb28-3"&gt;&lt;a href="#cb28-3" aria-hidden="true" tabindex="-1"&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; 741 + &lt;span id="cb28-4"&gt;&lt;a href="#cb28-4" aria-hidden="true" tabindex="-1"&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; 742 + &lt;span id="cb28-5"&gt;&lt;a href="#cb28-5" aria-hidden="true" tabindex="-1"&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; 743 + &lt;span id="cb28-6"&gt;&lt;a href="#cb28-6" aria-hidden="true" tabindex="-1"&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; 744 + &lt;span id="cb28-7"&gt;&lt;a href="#cb28-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 745 + &lt;span id="cb28-8"&gt;&lt;a href="#cb28-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 746 746 &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; 747 747 &lt;p&gt;With all our types ready, we can get on with generating type aliases:&lt;/p&gt; 748 - &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; 749 - &lt;span id="cb29-2"&gt;&lt;a href="#cb29-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 750 - &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; 751 - &lt;span id="cb29-4"&gt;&lt;a href="#cb29-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 752 - &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; 753 - &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; 754 - &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; 755 - &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; 756 - &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; 757 - &lt;span id="cb29-10"&gt;&lt;a href="#cb29-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 758 - &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; 759 - &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; 760 - &lt;span id="cb29-13"&gt;&lt;a href="#cb29-13" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 761 - &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; 762 - &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; 763 - &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; 764 - &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; 765 - &lt;span id="cb29-18"&gt;&lt;a href="#cb29-18" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 766 - &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; 767 - &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; 768 - &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; 769 - &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; 770 - &lt;span id="cb29-23"&gt;&lt;a href="#cb29-23" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 771 - &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; 772 - &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; 748 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 749 + &lt;span id="cb29-2"&gt;&lt;a href="#cb29-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 750 + &lt;span id="cb29-3"&gt;&lt;a href="#cb29-3" aria-hidden="true" tabindex="-1"&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; 751 + &lt;span id="cb29-4"&gt;&lt;a href="#cb29-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 752 + &lt;span id="cb29-5"&gt;&lt;a href="#cb29-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="kw"&gt;fn&lt;/span&gt; generate_type_aliases(&lt;/span&gt; 753 + &lt;span id="cb29-6"&gt;&lt;a href="#cb29-6" aria-hidden="true" tabindex="-1"&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; 754 + &lt;span id="cb29-7"&gt;&lt;a href="#cb29-7" aria-hidden="true" tabindex="-1"&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; 755 + &lt;span id="cb29-8"&gt;&lt;a href="#cb29-8" aria-hidden="true" tabindex="-1"&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; 756 + &lt;span id="cb29-9"&gt;&lt;a href="#cb29-9" aria-hidden="true" tabindex="-1"&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; 757 + &lt;span id="cb29-10"&gt;&lt;a href="#cb29-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 758 + &lt;span id="cb29-11"&gt;&lt;a href="#cb29-11" aria-hidden="true" tabindex="-1"&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; 759 + &lt;span id="cb29-12"&gt;&lt;a href="#cb29-12" aria-hidden="true" tabindex="-1"&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; 760 + &lt;span id="cb29-13"&gt;&lt;a href="#cb29-13" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 761 + &lt;span id="cb29-14"&gt;&lt;a href="#cb29-14" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="co"&gt;// 3&lt;/span&gt;&lt;/span&gt; 762 + &lt;span id="cb29-15"&gt;&lt;a href="#cb29-15" aria-hidden="true" tabindex="-1"&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; 763 + &lt;span id="cb29-16"&gt;&lt;a href="#cb29-16" aria-hidden="true" tabindex="-1"&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; 764 + &lt;span id="cb29-17"&gt;&lt;a href="#cb29-17" aria-hidden="true" tabindex="-1"&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; 765 + &lt;span id="cb29-18"&gt;&lt;a href="#cb29-18" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 766 + &lt;span id="cb29-19"&gt;&lt;a href="#cb29-19" aria-hidden="true" tabindex="-1"&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; 767 + &lt;span id="cb29-20"&gt;&lt;a href="#cb29-20" aria-hidden="true" tabindex="-1"&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; 768 + &lt;span id="cb29-21"&gt;&lt;a href="#cb29-21" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 769 + &lt;span id="cb29-22"&gt;&lt;a href="#cb29-22" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 770 + &lt;span id="cb29-23"&gt;&lt;a href="#cb29-23" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 771 + &lt;span id="cb29-24"&gt;&lt;a href="#cb29-24" aria-hidden="true" tabindex="-1"&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; 772 + &lt;span id="cb29-25"&gt;&lt;a href="#cb29-25" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 773 773 &lt;p&gt;&lt;strong&gt;1. The return value&lt;/strong&gt;&lt;br /&gt; 774 774 We 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; 775 775 &lt;p&gt;&lt;strong&gt;2. Format identifier?&lt;/strong&gt;&lt;br /&gt; 776 776 I’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; 777 - &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; 778 - &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; 779 - &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; 777 + &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" tabindex="-1"&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; 778 + &lt;span id="cb30-2"&gt;&lt;a href="#cb30-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// the others are of the form&lt;/span&gt;&lt;/span&gt; 779 + &lt;span id="cb30-3"&gt;&lt;a href="#cb30-3" aria-hidden="true" tabindex="-1"&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; 780 780 &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; 781 781 &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; 782 - &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; 783 - &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; 784 - &lt;span id="cb31-3"&gt;&lt;a href="#cb31-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 785 - &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; 786 - &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; 782 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt; 783 + &lt;span id="cb31-2"&gt;&lt;a href="#cb31-2" aria-hidden="true" tabindex="-1"&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; 784 + &lt;span id="cb31-3"&gt;&lt;a href="#cb31-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 785 + &lt;span id="cb31-4"&gt;&lt;a href="#cb31-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt; 786 + &lt;span id="cb31-5"&gt;&lt;a href="#cb31-5" aria-hidden="true" tabindex="-1"&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; 787 787 &lt;p&gt;Here is the same but with macros expanded:&lt;/p&gt; 788 - &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; 789 - &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; 790 - &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; 791 - &lt;span id="cb32-4"&gt;&lt;a href="#cb32-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 792 - &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; 793 - &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; 794 - &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; 788 + &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" tabindex="-1"&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; 789 + &lt;span id="cb32-2"&gt;&lt;a href="#cb32-2" aria-hidden="true" tabindex="-1"&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; 790 + &lt;span id="cb32-3"&gt;&lt;a href="#cb32-3" aria-hidden="true" tabindex="-1"&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; 791 + &lt;span id="cb32-4"&gt;&lt;a href="#cb32-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 792 + &lt;span id="cb32-5"&gt;&lt;a href="#cb32-5" aria-hidden="true" tabindex="-1"&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; 793 + &lt;span id="cb32-6"&gt;&lt;a href="#cb32-6" aria-hidden="true" tabindex="-1"&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; 794 + &lt;span id="cb32-7"&gt;&lt;a href="#cb32-7" aria-hidden="true" tabindex="-1"&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; 795 795 &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; 796 - &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; 797 - &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; 798 - &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; 799 - &lt;span id="cb33-4"&gt;&lt;a href="#cb33-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 800 - &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; 801 - &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; 802 - &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; 796 + &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" tabindex="-1"&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; 797 + &lt;span id="cb33-2"&gt;&lt;a href="#cb33-2" aria-hidden="true" tabindex="-1"&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; 798 + &lt;span id="cb33-3"&gt;&lt;a href="#cb33-3" aria-hidden="true" tabindex="-1"&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; 799 + &lt;span id="cb33-4"&gt;&lt;a href="#cb33-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 800 + &lt;span id="cb33-5"&gt;&lt;a href="#cb33-5" aria-hidden="true" tabindex="-1"&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; 801 + &lt;span id="cb33-6"&gt;&lt;a href="#cb33-6" aria-hidden="true" tabindex="-1"&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; 802 + &lt;span id="cb33-7"&gt;&lt;a href="#cb33-7" aria-hidden="true" tabindex="-1"&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; 803 803 &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; 804 804 &lt;p&gt;&lt;strong&gt;3. The TokenStream Vector&lt;/strong&gt;&lt;/p&gt; 805 805 &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; 806 806 &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; 807 807 &lt;h4 id="getting-it-together"&gt;Getting it together&lt;/h4&gt; 808 808 &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; 809 - &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; 810 - &lt;span id="cb34-2"&gt;&lt;a href="#cb34-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 811 - &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; 812 - &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; 813 - &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; 814 - &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; 815 - &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; 816 - &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; 817 - &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; 818 - &lt;span id="cb34-10"&gt;&lt;a href="#cb34-10" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 819 - &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; 820 - &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; 821 - &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; 822 - &lt;span id="cb34-14"&gt;&lt;a href="#cb34-14" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 823 - &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; 824 - &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; 825 - &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; 826 - &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; 827 - &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; 828 - &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; 829 - &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; 830 - &lt;span id="cb34-22"&gt;&lt;a href="#cb34-22" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 831 - &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; 832 - &lt;span id="cb34-24"&gt;&lt;a href="#cb34-24" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 833 - &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; 834 - &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; 835 - &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; 836 - &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; 837 - &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; 838 - &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; 839 - &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; 809 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// src/lib.rs&lt;/span&gt;&lt;/span&gt; 810 + &lt;span id="cb34-2"&gt;&lt;a href="#cb34-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 811 + &lt;span id="cb34-3"&gt;&lt;a href="#cb34-3" aria-hidden="true" tabindex="-1"&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; 812 + &lt;span id="cb34-4"&gt;&lt;a href="#cb34-4" aria-hidden="true" tabindex="-1"&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; 813 + &lt;span id="cb34-5"&gt;&lt;a href="#cb34-5" aria-hidden="true" tabindex="-1"&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; 814 + &lt;span id="cb34-6"&gt;&lt;a href="#cb34-6" aria-hidden="true" tabindex="-1"&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; 815 + &lt;span id="cb34-7"&gt;&lt;a href="#cb34-7" aria-hidden="true" tabindex="-1"&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; 816 + &lt;span id="cb34-8"&gt;&lt;a href="#cb34-8" aria-hidden="true" tabindex="-1"&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; 817 + &lt;span id="cb34-9"&gt;&lt;a href="#cb34-9" aria-hidden="true" tabindex="-1"&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; 818 + &lt;span id="cb34-10"&gt;&lt;a href="#cb34-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 819 + &lt;span id="cb34-11"&gt;&lt;a href="#cb34-11" aria-hidden="true" tabindex="-1"&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; 820 + &lt;span id="cb34-12"&gt;&lt;a href="#cb34-12" aria-hidden="true" tabindex="-1"&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; 821 + &lt;span id="cb34-13"&gt;&lt;a href="#cb34-13" aria-hidden="true" tabindex="-1"&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; 822 + &lt;span id="cb34-14"&gt;&lt;a href="#cb34-14" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 823 + &lt;span id="cb34-15"&gt;&lt;a href="#cb34-15" aria-hidden="true" tabindex="-1"&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; 824 + &lt;span id="cb34-16"&gt;&lt;a href="#cb34-16" aria-hidden="true" tabindex="-1"&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; 825 + &lt;span id="cb34-17"&gt;&lt;a href="#cb34-17" aria-hidden="true" tabindex="-1"&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; 826 + &lt;span id="cb34-18"&gt;&lt;a href="#cb34-18" aria-hidden="true" tabindex="-1"&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; 827 + &lt;span id="cb34-19"&gt;&lt;a href="#cb34-19" aria-hidden="true" tabindex="-1"&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; 828 + &lt;span id="cb34-20"&gt;&lt;a href="#cb34-20" aria-hidden="true" tabindex="-1"&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; 829 + &lt;span id="cb34-21"&gt;&lt;a href="#cb34-21" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; )&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 830 + &lt;span id="cb34-22"&gt;&lt;a href="#cb34-22" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 831 + &lt;span id="cb34-23"&gt;&lt;a href="#cb34-23" aria-hidden="true" tabindex="-1"&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; 832 + &lt;span id="cb34-24"&gt;&lt;a href="#cb34-24" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 833 + &lt;span id="cb34-25"&gt;&lt;a href="#cb34-25" aria-hidden="true" tabindex="-1"&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; 834 + &lt;span id="cb34-26"&gt;&lt;a href="#cb34-26" aria-hidden="true" tabindex="-1"&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; 835 + &lt;span id="cb34-27"&gt;&lt;a href="#cb34-27" aria-hidden="true" tabindex="-1"&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; 836 + &lt;span id="cb34-28"&gt;&lt;a href="#cb34-28" aria-hidden="true" tabindex="-1"&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; 837 + &lt;span id="cb34-29"&gt;&lt;a href="#cb34-29" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 838 + &lt;span id="cb34-30"&gt;&lt;a href="#cb34-30" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;+&lt;/span&gt; &lt;span class="op"&gt;};&lt;/span&gt;&lt;/span&gt; 839 + &lt;span id="cb34-31"&gt;&lt;a href="#cb34-31" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 840 840 &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; 841 841 &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; 842 842 &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; ··· 868 868 &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; 869 869 &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; 870 870 &lt;p&gt;Enough of that, lets get testing:&lt;/p&gt; 871 - &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; 872 - &lt;span id="cb37-2"&gt;&lt;a href="#cb37-2" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 873 - &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; 874 - &lt;span id="cb37-4"&gt;&lt;a href="#cb37-4" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 875 - &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; 876 - &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; 877 - &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; 878 - &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; 879 - &lt;span id="cb37-9"&gt;&lt;a href="#cb37-9" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 880 - &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; 881 - &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; 882 - &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; 883 - &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; 871 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;// tests/smoke.rs&lt;/span&gt;&lt;/span&gt; 872 + &lt;span id="cb37-2"&gt;&lt;a href="#cb37-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 873 + &lt;span id="cb37-3"&gt;&lt;a href="#cb37-3" aria-hidden="true" tabindex="-1"&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; 874 + &lt;span id="cb37-4"&gt;&lt;a href="#cb37-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 875 + &lt;span id="cb37-5"&gt;&lt;a href="#cb37-5" aria-hidden="true" tabindex="-1"&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; 876 + &lt;span id="cb37-6"&gt;&lt;a href="#cb37-6" aria-hidden="true" tabindex="-1"&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; 877 + &lt;span id="cb37-7"&gt;&lt;a href="#cb37-7" aria-hidden="true" tabindex="-1"&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; 878 + &lt;span id="cb37-8"&gt;&lt;a href="#cb37-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 879 + &lt;span id="cb37-9"&gt;&lt;a href="#cb37-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 880 + &lt;span id="cb37-10"&gt;&lt;a href="#cb37-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt; 881 + &lt;span id="cb37-11"&gt;&lt;a href="#cb37-11" aria-hidden="true" tabindex="-1"&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; 882 + &lt;span id="cb37-12"&gt;&lt;a href="#cb37-12" aria-hidden="true" tabindex="-1"&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; 883 + &lt;span id="cb37-13"&gt;&lt;a href="#cb37-13" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 884 884 &lt;p&gt;Run &lt;code&gt;cargo +nightly test&lt;/code&gt;. You should see a pleasing message:&lt;/p&gt; 885 885 &lt;pre&gt;&lt;code&gt;running 1 test 886 886 test tests::works ... ok&lt;/code&gt;&lt;/pre&gt; 887 887 &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; 888 - &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; 889 - &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; 890 - &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; 891 - &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; 892 - &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; 893 - &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; 894 - &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; 895 - &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; 896 - &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; 897 - &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; 898 - &lt;span id="cb39-11"&gt;&lt;a href="#cb39-11" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 899 - &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; 888 + &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" tabindex="-1"&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; 889 + &lt;span id="cb39-2"&gt;&lt;a href="#cb39-2" aria-hidden="true" tabindex="-1"&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; 890 + &lt;span id="cb39-3"&gt;&lt;a href="#cb39-3" aria-hidden="true" tabindex="-1"&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; 891 + &lt;span id="cb39-4"&gt;&lt;a href="#cb39-4" aria-hidden="true" tabindex="-1"&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; 892 + &lt;span id="cb39-5"&gt;&lt;a href="#cb39-5" aria-hidden="true" tabindex="-1"&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; 893 + &lt;span id="cb39-6"&gt;&lt;a href="#cb39-6" aria-hidden="true" tabindex="-1"&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; 894 + &lt;span id="cb39-7"&gt;&lt;a href="#cb39-7" aria-hidden="true" tabindex="-1"&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; 895 + &lt;span id="cb39-8"&gt;&lt;a href="#cb39-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 896 + &lt;span id="cb39-9"&gt;&lt;a href="#cb39-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;)&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 897 + &lt;span id="cb39-10"&gt;&lt;a href="#cb39-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 898 + &lt;span id="cb39-11"&gt;&lt;a href="#cb39-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 899 + &lt;span id="cb39-12"&gt;&lt;a href="#cb39-12" aria-hidden="true" tabindex="-1"&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; 900 900 &lt;p&gt;A sight for sore eyes.&lt;/p&gt; 901 901 &lt;p&gt;Here is a more complex example that generates ten multiples of the first ten natural numbers:&lt;/p&gt; 902 - &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; 903 - &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; 904 - &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; 905 - &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; 906 - &lt;span id="cb40-5"&gt;&lt;a href="#cb40-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 907 - &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; 908 - &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; 909 - &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; 910 - &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; 911 - &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; 912 - &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; 902 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="at"&gt;#[&lt;/span&gt;curry&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt; 903 + &lt;span id="cb40-2"&gt;&lt;a href="#cb40-2" aria-hidden="true" tabindex="-1"&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; 904 + &lt;span id="cb40-3"&gt;&lt;a href="#cb40-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; x &lt;span class="op"&gt;*&lt;/span&gt; y&lt;/span&gt; 905 + &lt;span id="cb40-4"&gt;&lt;a href="#cb40-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 906 + &lt;span id="cb40-5"&gt;&lt;a href="#cb40-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 907 + &lt;span id="cb40-6"&gt;&lt;a href="#cb40-6" aria-hidden="true" tabindex="-1"&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; 908 + &lt;span id="cb40-7"&gt;&lt;a href="#cb40-7" aria-hidden="true" tabindex="-1"&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; 909 + &lt;span id="cb40-8"&gt;&lt;a href="#cb40-8" aria-hidden="true" tabindex="-1"&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; 910 + &lt;span id="cb40-9"&gt;&lt;a href="#cb40-9" aria-hidden="true" tabindex="-1"&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; 911 + &lt;span id="cb40-10"&gt;&lt;a href="#cb40-10" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;.&lt;/span&gt;collect()&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 912 + &lt;span id="cb40-11"&gt;&lt;a href="#cb40-11" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 913 913 &lt;h3 id="notes"&gt;Notes&lt;/h3&gt; 914 914 &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; 915 - &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; 916 - &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; 917 - &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; 918 - &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; 919 - &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; 920 - &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; 921 - &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; 915 + &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" tabindex="-1"&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; 916 + &lt;span id="cb41-2"&gt;&lt;a href="#cb41-2" aria-hidden="true" tabindex="-1"&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; 917 + &lt;span id="cb41-3"&gt;&lt;a href="#cb41-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;{&lt;/span&gt;&lt;/span&gt; 918 + &lt;span id="cb41-4"&gt;&lt;a href="#cb41-4" aria-hidden="true" tabindex="-1"&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; 919 + &lt;span id="cb41-5"&gt;&lt;a href="#cb41-5" aria-hidden="true" tabindex="-1"&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; 920 + &lt;span id="cb41-6"&gt;&lt;a href="#cb41-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt; 921 + &lt;span id="cb41-7"&gt;&lt;a href="#cb41-7" aria-hidden="true" tabindex="-1"&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; 922 922 &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; 923 923 &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; 924 924 &lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt; ··· 995 995 <title>Rapid Refactoring With Vim</title> 996 996 <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; 997 997 &lt;p&gt;Here’s a small sample of what had to be done (note the lines prefixed with the arrow):&lt;/p&gt; 998 - &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; 999 - &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;/span&gt; 1000 - &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; 1001 - &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; 1002 - &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; 1003 - &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 class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 1004 - &lt;span id="cb1-7"&gt;&lt;a href="#cb1-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; 1005 - &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true"&gt;&lt;/a&gt; )&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 1006 - &lt;span id="cb1-9"&gt;&lt;a href="#cb1-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; 998 + &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" tabindex="-1"&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; 999 + &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;/span&gt; 1000 + &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt; 1001 + &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true" tabindex="-1"&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; 1002 + &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="pp"&gt;assert_eq!&lt;/span&gt;(&lt;/span&gt; 1003 + &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true" tabindex="-1"&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 class="op"&gt;,&lt;/span&gt;&lt;/span&gt; 1004 + &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true" tabindex="-1"&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; 1005 + &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; )&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 1006 + &lt;span id="cb1-9"&gt;&lt;a href="#cb1-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 1007 1007 &lt;p&gt;had to be converted to:&lt;/p&gt; 1008 - &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; 1009 - &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true"&gt;&lt;/a&gt; &lt;/span&gt; 1010 - &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; 1011 - &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; 1012 - &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; 1013 - &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; 1014 - &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; 1015 - &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; 1016 - &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; 1008 + &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" tabindex="-1"&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; 1009 + &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;/span&gt; 1010 + &lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="at"&gt;#[&lt;/span&gt;test&lt;span class="at"&gt;]&lt;/span&gt;&lt;/span&gt; 1011 + &lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true" tabindex="-1"&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; 1012 + &lt;span id="cb2-5"&gt;&lt;a href="#cb2-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="pp"&gt;assert_eq!&lt;/span&gt;(&lt;/span&gt; 1013 + &lt;span id="cb2-6"&gt;&lt;a href="#cb2-6" aria-hidden="true" tabindex="-1"&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; 1014 + &lt;span id="cb2-7"&gt;&lt;a href="#cb2-7" aria-hidden="true" tabindex="-1"&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; 1015 + &lt;span id="cb2-8"&gt;&lt;a href="#cb2-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; )&lt;span class="op"&gt;;&lt;/span&gt;&lt;/span&gt; 1016 + &lt;span id="cb2-9"&gt;&lt;a href="#cb2-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="op"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 1017 1017 &lt;h3 id="the-arglist"&gt;The arglist&lt;/h3&gt; 1018 1018 &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; 1019 - &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; 1020 - &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; 1021 - &lt;span id="cb3-3"&gt;&lt;a href="#cb3-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 1022 - &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; 1023 - &lt;span id="cb3-5"&gt;&lt;a href="#cb3-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 1024 - &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; 1025 - &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; 1019 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# `grep -l pattern files` lists all the files&lt;/span&gt;&lt;/span&gt; 1020 + &lt;span id="cb3-2"&gt;&lt;a href="#cb3-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# matching the pattern&lt;/span&gt;&lt;/span&gt; 1021 + &lt;span id="cb3-3"&gt;&lt;a href="#cb3-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 1022 + &lt;span id="cb3-4"&gt;&lt;a href="#cb3-4" aria-hidden="true" tabindex="-1"&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; &lt;span class="at"&gt;-l&lt;/span&gt; &lt;span class="st"&gt;&amp;#39;cfg\(test\)&amp;#39;&lt;/span&gt; ./&lt;span class="pp"&gt;**&lt;/span&gt;/&lt;span class="pp"&gt;*&lt;/span&gt;.rs&lt;span class="va"&gt;)&lt;/span&gt;&lt;/span&gt; 1023 + &lt;span id="cb3-5"&gt;&lt;a href="#cb3-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 1024 + &lt;span id="cb3-6"&gt;&lt;a href="#cb3-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# expands to something like:&lt;/span&gt;&lt;/span&gt; 1025 + &lt;span id="cb3-7"&gt;&lt;a href="#cb3-7" aria-hidden="true" tabindex="-1"&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; 1026 1026 &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; 1027 1027 &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; 1028 1028 &lt;pre&gt;&lt;code&gt;:argdo s/from_str/from_value/g&lt;/code&gt;&lt;/pre&gt; ··· 1204 1204 &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; 1205 1205 &lt;h3 id="directory-structure"&gt;Directory structure&lt;/h3&gt; 1206 1206 &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; 1207 - &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="ex"&gt;chronological&lt;/span&gt; order!&lt;/span&gt; 1208 - &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; 1209 - &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="kw"&gt;`&lt;/span&gt;&lt;span class="fu"&gt;ls&lt;/span&gt;&lt;span class="kw"&gt;`&lt;/span&gt; &lt;span class="ex"&gt;mangled&lt;/span&gt; our file paths&lt;/span&gt; 1210 - &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; 1211 - &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 1212 - &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; 1213 - &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; 1214 - &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; 1207 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="va"&gt;posts&lt;/span&gt;&lt;span class="op"&gt;=&lt;/span&gt;&lt;span class="va"&gt;$(&lt;/span&gt;&lt;span class="fu"&gt;ls&lt;/span&gt; &lt;span class="at"&gt;-t&lt;/span&gt; ./posts&lt;span class="va"&gt;)&lt;/span&gt; &lt;span class="co"&gt;# chronological order!&lt;/span&gt;&lt;/span&gt; 1208 + &lt;span id="cb1-2"&gt;&lt;a href="#cb1-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="cf"&gt;for&lt;/span&gt; f &lt;span class="kw"&gt;in&lt;/span&gt; &lt;span class="va"&gt;$posts&lt;/span&gt;&lt;span class="kw"&gt;;&lt;/span&gt; &lt;span class="cf"&gt;do&lt;/span&gt;&lt;/span&gt; 1209 + &lt;span id="cb1-3"&gt;&lt;a href="#cb1-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="va"&gt;file&lt;/span&gt;&lt;span class="op"&gt;=&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; 1210 + &lt;span id="cb1-4"&gt;&lt;a href="#cb1-4" aria-hidden="true" tabindex="-1"&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; 1211 + &lt;span id="cb1-5"&gt;&lt;a href="#cb1-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 1212 + &lt;span id="cb1-6"&gt;&lt;a href="#cb1-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="va"&gt;html&lt;/span&gt;&lt;span class="op"&gt;=&lt;/span&gt;&lt;span class="va"&gt;$(&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; 1213 + &lt;span id="cb1-7"&gt;&lt;a href="#cb1-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt; &lt;span class="bu"&gt;echo&lt;/span&gt; &lt;span class="at"&gt;-e&lt;/span&gt; &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; 1214 + &lt;span id="cb1-8"&gt;&lt;a href="#cb1-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="cf"&gt;done&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 1215 1215 &lt;h3 id="assets"&gt;Assets&lt;/h3&gt; 1216 1216 &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; 1217 - &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; 1218 - &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; 1219 - &lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 1220 - &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; 1221 - &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; 1222 - &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; 1223 - &lt;span id="cb2-7"&gt;&lt;a href="#cb2-7" aria-hidden="true"&gt;&lt;/a&gt;&lt;/span&gt; 1224 - &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; 1225 - &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; 1226 - &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; 1217 + &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" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# strip file extension&lt;/span&gt;&lt;/span&gt; 1218 + &lt;span id="cb2-2"&gt;&lt;a href="#cb2-2" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="va"&gt;ext&lt;/span&gt;&lt;span class="op"&gt;=&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="op"&gt;##&lt;/span&gt;&lt;span class="pp"&gt;*&lt;/span&gt;.&lt;span class="va"&gt;}&lt;/span&gt;&lt;span class="st"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; 1219 + &lt;span id="cb2-3"&gt;&lt;a href="#cb2-3" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 1220 + &lt;span id="cb2-4"&gt;&lt;a href="#cb2-4" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# generate a random file name&lt;/span&gt;&lt;/span&gt; 1221 + &lt;span id="cb2-5"&gt;&lt;a href="#cb2-5" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="va"&gt;id&lt;/span&gt;&lt;span class="op"&gt;=&lt;/span&gt;&lt;span class="va"&gt;$(&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; &lt;span class="at"&gt;-dc&lt;/span&gt; &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; &lt;span class="at"&gt;-w&lt;/span&gt; 2 &lt;span class="kw"&gt;|&lt;/span&gt; &lt;span class="fu"&gt;head&lt;/span&gt; &lt;span class="at"&gt;-n&lt;/span&gt; 1 &lt;span class="va"&gt;)&lt;/span&gt;&lt;/span&gt; 1222 + &lt;span id="cb2-6"&gt;&lt;a href="#cb2-6" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="va"&gt;id&lt;/span&gt;&lt;span class="op"&gt;=&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; 1223 + &lt;span id="cb2-7"&gt;&lt;a href="#cb2-7" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;/span&gt; 1224 + &lt;span id="cb2-8"&gt;&lt;a href="#cb2-8" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="co"&gt;# copy to my file host&lt;/span&gt;&lt;/span&gt; 1225 + &lt;span id="cb2-9"&gt;&lt;a href="#cb2-9" aria-hidden="true" tabindex="-1"&gt;&lt;/a&gt;&lt;span class="fu"&gt;scp&lt;/span&gt; &lt;span class="at"&gt;-P&lt;/span&gt; 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; 1226 + &lt;span id="cb2-10"&gt;&lt;a href="#cb2-10" aria-hidden="true" tabindex="-1"&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; 1227 1227 &lt;h3 id="templating"&gt;Templating&lt;/h3&gt; 1228 1228 &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> 1229 1229 <link>https://peppe.rs/posts/static_sites_with_bash/</link>
+34 -34
docs/posts/SDL2_devlog/index.html
··· 60 60 </figure> 61 61 <h3 id="day-17">Day 17</h3> 62 62 <p>I decided to give the text-only statusline a touch up, by adding a active color and dither level preview. Aligning the “widget” to the right of statusline involved a lot more than I thought, so I created a ghetto CSS-like rectangle placement system to position containers inside containers:</p> 63 - <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="co">// roughly something like this</span></span> 64 - <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a><span class="kw">let</span> statusline <span class="op">=</span> </span> 65 - <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="pp">Container::</span>new(<span class="pp">Offset::</span>Left(<span class="dv">0</span>)<span class="op">,</span> <span class="pp">Offset::</span>Bottom(<span class="dv">40</span>))</span> 66 - <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="op">.</span>width(<span class="pp">Size::</span>Max)</span> 67 - <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> <span class="op">.</span>height(<span class="pp">Size::</span>Absolute(<span class="dv">20</span>))<span class="op">;</span></span> 68 - <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a> </span> 69 - <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a><span class="kw">let</span> <span class="kw">mut</span> primary <span class="op">=</span> <span class="pp">Container::</span>uninit()</span> 70 - <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a> <span class="op">.</span>width(<span class="pp">Size::</span>Absolute(<span class="dv">16</span>))</span> 71 - <span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a> <span class="op">.</span>height(<span class="pp">Size::</span>Absolute(<span class="dv">16</span>))<span class="op">;</span></span> 72 - <span id="cb1-10"><a href="#cb1-10" aria-hidden="true"></a> </span> 73 - <span id="cb1-11"><a href="#cb1-11" aria-hidden="true"></a>container<span class="op">.</span>place(</span> 74 - <span id="cb1-12"><a href="#cb1-12" aria-hidden="true"></a> <span class="op">&amp;</span><span class="kw">mut</span> padding_box<span class="op">,</span></span> 75 - <span id="cb1-13"><a href="#cb1-13" aria-hidden="true"></a> <span class="pp">HorAlign::</span>Right<span class="op">,</span></span> 76 - <span id="cb1-14"><a href="#cb1-14" aria-hidden="true"></a> <span class="pp">VertAlign::</span>Center</span> 77 - <span id="cb1-15"><a href="#cb1-15" aria-hidden="true"></a>)<span class="op">;</span></span></code></pre></div> 63 + <div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">// roughly something like this</span></span> 64 + <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> statusline <span class="op">=</span> </span> 65 + <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="pp">Container::</span>new(<span class="pp">Offset::</span>Left(<span class="dv">0</span>)<span class="op">,</span> <span class="pp">Offset::</span>Bottom(<span class="dv">40</span>))</span> 66 + <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>width(<span class="pp">Size::</span>Max)</span> 67 + <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>height(<span class="pp">Size::</span>Absolute(<span class="dv">20</span>))<span class="op">;</span></span> 68 + <span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> </span> 69 + <span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> <span class="kw">mut</span> primary <span class="op">=</span> <span class="pp">Container::</span>uninit()</span> 70 + <span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>width(<span class="pp">Size::</span>Absolute(<span class="dv">16</span>))</span> 71 + <span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>height(<span class="pp">Size::</span>Absolute(<span class="dv">16</span>))<span class="op">;</span></span> 72 + <span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> </span> 73 + <span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>container<span class="op">.</span>place(</span> 74 + <span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> <span class="op">&amp;</span><span class="kw">mut</span> padding_box<span class="op">,</span></span> 75 + <span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> <span class="pp">HorAlign::</span>Right<span class="op">,</span></span> 76 + <span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> <span class="pp">VertAlign::</span>Center</span> 77 + <span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span></code></pre></div> 78 78 <p>The result (brush preview on the bottom right):</p> 79 79 <figure> 80 80 <video src="https://u.peppe.rs/OtU.mp4" controls=""><a href="https://u.peppe.rs/OtU.mp4">Day 17</a></video><figcaption aria-hidden="true">Day 17</figcaption> 81 81 </figure> 82 82 <h3 id="day-16">Day 16</h3> 83 83 <p>The embedded lisp is coming along nicely, users can load a custom <code>rc.lisp</code>, which is evaluated on startup. To disable to grid on start, for example:</p> 84 - <div class="sourceCode" id="cb2"><pre class="sourceCode scheme"><code class="sourceCode scheme"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a><span class="co">;;; rc.lisp</span></span> 85 - <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a>(toggle-grid)</span></code></pre></div> 84 + <div class="sourceCode" id="cb2"><pre class="sourceCode scheme"><code class="sourceCode scheme"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">;;; rc.lisp</span></span> 85 + <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>(toggle-grid)</span></code></pre></div> 86 86 <p>Some aliases to switch between brushes:</p> 87 - <div class="sourceCode" id="cb3"><pre class="sourceCode scheme"><code class="sourceCode scheme"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="co">;;; rc.lisp</span></span> 88 - <span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a>(<span class="ex">define</span><span class="fu"> </span>(brush kind)</span> 89 - <span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a> (<span class="kw">cond</span></span> 90 - <span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a> ((<span class="kw">eq?</span> kind &#39;f) (brush-fill))</span> 91 - <span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a> ((<span class="kw">eq?</span> kind &#39;c) (brush-circle))</span> 92 - <span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a> ((<span class="kw">eq?</span> kind &#39;l) (brush-line))</span> 93 - <span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a> ((<span class="kw">eq?</span> kind &#39;l+) (brush-line-extend))</span> 94 - <span id="cb3-8"><a href="#cb3-8" aria-hidden="true"></a> (<span class="kw">else</span> (brush-circle))))</span></code></pre></div> 87 + <div class="sourceCode" id="cb3"><pre class="sourceCode scheme"><code class="sourceCode scheme"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">;;; rc.lisp</span></span> 88 + <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>(<span class="ex">define</span><span class="fu"> </span>(brush kind)</span> 89 + <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> (<span class="kw">cond</span></span> 90 + <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind &#39;f) (brush-fill))</span> 91 + <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind &#39;c) (brush-circle))</span> 92 + <span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind &#39;l) (brush-line))</span> 93 + <span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind &#39;l+) (brush-line-extend))</span> 94 + <span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> (<span class="kw">else</span> (brush-circle))))</span></code></pre></div> 95 95 <p>The following script draws a straight line along a given axis, at a given distance from the canvas boundary:</p> 96 96 <figure> 97 97 <video src="https://u.peppe.rs/b3i.mp4" controls=""><a href="https://u.peppe.rs/b3i.mp4">Day 16</a></video><figcaption aria-hidden="true">Day 16</figcaption> 98 98 </figure> 99 99 <h3 id="day-15">Day 15</h3> 100 100 <p>I began writing a standard library for the lisp, in lisp. It includes basic list operations: <code>car</code>, <code>cdr</code>, <code>null?</code>, <code>list</code>, higher order functions: <code>map</code>, <code>filter</code>, <code>fold</code>:</p> 101 - <div class="sourceCode" id="cb4"><pre class="sourceCode lisp"><code class="sourceCode commonlisp"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a>(define (member? item ls)</span> 102 - <span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a> (fold <span class="dv">#f</span></span> 103 - <span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a> (<span class="kw">lambda</span> (acc x) (<span class="kw">or</span> acc (eq? item x)))</span> 104 - <span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a> ls))</span></code></pre></div> 101 + <div class="sourceCode" id="cb4"><pre class="sourceCode lisp"><code class="sourceCode commonlisp"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>(define (member? item ls)</span> 102 + <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> (fold <span class="dv">#f</span></span> 103 + <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> (<span class="kw">lambda</span> (acc x) (<span class="kw">or</span> acc (eq? item x)))</span> 104 + <span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> ls))</span></code></pre></div> 105 105 <h3 id="day-14">Day 14</h3> 106 106 <p>I attempted a <a href="https://peppe.rs/art/ramen_noodles.png">small art piece</a> using the editor, while it was largely usable, I felt a certain lack of feedback. The brushes just didn’t relay as much info as I’d have liked, for example, the approximate points of the line or the angle made by the line against the x-axis. Unfortunately, the existing infrastructure around brushes and line drawing didn’t easily allow for this either. I went ahead and reimplemented brushes, and added a new flood fill brush too:</p> 107 107 <figure> ··· 134 134 </figure> 135 135 <h3 id="day-8">Day 8</h3> 136 136 <p>One of my favourite features of GIMP was symmetric editing. I added some coordinate geometry primitives to my pixmap abstraction, allowing for mirroring and reflecting figures about lines or points. The result was an ergonomic function that applies symmetry to any painting operation, (undo/redo works as expected):</p> 137 - <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">let</span> line <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>pixmap<span class="op">.</span>get_line(start<span class="op">,</span> end)<span class="op">;</span></span> 138 - <span id="cb5-2"><a href="#cb5-2" aria-hidden="true"></a><span class="kw">let</span> sym_line <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>symmetry<span class="op">.</span>apply(<span class="op">&amp;</span>line)<span class="op">;</span></span> 139 - <span id="cb5-3"><a href="#cb5-3" aria-hidden="true"></a><span class="kw">for</span> point on line<span class="op">.</span>extend(sym_line) <span class="op">{</span></span> 140 - <span id="cb5-4"><a href="#cb5-4" aria-hidden="true"></a> <span class="co">// draw to window</span></span> 141 - <span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 137 + <div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> line <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>pixmap<span class="op">.</span>get_line(start<span class="op">,</span> end)<span class="op">;</span></span> 138 + <span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> sym_line <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>symmetry<span class="op">.</span>apply(<span class="op">&amp;</span>line)<span class="op">;</span></span> 139 + <span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="kw">for</span> point on line<span class="op">.</span>extend(sym_line) <span class="op">{</span></span> 140 + <span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="co">// draw to window</span></span> 141 + <span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 142 142 <figure> 143 143 <video src="https://u.peppe.rs/B1.mp4" controls=""><a href="https://u.peppe.rs/B1.mp4">Day 8</a></video><figcaption aria-hidden="true">Day 8</figcaption> 144 144 </figure>
+256 -256
docs/posts/auto-currying_rust_functions/index.html
··· 98 98 <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> 99 99 <h3 id="definitions">Definitions</h3> 100 100 <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> 101 - <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> 102 - <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> 103 - <span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 101 + <div class="sourceCode" id="cb4"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 102 + <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> 103 + <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 104 104 <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> 105 - <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> 106 - <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> 107 - <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> 108 - <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> 109 - <span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a> <span class="op">}</span></span> 110 - <span id="cb5-6"><a href="#cb5-6" aria-hidden="true"></a> <span class="op">}</span></span> 111 - <span id="cb5-7"><a href="#cb5-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 105 + <div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></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> 106 + <span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></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> 107 + <span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></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> 108 + <span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> 109 + <span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 110 + <span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 111 + <span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 112 112 <p>A couple of things to note:</p> 113 113 <p><strong>Return types</strong><br /> 114 114 We have placed <code>?</code>s in place of return types. Let’s try to fix that. <code>add_curried3</code> returns the ‘value’, so <code>u32</code> is accurate. <code>add_curried2</code> returns <code>add_curried3</code>. What is the type of <code>add_curried3</code>? It is a function that takes in a <code>u32</code> and returns a <code>u32</code>. So a <code>fn(u32) -&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> ··· 121 121 A function cannot access it’s environment. Our solution will not work. <code>add_curried3</code> attempts to access <code>x</code>, which is not allowed! A closure<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> however, can. If we are returning a closure, our return type must be <code>impl Fn</code>, and not <code>fn</code>. The difference between the <code>Fn</code> trait and function pointers is beyond the scope of this post.</p> 122 122 <h3 id="refinement">Refinement</h3> 123 123 <p>Armed with knowledge, we refine our expected output, this time, employing closures:</p> 124 - <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> 125 - <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> 126 - <span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 124 + <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></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> 125 + <span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> 126 + <span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 127 127 <p>Alas, that does not compile either! It errors out with the following message:</p> 128 128 <pre><code>error[E0562]: `impl Trait` not allowed outside of function 129 129 and inherent method return types ··· 134 134 </code></pre> 135 135 <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> 136 136 <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> 137 - <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> 138 - <span id="cb10-2"><a href="#cb10-2" aria-hidden="true"></a></span> 139 - <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> 140 - <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> 141 - <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> 142 - <span id="cb10-6"><a href="#cb10-6" aria-hidden="true"></a></span> 143 - <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> 144 - <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> 145 - <span id="cb10-9"><a href="#cb10-9" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 137 + <div class="sourceCode" id="cb10"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span> <span class="co">// allows us to use `impl Fn` in type aliases!</span></span> 138 + <span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span> 139 + <span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></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> 140 + <span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></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> 141 + <span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></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> 142 + <span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a></span> 143 + <span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></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> 144 + <span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> 145 + <span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 146 146 <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> 147 147 <h3 id="the-in-betweens">The In-Betweens</h3> 148 148 <p>Let us write the magical bits that take us from function to curried function.</p> ··· 176 176 <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> 177 177 <h4 id="the-attribute-macro">The attribute macro</h4> 178 178 <p>Drop this into <code>src/lib.rs</code>, to get the ball rolling.</p> 179 - <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> 180 - <span id="cb13-2"><a href="#cb13-2" aria-hidden="true"></a></span> 181 - <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> 182 - <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> 183 - <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> 184 - <span id="cb13-6"><a href="#cb13-6" aria-hidden="true"></a></span> 185 - <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> 186 - <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> 187 - <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> 188 - <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> 189 - <span id="cb13-11"><a href="#cb13-11" aria-hidden="true"></a><span class="op">}</span></span> 190 - <span id="cb13-12"><a href="#cb13-12" aria-hidden="true"></a></span> 191 - <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> 179 + <div class="sourceCode" id="cb13"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 180 + <span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span> 181 + <span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="kw">use</span> <span class="pp">proc_macro::</span>TokenStream<span class="op">;</span> <span class="co">// 1</span></span> 182 + <span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="kw">use</span> <span class="pp">quote::</span>quote<span class="op">;</span></span> 183 + <span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></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> 184 + <span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a></span> 185 + <span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>proc_macro_attribute<span class="at">]</span> <span class="co">// 2</span></span> 186 + <span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></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> 187 + <span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></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> 188 + <span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a> generate_curry(parsed)<span class="op">.</span>into() <span class="co">// 4</span></span> 189 + <span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 190 + <span id="cb13-12"><a href="#cb13-12" aria-hidden="true" tabindex="-1"></a></span> 191 + <span id="cb13-13"><a href="#cb13-13" aria-hidden="true" tabindex="-1"></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> 192 192 <p><strong>1. Imports</strong></p> 193 193 <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> 194 194 <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> ··· 200 200 <p><strong>4. Returning <code>TokenStream</code>s </strong></p> 201 201 <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> 202 202 <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> 203 - <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> 204 - <span id="cb14-2"><a href="#cb14-2" aria-hidden="true"></a></span> 205 - <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> 206 - <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> 207 - <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> 208 - <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> 209 - <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> 210 - <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> 211 - <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> 212 - <span id="cb14-10"><a href="#cb14-10" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 203 + <div class="sourceCode" id="cb14"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 204 + <span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a></span> 205 + <span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></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> 206 + <span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></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> 207 + <span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></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> 208 + <span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></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> 209 + <span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></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> 210 + <span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></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> 211 + <span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></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> 212 + <span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 213 213 <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> 214 214 <pre><code> .-- syn::Ident (ident) 215 215 / ··· 221 221 <p>Enough analysis, lets produce our first bit of Rust code.</p> 222 222 <h4 id="function-body">Function Body</h4> 223 223 <p>Recall that the body of a curried <code>add</code> should look like this:</p> 224 - <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> 224 + <div class="sourceCode" id="cb16"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>y<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span></code></pre></div> 225 225 <p>And in general:</p> 226 - <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> 226 + <div class="sourceCode" id="cb17"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="kw">return</span> <span class="kw">move</span> <span class="op">|</span>arg2<span class="op">|</span> <span class="kw">move</span> <span class="op">|</span>arg3<span class="op">|</span> <span class="op">...</span> <span class="op">|</span>argN<span class="op">|</span> <span class="op">&lt;</span>function body here<span class="op">&gt;</span></span></code></pre></div> 227 227 <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> 228 - <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> 229 - <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> 230 - <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> 231 - <span id="cb18-4"><a href="#cb18-4" aria-hidden="true"></a></span> 232 - <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> 233 - <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> 234 - <span id="cb18-7"><a href="#cb18-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 228 + <div class="sourceCode" id="cb18"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 229 + <span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="kw">use</span> <span class="pp">syn::punctuated::</span>Punctuated<span class="op">;</span></span> 230 + <span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></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> 231 + <span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a></span> 232 + <span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></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> 233 + <span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></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> 234 + <span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 235 235 <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> 236 - <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> 237 - <span id="cb19-2"><a href="#cb19-2" aria-hidden="true"></a></span> 238 - <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> 239 - <span id="cb19-4"><a href="#cb19-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> 240 - <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> 241 - <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> 242 - <span id="cb19-7"><a href="#cb19-7" aria-hidden="true"></a> <span class="op">}</span></span> 243 - <span id="cb19-8"><a href="#cb19-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 236 + <div class="sourceCode" id="cb19"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 237 + <span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a></span> 238 + <span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></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> 239 + <span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">match</span> a <span class="op">{</span></span> 240 + <span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> <span class="pp">FnArg::</span>Typed(p) <span class="op">=&gt;</span> p<span class="op">.</span>pat<span class="op">,</span></span> 241 + <span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></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> 242 + <span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 243 + <span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 244 244 <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> 245 245 <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> 246 246 <p>With that done, we should be able to write the codegen for the function body:</p> 247 - <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> 248 - <span id="cb20-2"><a href="#cb20-2" aria-hidden="true"></a></span> 249 - <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> 250 - <span id="cb20-4"><a href="#cb20-4" aria-hidden="true"></a> <span class="pp">quote!</span> <span class="op">{</span></span> 251 - <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> 252 - <span id="cb20-6"><a href="#cb20-6" aria-hidden="true"></a> <span class="op">}</span></span> 253 - <span id="cb20-7"><a href="#cb20-7" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 247 + <div class="sourceCode" id="cb20"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 248 + <span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a></span> 249 + <span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></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> 250 + <span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="pp">quote!</span> <span class="op">{</span></span> 251 + <span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></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> 252 + <span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 253 + <span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 254 254 <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> 255 255 <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> 256 256 <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> 257 257 <p>Let us test our first bit of codegen! Modify <code>generate_curry</code> like so:</p> 258 - <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> 259 - <span id="cb21-2"><a href="#cb21-2" aria-hidden="true"></a></span> 260 - <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> 261 - <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> 262 - <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> 263 - <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> 264 - <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> 265 - <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> 266 - <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> 267 - <span id="cb21-10"><a href="#cb21-10" aria-hidden="true"></a></span> 268 - <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> 269 - <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> 270 - <span id="cb21-13"><a href="#cb21-13" aria-hidden="true"></a></span> 271 - <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> 272 - <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> 273 - <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> 274 - <span id="cb21-17"><a href="#cb21-17" aria-hidden="true"></a></span> 275 - <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> 276 - <span id="cb21-19"><a href="#cb21-19" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> 258 + <div class="sourceCode" id="cb21"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 259 + <span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a></span> 260 + <span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></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> 261 + <span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> 262 + <span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> 263 + <span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> 264 + <span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> 265 + <span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> 266 + <span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> 267 + <span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a></span> 268 + <span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></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> 269 + <span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></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> 270 + <span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a></span> 271 + <span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> <span class="co">// remember, our curried body starts with the second argument!</span></span> 272 + <span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></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> 273 + <span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></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> 274 + <span id="cb21-17"><a href="#cb21-17" aria-hidden="true" tabindex="-1"></a></span> 275 + <span id="cb21-18"><a href="#cb21-18" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> <span class="pp">TokenStream::</span>new()<span class="op">;</span></span> 276 + <span id="cb21-19"><a href="#cb21-19" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div> 277 277 <p>Add a little test to <code>tests/</code>:</p> 278 - <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> 279 - <span id="cb22-2"><a href="#cb22-2" aria-hidden="true"></a></span> 280 - <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> 281 - <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> 282 - <span id="cb22-5"><a href="#cb22-5" aria-hidden="true"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> 283 - <span id="cb22-6"><a href="#cb22-6" aria-hidden="true"></a><span class="op">}</span></span> 284 - <span id="cb22-7"><a href="#cb22-7" aria-hidden="true"></a></span> 285 - <span id="cb22-8"><a href="#cb22-8" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> 286 - <span id="cb22-9"><a href="#cb22-9" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> 287 - <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> 288 - <span id="cb22-11"><a href="#cb22-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 278 + <div class="sourceCode" id="cb22"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="co">// tests/smoke.rs</span></span> 279 + <span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a></span> 280 + <span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span><span class="pp">currying::</span>curry<span class="at">]</span></span> 281 + <span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 282 + <span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> 283 + <span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 284 + <span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a></span> 285 + <span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>test<span class="at">]</span></span> 286 + <span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> works() <span class="op">{</span></span> 287 + <span id="cb22-10"><a href="#cb22-10" aria-hidden="true" tabindex="-1"></a> <span class="pp">assert!</span>(<span class="cn">true</span>)<span class="op">;</span></span> 288 + <span id="cb22-11"><a href="#cb22-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 289 289 <p>You should find something like this in the output of <code>cargo test</code>:</p> 290 290 <pre><code>return move | y | move | z | { x + y + z }</code></pre> 291 291 <p>Glorious <code>println!</code> debugging!</p> 292 292 <h4 id="function-signature">Function signature</h4> 293 293 <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> 294 294 <p>Recall what our generated type aliases should look like, for our <code>add</code> function:</p> 295 - <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> 296 - <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> 297 - <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> 295 + <div class="sourceCode" id="cb24"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> 296 + <span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></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> 297 + <span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></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> 298 298 <p>In general:</p> 299 - <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> 300 - <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> 301 - <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> 302 - <span id="cb25-4"><a href="#cb25-4" aria-hidden="true"></a><span class="op">.</span></span> 303 - <span id="cb25-5"><a href="#cb25-5" aria-hidden="true"></a><span class="op">.</span></span> 304 - <span id="cb25-6"><a href="#cb25-6" aria-hidden="true"></a><span class="op">.</span></span> 305 - <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> 299 + <div class="sourceCode" id="cb25"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></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> 300 + <span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></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> 301 + <span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></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> 302 + <span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="op">.</span></span> 303 + <span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="op">.</span></span> 304 + <span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a><span class="op">.</span></span> 305 + <span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></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> 306 306 <p>To codegen that, we need the types of:</p> 307 307 <ul> 308 308 <li>all our inputs (arguments)</li> 309 309 <li>the output (the return type)</li> 310 310 </ul> 311 311 <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> 312 - <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> 313 - <span id="cb26-2"><a href="#cb26-2" aria-hidden="true"></a></span> 314 - <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> 315 - <span id="cb26-4"><a href="#cb26-4" aria-hidden="true"></a></span> 316 - <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> 317 - <span id="cb26-6"><a href="#cb26-6" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> 318 - <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> 319 - <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> 320 - <span id="cb26-9"><a href="#cb26-9" aria-hidden="true"></a> <span class="op">}</span></span> 321 - <span id="cb26-10"><a href="#cb26-10" aria-hidden="true"></a><span class="op">}</span></span> 322 - <span id="cb26-11"><a href="#cb26-11" aria-hidden="true"></a></span> 323 - <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> 324 - <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> 325 - <span id="cb26-14"><a href="#cb26-14" aria-hidden="true"></a></span> 326 - <span id="cb26-15"><a href="#cb26-15" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 312 + <div class="sourceCode" id="cb26"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 313 + <span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a></span> 314 + <span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></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> 315 + <span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a></span> 316 + <span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></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> 317 + <span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">match</span> a <span class="op">{</span></span> 318 + <span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></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> 319 + <span id="cb26-8"><a href="#cb26-8" aria-hidden="true" tabindex="-1"></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> 320 + <span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 321 + <span id="cb26-10"><a href="#cb26-10" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 322 + <span id="cb26-11"><a href="#cb26-11" aria-hidden="true" tabindex="-1"></a></span> 323 + <span id="cb26-12"><a href="#cb26-12" aria-hidden="true" tabindex="-1"></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> 324 + <span id="cb26-13"><a href="#cb26-13" aria-hidden="true" tabindex="-1"></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> 325 + <span id="cb26-14"><a href="#cb26-14" aria-hidden="true" tabindex="-1"></a></span> 326 + <span id="cb26-15"><a href="#cb26-15" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 327 327 <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> 328 328 <ol type="1"> 329 329 <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> 330 330 <li><p>A <code>ReturnType</code> encloses the arrow of the return as well, we need to get rid of that. Recall:</p> 331 - <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> 332 - <span id="cb27-2"><a href="#cb27-2" aria-hidden="true"></a><span class="co">// and not</span></span> 333 - <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> 331 + <div class="sourceCode" id="cb27"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span></span> 332 + <span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="co">// and not</span></span> 333 + <span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></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> 334 334 </ol> 335 335 <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> 336 - <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> 337 - <span id="cb28-2"><a href="#cb28-2" aria-hidden="true"></a></span> 338 - <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> 339 - <span id="cb28-4"><a href="#cb28-4" aria-hidden="true"></a> <span class="kw">match</span> a <span class="op">{</span></span> 340 - <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> 341 - <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> 342 - <span id="cb28-7"><a href="#cb28-7" aria-hidden="true"></a> <span class="op">}</span></span> 343 - <span id="cb28-8"><a href="#cb28-8" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 336 + <div class="sourceCode" id="cb28"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 337 + <span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a></span> 338 + <span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></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> 339 + <span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">match</span> a <span class="op">{</span></span> 340 + <span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a> <span class="pp">ReturnType::</span>Type(_<span class="op">,</span> p) <span class="op">=&gt;</span> p<span class="op">,</span></span> 341 + <span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></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> 342 + <span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 343 + <span id="cb28-8"><a href="#cb28-8" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 344 344 <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> 345 345 <p>With all our types ready, we can get on with generating type aliases:</p> 346 - <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> 347 - <span id="cb29-2"><a href="#cb29-2" aria-hidden="true"></a></span> 348 - <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> 349 - <span id="cb29-4"><a href="#cb29-4" aria-hidden="true"></a></span> 350 - <span id="cb29-5"><a href="#cb29-5" aria-hidden="true"></a><span class="kw">fn</span> generate_type_aliases(</span> 351 - <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> 352 - <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> 353 - <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> 354 - <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> 355 - <span id="cb29-10"><a href="#cb29-10" aria-hidden="true"></a></span> 356 - <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> 357 - <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> 358 - <span id="cb29-13"><a href="#cb29-13" aria-hidden="true"></a></span> 359 - <span id="cb29-14"><a href="#cb29-14" aria-hidden="true"></a> <span class="co">// 3</span></span> 360 - <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> 361 - <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> 362 - <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> 363 - <span id="cb29-18"><a href="#cb29-18" aria-hidden="true"></a></span> 364 - <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> 365 - <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> 366 - <span id="cb29-21"><a href="#cb29-21" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> 367 - <span id="cb29-22"><a href="#cb29-22" aria-hidden="true"></a> <span class="op">}</span></span> 368 - <span id="cb29-23"><a href="#cb29-23" aria-hidden="true"></a></span> 369 - <span id="cb29-24"><a href="#cb29-24" aria-hidden="true"></a> <span class="kw">return</span> type_aliases<span class="op">;</span></span> 370 - <span id="cb29-25"><a href="#cb29-25" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 346 + <div class="sourceCode" id="cb29"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 347 + <span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a></span> 348 + <span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></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> 349 + <span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a></span> 350 + <span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> generate_type_aliases(</span> 351 + <span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></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> 352 + <span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></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> 353 + <span id="cb29-8"><a href="#cb29-8" aria-hidden="true" tabindex="-1"></a> fn_name<span class="op">:</span> <span class="op">&amp;</span><span class="pp">syn::</span>Ident<span class="op">,</span></span> 354 + <span id="cb29-9"><a href="#cb29-9" aria-hidden="true" tabindex="-1"></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> 355 + <span id="cb29-10"><a href="#cb29-10" aria-hidden="true" tabindex="-1"></a></span> 356 + <span id="cb29-11"><a href="#cb29-11" aria-hidden="true" tabindex="-1"></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> 357 + <span id="cb29-12"><a href="#cb29-12" aria-hidden="true" tabindex="-1"></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> 358 + <span id="cb29-13"><a href="#cb29-13" aria-hidden="true" tabindex="-1"></a></span> 359 + <span id="cb29-14"><a href="#cb29-14" aria-hidden="true" tabindex="-1"></a> <span class="co">// 3</span></span> 360 + <span id="cb29-15"><a href="#cb29-15" aria-hidden="true" tabindex="-1"></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> 361 + <span id="cb29-16"><a href="#cb29-16" aria-hidden="true" tabindex="-1"></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> 362 + <span id="cb29-17"><a href="#cb29-17" aria-hidden="true" tabindex="-1"></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> 363 + <span id="cb29-18"><a href="#cb29-18" aria-hidden="true" tabindex="-1"></a></span> 364 + <span id="cb29-19"><a href="#cb29-19" aria-hidden="true" tabindex="-1"></a> type_aliases<span class="op">.</span>push(<span class="pp">quote!</span> <span class="op">{</span></span> 365 + <span id="cb29-20"><a href="#cb29-20" aria-hidden="true" tabindex="-1"></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> 366 + <span id="cb29-21"><a href="#cb29-21" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span>)<span class="op">;</span></span> 367 + <span id="cb29-22"><a href="#cb29-22" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 368 + <span id="cb29-23"><a href="#cb29-23" aria-hidden="true" tabindex="-1"></a></span> 369 + <span id="cb29-24"><a href="#cb29-24" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> type_aliases<span class="op">;</span></span> 370 + <span id="cb29-25"><a href="#cb29-25" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 371 371 <p><strong>1. The return value</strong><br /> 372 372 We 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> 373 373 <p><strong>2. Format identifier?</strong><br /> 374 374 I’ve got some explanation to do on this line. Clearly, we are trying to write the first type alias, and initialize our <code>TokenStream</code> vector with <code>T0</code>, because it is different from the others:</p> 375 - <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> 376 - <span id="cb30-2"><a href="#cb30-2" aria-hidden="true"></a><span class="co">// the others are of the form</span></span> 377 - <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> 375 + <div class="sourceCode" id="cb30"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> T0 <span class="op">=</span> something</span> 376 + <span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="co">// the others are of the form</span></span> 377 + <span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></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> 378 378 <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> 379 379 <p>Picture this, we have two functions, <code>add</code> and <code>subtract</code>, that we wish to curry with our macro:</p> 380 - <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> 381 - <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> 382 - <span id="cb31-3"><a href="#cb31-3" aria-hidden="true"></a></span> 383 - <span id="cb31-4"><a href="#cb31-4" aria-hidden="true"></a><span class="at">#[</span>curry<span class="at">]</span></span> 384 - <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> 380 + <div class="sourceCode" id="cb31"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>curry<span class="at">]</span></span> 381 + <span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></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> 382 + <span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a></span> 383 + <span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>curry<span class="at">]</span></span> 384 + <span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></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> 385 385 <p>Here is the same but with macros expanded:</p> 386 - <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> 387 - <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> 388 - <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> 389 - <span id="cb32-4"><a href="#cb32-4" aria-hidden="true"></a></span> 390 - <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> 391 - <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> 392 - <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> 386 + <div class="sourceCode" id="cb32"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> 387 + <span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></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> 388 + <span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></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> 389 + <span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a></span> 390 + <span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> 391 + <span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></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> 392 + <span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></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> 393 393 <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> 394 - <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> 395 - <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> 396 - <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> 397 - <span id="cb33-4"><a href="#cb33-4" aria-hidden="true"></a></span> 398 - <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> 399 - <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> 400 - <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> 394 + <div class="sourceCode" id="cb33"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> 395 + <span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></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> 396 + <span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></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> 397 + <span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a></span> 398 + <span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> _sub_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> 399 + <span id="cb33-6"><a href="#cb33-6" aria-hidden="true" tabindex="-1"></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> 400 + <span id="cb33-7"><a href="#cb33-7" aria-hidden="true" tabindex="-1"></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> 401 401 <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> 402 402 <p><strong>3. The TokenStream Vector</strong></p> 403 403 <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> 404 404 <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> 405 405 <h4 id="getting-it-together">Getting it together</h4> 406 406 <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> 407 - <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> 408 - <span id="cb34-2"><a href="#cb34-2" aria-hidden="true"></a></span> 409 - <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> 410 - <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> 411 - <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> 412 - <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> 413 - <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> 414 - <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> 415 - <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> 416 - <span id="cb34-10"><a href="#cb34-10" aria-hidden="true"></a></span> 417 - <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> 418 - <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> 419 - <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> 420 - <span id="cb34-14"><a href="#cb34-14" aria-hidden="true"></a></span> 421 - <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> 422 - <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> 423 - <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> 424 - <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> 425 - <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> 426 - <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> 427 - <span id="cb34-21"><a href="#cb34-21" aria-hidden="true"></a><span class="op">+</span> )<span class="op">;</span></span> 428 - <span id="cb34-22"><a href="#cb34-22" aria-hidden="true"></a></span> 429 - <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> 430 - <span id="cb34-24"><a href="#cb34-24" aria-hidden="true"></a></span> 431 - <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> 432 - <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> 433 - <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> 434 - <span id="cb34-28"><a href="#cb34-28" aria-hidden="true"></a><span class="op">+</span> #curried_body <span class="op">;</span></span> 435 - <span id="cb34-29"><a href="#cb34-29" aria-hidden="true"></a><span class="op">+</span> <span class="op">}</span></span> 436 - <span id="cb34-30"><a href="#cb34-30" aria-hidden="true"></a><span class="op">+</span> <span class="op">};</span></span> 437 - <span id="cb34-31"><a href="#cb34-31" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> 407 + <div class="sourceCode" id="cb34"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="co">// src/lib.rs</span></span> 408 + <span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span> 409 + <span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></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> 410 + <span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_body <span class="op">=</span> parsed<span class="op">.</span>block<span class="op">;</span></span> 411 + <span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> sig <span class="op">=</span> parsed<span class="op">.</span>sig<span class="op">;</span></span> 412 + <span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> vis <span class="op">=</span> parsed<span class="op">.</span>vis<span class="op">;</span></span> 413 + <span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_name <span class="op">=</span> sig<span class="op">.</span>ident<span class="op">;</span></span> 414 + <span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_args <span class="op">=</span> sig<span class="op">.</span>inputs<span class="op">;</span></span> 415 + <span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> fn_return_type <span class="op">=</span> sig<span class="op">.</span>output<span class="op">;</span></span> 416 + <span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a></span> 417 + <span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></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> 418 + <span id="cb34-12"><a href="#cb34-12" aria-hidden="true" tabindex="-1"></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> 419 + <span id="cb34-13"><a href="#cb34-13" aria-hidden="true" tabindex="-1"></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> 420 + <span id="cb34-14"><a href="#cb34-14" aria-hidden="true" tabindex="-1"></a></span> 421 + <span id="cb34-15"><a href="#cb34-15" aria-hidden="true" tabindex="-1"></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> 422 + <span id="cb34-16"><a href="#cb34-16" aria-hidden="true" tabindex="-1"></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> 423 + <span id="cb34-17"><a href="#cb34-17" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> <span class="kw">let</span> type_aliases <span class="op">=</span> generate_type_aliases(</span> 424 + <span id="cb34-18"><a href="#cb34-18" aria-hidden="true" tabindex="-1"></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> 425 + <span id="cb34-19"><a href="#cb34-19" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> extract_return_type(fn_return_type)<span class="op">,</span></span> 426 + <span id="cb34-20"><a href="#cb34-20" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> <span class="op">&amp;</span>fn_name<span class="op">,</span></span> 427 + <span id="cb34-21"><a href="#cb34-21" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> )<span class="op">;</span></span> 428 + <span id="cb34-22"><a href="#cb34-22" aria-hidden="true" tabindex="-1"></a></span> 429 + <span id="cb34-23"><a href="#cb34-23" aria-hidden="true" tabindex="-1"></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> 430 + <span id="cb34-24"><a href="#cb34-24" aria-hidden="true" tabindex="-1"></a></span> 431 + <span id="cb34-25"><a href="#cb34-25" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> <span class="kw">return</span> <span class="pp">quote!</span> <span class="op">{</span></span> 432 + <span id="cb34-26"><a href="#cb34-26" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> #(#type_aliases)<span class="op">;*</span> <span class="op">;</span></span> 433 + <span id="cb34-27"><a href="#cb34-27" aria-hidden="true" tabindex="-1"></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> 434 + <span id="cb34-28"><a href="#cb34-28" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> #curried_body <span class="op">;</span></span> 435 + <span id="cb34-29"><a href="#cb34-29" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> <span class="op">}</span></span> 436 + <span id="cb34-30"><a href="#cb34-30" aria-hidden="true" tabindex="-1"></a><span class="op">+</span> <span class="op">};</span></span> 437 + <span id="cb34-31"><a href="#cb34-31" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div> 438 438 <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> 439 439 <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> 440 440 <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> ··· 466 466 <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> 467 467 <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> 468 468 <p>Enough of that, lets get testing:</p> 469 - <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> 470 - <span id="cb37-2"><a href="#cb37-2" aria-hidden="true"></a></span> 471 - <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> 472 - <span id="cb37-4"><a href="#cb37-4" aria-hidden="true"></a></span> 473 - <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> 474 - <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> 475 - <span id="cb37-7"><a href="#cb37-7" aria-hidden="true"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> 476 - <span id="cb37-8"><a href="#cb37-8" aria-hidden="true"></a><span class="op">}</span></span> 477 - <span id="cb37-9"><a href="#cb37-9" aria-hidden="true"></a></span> 478 - <span id="cb37-10"><a href="#cb37-10" aria-hidden="true"></a><span class="at">#[</span>test<span class="at">]</span></span> 479 - <span id="cb37-11"><a href="#cb37-11" aria-hidden="true"></a><span class="kw">fn</span> works() <span class="op">{</span></span> 480 - <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> 481 - <span id="cb37-13"><a href="#cb37-13" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 469 + <div class="sourceCode" id="cb37"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="co">// tests/smoke.rs</span></span> 470 + <span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a></span> 471 + <span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="at">#![</span>feature<span class="at">(</span>type_alias_impl_trait<span class="at">)]</span></span> 472 + <span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a></span> 473 + <span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span><span class="pp">crate_name::</span>curry<span class="at">]</span></span> 474 + <span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> add(x<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> z<span class="op">:</span> <span class="dt">u32</span>) <span class="op">-&gt;</span> <span class="dt">u32</span> <span class="op">{</span></span> 475 + <span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a> x <span class="op">+</span> y <span class="op">+</span> z</span> 476 + <span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 477 + <span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a></span> 478 + <span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>test<span class="at">]</span></span> 479 + <span id="cb37-11"><a href="#cb37-11" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> works() <span class="op">{</span></span> 480 + <span id="cb37-12"><a href="#cb37-12" aria-hidden="true" tabindex="-1"></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> 481 + <span id="cb37-13"><a href="#cb37-13" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 482 482 <p>Run <code>cargo +nightly test</code>. You should see a pleasing message:</p> 483 483 <pre><code>running 1 test 484 484 test tests::works ... ok</code></pre> 485 485 <p>Take a look at the expansion for our curry macro, via <code>cargo +nightly expand --tests smoke</code>:</p> 486 - <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> 487 - <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> 488 - <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> 489 - <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> 490 - <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> 491 - <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> 492 - <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> 493 - <span id="cb39-8"><a href="#cb39-8" aria-hidden="true"></a> <span class="op">}</span></span> 494 - <span id="cb39-9"><a href="#cb39-9" aria-hidden="true"></a> <span class="op">}</span>)<span class="op">;</span></span> 495 - <span id="cb39-10"><a href="#cb39-10" aria-hidden="true"></a><span class="op">}</span></span> 496 - <span id="cb39-11"><a href="#cb39-11" aria-hidden="true"></a></span> 497 - <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> 486 + <div class="sourceCode" id="cb39"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> _add_T0 <span class="op">=</span> <span class="dt">u32</span><span class="op">;</span></span> 487 + <span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></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> 488 + <span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></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> 489 + <span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></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> 490 + <span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></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> 491 + <span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">move</span> <span class="op">|</span>z<span class="op">|</span> <span class="op">{</span></span> 492 + <span id="cb39-7"><a href="#cb39-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> x <span class="op">+</span> y <span class="op">+</span> z<span class="op">;</span></span> 493 + <span id="cb39-8"><a href="#cb39-8" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 494 + <span id="cb39-9"><a href="#cb39-9" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span>)<span class="op">;</span></span> 495 + <span id="cb39-10"><a href="#cb39-10" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 496 + <span id="cb39-11"><a href="#cb39-11" aria-hidden="true" tabindex="-1"></a></span> 497 + <span id="cb39-12"><a href="#cb39-12" aria-hidden="true" tabindex="-1"></a><span class="co">// a bunch of other stuff generated by #[test] and assert_eq!</span></span></code></pre></div> 498 498 <p>A sight for sore eyes.</p> 499 499 <p>Here is a more complex example that generates ten multiples of the first ten natural numbers:</p> 500 - <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> 501 - <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> 502 - <span id="cb40-3"><a href="#cb40-3" aria-hidden="true"></a> x <span class="op">*</span> y</span> 503 - <span id="cb40-4"><a href="#cb40-4" aria-hidden="true"></a><span class="op">}</span></span> 504 - <span id="cb40-5"><a href="#cb40-5" aria-hidden="true"></a></span> 505 - <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> 506 - <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> 507 - <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> 508 - <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> 509 - <span id="cb40-10"><a href="#cb40-10" aria-hidden="true"></a> <span class="op">.</span>collect()<span class="op">;</span></span> 510 - <span id="cb40-11"><a href="#cb40-11" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 500 + <div class="sourceCode" id="cb40"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="at">#[</span>curry<span class="at">]</span></span> 501 + <span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></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> 502 + <span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a> x <span class="op">*</span> y</span> 503 + <span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 504 + <span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a></span> 505 + <span id="cb40-6"><a href="#cb40-6" aria-hidden="true" tabindex="-1"></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> 506 + <span id="cb40-7"><a href="#cb40-7" aria-hidden="true" tabindex="-1"></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> 507 + <span id="cb40-8"><a href="#cb40-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> (<span class="dv">1</span><span class="op">..=</span><span class="dv">10</span>)</span> 508 + <span id="cb40-9"><a href="#cb40-9" aria-hidden="true" tabindex="-1"></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> 509 + <span id="cb40-10"><a href="#cb40-10" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>collect()<span class="op">;</span></span> 510 + <span id="cb40-11"><a href="#cb40-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 511 511 <h3 id="notes">Notes</h3> 512 512 <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> 513 - <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> 514 - <span id="cb41-2"><a href="#cb41-2" aria-hidden="true"></a><span class="kw">let</span> g<span class="op">;</span></span> 515 - <span id="cb41-3"><a href="#cb41-3" aria-hidden="true"></a><span class="op">{</span></span> 516 - <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> 517 - <span id="cb41-5"><a href="#cb41-5" aria-hidden="true"></a> g <span class="op">=</span> v(x)<span class="op">;</span></span> 518 - <span id="cb41-6"><a href="#cb41-6" aria-hidden="true"></a><span class="op">}</span></span> 519 - <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> 513 + <div class="sourceCode" id="cb41"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> v <span class="op">=</span> add(<span class="dv">5</span>)<span class="op">;</span></span> 514 + <span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> g<span class="op">;</span></span> 515 + <span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="op">{</span></span> 516 + <span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> x <span class="op">=</span> <span class="dv">5</span><span class="op">;</span></span> 517 + <span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a> g <span class="op">=</span> v(x)<span class="op">;</span></span> 518 + <span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 519 + <span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></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> 520 520 <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> 521 521 <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> 522 522 <h3 id="conclusion">Conclusion</h3>
+70 -70
docs/posts/gripes_with_go/index.html
··· 54 54 <h3 id="lack-of-sum-types">Lack of Sum types</h3> 55 55 <p>A “Sum” type is a data type that can hold one of many states at a given time, similar to how a boolean can hold a true or a false, not too different from an <code>enum</code> type in C. Go lacks <code>enum</code> types unfortunately, and you are forced to resort to crafting your own substitute.</p> 56 56 <p>A type to represent gender for example:</p> 57 - <div class="sourceCode" id="cb1"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="kw">type</span> Gender <span class="dt">int</span></span> 58 - <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a></span> 59 - <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="kw">const</span> (</span> 60 - <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> Male Gender = <span class="ot">iota</span> <span class="co">// assigns Male to 0</span></span> 61 - <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> Female <span class="co">// assigns Female to 1</span></span> 62 - <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a> Other <span class="co">// assigns Other to 2</span></span> 63 - <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a>)</span> 64 - <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a></span> 65 - <span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a>fmt.Println(<span class="st">&quot;My gender is &quot;</span>, Male)</span> 66 - <span id="cb1-10"><a href="#cb1-10" aria-hidden="true"></a><span class="co">// My gender is 0</span></span> 67 - <span id="cb1-11"><a href="#cb1-11" aria-hidden="true"></a><span class="co">// Oops! We have to implement String() for Gender ...</span></span> 68 - <span id="cb1-12"><a href="#cb1-12" aria-hidden="true"></a></span> 69 - <span id="cb1-13"><a href="#cb1-13" aria-hidden="true"></a><span class="kw">func</span> (g Gender) String() <span class="dt">string</span> {</span> 70 - <span id="cb1-14"><a href="#cb1-14" aria-hidden="true"></a> <span class="kw">switch</span> (g) {</span> 71 - <span id="cb1-15"><a href="#cb1-15" aria-hidden="true"></a> <span class="kw">case</span> <span class="dv">0</span>: <span class="kw">return</span> <span class="st">&quot;Male&quot;</span></span> 72 - <span id="cb1-16"><a href="#cb1-16" aria-hidden="true"></a> <span class="kw">case</span> <span class="dv">1</span>: <span class="kw">return</span> <span class="st">&quot;Female&quot;</span></span> 73 - <span id="cb1-17"><a href="#cb1-17" aria-hidden="true"></a> <span class="kw">default</span>: <span class="kw">return</span> <span class="st">&quot;Other&quot;</span></span> 74 - <span id="cb1-18"><a href="#cb1-18" aria-hidden="true"></a> }</span> 75 - <span id="cb1-19"><a href="#cb1-19" aria-hidden="true"></a>}</span> 76 - <span id="cb1-20"><a href="#cb1-20" aria-hidden="true"></a></span> 77 - <span id="cb1-21"><a href="#cb1-21" aria-hidden="true"></a><span class="co">// You can accidentally do stupid stuff like:</span></span> 78 - <span id="cb1-22"><a href="#cb1-22" aria-hidden="true"></a>gender := Male + <span class="dv">1</span></span></code></pre></div> 57 + <div class="sourceCode" id="cb1"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> Gender <span class="dt">int</span></span> 58 + <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span> 59 + <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> <span class="op">(</span></span> 60 + <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> Male Gender <span class="op">=</span> <span class="ot">iota</span> <span class="co">// assigns Male to 0</span></span> 61 + <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> Female <span class="co">// assigns Female to 1</span></span> 62 + <span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> Other <span class="co">// assigns Other to 2</span></span> 63 + <span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="op">)</span></span> 64 + <span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span> 65 + <span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>fmt<span class="op">.</span>Println<span class="op">(</span><span class="st">&quot;My gender is &quot;</span><span class="op">,</span> Male<span class="op">)</span></span> 66 + <span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="co">// My gender is 0</span></span> 67 + <span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co">// Oops! We have to implement String() for Gender ...</span></span> 68 + <span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a></span> 69 + <span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="kw">func</span> <span class="op">(</span>g Gender<span class="op">)</span> String<span class="op">()</span> <span class="dt">string</span> <span class="op">{</span></span> 70 + <span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">switch</span> <span class="op">(</span>g<span class="op">)</span> <span class="op">{</span></span> 71 + <span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> <span class="dv">0</span><span class="op">:</span> <span class="kw">return</span> <span class="st">&quot;Male&quot;</span></span> 72 + <span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a> <span class="kw">case</span> <span class="dv">1</span><span class="op">:</span> <span class="kw">return</span> <span class="st">&quot;Female&quot;</span></span> 73 + <span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a> <span class="kw">default</span><span class="op">:</span> <span class="kw">return</span> <span class="st">&quot;Other&quot;</span></span> 74 + <span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 75 + <span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 76 + <span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a></span> 77 + <span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a><span class="co">// You can accidentally do stupid stuff like:</span></span> 78 + <span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a>gender <span class="op">:=</span> Male <span class="op">+</span> <span class="dv">1</span></span></code></pre></div> 79 79 <p>The Haskell equivalent of the same:</p> 80 - <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a><span class="kw">data</span> <span class="dt">Gender</span> <span class="ot">=</span> <span class="dt">Male</span></span> 81 - <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> <span class="op">|</span> <span class="dt">Female</span></span> 82 - <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="op">|</span> <span class="dt">Other</span></span> 83 - <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">deriving</span> (<span class="dt">Show</span>)</span> 84 - <span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a></span> 85 - <span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a><span class="fu">print</span> <span class="op">$</span> <span class="st">&quot;My gender is &quot;</span> <span class="op">++</span> (<span class="fu">show</span> <span class="dt">Male</span>)</span></code></pre></div> 80 + <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Gender</span> <span class="ot">=</span> <span class="dt">Male</span></span> 81 + <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="dt">Female</span></span> 82 + <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="op">|</span> <span class="dt">Other</span></span> 83 + <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">deriving</span> (<span class="dt">Show</span>)</span> 84 + <span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a></span> 85 + <span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span> <span class="op">$</span> <span class="st">&quot;My gender is &quot;</span> <span class="op">++</span> (<span class="fu">show</span> <span class="dt">Male</span>)</span></code></pre></div> 86 86 <h3 id="type-assertions">Type Assertions</h3> 87 87 <p>A downcast with an optional error check? What could go wrong?</p> 88 88 <p>Type assertions in Go allow you to do:</p> 89 - <div class="sourceCode" id="cb3"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="kw">var</span> x <span class="kw">interface</span>{} = <span class="dv">7</span></span> 90 - <span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a>y, goodToGo := x.(<span class="dt">int</span>)</span> 91 - <span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a><span class="kw">if</span> goodToGo {</span> 92 - <span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a> fmt.Println(y)</span> 93 - <span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a>}</span></code></pre></div> 89 + <div class="sourceCode" id="cb3"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">var</span> x <span class="kw">interface</span><span class="op">{}</span> <span class="op">=</span> <span class="dv">7</span></span> 90 + <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">,</span> goodToGo <span class="op">:=</span> x<span class="op">.(</span><span class="dt">int</span><span class="op">)</span></span> 91 + <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="kw">if</span> goodToGo <span class="op">{</span></span> 92 + <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> fmt<span class="op">.</span>Println<span class="op">(</span>y<span class="op">)</span></span> 93 + <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 94 94 <p>The error check however is optional:</p> 95 - <div class="sourceCode" id="cb4"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a><span class="kw">var</span> x <span class="kw">interface</span>{} = <span class="dv">7</span></span> 96 - <span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a><span class="kw">var</span> y := x.(<span class="dt">float64</span>)</span> 97 - <span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a>fmt.Println(y)</span> 98 - <span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a><span class="co">// results in a runtime error:</span></span> 99 - <span id="cb4-5"><a href="#cb4-5" aria-hidden="true"></a><span class="co">// panic: interface conversion: interface {} is int, not float64</span></span></code></pre></div> 95 + <div class="sourceCode" id="cb4"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">var</span> x <span class="kw">interface</span><span class="op">{}</span> <span class="op">=</span> <span class="dv">7</span></span> 96 + <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="kw">var</span> y <span class="op">:=</span> x<span class="op">.(</span><span class="dt">float64</span><span class="op">)</span></span> 97 + <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>fmt<span class="op">.</span>Println<span class="op">(</span>y<span class="op">)</span></span> 98 + <span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="co">// results in a runtime error:</span></span> 99 + <span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="co">// panic: interface conversion: interface {} is int, not float64</span></span></code></pre></div> 100 100 <h3 id="date-and-time">Date and Time</h3> 101 101 <p>Anyone that has written Go previously, will probably already know what I am getting at here. For the uninitiated, parsing and formatting dates in Go requires a “layout”. This “layout” is based on magical reference date:</p> 102 102 <pre><code>Mon Jan 2 15:04:05 MST 2006</code></pre> 103 103 <p>Which is the date produced when you write the first seven natural numbers like so:</p> 104 104 <pre><code>01/02 03:04:05 &#39;06 -0700</code></pre> 105 105 <p>Parsing a string in <code>YYYY-MM-DD</code> format would look something like:</p> 106 - <div class="sourceCode" id="cb7"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true"></a><span class="kw">const</span> layout = <span class="st">&quot;2006-01-02&quot;</span></span> 107 - <span id="cb7-2"><a href="#cb7-2" aria-hidden="true"></a>time.Parse(layout, <span class="st">&quot;2020-08-01&quot;</span>)</span></code></pre></div> 106 + <div class="sourceCode" id="cb7"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> layout <span class="op">=</span> <span class="st">&quot;2006-01-02&quot;</span></span> 107 + <span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>time<span class="op">.</span>Parse<span class="op">(</span>layout<span class="op">,</span> <span class="st">&quot;2020-08-01&quot;</span><span class="op">)</span></span></code></pre></div> 108 108 <p>This so-called “intuitive” method of formatting dates doesn’t allow you to print <code>0000 hrs</code> as <code>2400 hrs</code>, it doesn’t allow you to omit the leading zero in 24 hour formats. It is rife with inconveniences, if only there were a <a href="https://man7.org/linux/man-pages/man3/strftime.3.html">tried and tested</a> date formatting convention …</p> 109 109 <h3 id="statements-over-expressions">Statements over Expressions</h3> 110 110 <p>Statements have side effects, expressions return values. More often than not, expressions are easier to understand at a glance: evaluate the LHS and assign the same to the RHS.</p> 111 111 <p>Rust allows you to create local namespaces, and treats blocks (<code>{}</code>) as expressions:</p> 112 - <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">let</span> twenty_seven <span class="op">=</span> <span class="op">{</span></span> 113 - <span id="cb8-2"><a href="#cb8-2" aria-hidden="true"></a> <span class="kw">let</span> three <span class="op">=</span> <span class="dv">1</span> <span class="op">+</span> <span class="dv">2</span><span class="op">;</span></span> 114 - <span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a> <span class="kw">let</span> nine <span class="op">=</span> three <span class="op">*</span> three<span class="op">;</span></span> 115 - <span id="cb8-4"><a href="#cb8-4" aria-hidden="true"></a> nine <span class="op">*</span> three</span> 116 - <span id="cb8-5"><a href="#cb8-5" aria-hidden="true"></a><span class="op">};</span></span></code></pre></div> 112 + <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> twenty_seven <span class="op">=</span> <span class="op">{</span></span> 113 + <span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> three <span class="op">=</span> <span class="dv">1</span> <span class="op">+</span> <span class="dv">2</span><span class="op">;</span></span> 114 + <span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> nine <span class="op">=</span> three <span class="op">*</span> three<span class="op">;</span></span> 115 + <span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> nine <span class="op">*</span> three</span> 116 + <span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div> 117 117 <p>The Go equivalent of the same:</p> 118 - <div class="sourceCode" id="cb9"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true"></a>twenty_seven := <span class="ot">nil</span></span> 119 - <span id="cb9-2"><a href="#cb9-2" aria-hidden="true"></a></span> 120 - <span id="cb9-3"><a href="#cb9-3" aria-hidden="true"></a>three := <span class="dv">1</span> + <span class="dv">2</span></span> 121 - <span id="cb9-4"><a href="#cb9-4" aria-hidden="true"></a>nine := three * three</span> 122 - <span id="cb9-5"><a href="#cb9-5" aria-hidden="true"></a>twenty_seven = nine * three</span></code></pre></div> 118 + <div class="sourceCode" id="cb9"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>twenty_seven <span class="op">:=</span> <span class="ot">nil</span></span> 119 + <span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a></span> 120 + <span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>three <span class="op">:=</span> <span class="dv">1</span> <span class="op">+</span> <span class="dv">2</span></span> 121 + <span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a>nine <span class="op">:=</span> three <span class="op">*</span> three</span> 122 + <span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a>twenty_seven <span class="op">=</span> nine <span class="op">*</span> three</span></code></pre></div> 123 123 <h3 id="erroring-out-on-unused-variables">Erroring out on unused variables</h3> 124 124 <p>Want to quickly prototype something? Go says no! In all seriousness, a warning would suffice, I don’t want to have to go back and comment each unused import out, only to come back and uncomment them a few seconds later.</p> 125 125 <h3 id="error-handling">Error handling</h3> 126 - <div class="sourceCode" id="cb10"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true"></a><span class="kw">if</span> err != <span class="ot">nil</span> { ... }</span></code></pre></div> 126 + <div class="sourceCode" id="cb10"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">if</span> err <span class="op">!=</span> <span class="ot">nil</span> <span class="op">{</span> <span class="op">...</span> <span class="op">}</span></span></code></pre></div> 127 127 <p>Need I say more? I will, for good measure:</p> 128 128 <ol type="1"> 129 129 <li>Error handling is optional</li> 130 130 <li>Errors are propagated via a clunky <code>if</code> + <code>return</code> statement</li> 131 131 </ol> 132 132 <p>I prefer Haskell’s “Monadic” error handling, which is employed by Rust as well:</p> 133 - <div class="sourceCode" id="cb11"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true"></a><span class="co">// 1. error handling is compulsory</span></span> 134 - <span id="cb11-2"><a href="#cb11-2" aria-hidden="true"></a><span class="co">// 2. errors are propagated with the `?` operator</span></span> 135 - <span id="cb11-3"><a href="#cb11-3" aria-hidden="true"></a><span class="kw">fn</span> foo() <span class="op">-&gt;</span> <span class="dt">Result</span><span class="op">&lt;</span><span class="dt">String</span><span class="op">,</span> <span class="pp">io::</span><span class="bu">Error</span><span class="op">&gt;</span> <span class="op">{</span></span> 136 - <span id="cb11-4"><a href="#cb11-4" aria-hidden="true"></a> <span class="kw">let</span> <span class="kw">mut</span> f <span class="op">=</span> <span class="pp">File::</span>open(<span class="st">&quot;foo.txt&quot;</span>)<span class="op">?;</span> <span class="co">// return if error</span></span> 137 - <span id="cb11-5"><a href="#cb11-5" aria-hidden="true"></a> <span class="kw">let</span> <span class="kw">mut</span> s <span class="op">=</span> <span class="dt">String</span><span class="pp">::</span>new()<span class="op">;</span></span> 138 - <span id="cb11-6"><a href="#cb11-6" aria-hidden="true"></a></span> 139 - <span id="cb11-7"><a href="#cb11-7" aria-hidden="true"></a> f<span class="op">.</span>read_to_string(<span class="op">&amp;</span><span class="kw">mut</span> s)<span class="op">?;</span> <span class="co">// return if error</span></span> 140 - <span id="cb11-8"><a href="#cb11-8" aria-hidden="true"></a></span> 141 - <span id="cb11-9"><a href="#cb11-9" aria-hidden="true"></a> <span class="cn">Ok</span>(s) <span class="co">// all good, return a string inside a `Result` context</span></span> 142 - <span id="cb11-10"><a href="#cb11-10" aria-hidden="true"></a><span class="op">}</span></span> 143 - <span id="cb11-11"><a href="#cb11-11" aria-hidden="true"></a></span> 144 - <span id="cb11-12"><a href="#cb11-12" aria-hidden="true"></a><span class="kw">fn</span> main() <span class="op">{</span></span> 145 - <span id="cb11-13"><a href="#cb11-13" aria-hidden="true"></a> <span class="co">// `contents` is an enum known as Result:</span></span> 146 - <span id="cb11-14"><a href="#cb11-14" aria-hidden="true"></a> <span class="kw">let</span> contents <span class="op">=</span> foo()<span class="op">;</span></span> 147 - <span id="cb11-15"><a href="#cb11-15" aria-hidden="true"></a> <span class="kw">match</span> contents <span class="op">{</span></span> 148 - <span id="cb11-16"><a href="#cb11-16" aria-hidden="true"></a> <span class="cn">Ok</span>(c) <span class="op">=&gt;</span> <span class="pp">println!</span>(c)<span class="op">,</span></span> 149 - <span id="cb11-17"><a href="#cb11-17" aria-hidden="true"></a> <span class="cn">Err</span>(e) <span class="op">=&gt;</span> <span class="pp">eprintln!</span>(e)</span> 150 - <span id="cb11-18"><a href="#cb11-18" aria-hidden="true"></a> <span class="op">}</span></span> 151 - <span id="cb11-19"><a href="#cb11-19" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> 133 + <div class="sourceCode" id="cb11"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="co">// 1. error handling is compulsory</span></span> 134 + <span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="co">// 2. errors are propagated with the `?` operator</span></span> 135 + <span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> foo() <span class="op">-&gt;</span> <span class="dt">Result</span><span class="op">&lt;</span><span class="dt">String</span><span class="op">,</span> <span class="pp">io::</span><span class="bu">Error</span><span class="op">&gt;</span> <span class="op">{</span></span> 136 + <span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> <span class="kw">mut</span> f <span class="op">=</span> <span class="pp">File::</span>open(<span class="st">&quot;foo.txt&quot;</span>)<span class="op">?;</span> <span class="co">// return if error</span></span> 137 + <span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> <span class="kw">mut</span> s <span class="op">=</span> <span class="dt">String</span><span class="pp">::</span>new()<span class="op">;</span></span> 138 + <span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a></span> 139 + <span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a> f<span class="op">.</span>read_to_string(<span class="op">&amp;</span><span class="kw">mut</span> s)<span class="op">?;</span> <span class="co">// return if error</span></span> 140 + <span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a></span> 141 + <span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a> <span class="cn">Ok</span>(s) <span class="co">// all good, return a string inside a `Result` context</span></span> 142 + <span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span> 143 + <span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a></span> 144 + <span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> main() <span class="op">{</span></span> 145 + <span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a> <span class="co">// `contents` is an enum known as Result:</span></span> 146 + <span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> contents <span class="op">=</span> foo()<span class="op">;</span></span> 147 + <span id="cb11-15"><a href="#cb11-15" aria-hidden="true" tabindex="-1"></a> <span class="kw">match</span> contents <span class="op">{</span></span> 148 + <span id="cb11-16"><a href="#cb11-16" aria-hidden="true" tabindex="-1"></a> <span class="cn">Ok</span>(c) <span class="op">=&gt;</span> <span class="pp">println!</span>(c)<span class="op">,</span></span> 149 + <span id="cb11-17"><a href="#cb11-17" aria-hidden="true" tabindex="-1"></a> <span class="cn">Err</span>(e) <span class="op">=&gt;</span> <span class="pp">eprintln!</span>(e)</span> 150 + <span id="cb11-18"><a href="#cb11-18" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span> 151 + <span id="cb11-19"><a href="#cb11-19" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> 152 152 <h3 id="conclusion">Conclusion</h3> 153 153 <p>I did not want to conclude without talking about stylistic choices, lack of metaprogramming, bizzare export rules, but, I am too busy converting my <code>interface{}</code> types into actual generic code for Go v2.</p> 154 154
+4 -4
docs/posts/nixOS/index.html
··· 58 58 <p>Builds may be generated by specifying a <code>default.nix</code> file, and running <code>nix-build</code>. Conventional package managers require you to specify a dependency list, but there is no guarantee that this list is complete. The package will build on your machine even if you forget a dependency. However, with Nix, packages are installed to <code>/nix/store</code>, and not global paths such as <code>/usr/bin/...</code>, if your project builds, it means you have included every last one.</p> 59 59 <p>Issues on most my projects have been “unable to build because <code>libxcb</code> is missing”, or “this version of <code>openssl</code> is too old”. Tools like <code>cargo</code> and <code>pip</code> are poor package managers. While they <em>can</em> guarantee that Rust or Python dependencies are met, they make assumptions about the target system.</p> 60 60 <p>For example, <a href="https://github.com/nerdypepper/site">this website</a> is now built using Nix, anyone using Nix may simply, clone the repository and run <code>./generate.sh</code>, and it would <em>just work</em>, while keeping your global namespace clean™:</p> 61 - <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">#! /usr/bin/env nix-shell</span></span> 62 - <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a><span class="co">#! nix-shell -i bash -p eva pandoc esh</span></span> 63 - <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a></span> 64 - <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a><span class="co"># some bash magic ;)</span></span></code></pre></div> 61 + <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">#! /usr/bin/env nix-shell</span></span> 62 + <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co">#! nix-shell -i bash -p eva pandoc esh</span></span> 63 + <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a></span> 64 + <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co"># some bash magic ;)</span></span></code></pre></div> 65 65 <p>Dependencies are included with the <code>-p</code> flag, the shell script is executed with an interpreter, specified with the <code>-i</code> flag.</p> 66 66 <h3 id="impressions">Impressions</h3> 67 67 <p>NixOS is by no means, simple. As a newcomer, using Nix was not easy, heck, I had to learn a purely functional, lazy language to just build programs. There is a lot to be desired on the tooling front as well. A well fleshed out LSP plugin would be nice (<a href="https://github.com/nix-community/rnix-lsp">rnix-lsp looks promising</a>).</p>
+25 -25
docs/posts/rapid_refactoring_with_vim/index.html
··· 44 44 <div class="post-text"> 45 45 <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> 46 46 <p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> 47 - <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> 48 - <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a> </span> 49 - <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> 50 - <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 51 - <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> 52 - <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 class="op">,</span></span> 53 - <span id="cb1-7"><a href="#cb1-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> 54 - <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a> )<span class="op">;</span></span> 55 - <span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> 47 + <div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_str<span class="op">};</span></span> 48 + <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> </span> 49 + <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="at">#[</span>test<span class="at">]</span></span> 50 + <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 51 + <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="pp">assert_eq!</span>(</span> 52 + <span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></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 class="op">,</span></span> 53 + <span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></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> 54 + <span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> )<span class="op">;</span></span> 55 + <span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div> 56 56 <p>had to be converted to:</p> 57 - <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> 58 - <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> </span> 59 - <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span> 60 - <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 61 - <span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span> 62 - <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> 63 - <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> 64 - <span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a> )<span class="op">;</span></span> 65 - <span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div> 57 + <div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_value<span class="op">};</span></span> 58 + <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> </span> 59 + <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="at">#[</span>test<span class="at">]</span></span> 60 + <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 61 + <span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="pp">assert_eq!</span>(</span> 62 + <span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></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> 63 + <span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></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> 64 + <span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> )<span class="op">;</span></span> 65 + <span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div> 66 66 <h3 id="the-arglist">The arglist</h3> 67 67 <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> 68 - <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> 69 - <span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a><span class="co"># matching the pattern</span></span> 70 - <span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a></span> 71 - <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> 72 - <span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a></span> 73 - <span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a><span class="co"># expands to something like:</span></span> 74 - <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> 68 + <div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># `grep -l pattern files` lists all the files</span></span> 69 + <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co"># matching the pattern</span></span> 70 + <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span> 71 + <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="ex">vim</span> <span class="va">$(</span><span class="fu">grep</span> <span class="at">-l</span> <span class="st">&#39;cfg\(test\)&#39;</span> ./<span class="pp">**</span>/<span class="pp">*</span>.rs<span class="va">)</span></span> 72 + <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a></span> 73 + <span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co"># expands to something like:</span></span> 74 + <span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="ex">vim</span> push_rules.rs room/member.rs key/verification/lib.rs</span></code></pre></div> 75 75 <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> 76 76 <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> 77 77 <pre><code>:argdo s/from_str/from_value/g</code></pre>
+10 -10
docs/posts/self-hosting_git/index.html
··· 47 47 <p>cgit is <em>very</em> bare bones. It is <a href="https://tools.ietf.org/html/rfc3875">cgi-based</a> web interface to git, and nothing more. You may browse repositories, view diffs, commit logs and even clone via http. If you are looking to replace Github with cgit, keep in mind that cgit does not handle issues or pull/merge requests. If people wish to contribute to your work, they would have to send you a patch via email.</p> 48 48 <h3 id="setup">Setup</h3> 49 49 <p>Installing cgit is fairly straightforward, if you would like to compile it from source:</p> 50 - <div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="co"># fetch</span></span> 51 - <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a><span class="fu">git</span> clone https://git.zx2c4.com <span class="kw">&amp;&amp;</span> <span class="bu">cd</span> cgit</span> 52 - <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="fu">git</span> submodule init</span> 53 - <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a><span class="fu">git</span> submodule update</span> 54 - <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a></span> 55 - <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a><span class="co"># install</span></span> 56 - <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a><span class="fu">make</span> NO_LUA=1</span> 57 - <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="fu">sudo</span> make install</span></code></pre></div> 50 + <div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># fetch</span></span> 51 + <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> clone https://git.zx2c4.com <span class="kw">&amp;&amp;</span> <span class="bu">cd</span> cgit</span> 52 + <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> submodule init</span> 53 + <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> submodule update</span> 54 + <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span> 55 + <span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co"># install</span></span> 56 + <span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="fu">make</span> NO_LUA=1</span> 57 + <span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> make install</span></code></pre></div> 58 58 <p>This would drop the cgit cgi script (and the default css) into <code>/var/www/htdocs/cgit</code>. You may configure cgit by editing <code>/etc/cgitrc</code>. I specify the <code>NO_LUA</code> flag to compile without lua support, exclude that flag if you would like to extend cgit via lua scripts.</p> 59 59 <h3 id="going-live">Going live</h3> 60 60 <p>You might want to use, <a href="https://github.com/gnosek/fcgiwrap">fcgiwrap</a>, a <a href="http://www.nongnu.org/fastcgi">fastcgi</a> wrapper for <code>cgi</code> scripts,</p> 61 - <div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a><span class="fu">sudo</span> apt install fcgiwrap</span> 62 - <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a><span class="fu">sudo</span> systemctl start fcgiwrap.socket</span></code></pre></div> 61 + <div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> apt install fcgiwrap</span> 62 + <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sudo</span> systemctl start fcgiwrap.socket</span></code></pre></div> 63 63 <p>Expose the cgit cgi script to the web via <code>nginx</code>:</p> 64 64 <pre><code># nginx.conf 65 65 server {
+18 -18
docs/posts/static_sites_with_bash/index.html
··· 47 47 <p>I chose to write in markdown, and convert to html with <a href="https://kristaps.bsd.lv/lowdown/">lowdown</a>.</p> 48 48 <h3 id="directory-structure">Directory structure</h3> 49 49 <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> 50 - <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="ex">chronological</span> order!</span> 51 - <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> 52 - <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="kw">`</span><span class="fu">ls</span><span class="kw">`</span> <span class="ex">mangled</span> our file paths</span> 53 - <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> 54 - <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a></span> 55 - <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> 56 - <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> 57 - <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="kw">done</span></span></code></pre></div> 50 + <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="va">posts</span><span class="op">=</span><span class="va">$(</span><span class="fu">ls</span> <span class="at">-t</span> ./posts<span class="va">)</span> <span class="co"># chronological order!</span></span> 51 + <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> f <span class="kw">in</span> <span class="va">$posts</span><span class="kw">;</span> <span class="cf">do</span></span> 52 + <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="va">file</span><span class="op">=</span><span class="st">&quot;./posts/&quot;</span><span class="va">$f</span> <span class="co"># `ls` mangled our file paths</span></span> 53 + <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">&quot;generating post </span><span class="va">$file</span><span class="st">&quot;</span></span> 54 + <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span> 55 + <span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> <span class="va">html</span><span class="op">=</span><span class="va">$(</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> 56 + <span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="at">-e</span> <span class="st">&quot;html&quot;</span> <span class="op">&gt;&gt;</span> docs/index.html</span> 57 + <span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="cf">done</span></span></code></pre></div> 58 58 <h3 id="assets">Assets</h3> 59 59 <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> 60 - <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> 61 - <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> 62 - <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a></span> 63 - <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a><span class="co"># generate a random file name</span></span> 64 - <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> 65 - <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> 66 - <span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a></span> 67 - <span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a><span class="co"># copy to my file host</span></span> 68 - <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> 69 - <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> 60 + <div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># strip file extension</span></span> 61 + <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="va">ext</span><span class="op">=</span><span class="st">&quot;</span><span class="va">${1</span><span class="op">##</span><span class="pp">*</span>.<span class="va">}</span><span class="st">&quot;</span></span> 62 + <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a></span> 63 + <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co"># generate a random file name</span></span> 64 + <span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="va">id</span><span class="op">=</span><span class="va">$(</span> <span class="fu">cat</span> /dev/urandom <span class="kw">|</span> <span class="fu">tr</span> <span class="at">-dc</span> <span class="st">&#39;a-zA-Z0-9&#39;</span> <span class="kw">|</span> <span class="ex">fold</span> <span class="at">-w</span> 2 <span class="kw">|</span> <span class="fu">head</span> <span class="at">-n</span> 1 <span class="va">)</span></span> 65 + <span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="va">id</span><span class="op">=</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> 66 + <span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a></span> 67 + <span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="co"># copy to my file host</span></span> 68 + <span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="fu">scp</span> <span class="at">-P</span> 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> 69 + <span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></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> 70 70 <h3 id="templating">Templating</h3> 71 71 <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> 72 72