or the 3-acts drama
Tomasz Drwięga
@tomusdrw
Parity Technologies
The Story
Working mostly as a Javascript developer (full stack).
Rather random gigs than full time job.
A lot of time for experimentation.
Let's write a web server and see how good Rust is!
[dependencies]
router = "*"
staticfile = "*"
[dependencies.iron]
version = "*"
[dependencies.logger]
git = "https://github.com/iron/logger.git"
Wildcards? WTF? The deps were uncompatible.
Implementation of first non-trivial route was killed by `'static` and move semantics.
After few hours: "Nah, will do it faster in node.js"
Hey! We are starting a new company and will write an Ethereum Client. We want you to join.
Hey! We are starting a new company and will write an Ethereum Client. We want you to join.
Cool! I don't really know C++ that well though.
Hey! We are starting a new company and will write an Ethereum Client. We want you to join.
Cool! I don't really know C++ that well though.
We plan to do it in Rust!
Hey! We are starting a new company and will write an Ethereum Client. We want you to join.
Cool! I don't really know C++ that well though.
We plan to do it in Rust!
Well, I don't know Rust at all then!
Hey! We are starting a new company and will write an Ethereum Client. We want you to join.
Cool! I don't really know C++ that well though.
We plan to do it in Rust!
Well, I don't know Rust at all then!
No worries, we neither!
The Confrontation
A new project from scratch, almost no deps at all.
Writing Rust is actually pretty fun!
enum MyType {
VariantA,
VariantB,
VariantC,
}
But Tomek: C has enums, Java also has them, a lot of langs do - nothing special.
enum MyType {
VariantA(u64, String),
VariantB {
value: u64,
other: String,
},
VariantC,
}
Proper algebraic data types (in func-lang lingo)
For normcores: tuples & tagged unions
enum MyType {
VariantA(u64, String),
VariantB {
value: u64,
other: String,
},
VariantC,
}
match x {
MyType::VariantA(a, b) => {
...
},
MyType::VariantB { value, .. } => {
...
},
_ => {},
}
Parity Ethereum
Substrate
Verify & Execute to obtain the latest state
My first task was to write EVM.
A deterministic and very specific kind of interpreter.
Quite similar to GoL.
Second task was to write a transaction queue.
It took me around 1 month, during which the Rust Compiler was teaching me how to write concurrent programs.
Previous experience: sprinkling "synchronized" in Java code.
use std::{time::Duration as D, thread};
let mut my_var = 5;
thread::spawn(|| {
my_var += 1;
thread::sleep(D::from_millis(1000));
});
loop {
println!("my_var = {}", my_var);
thread::sleep(D::from_millis(500));
}
use std::{time::Duration as D, thread};
struct State {
counter: u64,
}
let mut state = State { counter: 5 };
thread::spawn(|| {
state.counter += 1;
thread::sleep(D::from_millis(1000));
});
loop {
println!("counter = {}", state.counter);
thread::sleep(D::from_millis(500));
}
Borrow checker & move semantics gives the compiler magical powers.
Send + Sync
Shared memory might not be the best concurrency model (practical though) and Rust supports other ways too.
The Progress & The Future(s)
Ecosystem
I want to show my fellow nodejs devs how easy it is to create an asynchronous web server.
Hyper? Iron? Rocket? Actix-web?
Async/Await
Feels very rushed currently, while I really like the poll model of futures, I think there is still some work needed to clean things up.
Async/Await
Feels very rushed currently, while I really like the poll model of futures, I think there is still some work needed to clean things up.
Rust 1.39.0 🎉
impl Trait / trait aliases
Great progress, but still very incomplete.
Missing in traits, as generics, etc.
#[derive(Clone)]
struct SharedHandle<T> {
handle: Arc<T>,
}
Reported on 2015-07-09 (sic!)
Lifetime issues
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:8:19
| - temporary value is freed at the end of this statement
| |
8 | let my_iter = v.into_iter().map(|x| x*x).collect::<Vec<_>>().iter();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| creates a temporary which is freed while still in use
9 | for i in my_iter {
| ------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
Tomasz Drwięga
@tomusdrw
Parity Technologies