$$ % Typography and symbols

ewcommand{\msf}[1]{\mathsf{#1}}

ewcommand{\ctx}{\Gamma}

ewcommand{\qamp}{&\quad}

ewcommand{\qqamp}{&&\quad}

ewcommand{\Coloneqq}{::=}

ewcommand{\proves}{\vdash}

ewcommand{\star}[1]{#1^{*}}

ewcommand{\eps}{\varepsilon}

ewcommand{

ul}{\varnothing}

ewcommand{\brc}[1]{\{{#1}\}}

ewcommand{\binopm}[2]{#1~\bar{\oplus}~#2}

ewcommand{\mag}[1]{|{#1}|}

ewcommand{\aequiv}{\equiv_\alpha}

ewcommand{\semi}[2]{{#1};~{#2}} % Untyped lambda calculus

ewcommand{\fun}[2]{\lambda ~ {#1} ~ . ~ {#2}}

ewcommand{\app}[2]{#1 ~ #2}

ewcommand{\fix}[3]{\msf{fix}~({#1} : {#2}) ~ . ~ #3 }

ewcommand{\truet}{\msf{true}}

ewcommand{\falset}{\msf{false}}

ewcommand{\define}[2]{{#1} \triangleq {#2}} % Typed lambda calculus - expressions

ewcommand{\funt}[3]{\lambda ~ \left(#1 : #2\right) ~ . ~ #3}

ewcommand{\lett}[4]{\msf{let} ~ \hasType{#1}{#2} = #3 ~ \msf{in} ~ #4}

ewcommand{\letrec}[4]{\msf{letrec} ~ \hasType{#1}{#2} = #3 ~ \msf{in} ~ #4}a

ewcommand{\ift}[3]{\msf{if} ~ {#1} ~ \msf{then} ~ {#2} ~ \msf{else} ~ {#3}}

ewcommand{\rec}[5]{\msf{rec}(#1; ~ #2.#3.#4)(#5)}

ewcommand{\case}[5]{\msf{case} ~ {#1} ~ \{ L(#2) \to #3 \mid R(#4) \to #5 \}}

ewcommand{\pair}[2]{\left({#1},~{#2}\right)}

ewcommand{\proj}[2]{#1 . #2}

ewcommand{\inj}[3]{\msf{inj} ~ #1 = #2 ~ \msf{as} ~ #3}

ewcommand{\letv}[3]{\msf{let} ~ {#1} = {#2} ~ \msf{in} ~ {#3}}

ewcommand{\fold}[2]{\msf{fold}~{#1}~\msf{as}~{#2}}

ewcommand{\unfold}[1]{\msf{unfold}~{#1}}

ewcommand{\poly}[2]{\Lambda~{#1}~.~ #2}

ewcommand{\polyapp}[2]{{#1}~\left[{#2}\right]}

ewcommand{\export}[3]{\msf{export}~ #1 ~\msf{without}~{#2}~\msf{as}~ #3}

ewcommand{\import}[4]{\msf{import} ~ ({#1}, {#2}) = {#3} ~ \msf{in} ~ #4} % Typed lambda calculus - types

ewcommand{\tnum}{\msf{num}}

ewcommand{\tstr}{\msf{string}}

ewcommand{\tint}{\msf{int}}

ewcommand{\tbool}{\msf{bool}}

ewcommand{\tfun}[2]{#1 \rightarrow #2}

ewcommand{\tprod}[2]{#1 \times #2}

ewcommand{\tsum}[2]{#1 + #2}

ewcommand{\trec}[2]{\mu~{#1}~.~{#2}}

ewcommand{\tvoid}{\msf{void}}

ewcommand{\tunit}{\msf{unit}}

ewcommand{\tpoly}[2]{\forall~{#1}~.~{#2}}

ewcommand{\tmod}[2]{\exists ~ {#1} ~ . ~ #2} % WebAssembly

ewcommand{\wconst}[1]{\msf{i32.const}~{#1}}

ewcommand{\wbinop}[1]{\msf{i32}.{#1}}

ewcommand{\wgetlocal}[1]{\msf{get\_local}~{#1}}

ewcommand{\wsetlocal}[1]{\msf{set\_local}~{#1}}

ewcommand{\wgetglobal}[1]{\msf{get\_global}~{#1}}

ewcommand{\wsetglobal}[1]{\msf{set\_global}~{#1}}

ewcommand{\wload}{\msf{i32.load}}

ewcommand{\wstore}{\msf{i32.store}}

ewcommand{\wsize}{\msf{memory.size}}

ewcommand{\wgrow}{\msf{memory.grow}}

ewcommand{\wunreachable}{\msf{unreachable}}

ewcommand{\wblock}[1]{\msf{block}~{#1}}

ewcommand{\wloop}[1]{\msf{loop}~{#1}}

ewcommand{\wbr}[1]{\msf{br}~{#1}}

ewcommand{\wbrif}[1]{\msf{br\_if}~{#1}}

ewcommand{\wreturn}{\msf{return}}

ewcommand{\wcall}[1]{\msf{call}~{#1}}

ewcommand{\wlabel}[2]{\msf{label}~\{#1\}~{#2}}

ewcommand{\wframe}[2]{\msf{frame}~({#1}, {#2})}

ewcommand{\wtrapping}{\msf{trapping}}

ewcommand{\wbreaking}[1]{\msf{breaking}~{#1}}

ewcommand{\wreturning}[1]{\msf{returning}~{#1}}

ewcommand{\wconfig}[5]{\{\msf{module}{:}~{#1};~\msf{mem}{:}~{#2};~\msf{locals}{:}~{#3};~\msf{stack}{:}~{#4};~\msf{instrs}{:}~{#5}\}}

ewcommand{\wfunc}[4]{\{\msf{params}{:}~{#1};~\msf{locals}{:}~{#2};~\msf{return}~{#3};~\msf{body}{:}~{#4}\}}

ewcommand{\wmodule}[1]{\{\msf{funcs}{:}~{#1}\}}

ewcommand{\wcg}{\msf{globals}}

ewcommand{\wcf}{\msf{funcs}}

ewcommand{\wci}{\msf{instrs}}

ewcommand{\wcs}{\msf{stack}}

ewcommand{\wcl}{\msf{locals}}

ewcommand{\wclab}{\msf{labels}}

ewcommand{\wcm}{\msf{mem}}

ewcommand{\wcmod}{\msf{module}}

ewcommand{\wsteps}[2]{\steps{\brc{#1}}{\brc{#2}}}

ewcommand{\with}{\underline{\msf{with}}}

ewcommand{\wvalid}[2]{{#1} \vdash {#2}~\msf{valid}}

ewcommand{\wif}[2]{\msf{if}~{#1}~{\msf{else}}~{#2}}

ewcommand{\wfor}[4]{\msf{for}~(\msf{init}~{#1})~(\msf{cond}~{#2})~(\msf{post}~{#3})~{#4}} % assign4.3 custom

ewcommand{\wtry}[2]{\msf{try}~{#1}~\msf{catch}~{#2}}

ewcommand{\wraise}{\msf{raise}}

ewcommand{\wraising}[1]{\msf{raising}~{#1}}

ewcommand{\wconst}[1]{\msf{i32.const}~{#1}}

ewcommand{\wbinop}[1]{\msf{i32}.{#1}}

ewcommand{\wgetlocal}[1]{\msf{get\_local}~{#1}}

ewcommand{\wsetlocal}[1]{\msf{set\_local}~{#1}}

ewcommand{\wgetglobal}[1]{\msf{get\_global}~{#1}}

ewcommand{\wsetglobal}[1]{\msf{set\_global}~{#1}}

ewcommand{\wload}{\msf{i32.load}}

ewcommand{\wstore}{\msf{i32.store}}

ewcommand{\wsize}{\msf{memory.size}}

ewcommand{\wgrow}{\msf{memory.grow}}

ewcommand{\wunreachable}{\msf{unreachable}}

ewcommand{\wblock}[1]{\msf{block}~{#1}}

ewcommand{\wloop}[1]{\msf{loop}~{#1}}

ewcommand{\wbr}[1]{\msf{br}~{#1}}

ewcommand{\wbrif}[1]{\msf{br\_if}~{#1}}

ewcommand{\wreturn}{\msf{return}}

ewcommand{\wcall}[1]{\msf{call}~{#1}}

ewcommand{\wlabel}[2]{\msf{label}~\{#1\}~{#2}}

ewcommand{\wframe}[2]{\msf{frame}~({#1}, {#2})}

ewcommand{\wtrapping}{\msf{trapping}}

ewcommand{\wbreaking}[1]{\msf{breaking}~{#1}}

ewcommand{\wreturning}[1]{\msf{returning}~{#1}}

ewcommand{\wconfig}[5]{\{\msf{module}{:}~{#1};~\msf{mem}{:}~{#2};~\msf{locals}{:}~{#3};~\msf{stack}{:}~{#4};~\msf{instrs}{:}~{#5}\}}

ewcommand{\wfunc}[4]{\{\msf{params}{:}~{#1};~\msf{locals}{:}~{#2};~\msf{return}~{#3};~\msf{body}{:}~{#4}\}}

ewcommand{\wmodule}[1]{\{\msf{funcs}{:}~{#1}\}}

ewcommand{\wcg}{\msf{globals}}

ewcommand{\wcf}{\msf{funcs}}

ewcommand{\wci}{\msf{instrs}}

ewcommand{\wcs}{\msf{stack}}

ewcommand{\wcl}{\msf{locals}}

ewcommand{\wcm}{\msf{mem}}

ewcommand{\wcmod}{\msf{module}}

ewcommand{\wsteps}[2]{\steps{\brc{#1}}{\brc{#2}}}

ewcommand{\with}{\underline{\msf{with}}}

ewcommand{\wvalid}[2]{{#1} \vdash {#2}~\msf{valid}} % assign4.3 custom

ewcommand{\wtry}[2]{\msf{try}~{#1}~\msf{catch}~{#2}}

ewcommand{\wraise}{\msf{raise}}

ewcommand{\wraising}[1]{\msf{raising}~{#1}}

ewcommand{\wif}[2]{\msf{if}~{#1}~{\msf{else}}~{#2}}

ewcommand{\wfor}[4]{\msf{for}~(\msf{init}~{#1})~(\msf{cond}~{#2})~(\msf{post}~{#3})~{#4}}

ewcommand{\windirect}[1]{\msf{call\_indirect}~{#1}} % session types

ewcommand{\ssend}[2]{\msf{send}~{#1};~{#2}}

ewcommand{\srecv}[2]{\msf{recv}~{#1};~{#2}}

ewcommand{\soffer}[4]{\msf{offer}~\{{#1}\colon({#2})\mid{#3}\colon({#4})\}}

ewcommand{\schoose}[4]{\msf{choose}~\{{#1}\colon({#2})\mid{#3}\colon({#4})\}}

ewcommand{\srec}[1]{\msf{label};~{#1}}

ewcommand{\sgoto}[1]{\msf{goto}~{#1}}

ewcommand{\dual}[1]{\overline{#1}} % Inference rules

ewcommand{\inferrule}[3][]{\cfrac{#2}{#3}\;{#1}}

ewcommand{\ir}[3]{\inferrule[\text{(#1)}]{#2}{#3}}

ewcommand{\s}{\hspace{1em}}

ewcommand{

l}{\\[2em]}

ewcommand{\evalto}{\boldsymbol{\overset{*}{\mapsto}}}

ewcommand{\steps}[2]{#1 \boldsymbol{\mapsto} #2}

ewcommand{\evals}[2]{#1 \evalto #2}

ewcommand{\subst}[3]{[#1 \rightarrow #2] ~ #3}

ewcommand{\dynJ}[2]{#1 \proves #2}

ewcommand{\dynJC}[1]{\dynJ{\ctx}{#1}}

ewcommand{\typeJ}[3]{#1 \proves \hasType{#2}{#3}}

ewcommand{\typeJC}[2]{\typeJ{\ctx}{#1}{#2}}

ewcommand{\hasType}[2]{#1 : #2}

ewcommand{\val}[1]{#1~\msf{val}}

ewcommand{

um}[1]{\msf{Int}(#1)}

ewcommand{\err}[1]{#1~\msf{err}}

ewcommand{\trans}[2]{#1 \leadsto #2}

ewcommand{\size}[1]{\left|#1\right|} $$

Automatic Type Coercions with Procedural Macros in Rust

I briefly demonstrate how to use procedural macros to automatically perform type coercion in Rust, mimicking the behavior of dynamic languages.

As an eminently lazy Rust programmer, I often take one too many shortcuts. For example, the other day I was writing a function that takes as input a string.

fn foo ( s : String ) -> ... {}

Except I frequently invoked this function with a variety of things that turned into strings, including &str , i32 , and other types, so much that I was frequently invoking the ToString trait. So I thought, why not move this into the API?

fn foo < S : Into < String >> ( s : S ) -> ... { let s : String = s .into (); ... }

Then I started thinking, how far can we take this pattern? Because the .into() function relies on Rust’s trait resolution to figure out what type to coerce into, it feels kind of like a dynamically typed language. For example, here’s a function that concatenates two strings:

fn concat < T1 : Into < String > , T2 : Into < String >> ( t1 : T1 , t2 : T2 ) -> String { let t1 : String = t1 .into (); let t2 : String = t2 .into (); t1 + & t2 } fn main () { println! ( "{}, {}" , concat ( "A" , "B" ), concat ( 1 , "Hello" .to_string ())); // prints "AB, 1Hello" }

At this point, it’s clear that on the implementation side, this API style requires a fair amount of boilerplate. We can abstract that away through a procedural macro!

#[auto_into] fn concat ( t1 : String , t2 : String ) -> String { t1 + & t2 }

See here for the barebones macro implementation (nightly required). This is cool because, in a sense, it makes your API strictly more general than before. Any calls that were valid before are still valid after #[auto_into] , except now you can pass any values which could be coerced into the desired input types.

That said, is this a good idea? Probably not in the general case, since implicit type coercions are a scary source of bugs, particularly for a language like Rust that errs on the side of explicitness. But it’s a neat pattern enabled by the trait system that could be used for more practical designs, e.g. a student in my Programming Languages course used a similar idea to implement a fluent API for dependency injection in Rust.