title | subtitle | transition |
---|---|---|
async in Rust 2018 |
The future of futures |
fade |
Katharina Fey ( @spacekookie
)
- Active FOSS developer
- Avid tea drinker
- Hobbyist hardware maker
I also make some attrocious puns
I do Rust things!
- Contributer of the CLI working group
- Member of the community team &
berlin.rs
- Maintainer of several
use[ful|less]
libraries
Race conditions
vs
Inefficient scaling
Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.
Memory safety
Thread safety
- Data is always owned
- Data can always be borrowed
- Strict rules around borrowing and owning
let mut f = File::open("foo.txt")?;
for _ in 0..3 {
thread::spawn(move || {
// ... read data from file or something ...
drop(f); // close the file
});
}
This would not compile!
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
|
fn main() {
let f = File::open("foo.txt")?;
read_from_file(f);
read_from_file(f);
}
fn read_from_file(f: File) { ... }
This will also not compile!
Rust breaks down these spectrums
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.
--- Rust book, foreword
"Do this thing but don't make me wait"
Not just a new Thread
No, not that type of Futures
Future
= calculation that hasn't happened yet
- Is probably gonna happen at some point
- Just keep asking
Event Loop = runtime for Future
s
- Keeps polling
Future
until it is ready - Runs your code whenever it can be run
I'm here to show code and talk history
- Rust had green threading support
- Enabled non-blocking IO
Included a runtime in stdlib
- This came with a lot of problems
Rust wanted to go in a different direction
libgreen
is dead, long live libgreen
Sorry, did I say libgreen, I meant mio.rs
"Metal IO" 🤘
- Light, non-blocking IO library
- Abstracts async over different platforms
- Eventually developed an ecosystem around it
= no discernible runtime overhead*
= code that you can't write better by hand
- zero cost abstraction for futures
- Building async state-machines
- Wraps around
mio.rs
andfutures.rs
- Event reactor
It's state machines all the way down
// ... define `stdin` and `stdout`
let reader = BufReader::new(stdin);
let buffer = Vec::new();
let fut = io::read_until(reader, b'\n', buffer)
.and_then(move |(stdin, buffer)| {
stdout.write_all(&buffer).map_err(|e| panic!(e))
}).map_err(|e| panic!(e));
// Actually run _here_
tokio::run(fut);
Reminder: Futures are zero-cost-abstractions.
They disappear from the code at compile-time!
Write code that looks synchronous but really isn't
async fn do_this_thing() { ... }
// ...
await!( do_this_thing() );
- Implements async/await features in compiler
- Provides library ecosystem
kinda 😅
async
/await
syntax in nightly compiler- library ecosystem is still being polished
Expect more concrete progress early-2019
Follow me on twitter @spacekookie
- I do Rust work at Ferrous Systems
- I do Distributed Systems at Asquera