-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides.tex
416 lines (363 loc) · 11.8 KB
/
slides.tex
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
\documentclass{beamer}
\usetheme{metropolis}
%\setsansfont[BoldFont={Fira Sans SemiBold}]{Fira Sans Book}
%\setsansfont{Fontin}
%\setsansfont{Gillius ADF No2}
%\setsansfont{Phetsarath OT}
\setsansfont{Source Sans Pro}
\setmonofont{Source Code Pro}
\hypersetup{colorlinks=true,
linkcolor=mRustLightOrange,
menucolor=mRustLightOrange,
pagecolor=mRustLightOrange,
urlcolor=mRustLightOrange}
\usepackage{csquotes}
\usepackage{comment}
\usepackage{xcolor}
\usepackage{minted}
\newfontfamily\codefont{Source Code Pro}
\newcommand\code[1]{\,{\color[HTML]{884400}#1}\,}
\newcommand\source[1]{$\rightarrow$ via #1}
\title{Fluence Compute Engine (FCE), interior mutability, and TryInto}
\date{\today}
\author{Lukas Prokop}
\institute{RustGraz community\vfill\hfill\includegraphics[height=2cm]{images/rustacean-orig-noshadow.png}}
\begin{document}
\maketitle
%\section{Prologue}
\section{Fluence Compute Engine}
\begin{frame}[fragile]{FCE}
What is FCE?\vspace{8pt}
\begin{quote}
\href{https://github.com/fluencelabs/fce}{Fluence Compute Engine (FCE)} is a general purpose Wasm runtime that could be used in different scenarios, especially in programs based on the ECS pattern or plugin architecture. It runs multi-module WebAssembly applications with interface-types and shared-nothing linking scheme.
\end{quote}
Presentation by \href{https://github.com/michaelvoronov}{michaelvoronov}:\vspace{8pt}
\begin{quote}
Mike Voronov has more than 10 years of experience in C++ and 2+ years experience in Rust and WebAssembly
\end{quote}
\end{frame}
\section{Interior mutability}
\begin{frame}[fragile]{Reference semantics}
\textbf{Revision:} shared versus mutable references.
\begin{minted}{rust}
fn overwrite(base: &u32, new_value: &u32) {
base = new_value;
}
fn main() {
let value = 3;
overwrite(&value, &42);
println!("{}", value);
}
\end{minted}
\textbf{Does it compile?} \pause No, \emph{lifetime mismatch}.
\end{frame}
\begin{frame}[fragile]{Reference semantics}
\begin{minted}{rust}
fn overwrite<'a>(base: &'a u32,
new_value: &'a u32) {
base = new_value;
}
fn main() {
let value = 3;
overwrite(&value, &42);
println!("{}", value);
}
\end{minted}
\textbf{Does it compile?} \pause No, \emph{cannot assign to immutable argument 'base'
}.
\end{frame}
\begin{frame}[fragile]{Reference semantics}
\begin{minted}{rust}
fn overwrite<'a>(mut base: &'a u32,
new_value: &'a u32) {
base = new_value;
}
fn main() {
let value = 3;
overwrite(&value, &42);
println!("{}", value);
}
\end{minted}
\textbf{Does it compile?} \pause Yes, but prints \texttt{3}, not \texttt{42}.
\end{frame}
\begin{frame}[fragile]{Reference semantics}
Modifying the reference \dots
\begin{minted}{rust}
fn overwrite<'a>(mut base: &'a u32,
new_value: &'a u32);
\end{minted}
versus
\begin{minted}{rust}
fn overwrite<'a>(base: &'a mut u32,
new_value: &'a u32);
\end{minted}
\dots{} modifying the value behind a reference.
\end{frame}
\begin{frame}[fragile]{Reference semantics}
\begin{minted}{rust}
fn overwrite<'a>(base: &'a mut u32,
new_value: &'a u32) {
*base = *new_value;
}
fn main() {
let mut value = 3;
overwrite(&mut value, &mut 42);
println!("{}", value); // prints 42
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Excursion: constant variables}
\textbf{Small excursion:}
Benedikt also pointed out the following snippet:
\begin{minted}{rust}
fn overwrite<'a>(base: &'a mut u32,
new_value: &'a u32) {
*base = *new_value;
}
fn main() {
let mut value = 3;
overwrite(&mut 42, &mut 42);
println!("{}", value); // prints 3
}
\end{minted}
Apparently, rust creates \emph{two} local variables with value \emph{42}.
This is interesting since constants of same value need not be allocated twice usually.
\end{frame}
\begin{frame}[fragile]{Mutability semantics}
\begin{enumerate}
\item A variable is mutable; or not.
\item A reference is mutable; or not.
\item If a struct instance bound to a variable is mutable; its members are mutable too (\emph{inherited mutability}).
\item Mutability checks happen at \emph{compile time}.
\end{enumerate}
\begin{minted}[fontsize=\small]{rust}
struct User {
id: u32,
posts_count: u32,
}
let mut meisterluk = User {
id: 1,
posts_count: 42
};
\end{minted}
\end{frame}
\begin{frame}[standout]
What about a data structure that modifying elements in the background without knowledge of the user?
\end{frame}
\begin{frame}[fragile]{Interior mutability types in rust}
Multithreaded? Use atomic datatypes or locks (Mutex, RwLock, \dots)!
Otherwise, \dots
\begin{description}
\item[\mintinline{rust}{Cell<T>}] Provides interior mutability for some value.
\item[\mintinline{rust}{RefCell<T>}] Provides interior mutability for some value and returns references in its API.
\item[\mintinline{rust}{UnsafeCell<T>}] Underlying primitive for the types above
\end{description}
\dots{} implemented with \emph{runtime} checks!
\end{frame}
\begin{frame}[fragile]{Cell}
\mintinline{rust}{Cell<T>:} This type wraps an existing value and provides interior mutability.
\begin{minted}[fontsize=\scriptsize]{rust}
use std::cell::Cell;
struct User {
id: u32,
count_posts: Cell<u32>,
}
fn main() {
let meisterluk = User {
id: 1,
count_posts: Cell::new(42),
};
// meisterluk.id = 4; // error: meisterluk is not mutable
meisterluk.count_posts.replace(55);
println!("User=({}, {})",
meisterluk.id,
meisterluk.count_posts.get()
);
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Cell API}
\mintinline{rust}{Cell<T>:} This type wraps an existing value and provides interior mutability.
\begin{enumerate}
\item \mintinline{rust}{pub const fn new(value: T) -> Cell<T>}
\item \mintinline{rust}{pub fn set(&self, val: T)}
\item \mintinline{rust}{pub fn swap(&self, other: &Cell<T>)}
\item \mintinline{rust}{pub fn replace(&self, val: T) -> T}
\item \mintinline{rust}{pub fn into_inner(self) -> T}
\item \mintinline{rust}{pub const fn new(value: T) -> Cell<T>}
\end{enumerate}
\end{frame}
\begin{frame}[fragile]{RefCell}
\mintinline{rust}{RefCell<T>:} This type wraps an existing value and provides references for interior mutability.
\begin{minted}[fontsize=\scriptsize]{rust}
use std::cell::RefCell;
struct User {
id: u32,
count_posts: RefCell<u32>,
}
fn main() {
let meisterluk = User {
id: 1,
count_posts: RefCell::new(42),
};
meisterluk.count_posts.replace(55);
println!("User=({}, {})",
meisterluk.id,
meisterluk.count_posts.borrow());
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{RefCell API}
\begin{enumerate}
\item \mintinline{rust}{pub const fn new(value: T) -> RefCell<T>}
\item \mintinline{rust}{pub fn borrow(&self) -> Ref<'_, T>}
\item \mintinline{rust}{pub fn try_borrow(&self)} \\
\mintinline{rust}{-> Result<Ref<'_, T>, BorrowError>}
\item \mintinline{rust}{pub fn borrow_mut(&self) -> RefMut<'_, T>}
\item \mintinline{rust}{pub fn try_borrow_mut(&self)} \\
\mintinline{rust}{-> Result<RefMut<'_, T>, BorrowMutError>}
\item \mintinline{rust}{pub fn as_ptr(&self) -> *mut T}
\item \mintinline{rust}{pub fn get_mut(&mut self) -> &mut T}
\item \mintinline{rust}{pub unsafe fn try_borrow_unguarded(&self)} \\
\mintinline{rust}{-> Result<&T, BorrowError>}
\end{enumerate}
\end{frame}
\begin{frame}[fragile]{RefCell}
\begin{minted}[fontsize=\scriptsize]{rust}
use std::cell::RefCell;
struct User {
id: u32,
count_posts: RefCell<u32>,
}
fn main() {
let meisterluk = User {
id: 1,
count_posts: RefCell::new(42),
};
let a = meisterluk.count_posts.borrow_mut();
let _ = meisterluk.count_posts.borrow_mut();
}
\end{minted}
Runtime error!
\begin{minted}[fontsize=\scriptsize]{text}
thread 'main' panicked at 'already borrowed: BorrowMutError',
src/main.rs:14:36
note: run with `RUST_BACKTRACE=1` environment variable
to display a backtrace
\end{minted}
\end{frame}
\section{TryInto / Into / TryFrom / From}
\begin{frame}[fragile]{Motivation}
\begin{enumerate}
\item Sometimes it is convenient to convert one type into another (\emph{coercion}, \emph{casting})
\item Usually done explicitly in rust with \mintinline{rust}{as} keyword
\item But when calling a function, we often know the source and target target. How can we convert it?
\end{enumerate}
\end{frame}
\begin{frame}[fragile]{4 traits}
\begin{minted}[fontsize=\small]{rust}
pub trait Into<T>: Sized {
fn into(self) -> T;
}
pub trait TryInto<T>: Sized {
type Error;
fn try_into(self)
-> Result<T, Self::Error>;
}
pub trait From<T>: Sized {
fn from(_: T) -> Self;
}
pub trait TryFrom<T>: Sized {
type Error;
fn try_from(value: T)
-> Result<Self, Self::Error>;
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{4 traits}
\begin{enumerate}
\item Try traits permit conversion to fail.
\item All traits are reflexive (T can be converted \emph{into} T).
\item Prefer to implement \mintinline{rust}{TryFrom} instead of \mintinline{rust}{TryInto}
\item Implementing \mintinline{rust}{From} automatically provides one with an implementation of \mintinline{rust}{Into}.
\end{enumerate}
\end{frame}
\begin{frame}[fragile]{Example implementation}
Example via \href{https://doc.rust-lang.org/stable/rust-by-example/conversion/try_from_try_into.html}{Rust by Example}:
\begin{minted}[fontsize=\small]{rust}
use std::convert::{TryFrom, TryInto};
#[derive(Debug, PartialEq)]
struct EvenNumber(i32);
impl TryFrom<i32> for EvenNumber {
type Error = ();
fn try_from(value: i32)
-> Result<Self, Self::Error>
{
if value % 2 == 0 {
Ok(EvenNumber(value))
} else {
Err(())
} } }
\end{minted}
\end{frame}
\begin{frame}[fragile]{Example implementation}
\begin{minted}[fontsize=\small]{rust}
fn main() {
// TryFrom
assert_eq!(EvenNumber::try_from(8),
Ok(EvenNumber(8)));
assert_eq!(EvenNumber::try_from(5),
Err(()));
// TryInto
let result: Result<EvenNumber, ()> = 8i32.try_into();
assert_eq!(result, Ok(EvenNumber(8)));
let result: Result<EvenNumber, ()> = 5i32.try_into();
assert_eq!(result, Err(()));
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Strings to vectors}
Strings can be converted into a vector of bytes.
\begin{minted}{rust}
fn main() {
let a: Vec<u8> = String::from("hello").into();
println!("{}", a[0]);
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Into as trait bound}
Trait bounds can specify that any type convertible into another will be accepted.
\begin{minted}[fontsize=\small]{rust}
fn is_hello<T: Into<Vec<u8>>>(s: T) {
let bytes = b"hello".to_vec();
assert_eq!(bytes, s.into());
}
let s = "hello".to_string();
is_hello(s);
\end{minted}
\end{frame}
\section{Epilogue}
\begin{frame}[fragile]{Quiz}
\begin{description}
\item[Who maintains the WASM standard?] \hfill{} \\
~\uncover<2->{WebAssembly became a World Wide Web Consortium recommendation in Dec 2019}
\item[What is mutable about a mutable reference?] \hfill{} \\
~\uncover<3->{The value the reference is pointing to}
\item[How can you implement casting for your own type?] \hfill{} \\
~\uncover<4->{Implement Into/TryInto or From/TryFrom}
\item[When do you implement Into instead of TryInto?] \hfill{} \\
~\uncover<5->{If the conversion cannot fail under any circumstances.}
\end{description}
\end{frame}
\begin{frame}[fragile]{Next time}
\begin{tabular}{ll}
\textbf{Next meetup} & Wed, 2021/01/27 \\
\textbf{Topic} & Cross-compilation: \\
& compiling for the raspberry PI
\end{tabular}
\end{frame}
\begin{frame}[standout]
Thank you!
\includegraphics[width=40pt]{images/rustacean-flat-happy.png}
\end{frame}
\end{document}