-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrust-async.html
531 lines (472 loc) · 18.7 KB
/
rust-async.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://twemoji.maxcdn.com/2/twemoji.min.js?11.0"></script>
<meta name="generator" content="pandoc">
<title>async in Rust 2018</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="template/css/reveal.css">
<style type="text/css">
code{white-space: pre;}
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; position: absolute; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; }
pre.numberSource a.sourceLine:empty
{ position: absolute; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: absolute; left: -5em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.reveal a[href^="#"]:visited {
color: #8ea9d2;
}
</style>
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = "template/css/theme/moon.css";
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<link rel="stylesheet" href="template/css/theme/moon.css" id="theme">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'template/css/print/pdf.css' : 'template/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="template/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 class="title">async in Rust 2018</h1>
<p class="subtitle">The future of futures</p>
</section>
<section id="whoami1" class="slide level2">
<h2><code>whoami(1)</code></h2>
<p>Katharina Fey ( <code>@spacekookie</code> )</p>
<ul>
<li>Active FOSS developer</li>
<li>Avid tea drinker</li>
<li>Hobbyist hardware maker</li>
</ul>
</section>
<section class="slide level2">
<p>I also make some attrocious puns</p>
</section>
<section class="slide level2">
<p><img src="images/twitter_a.png" height=275px/></p>
<p><img src="images/twitter_b.png" height=250px/></p>
</section>
<section id="whoami2" class="slide level2">
<h2><code>whoami(2)</code></h2>
<p>I do Rust things!</p>
<ul>
<li>Contributer of the CLI working group</li>
<li>Member of the community team & <code>berlin.rs</code></li>
<li>Maintainer of several <code>use[ful|less]</code> libraries</li>
</ul>
</section>
<section id="concurrency-is-hard" class="slide level2">
<h2>Concurrency is hard</h2>
</section>
<section class="slide level2">
<p><img src="images/concurrency.gif" width=650px></p>
</section>
<section class="slide level2">
<p>Race conditions</p>
<p><strong>vs</strong></p>
<p>Inefficient scaling</p>
</section>
<section class="slide level2">
<h3 id="what-thread-owns-data">What thread owns data?</h3>
</section>
<section id="rust" class="slide level2">
<h2>Rust</h2>
</section>
<section class="slide level2">
<blockquote>
<p>Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.</p>
</blockquote>
</section>
<section class="slide level2">
<p><img src="images/confused_travolta.gif" height=350px /></p>
</section>
<section id="safety" class="slide level2">
<h2>Safety</h2>
</section>
<section class="slide level2">
<p>Memory safety</p>
<div class="fragment" data-fragment-index="3">
<p><strong>and</strong></p>
<p>Thread safety</p>
</div>
</section>
<section id="ownership" class="slide level2">
<h2>Ownership</h2>
</section>
<section class="slide level2">
<ul>
<li>Data is always owned</li>
<li>Data can always be borrowed</li>
<li>Strict rules around borrowing and owning</li>
</ul>
</section>
<section class="slide level2">
<div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="kw">let</span> <span class="kw">mut</span> f = File::open(<span class="st">"foo.txt"</span>)?;</a>
<a class="sourceLine" id="cb1-2" data-line-number="2"></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="kw">for</span> _ <span class="kw">in</span> <span class="dv">0.</span>.<span class="dv">3</span> <span class="op">{</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"> thread::spawn(<span class="kw">move</span> || <span class="op">{</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"> <span class="co">// ... read data from file or something ...</span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6"></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"> drop(f); <span class="co">// close the file</span></a>
<a class="sourceLine" id="cb1-8" data-line-number="8"> <span class="op">}</span>);</a>
<a class="sourceLine" id="cb1-9" data-line-number="9"><span class="op">}</span></a></code></pre></div>
</section>
<section class="slide level2">
<p>This would not compile!</p>
</section>
<section class="slide level2">
<pre><code>error[E0382]: capture of moved value: `f` --> main.rs:13:14
|
10 | thread::spawn(move || {
| ------- value moved (into closure) here
...
13 | drop(f);
| ^ value captured here after move
|</code></pre>
</section>
<section class="slide level2">
<div class="sourceCode" id="cb3"><pre class="sourceCode rust"><code class="sourceCode rust"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="kw">fn</span> main() <span class="op">{</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"> <span class="kw">let</span> f = File::open(<span class="st">"foo.txt"</span>)?;</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"></a>
<a class="sourceLine" id="cb3-4" data-line-number="4"> read_from_file(f);</a>
<a class="sourceLine" id="cb3-5" data-line-number="5"> read_from_file(f);</a>
<a class="sourceLine" id="cb3-6" data-line-number="6"><span class="op">}</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"></a>
<a class="sourceLine" id="cb3-8" data-line-number="8"><span class="kw">fn</span> read_from_file(f: File) <span class="op">{</span> ... <span class="op">}</span></a></code></pre></div>
</section>
<section class="slide level2">
<p>This will also not compile!</p>
</section>
<section class="slide level2">
<p><img src="images/purity.png" width=800px></p>
</section>
<section class="slide level2">
<p><strong>Rust breaks down these spectrums</strong></p>
</section>
<section class="slide level2">
<blockquote>
<p>No matter what kind of code you are writing now, Rust empowers you to reach farther, to program with confidence in a wider variety of domains than you did before.</p>
</blockquote>
<p><small>— Rust book, foreword</small></p>
</section>
<section id="async" class="slide level2">
<h2>Async</h2>
<div class="fragment" data-fragment-index="2">
<p>What is that?</p>
</div>
<div class="fragment" data-fragment-index="3">
<p>What does it know?</p>
</div>
<div class="fragment" data-fragment-index="4">
<p>Does it know anything?</p>
</div>
<div class="fragment" data-fragment-index="5">
<p>Let’s find out!</p>
</div>
</section>
<section class="slide level2">
<p>“Do this thing but don’t make me wait”</p>
</section>
<section class="slide level2">
<p>Not just a new Thread</p>
</section>
<section id="futures" class="slide level2">
<h2>Futures</h2>
</section>
<section class="slide level2">
<p><img data-src="images/back_to_the_future.gif" /></p>
</section>
<section class="slide level2">
<p>No, not that type of Futures</p>
</section>
<section class="slide level2">
<p><strong><code>Future</code> = calculation that hasn’t happened yet</strong></p>
<ul>
<li class="fragment">Is <em>probably</em> gonna happen at some point</li>
<li class="fragment">Just keep asking</li>
</ul>
</section>
<section class="slide level2">
<p><strong>Event Loop = runtime for <code>Future</code>s</strong></p>
<ul>
<li>Keeps polling <code>Future</code> until it is ready</li>
<li>Runs your code whenever it can be run</li>
</ul>
</section>
<section id="why" class="slide level2">
<h2>Why?</h2>
</section>
<section class="slide level2">
<p><img data-src="images/apache-vs-nginx.png" /></p>
</section>
<section class="slide level2">
</section>
<section class="slide level2">
<p>I’m here to show code and talk history</p>
<div class="fragment" data-fragment-index="2">
<p>and I’m <del>all</del> mostly out of code</p>
</div>
</section>
<section class="slide level2">
<p><img data-src="images/back_to_the_future.gif" /></p>
</section>
<section id="late-2013" class="slide level2">
<h2>Late 2013</h2>
</section>
<section class="slide level2">
<h3 id="libgreen"><code>libgreen</code></h3>
</section>
<section class="slide level2">
<p><img data-src="images/libgreen_add.png" /></p>
<ul>
<li>Rust had green threading support</li>
<li>Enabled non-blocking IO</li>
</ul>
</section>
<section class="slide level2">
<p>Included a runtime in <code>stdlib</code></p>
<ul>
<li>This came with a lot of problems</li>
</ul>
</section>
<section class="slide level2">
<p>Rust wanted to go in a different direction</p>
</section>
<section id="late-2014" class="slide level2">
<h2>Late 2014</h2>
</section>
<section class="slide level2">
<p><strong><code>libgreen</code> is dead, long live <code>libgreen</code></strong></p>
<p><img data-src="images/libgreen_remove.png" /></p>
</section>
<section class="slide level2">
<p>Sorry, did I say <del>libgreen</del>, I meant <code>mio.rs</code></p>
</section>
<section id="mio.rs" class="slide level2">
<h2><code>mio.rs</code></h2>
<p><small>“Metal IO” 🤘</small></p>
</section>
<section class="slide level2">
<ul>
<li>Light, non-blocking IO library</li>
<li>Abstracts async over different platforms</li>
<li>Eventually developed an ecosystem around it</li>
</ul>
</section>
<section class="slide level2">
<p><img src="images/hit-you-with-some-knowledge.gif" width=450px /></p>
</section>
<section class="slide level2">
<h3 id="zero-cost-abstractions">Zero Cost Abstractions</h3>
</section>
<section class="slide level2">
<p><strong>= no discernible* runtime overhead</strong></p>
<p><br /></p>
</section>
<section class="slide level2">
<p><strong>= code that you can’t write better by hand</strong></p>
<div class="fragment" data-fragment-index="2">
<p>Abstraction layers disappear at compile-time</p>
</div>
</section>
<section id="mid-2016" class="slide level2">
<h2>Mid 2016</h2>
</section>
<section class="slide level2">
<p><img src="images/futures-rs-logo.svg" /></p>
</section>
<section class="slide level2">
<ul>
<li><em>zero cost abstraction</em> for futures</li>
<li>Building async state-machines</li>
</ul>
</section>
<section class="slide level2">
<h3 id="tokio-core"><code>tokio-core</code></h3>
</section>
<section class="slide level2">
<ul>
<li>Wraps around <code>mio.rs</code> and <code>futures.rs</code></li>
<li>Event reactor</li>
</ul>
</section>
<section id="fast-foward-to-2018" class="slide level2">
<h2>Fast foward to 2018</h2>
</section>
<section class="slide level2">
<p>It’s state machines all the way down</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode rust"><code class="sourceCode rust"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="co">// ... define `stdin` and `stdout`</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"><span class="kw">let</span> reader = BufReader::new(stdin);</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"><span class="kw">let</span> buffer = <span class="dt">Vec</span>::new();</a>
<a class="sourceLine" id="cb4-4" data-line-number="4"></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="kw">let</span> fut = io::read_until(reader, b<span class="ch">'</span><span class="sc">\n</span><span class="ch">'</span>, buffer)</a>
<a class="sourceLine" id="cb4-6" data-line-number="6"> .and_then(<span class="kw">move</span> |(stdin, buffer)| <span class="op">{</span></a>
<a class="sourceLine" id="cb4-7" data-line-number="7"> stdout.write_all(&buffer).map_err(|e| <span class="pp">panic!</span>(e))</a>
<a class="sourceLine" id="cb4-8" data-line-number="8"> <span class="op">}</span>).map_err(|e| <span class="pp">panic!</span>(e));</a>
<a class="sourceLine" id="cb4-9" data-line-number="9"></a>
<a class="sourceLine" id="cb4-10" data-line-number="10"><span class="co">// Actually run _here_</span></a>
<a class="sourceLine" id="cb4-11" data-line-number="11">tokio::run(fut);</a></code></pre></div>
</section>
<section class="slide level2">
<p><strong>Reminder:</strong> Futures are zero-cost-abstractions.</p>
<p>They disappear from the code at compile-time!</p>
</section>
<section id="async-await" class="slide level2">
<h2><code>async</code> & <code>await</code></h2>
</section>
<section class="slide level2">
<p>Write code that looks synchronous but really isn’t</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><a class="sourceLine" id="cb5-1" data-line-number="1">async <span class="kw">fn</span> do_this_thing() <span class="op">{</span> ... <span class="op">}</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
<a class="sourceLine" id="cb5-3" data-line-number="3"><span class="co">// ...</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4"></a>
<a class="sourceLine" id="cb5-5" data-line-number="5"><span class="pp">await!</span>( do_this_thing() );</a></code></pre></div>
</section>
<section id="how" class="slide level2">
<h2>How?!</h2>
</section>
<section class="slide level2">
<h3 id="its-complicated">It’s complicated!</h3>
<div class="fragment" data-fragment-index="3">
<p>Clever people are working on it</p>
</div>
<div class="fragment" data-fragment-index="4">
<p>In Groups</p>
</div>
<div class="fragment" data-fragment-index="5">
<p>You might even call them “working groups”</p>
</div>
</section>
<section id="networking-wg" class="slide level2">
<h2>networking-WG</h2>
<ul>
<li>Implements async/await features in compiler</li>
<li>Provides library ecosystem</li>
</ul>
</section>
<section id="can-i-use-this" class="slide level2">
<h2>Can I use this?</h2>
</section>
<section class="slide level2">
<p><em>kinda</em> 😅</p>
</section>
<section class="slide level2">
<h3 id="roadmap">Roadmap</h3>
<ul>
<li><code>async</code>/<code>await</code> syntax in nightly compiler</li>
<li>library ecosystem is still being polished</li>
</ul>
<p>Expect more concrete progress early-2019</p>
</section>
<section id="you-made-it" class="slide level2">
<h2>You made it!</h2>
<p>Follow me on twitter <strong><code>@spacekookie</code></strong></p>
<p>Or: <strong><code>[email protected]</code></strong></p>
<p><br/></p>
<h4 id="thanks-to-my-employers">Thanks to my employers</h4>
<ul>
<li>I do <em>Rust</em> work at <strong>Ferrous Systems</strong></li>
<li>I do <em>Distributed Systems</em> at <strong>Asquera</strong></li>
</ul>
</section>
</div>
</div>
<script src="template/lib/js/head.min.js"></script>
<script src="template/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
// Display a presentation progress bar
progress: false,
// Display the page number of the current slide
slideNumber: true,
// Push each slide change to the browser history
history: true,
// Transition style
transition: 'fade', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'template/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'template/plugin/zoom-js/zoom.js', async: true },
{ src: 'template/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>