Default Methods in Rust Michael Sullivan August 14, 2013 1 / 30 - - PowerPoint PPT Presentation

default methods in rust
SMART_READER_LITE
LIVE PREVIEW

Default Methods in Rust Michael Sullivan August 14, 2013 1 / 30 - - PowerPoint PPT Presentation

Introduction Rust Fixing Default Trait Methods Other Conclusion Default Methods in Rust Michael Sullivan August 14, 2013 1 / 30 Introduction Rust Fixing Default Trait Methods Other Conclusion Outline Introduction Rust Fixing Default


slide-1
SLIDE 1

Introduction Rust Fixing Default Trait Methods Other Conclusion

Default Methods in Rust

Michael Sullivan August 14, 2013

1 / 30

slide-2
SLIDE 2

Introduction Rust Fixing Default Trait Methods Other Conclusion

Outline Introduction Rust Fixing Default Trait Methods Other

2 / 30

slide-3
SLIDE 3

Introduction Rust Fixing Default Trait Methods Other Conclusion

Disclaimer

❼ ❼ ❼

3 / 30

slide-4
SLIDE 4

Introduction Rust Fixing Default Trait Methods Other Conclusion

Disclaimer

❼ Rust is under heavy development. ❼ ❼

3 / 30

slide-5
SLIDE 5

Introduction Rust Fixing Default Trait Methods Other Conclusion

Disclaimer

❼ Rust is under heavy development. ❼ The things described in this talk may not be true

tomorrow.

3 / 30

slide-6
SLIDE 6

Introduction Rust Fixing Default Trait Methods Other Conclusion

Disclaimer

❼ Rust is under heavy development. ❼ The things described in this talk may not be true

tomorrow.

❼ What I discuss and how I present issues reflect my

personal biases in language design.

3 / 30

slide-7
SLIDE 7

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do we want in a programming language?

❼ ❼ ❼ ❼

4 / 30

slide-8
SLIDE 8

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ ❼ ❼

4 / 30

slide-9
SLIDE 9

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent

certain bugs

❼ ❼

4 / 30

slide-10
SLIDE 10

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent

certain bugs

❼ Concurrent: easy to build concurrent programs and to

take advantage of parallelism

4 / 30

slide-11
SLIDE 11

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do we want in a programming language?

❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent

certain bugs

❼ Concurrent: easy to build concurrent programs and to

take advantage of parallelism

❼ “Systemsy”: fine grained control, predictable performance

characteristics

4 / 30

slide-12
SLIDE 12

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do have?

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

5 / 30

slide-13
SLIDE 13

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do have?

❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ ❼ ❼

5 / 30

slide-14
SLIDE 14

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What do have?

❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ ❼

5 / 30

slide-15
SLIDE 15

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What 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 ❼

5 / 30

slide-16
SLIDE 16

Introduction Rust Fixing Default Trait Methods Other Conclusion

Goals What 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 / 30

slide-17
SLIDE 17

Introduction Rust Fixing Default Trait Methods Other Conclusion

Rust a systems language pursuing the trifecta safe, concurrent, fast

  • lkuper

6 / 30

slide-18
SLIDE 18

Introduction Rust Fixing Default Trait Methods Other Conclusion

Rust Design Status

7 / 30

slide-19
SLIDE 19

Introduction Rust Fixing Default Trait Methods Other Conclusion

Design Type system features

❼ Algebraic data type and pattern matching (no null

pointers!)

❼ Polymorphism: functions and types can have generic type

parameters

❼ Type inference on local variables ❼ A somewhat idiosyncratic typeclass system (“traits”) ❼ Data structures are immutable by default ❼ Region pointers allow safe pointers into non-heap objects

8 / 30

slide-20
SLIDE 20

Introduction Rust Fixing Default Trait Methods Other Conclusion

Design Other features

❼ Lightweight tasks with no shared state ❼ Control over memory allocation ❼ Move semantics, unique pointers

9 / 30

slide-21
SLIDE 21

Introduction Rust Fixing Default Trait Methods Other Conclusion

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

10 / 30

slide-22
SLIDE 22

Introduction Rust Fixing Default Trait Methods Other Conclusion

Status rustc

❼ Self-hosting rust compiler ❼ ❼

11 / 30

slide-23
SLIDE 23

Introduction Rust Fixing Default Trait Methods Other Conclusion

Status rustc

❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼

11 / 30

slide-24
SLIDE 24

Introduction Rust Fixing Default Trait Methods Other Conclusion

Status rustc

❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ Handles polymorphism and typeclasses by

monomorphizing

11 / 30

slide-25
SLIDE 25

Introduction Rust Fixing Default Trait Methods Other Conclusion

Status The catch

❼ Not quite ready for prime time ❼ ❼ ❼

12 / 30

slide-26
SLIDE 26

Introduction Rust Fixing Default Trait Methods Other Conclusion

Status The catch

❼ Not quite ready for prime time ❼ Lots of bugs and exposed sharp edges ❼ ❼

12 / 30

slide-27
SLIDE 27

Introduction Rust Fixing Default Trait Methods Other Conclusion

Status The catch

❼ Not quite ready for prime time ❼ Lots of bugs and exposed sharp edges ❼ Language still evolving ❼

12 / 30

slide-28
SLIDE 28

Introduction Rust Fixing Default Trait Methods Other Conclusion

Status The catch

❼ Not quite ready for prime time ❼ Lots of bugs and exposed sharp edges ❼ Language still evolving ❼ But getting really close!

12 / 30

slide-29
SLIDE 29

Introduction Rust Fixing Default Trait Methods Other Conclusion

Traits What are traits?

❼ Traits are interfaces that specify a set of methods for

types to implement

❼ Functions can be parameterized over types that

implement a certain trait

❼ Like typeclasses in Haskell

13 / 30

slide-30
SLIDE 30

Introduction Rust Fixing Default Trait Methods Other Conclusion

Traits Trait example

trait ToStr { fn to_str (& self) -> ~str; } impl ToStr for int { fn to_str (& self) -> ~str { int:: to_str (* self) } } fn exclaim <T: ToStr >(x: T) -> ~str { x.to_str () + ~"!" }

14 / 30

slide-31
SLIDE 31

Introduction Rust Fixing Default Trait Methods Other Conclusion

Traits More trait example

impl <T: ToStr > ToStr for ~[T] { fn to_str (& self) -> ~str { let strs = self.map(|x| x.to_str ()); fmt!("[%s]", strs) } } impl <T: ToStr > ToStr for Option <T> { fn to_str (& self) -> ~str { match self { &None => ~"None", &Some(ref t) => fmt!("Some (%s)", t.to_str ( } } }

15 / 30

slide-32
SLIDE 32

Introduction Rust Fixing Default Trait Methods Other Conclusion

Default methods A solution

❼ Sometimes you have a method that has a straightforward

“default”

❼ But want to be able to override it

16 / 30

slide-33
SLIDE 33

Introduction Rust Fixing Default Trait Methods Other Conclusion

Default methods A simple example: equality

trait Eq { fn eq(&self , other: &Self) -> bool; fn ne(&self , other: &Self) -> bool { !self.eq(other) } }

17 / 30

slide-34
SLIDE 34

Introduction Rust Fixing Default Trait Methods Other Conclusion

Default methods An implementation without overriding

❼ Implementations can choose to use the default

implementation...

impl Eq for int { fn eq(&self , other: &int) -> bool { *self == *other } }

18 / 30

slide-35
SLIDE 35

Introduction Rust Fixing Default Trait Methods Other Conclusion

Default methods An implementation with overriding

❼ ... or to override it

impl Eq for int { fn eq(&self , other: &int) -> bool { *self == *other } fn ne(&self , other: &Self) -> bool { *self != *other } }

19 / 30

slide-36
SLIDE 36

Introduction Rust Fixing Default Trait Methods Other Conclusion

Default methods Why override?

❼ Overriding can be useful for performance ❼

20 / 30

slide-37
SLIDE 37

Introduction Rust Fixing Default Trait Methods Other Conclusion

Default methods Why override?

❼ Overriding can be useful for performance ❼ And is sometimes semantically necessary (the default

implementation is not correct for floating point)

20 / 30

slide-38
SLIDE 38

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems The state at the start of the summer

❼ The above examples worked... ❼

21 / 30

slide-39
SLIDE 39

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems The state at the start of the summer

❼ The above examples worked... ❼ But anything much more complicated didn’t

21 / 30

slide-40
SLIDE 40

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Type parameters...

trait A<T> { fn g(&self , x: T) -> T { x } } impl A<int > for int { } fn main () { assert !(0i.g(2i) == 2i); }

❼ Triggered an ICE ❼

22 / 30

slide-41
SLIDE 41

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Type parameters...

trait A<T> { fn g(&self , x: T) -> T { x } } impl A<int > for int { } fn main () { assert !(0i.g(2i) == 2i); }

❼ Triggered an ICE ❼ Need to mediate between the different type parameters...

22 / 30

slide-42
SLIDE 42

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Calling a default method from another one

trait Cat { fn meow (& self) -> bool; fn scratch (& self) -> bool { self.purr () } fn purr (& self) -> bool { true } }

❼ Triggered an ICE

23 / 30

slide-43
SLIDE 43

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems And a bunch of related ones

❼ Calling a default method through a type parameters ❼ Packaging up an object with a default method ❼ ❼

24 / 30

slide-44
SLIDE 44

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems And a bunch of related ones

❼ Calling a default method through a type parameters ❼ Packaging up an object with a default method ❼ Originally fixed by searching for default methods in more

cases

24 / 30

slide-45
SLIDE 45

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems And a bunch of related ones

❼ Calling a default method through a type parameters ❼ Packaging up an object with a default method ❼ Originally fixed by searching for default methods in more

cases

❼ Eventually fixed by reworking how method lookup is done

in trans

24 / 30

slide-46
SLIDE 46

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Cross-crate calls

❼ Couldn’t call default methods on a trait in another library ❼ ❼ ❼

25 / 30

slide-47
SLIDE 47

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Cross-crate calls

❼ Couldn’t call default methods on a trait in another library ❼ Some false starts here - the library code is scary ❼ ❼

25 / 30

slide-48
SLIDE 48

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Cross-crate calls

❼ Couldn’t call default methods on a trait in another library ❼ Some false starts here - the library code is scary ❼ Solution is to properly export information about default

methods

25 / 30

slide-49
SLIDE 49

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Cross-crate calls

❼ Couldn’t call default methods on a trait in another library ❼ Some false starts here - the library code is scary ❼ Solution is to properly export information about default

methods

❼ Required a major rework of what information we track

about impls

25 / 30

slide-50
SLIDE 50

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Interacting with trait bounds

❼ Trait bounds on a trait’s type params didn’t work ❼

26 / 30

slide-51
SLIDE 51

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Interacting with trait bounds

❼ Trait bounds on a trait’s type params didn’t work ❼ Calling a function bounded over the trait didn’t work

26 / 30

slide-52
SLIDE 52

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Supertraits

❼ Couldn’t call methods on a supertrait if there was any

polymorphism

❼ ❼ ❼

27 / 30

slide-53
SLIDE 53

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Supertraits

❼ Couldn’t call methods on a supertrait if there was any

polymorphism

❼ Major rework to how supertrait calls are handled ❼ ❼

27 / 30

slide-54
SLIDE 54

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Supertraits

❼ Couldn’t call methods on a supertrait if there was any

polymorphism

❼ Major rework to how supertrait calls are handled ❼ Needed to actually check that an impl implemented

supertraits...

27 / 30

slide-55
SLIDE 55

Introduction Rust Fixing Default Trait Methods Other Conclusion

Problems Supertraits

❼ Couldn’t call methods on a supertrait if there was any

polymorphism

❼ Major rework to how supertrait calls are handled ❼ Needed to actually check that an impl implemented

supertraits...

❼ Which required improving the trait resolution algorithm...

27 / 30

slide-56
SLIDE 56

Introduction Rust Fixing Default Trait Methods Other Conclusion

Other things Other projects

❼ Improved the trait resolution algorithm, removing the

need for a hoky workaround in iterators

❼ Fixed some pattern matching codegen bugs ❼ Fixing some problems with objects and supertraits

28 / 30

slide-57
SLIDE 57

Introduction Rust Fixing Default Trait Methods Other Conclusion

Other things Fixed a lot of bugs #2410, #3121, #4055, #4099, #4102, #4102, #4103, #4350, #4396, #4946, #6554, #6868, #6909, #6959, #6967, #7183, #7266, #7278, #7295, #7301, #7341, #7460, #7481, #7536, #7569, #7571, #7661, #7675, #7862

29 / 30

slide-58
SLIDE 58

Introduction Rust Fixing Default Trait Methods Other Conclusion

Conclusion

❼ Rust is a new systems language out of Mozilla Research

that is designed to be fast, concurrent, and safe

❼ I worked on a bunch of different stuff on it this summer ❼ Default methods now work!

30 / 30