52
Introduction Rust Closures Conclusion Closures for Rust Michael Sullivan August 18, 2011 1 / 25

Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

  • Upload
    others

  • View
    28

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures for Rust

Michael Sullivan

August 18, 2011

1 / 25

Page 2: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Outline

Introduction

Rust

Closures

2 / 25

Page 3: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Disclaimer

� Rust is under heavy development.

� The things described in this talk may not be truetomorrow.

� What I discuss and how I present issues reflect mypersonal biases in language design.

3 / 25

Page 4: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Disclaimer

� Rust is under heavy development.

� The things described in this talk may not be truetomorrow.

� What I discuss and how I present issues reflect mypersonal biases in language design.

3 / 25

Page 5: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Disclaimer

� Rust is under heavy development.

� The things described in this talk may not be truetomorrow.

� What I discuss and how I present issues reflect mypersonal biases in language design.

3 / 25

Page 6: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Disclaimer

� Rust is under heavy development.

� The things described in this talk may not be truetomorrow.

� What I discuss and how I present issues reflect mypersonal biases in language design.

3 / 25

Page 7: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do we want in a programming language?

� Fast: generates efficient machine code

� Safe: type system provides guarantees that preventcertain bugs

� Concurrent: easy to build concurrent programs and totake advantage of parallelism

� “Systemsy”: fine grained control, predictable performancecharacteristics

4 / 25

Page 8: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do we want in a programming language?

� Fast: generates efficient machine code

� Safe: type system provides guarantees that preventcertain bugs

� Concurrent: easy to build concurrent programs and totake advantage of parallelism

� “Systemsy”: fine grained control, predictable performancecharacteristics

4 / 25

Page 9: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do we want in a programming language?

� Fast: generates efficient machine code

� Safe: type system provides guarantees that preventcertain bugs

� Concurrent: easy to build concurrent programs and totake advantage of parallelism

� “Systemsy”: fine grained control, predictable performancecharacteristics

4 / 25

Page 10: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do we want in a programming language?

� Fast: generates efficient machine code

� Safe: type system provides guarantees that preventcertain bugs

� Concurrent: easy to build concurrent programs and totake advantage of parallelism

� “Systemsy”: fine grained control, predictable performancecharacteristics

4 / 25

Page 11: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do we want in a programming language?

� Fast: generates efficient machine code

� Safe: type system provides guarantees that preventcertain bugs

� Concurrent: easy to build concurrent programs and totake advantage of parallelism

� “Systemsy”: fine grained control, predictable performancecharacteristics

4 / 25

Page 12: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do have?

� Firefox is in C++, which is Fast and Systemsy

� ML is (sometimes) fast and (very) safe

� Erlang is safe and concurrent

� Haskell is (sometimes) fast, (very) safe, and concurrent

� Java and C# are fast and safe

5 / 25

Page 13: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do have?

� Firefox is in C++, which is Fast and Systemsy

� ML is (sometimes) fast and (very) safe

� Erlang is safe and concurrent

� Haskell is (sometimes) fast, (very) safe, and concurrent

� Java and C# are fast and safe

5 / 25

Page 14: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do have?

� Firefox is in C++, which is Fast and Systemsy

� ML is (sometimes) fast and (very) safe

� Erlang is safe and concurrent

� Haskell is (sometimes) fast, (very) safe, and concurrent

� Java and C# are fast and safe

5 / 25

Page 15: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do have?

� Firefox is in C++, which is Fast and Systemsy

� ML is (sometimes) fast and (very) safe

� Erlang is safe and concurrent

� Haskell is (sometimes) fast, (very) safe, and concurrent

� Java and C# are fast and safe

5 / 25

Page 16: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

GoalsWhat do have?

� Firefox is in C++, which is Fast and Systemsy

� ML is (sometimes) fast and (very) safe

� Erlang is safe and concurrent

� Haskell is (sometimes) fast, (very) safe, and concurrent

� Java and C# are fast and safe

5 / 25

Page 17: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Rust

a systems languagepursuing the trifectasafe, concurrent, fast

-lkuper

6 / 25

Page 18: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Rust

DesignStatus

7 / 25

Page 19: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 20: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 21: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 22: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 23: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 24: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 25: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 26: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignGoals (straight from the docs)

� Compile-time error detection and prevention

� Run-time fault tolerance and containment

� System building, analysis and maintenance affordances

� Clarity and precision of expression

� Implementation simplicity

� Run-time efficiency

� High concurrency

8 / 25

Page 27: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignType system features

� Algebraic data type and pattern matching (no nullpointers!)

� Polymorphism: functions and types can have generic typeparameters

� Type inference on local variables

� Lightweight object system

� Data structures are immutable by default

9 / 25

Page 28: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

DesignOther features

� Lightweight tasks with no shared state

� Control over memory allocation

� Move semantics, unique pointers

� Function arguments can be passed by alias

� Typestate system tracks predicates that hold at points inthe program

10 / 25

Page 29: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Design...What?

“It’s like C++ grew up, went to grad school, started datingML, and is sharing an office with Erlang.”

11 / 25

Page 30: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Statusrustc

� Self-hosting rust compiler

� Uses LLVM as a backend

� Handles polymorphism through type passing (blech)

� Memory management through automatic referencecounting (eww)

12 / 25

Page 31: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Statusrustc

� Self-hosting rust compiler

� Uses LLVM as a backend

� Handles polymorphism through type passing (blech)

� Memory management through automatic referencecounting (eww)

12 / 25

Page 32: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Statusrustc

� Self-hosting rust compiler

� Uses LLVM as a backend

� Handles polymorphism through type passing (blech)

� Memory management through automatic referencecounting (eww)

12 / 25

Page 33: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Statusrustc

� Self-hosting rust compiler

� Uses LLVM as a backend

� Handles polymorphism through type passing (blech)

� Memory management through automatic referencecounting (eww)

12 / 25

Page 34: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

StatusThe catch

� Not ready for prime time

� Lots of bugs and exposed sharp edges

� Language still changing rapidly

13 / 25

Page 35: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

StatusThe catch

� Not ready for prime time

� Lots of bugs and exposed sharp edges

� Language still changing rapidly

13 / 25

Page 36: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

StatusThe catch

� Not ready for prime time

� Lots of bugs and exposed sharp edges

� Language still changing rapidly

13 / 25

Page 37: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures

What closures areClosures in rust

14 / 25

Page 38: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

What closures areDefinition

� In civilized languages, functions are first-class values andare allowed to reference variables in enclosing scopes

� That is, they close over their environments

15 / 25

Page 39: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

What closures areExample

function add(x) {

return function(y) { return x + y; };

}

var foo = add (42)(1337); // 1379

� Produces a function that adds x to its argument

� Note that the inner function outlives the enclosingfunction. x can’t just be stored on the stack.

16 / 25

Page 40: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

What closures areAnother Example

function scale(x, v) {

return map(function(y) { return x * y; },

v);

}

var v = scale(2, [1, 2, 3]); // [2, 4, 6]

� Multiplies every element in an array by some amount

� Note that here the lifetime of the inner function is shorterthan the lifetime of the enclosing one. x could just bestored on the stack.

17 / 25

Page 41: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

What closures areTraditional implementation

� Represent functions as a code pointer, environmentpointer pair

� Heap allocate stack frames (or at least the parts that areclosed over)

18 / 25

Page 42: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

What closures areTraditional implementation

� Represent functions as a code pointer, environmentpointer pair

� Heap allocate stack frames (or at least the parts that areclosed over)

18 / 25

Page 43: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustDesign constraints

� Want to be explicit about when we are allocating memory

� Don’t want to have to heap allocate closures when it isn’tnecessary

19 / 25

Page 44: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustSolutions

� Have two function types: block and fn

� Values of a block type may not be copied, but can bepassed by alias; this prevents them from escaping

� Values of fn type can be automatically coerced to blocktype when passed as function arguments; this allows morecode reuse

� Explicitly state what sort of function you writing

20 / 25

Page 45: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustSolutions

� Have two function types: block and fn

� Values of a block type may not be copied, but can bepassed by alias; this prevents them from escaping

� Values of fn type can be automatically coerced to blocktype when passed as function arguments; this allows morecode reuse

� Explicitly state what sort of function you writing

20 / 25

Page 46: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustSolutions

� Have two function types: block and fn

� Values of a block type may not be copied, but can bepassed by alias; this prevents them from escaping

� Values of fn type can be automatically coerced to blocktype when passed as function arguments; this allows morecode reuse

� Explicitly state what sort of function you writing

20 / 25

Page 47: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustSolutions

� Have two function types: block and fn

� Values of a block type may not be copied, but can bepassed by alias; this prevents them from escaping

� Values of fn type can be automatically coerced to blocktype when passed as function arguments; this allows morecode reuse

� Explicitly state what sort of function you writing

20 / 25

Page 48: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustLambda example

fn add(x: int) -> fn(int) -> int {

ret lambda(y: int) -> int { ret x + y; };

}

� lambda produces a fn that closes over its environment bycopying upvars into a heap allocated environment

� Since the variables are copied, changes made to thevariables in the enclosing scope will not be reflected inthe nested function

21 / 25

Page 49: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustBlock example

fn scale(x: int , v: &[int]) -> [int] {

map(block(y: &int) -> int { x * y }, v)

}

� block produces a block that closes over its environmentby storing pointers to the stack locations of the variablesin a stack allocated environment

22 / 25

Page 50: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustInference example

fn scale(x: int , v: &[int]) -> [int] {

map ({|&y| x * y}, v)

}

� Provides an abbreviation for block; the argument andreturn types are type inferred

� Only allowed to appear as a function argument, makingtype inference easy

23 / 25

Page 51: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Closures in rustCoercion example

fn add1(x: &int) -> int { x + 1 }

fn increment(v: &[int]) -> [int] {

map(add1 , v)

}

� add1 is coerced to a block when passed to map

24 / 25

Page 52: Closures for Rust - msully.net · Introduction Rust ClosuresConclusion Closures in rust Solutions Have two function types: block and fn Values of a block type may not be copied, but

Introduction Rust Closures Conclusion

Conclusion

� Rust is a new systems language out of Mozilla Researchthat is designed to be fast, concurrent, and safe

� Closures are a tricky design space in languages that wantto be explicit about performance

� Rust approaches the issues by separating functions intomultiple varieties.

25 / 25