BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and...
Transcript of BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and...
![Page 1: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/1.jpg)
BEAM + RustBEAM + RustA Match Made in Heaven
1 / 79
![Page 2: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/2.jpg)
Sonny ScrogginSonny Scroggin
scrogson
2 / 79
![Page 3: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/3.jpg)
3 / 79
![Page 4: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/4.jpg)
BEAM + RustBEAM + RustA Match Made in Heaven
4 / 79
![Page 5: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/5.jpg)
What is Rust?Why Rust?How to Rust?BEAM + Rust
5 / 79
![Page 6: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/6.jpg)
What isWhat isRust?Rust?
6 / 79
![Page 7: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/7.jpg)
Rust is a systems programminglanguage.
7 / 79
![Page 8: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/8.jpg)
Empowering everyone to buildreliable and ef�cient software.
8 / 79
![Page 9: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/9.jpg)
When it comes to systems programmingWhen it comes to systems programming
9 / 79
![Page 10: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/10.jpg)
Meh, I already know C/C++Meh, I already know C/C++
10 / 79
![Page 11: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/11.jpg)
Why Rust?Why Rust?11 / 79
![Page 12: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/12.jpg)
PerformancePerformanceRust is blazingly fast and memory-efficient: with nono
runtimeruntime or garbage collectorgarbage collector, it can power performance-critical services, run on embedded devices, and easily
integrate with other languagesintegrate with other languages.
12 / 79
![Page 13: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/13.jpg)
Safe Rust is generally fast enough without even trying.
13 / 79
![Page 14: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/14.jpg)
ReliabilityReliabilityRust’s rich type systemtype system and ownershipownership model guaranteememory-safetymemory-safety and thread-safetythread-safety — and enable you to
eliminate many classes of bugs at compile-time.
14 / 79
![Page 15: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/15.jpg)
ProductivityProductivityRust has great documentationdocumentation, a friendly compilerfriendly compiler with
useful error messages, and top-notch toolingtop-notch tooling.
15 / 79
![Page 16: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/16.jpg)
Rust was initially created to solveRust was initially created to solvetwo difficult problemstwo difficult problems
How do you make systems programming safe?How do you make concurrency painless?
16 / 79
![Page 17: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/17.jpg)
Memory-safetyMemory-safety
17 / 79
![Page 18: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/18.jpg)
Memory-safety is a term used to describe applicationsthat access the operating system's memory in a way that
doesn't cause errors.
18 / 79
![Page 19: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/19.jpg)
Common memory-safety issuesCommon memory-safety issuesBuffer over�owsRace conditionsNull pointersStack/Heap exhaustion/corruptionUse after free / double free
Memory-safety issues have been hovering at 70% for the past 12 years.
19 / 79
![Page 20: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/20.jpg)
Thread-safetyThread-safety
20 / 79
![Page 21: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/21.jpg)
Thread-safe code only manipulates shared data structuresin a manner that ensures that all threads behave properlyand fulfill their design specifications without unintended
interaction.
21 / 79
![Page 22: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/22.jpg)
Memory and concurrency bugs o�en come down to codeaccessing data when it shouldn't.
22 / 79
![Page 23: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/23.jpg)
Linus TorvaldsLinus TorvaldsBut I write bug-free C
code, why should Ibother?
23 / 79
![Page 24: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/24.jpg)
Linux Kernel VulnerabilitiesLinux Kernel Vulnerabilities
24 / 79
![Page 25: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/25.jpg)
Microso�: 70 percent of all securityMicroso�: 70 percent of all securitybugs are memory safety issuesbugs are memory safety issues
https://www.zdnet.com/article/microsoft-70-percent-of-all-security-bugs-are-memory-safety-issues
25 / 79
![Page 26: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/26.jpg)
OwnershipOwnership
26 / 79
![Page 27: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/27.jpg)
Every value has anEvery value has an"owning scope""owning scope"
27 / 79
![Page 28: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/28.jpg)
fn new_vec() { let mut vec = Vec::new(); // owned by new_vec's scope vec.push(0); vec.push(1); // scope ends, `vec` is destroyed}
28 / 79
![Page 29: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/29.jpg)
Create a new vector
let mut vec = Vec::new(); // owned by new_vec's scopefn new_vec() {
vec.push(0); vec.push(1); // scope ends, `vec` is destroyed}
28 / 79
![Page 30: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/30.jpg)
Push some elements
vec.push(0); vec.push(1);
fn new_vec() { let mut vec = Vec::new(); // owned by new_vec's scope
// scope ends, `vec` is destroyed}
28 / 79
![Page 31: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/31.jpg)
vec is now dropped
// scope ends, `vec` is destroyed
fn new_vec() { let mut vec = Vec::new(); // owned by new_vec's scope vec.push(0); vec.push(1);
}
28 / 79
![Page 32: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/32.jpg)
29 / 79
![Page 33: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/33.jpg)
fn new_vec() -> Vec<i32> { let mut vec = Vec::new(); vec.push(0); vec.push(1); vec} fn print_vec(vec: Vec<i32>) {
for i in vec iter() {
29 / 79
![Page 34: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/34.jpg)
Create a new vec
let mut vec = Vec::new();fn new_vec() -> Vec<i32> {
vec.push(0); vec.push(1); vec} fn print_vec(vec: Vec<i32>) {
for i in vec iter() {
29 / 79
![Page 35: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/35.jpg)
Push some integers
vec.push(0); vec.push(1);
fn new_vec() -> Vec<i32> { let mut vec = Vec::new();
vec} fn print_vec(vec: Vec<i32>) {
for i in vec iter() {
29 / 79
![Page 36: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/36.jpg)
Return the vec
vec
fn new_vec() -> Vec<i32> { let mut vec = Vec::new(); vec.push(0); vec.push(1);
} fn print_vec(vec: Vec<i32>) {
for i in vec.iter() {
29 / 79
![Page 37: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/37.jpg)
fn print_vec(vec: Vec<i32>) { for i in vec.iter() { println!("{}", i) }}
}
fn use vec() {
29 / 79
![Page 38: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/38.jpg)
print_vec takes ownership of vec
fn print_vec(vec: Vec<i32>) {
}
}
for i in vec.iter() { println!("{}", i) }
29 / 79
![Page 39: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/39.jpg)
Interate over each entry in the vec
for i in vec.iter() { println!("{}", i) }
} fn print_vec(vec: Vec<i32>) {
} fn use vec() {
29 / 79
![Page 40: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/40.jpg)
vec gets deallocated here
}
fn print_vec(vec: Vec<i32>) { for i in vec.iter() { println!("{}", i)
} fn use_vec() {
let vec = new vec();
29 / 79
![Page 41: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/41.jpg)
fn use_vec() { let vec = new_vec(); print_vec(vec);}
for i in vec.iter() { println!("{}", i) }}
29 / 79
![Page 42: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/42.jpg)
Take ownership of vec
let vec = new_vec();
for i in vec.iter() { println!("{}", i) }
} fn use_vec() {
print_vec(vec);}
29 / 79
![Page 43: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/43.jpg)
Pass ownership to print_vec
print_vec(vec);
for i in vec.iter() { println!("{}", i) }} fn use_vec() { let vec = new_vec();
}
29 / 79
![Page 44: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/44.jpg)
fn use_vec() { let vec = new_vec(); print_vec(vec); for i in vec.iter() { println!("{}", i * 2) }}
30 / 79
![Page 45: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/45.jpg)
Continue using vec
for i in vec.iter() { println!("{}", i * 2) }
fn use_vec() { let vec = new_vec(); print_vec(vec);
}
30 / 79
![Page 46: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/46.jpg)
error: use of moved value: `vec` for i in vec.iter() { ^~~
31 / 79
![Page 47: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/47.jpg)
How can we prevent vec from being dropped at the endof print_vec?
32 / 79
![Page 48: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/48.jpg)
BorrowingBorrowing
33 / 79
![Page 49: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/49.jpg)
&&To borrow a value, you make a reference to it
34 / 79
![Page 50: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/50.jpg)
fn print_vec(vec: &Vec<i32>) { // `vec` is borrowed for this scope for i in vec.iter() { println!("{}", i) } // the borrow ends}
35 / 79
![Page 51: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/51.jpg)
fn use_vec() { let vec = make_vec(); print_vec(&vec); for i in vec.iter() { println!("{}", i * 2) } // vec is destroyed here}
36 / 79
![Page 52: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/52.jpg)
Take ownership of vec
let vec = make_vec();fn use_vec() {
print_vec(&vec); for i in vec.iter() { println!("{}", i * 2) } // vec is destroyed here}
36 / 79
![Page 53: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/53.jpg)
Lend vec to print_vec
print_vec(&vec);
fn use_vec() { let vec = make_vec();
for i in vec.iter() { println!("{}", i * 2) } // vec is destroyed here}
36 / 79
![Page 54: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/54.jpg)
Continue using vec
for i in vec.iter() { println!("{}", i * 2) }
fn use_vec() { let vec = make_vec(); print_vec(&vec);
// vec is destroyed here}
36 / 79
![Page 55: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/55.jpg)
// vec is destroyed here
fn use_vec() { let vec = make_vec(); print_vec(&vec); for i in vec.iter() { println!("{}", i * 2) }
}
36 / 79
![Page 56: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/56.jpg)
References come in twoReferences come in twoflavorsflavors
37 / 79
![Page 57: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/57.jpg)
&T&TImmutable references
38 / 79
![Page 58: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/58.jpg)
&mut T&mut TMutable references
39 / 79
![Page 59: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/59.jpg)
Borrowing has no runtimeBorrowing has no runtimeoverheadoverhead
Rust checks these rules at compile time
40 / 79
![Page 60: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/60.jpg)
fn push_all(from: &Vec<i32>, to: &mut Vec<i32>) { for i in from.iter() { to.push(*i); }}
41 / 79
![Page 61: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/61.jpg)
push_all(&vec, &mut vec)push_all(&vec, &mut vec)
42 / 79
![Page 62: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/62.jpg)
error: cannot borrow `vec` as mutable because it is also borrowed as immutablepush_all(&vec, &mut vec); ^~~
43 / 79
![Page 63: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/63.jpg)
For memory-safety, this means you can program withouta garbage collector and without fear of segfaults, because
Rust will catch your mistakes.
44 / 79
![Page 64: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/64.jpg)
ConcurrencyConcurrency
45 / 79
![Page 65: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/65.jpg)
ChannelsChannelsTransfers ownership of the messages sent along itSend a pointer from one thread to another withoutfear of race conditions.Enforce thread isolation.
46 / 79
![Page 66: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/66.jpg)
LocksLocksA lock knows what data it protects.Guarantees data can only be accessed when the lockis held.State is never accidentally shared."Lock data, not code" is enforced in Rust.
47 / 79
![Page 67: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/67.jpg)
Thread-safety isn't justThread-safety isn't justdocumentation; it's law.documentation; it's law.
Every data type knows whether it can safely be sentbetween or accessed by multiple threads.Rust enforces this safe usage; there are no dataraces, even for lock-free data structures.
48 / 79
![Page 68: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/68.jpg)
Message PassingMessage PassingA ! B
49 / 79
![Page 69: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/69.jpg)
ChannelsChannels
std::sync::mpsc
fn channel<T>() -> (Sender<T>, Receiver<T>)fn send(&self, t: T) -> Result<(), SendError<T>>fn recv(&self) -> Result<T, RecvError> impl<T: Send> Send for Sender<T> { }
50 / 79
![Page 70: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/70.jpg)
This won't workThis won't worklet mut vec = Vec::new(); sender.send(vec); print_vec(&vec);
51 / 79
![Page 71: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/71.jpg)
error[E0382]: borrow of moved value: `vec` --> src/main.rs:13:15 |8 | let mut vec = Vec::new(); | ------- move occurs because `vec` has type `std::vec...11 | tx.send(vec); | --- value moved here12 |13 | print_vec(&vec); | ^^^^ value borrowed here after move
52 / 79
![Page 72: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/72.jpg)
spawn, send, recvspawn, send, recvlet (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}
53 / 79
![Page 73: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/73.jpg)
spawn, send, recvspawn, send, recv
Create a channel and deconstuct the sender and receiver
let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}
53 / 79
![Page 74: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/74.jpg)
spawn, send, recvspawn, send, recv
Spawn a new thread
std::thread::spawn(move || {
});
let (sender, receiver) = std::sync::mpsc::channel();
let result = expensive_computation(); sender.send(result).unwrap();
match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}
53 / 79
![Page 75: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/75.jpg)
spawn, send, recvspawn, send, recv
Run some computation
let result = expensive_computation();
let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || {
sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}
53 / 79
![Page 76: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/76.jpg)
spawn, send, recvspawn, send, recv
Send the result to the receiver
sender.send(result).unwrap();
let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation();
}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}
53 / 79
![Page 77: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/77.jpg)
spawn, send, recvspawn, send, recv
Receive the result
match receiver.recv() {
}
let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();});
Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)
53 / 79
![Page 78: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/78.jpg)
spawn, send, recvspawn, send, recv
Handle the success case
Ok(msg) => println!("{:?}", msg),
let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() {
Err(err) => println!("{:?}", err)}
53 / 79
![Page 79: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/79.jpg)
spawn, send, recvspawn, send, recv
Handle the error case
Err(err) => println!("{:?}", err)
let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg),
}
53 / 79
![Page 80: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/80.jpg)
spawn, send, recvspawn, send, recvlet (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}
53 / 79
![Page 81: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/81.jpg)
Equivalent in ElixirEquivalent in Elixirparent = self() spawn(fn -> result = expensive_computation() send(parent, result)end) receive do msg -> IO.inspect(msg)end
54 / 79
![Page 82: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/82.jpg)
For concurrency, this means you can choose from a widevariety of paradigms (message passing, shared state, lock-
free, purely functional), and Rust will help you avoidcommon pitfalls.
55 / 79
![Page 83: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/83.jpg)
BEAM + RustBEAM + Rust
56 / 79
![Page 84: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/84.jpg)
PortsNIFsNodes
57 / 79
![Page 85: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/85.jpg)
PortsPortsCommunicate with external programs over stdin/stdout
58 / 79
![Page 86: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/86.jpg)
Sending data to a portSending data to a portport = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)
59 / 79
![Page 87: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/87.jpg)
Sending data to a portSending data to a port
Open a port to an external binary
port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)
59 / 79
![Page 88: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/88.jpg)
Sending data to a portSending data to a port
Serialize our data to binary
data = :erlang.term_to_binary(term)
port = Port.open({:spawn_executable, "priv/binary"}, [:binary])
len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)
59 / 79
![Page 89: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/89.jpg)
Sending data to a portSending data to a port
Get the length of the encoded data
len = byte_size(data)
port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)
iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)
59 / 79
![Page 90: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/90.jpg)
Sending data to a portSending data to a port
Pack our length and data into an IO list
iodata = [<<len::big-unsigned-integer-size(64)>>, data]
port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)
Port.command(port, iodata)
59 / 79
![Page 91: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/91.jpg)
Sending data to a portSending data to a port
Send it to the port
Port.command(port, iodata)
port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data]
59 / 79
![Page 92: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/92.jpg)
Erlang Terms in RustErlang Terms in Rust
A Rust implementation of Erlang External Term Format
https://crates.io/crates/eetf
60 / 79
![Page 93: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/93.jpg)
pub enum Term { Atom(Atom), FixInteger(FixInteger), BigInteger(BigInteger), Float(Float), Pid(Pid), Port(Port), Reference(Reference), Binary(Binary), BitBinary(BitBinary), List(List), ImproperList(ImproperList), Tuple(Tuple), Map(Map),}
61 / 79
![Page 94: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/94.jpg)
use eetf::Term; let (tx, rx) = std::sync::mpsc::channel::<Term>(); std::thread::spawn(move || { let stdin = io::stdin(); let mut locked = stdin.lock(); let mut term_buffer = Vec::new(); loop { // ... }});
62 / 79
![Page 95: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/95.jpg)
loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}
63 / 79
![Page 96: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/96.jpg)
Start an infinite loop
loop {
}
let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();
63 / 79
![Page 97: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/97.jpg)
Create an 8-byte buffer
let mut buffer = [0; 8];loop {
if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}
63 / 79
![Page 98: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/98.jpg)
Read 8-bytes from stdin
if let Err(_) = locked.read_exact(&mut buffer) { break; };
loop { let mut buffer = [0; 8];
let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}
63 / 79
![Page 99: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/99.jpg)
Find the length of the data
let length = u64::from_be_bytes(buffer);
loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };
term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}
63 / 79
![Page 100: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/100.jpg)
Resize the term buffer to accomodate the size of the data
term_buffer.resize(length as usize, 0);
loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer);
if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}
63 / 79
![Page 101: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/101.jpg)
Read the data into the term buffer
if let Err(_) = locked.read_exact(&mut term_buffer) { break; };
loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0);
let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}
63 / 79
![Page 102: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/102.jpg)
Decode the binary data into a Term
let term = Term::decode(Cursor::new(&term_buffer)).unwrap();
loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };
tx.send(term).unwrap();}
63 / 79
![Page 103: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/103.jpg)
Send the Term to the reciever thread
tx.send(term).unwrap();
loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap();
}
63 / 79
![Page 104: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/104.jpg)
loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}
63 / 79
![Page 105: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/105.jpg)
while let Ok(term) = rx.recv() { match term { Term::FixInteger(FixInteger { value }) => // ..., Term::Atom(Atom { name }) => match name.as_ref() { "quit" => std::process::exit(0), text => println!("{}", text), }, other => println!("Received {:?}", other), }}
64 / 79
![Page 106: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/106.jpg)
NIFsNIFsNative Implemented Functions
65 / 79
![Page 108: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/108.jpg)
ExampleExampleImage processingImage processing
https://github.com/scrogson/mirage
67 / 79
![Page 109: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/109.jpg)
MirageMiragedefmodule Mirage do use Rustler, otp_app: :mirage defstruct bytes: nil, byte_size: nil, extension: nil, height: nil, width: nil, resource: nil def from_bytes(_path), do: :erlang.nif_error(:nif_not_loaded) def resize(_resource, _width, _height), do: :erlang.nif_error(:end
68 / 79
![Page 110: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/110.jpg)
NIF BoilerplateNIF Boilerplateuse rustler::schedule::SchedulerFlags::*; mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool {
mirage::load(env)
69 / 79
![Page 111: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/111.jpg)
NIF BoilerplateNIF Boilerplate
Setup the mapping from BEAM MFAs to NIF functions
rustler::rustler_export_nifs! {
}
mod atoms;mod mirage;
"Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)
fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}
69 / 79
![Page 112: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/112.jpg)
NIF BoilerplateNIF Boilerplate
Module name
"Elixir.Mirage",
use rustler::schedule::SchedulerFlags::*; mod atoms;mod mirage; rustler::rustler_export_nifs! {
[ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool {
mirage::load(env)
69 / 79
![Page 113: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/113.jpg)
NIF BoilerplateNIF Boilerplate
("from_bytes", 1, mirage::from_bytes, DirtyIo),
use rustler::schedule::SchedulerFlags::*; mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [
("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}
69 / 79
![Page 114: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/114.jpg)
NIF BoilerplateNIF Boilerplate
("resize", 3, mirage::resize, DirtyCpu),
mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo),
], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}
69 / 79
![Page 115: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/115.jpg)
NIF BoilerplateNIF Boilerplate
Function to call when the NIF loads
Some(load)
mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ],
} fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}
69 / 79
![Page 116: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/116.jpg)
NIF BoilerplateNIF Boilerplate
fn load(env: rustler::Env, _info: rustler::Term) -> bool {
}
mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)}
mirage::load(env)
69 / 79
![Page 117: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/117.jpg)
NIF BoilerplateNIF Boilerplate
Setup resource objects
mirage::load(env)
mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool {
}
69 / 79
![Page 118: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/118.jpg)
Resource ObjectsResource Objects
A way to pass pointers back and forth
struct Image { image: DynamicImage, format: ImageFormat,} pub fn load(env: Env) -> bool { rustler::resource_struct_init!(Image, env); true}
70 / 79
![Page 119: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/119.jpg)
Elixir StructElixir StructEncoding/DecodingEncoding/Decoding
%Mirage{}
defstruct byte_size: nil, extension: nil, height: nil, width: nil, resource: nil
71 / 79
![Page 120: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/120.jpg)
Elixir StructElixir StructEncoding/DecodingEncoding/Decoding
#[derive(NifStruct)]#[module = "Mirage"]struct Mirage { byte_size: usize, extension: Atom, height: u32, width: u32, resource: ResourceArc<Image>,}
72 / 79
![Page 121: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/121.jpg)
pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { let bytes: Binary = args[0].decode()?; match image::load_from_memory(bytes.as_slice()) { // ... }}
73 / 79
![Page 122: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/122.jpg)
<'a> = lifetime annotations
pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> {
}
let bytes: Binary = args[0].decode()?; match image::load_from_memory(bytes.as_slice()) { // ... }
73 / 79
![Page 123: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/123.jpg)
Decode the first argument into a Binary
let bytes: Binary = args[0].decode()?;
pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> {
match image::load_from_memory(bytes.as_slice()) { // ... }}
73 / 79
![Page 124: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/124.jpg)
Load the bytes into an image::Image
match image::load_from_memory(bytes.as_slice()) {
}
pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { let bytes: Binary = args[0].decode()?;
// ...
}
73 / 79
![Page 125: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/125.jpg)
if let Ok(format) = image::guess_format(&bytes.as_slice()) { let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), }; return Ok((ok(), mirage).encode(env));}return Err(rustler::Error::Atom("unsupported_image_format"));
74 / 79
![Page 126: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/126.jpg)
If we have a valid image format
if let Ok(format) = image::guess_format(&bytes.as_slice()) {
}
let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), }; return Ok((ok(), mirage).encode(env));
return Err(rustler::Error::Atom("unsupported_image_format"));
74 / 79
![Page 127: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/127.jpg)
Build up our Mirage struct
let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), };
if let Ok(format) = image::guess_format(&bytes.as_slice()) {
return Ok((ok(), mirage).encode(env));}return Err(rustler::Error::Atom("unsupported_image_format"));
74 / 79
![Page 128: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/128.jpg)
Return {:ok, %Mirage{}}
return Ok((ok(), mirage).encode(env));
if let Ok(format) = image::guess_format(&bytes.as_slice()) { let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), };
}return Err(rustler::Error::Atom("unsupported_image_format"));
74 / 79
![Page 129: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/129.jpg)
Return {:error, :unsupported_image_format}
return Err(rustler::Error::Atom("unsupported_image_format"));
if let Ok(format) = image::guess_format(&bytes.as_slice()) { let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), }; return Ok((ok(), mirage).encode(env));}
74 / 79
![Page 130: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/130.jpg)
NodesNodesCommunicate with applications using the Erlang
Distribution Protocol
75 / 79
![Page 131: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/131.jpg)
erl_disterl_dist
Rust Implementation of Erlang Distribution Protocol
https://crates.io/crates/erl_dist
76 / 79
![Page 132: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/132.jpg)
eiei
Rust Implementation of erl_interface
https://crates.io/crates/ei
77 / 79
![Page 133: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/133.jpg)
ConclusionConclusion
78 / 79
![Page 134: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.](https://reader035.fdocuments.net/reader035/viewer/2022071212/602428184011a643c977e1ef/html5/thumbnails/134.jpg)
79 / 79