aaron turon mozilla research c c ml haskell rust
play

Aaron Turon Mozilla Research C/C++ ML/Haskell Rust Safe - PowerPoint PPT Presentation

Aaron Turon Mozilla Research C/C++ ML/Haskell Rust Safe systems programming Why Mozilla? Browsers need control . Browsers need safety . Servo: Next-generation browser built in Rust. C++ What is control? void example() {


  1. Compiler enforces moves fn give() { fn take(vec: Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � take(vec); � … vec.push(2); Error: vec has been moved � }

  2. Compiler enforces moves fn give() { fn take(vec: Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � take(vec); � … vec.push(2); Error: vec has been moved � } Prevents: - use after free - double moves - …

  3. Borrow � � v. To receive something with the promise of returning it.

  4. Shared borrow (&T)

  5. Shared borrow (&T)

  6. Shared borrow (&T)

  7. Aliasing Mutation Shared borrow (&T)

  8. Aliasing Mutation Shared borrow (&T)

  9. Mutable borrow (&mut T)

  10. Mutable borrow (&mut T)

  11. Mutable borrow (&mut T)

  12. Mutable borrow (&mut T)

  13. Mutable borrow (&mut T)

  14. Mutable borrow (&mut T)

  15. Mutable borrow (&mut T)

  16. Aliasing Mutation Mutable borrow (&mut T)

  17. Aliasing Mutation Mutable borrow (&mut T)

  18. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � use(&vec); � … � }

  19. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � use(&vec); � … � } 1 data 2 vec length capacity

  20. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � use(&vec); � … � } 1 data 2 vec length capacity

  21. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); “Shared reference � use(&vec); � to Vec<int>” … � } 1 data 2 vec length capacity

  22. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); “Shared reference � use(&vec); � to Vec<int>” … � } Loan out vec 1 data 2 vec length capacity

  23. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); “Shared reference � use(&vec); � to Vec<int>” … � } Loan out vec 1 data 2 vec length capacity vec

  24. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � use(&vec); � … � } 1 data 2 vec length capacity vec

  25. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � use(&vec); � … � } 1 data 2 vec length capacity vec

  26. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � use(&vec); � … � } 1 data 2 vec length capacity

  27. fn lender() { fn use(vec: &Vec<int>) { let mut vec = Vec::new(); // … vec.push(1); } vec.push(2); � use(&vec); � … � } 1 data 2 vec length capacity

  28. Aliasing Mutation Shared references are immutable : fn use(vec: &Vec<int>) { vec.push(3); vec[1] += 2; }

  29. Aliasing Mutation Shared references are immutable : fn use(vec: &Vec<int>) { vec.push(3); vec[1] += 2; }

  30. Aliasing Mutation Shared references are immutable : fn use(vec: &Vec<int>) { vec.push(3); vec[1] += 2; }

  31. Aliasing Mutation Shared references are immutable : fn use(vec: &Vec<int>) { vec.push(3); vec[1] += 2; } Error: cannot mutate shared reference

  32. Aliasing Mutation * Shared references are immutable : fn use(vec: &Vec<int>) { vec.push(3); vec[1] += 2; } Error: cannot mutate shared reference * Actually: mutation only in controlled circumstances

  33. Mutable references fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } }

  34. Mutable references fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } mutable reference to Vec<int>

  35. Mutable references fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } mutable reference to Vec<int> push() is legal

  36. Efficient Iteration fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 to 2 3

  37. Efficient Iteration fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 to 2 3

  38. Efficient Iteration fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 to 2 3 elem

  39. Efficient Iteration fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 to 2 3 elem

  40. Efficient Iteration fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 1 to 2 3 elem

  41. Efficient Iteration fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 1 to 2 3 elem

  42. Efficient Iteration fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 1 to 2 3 elem

  43. What if from and to are equal? fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … from 1 to 2 3 elem

  44. What if from and to are equal? fn push_all(from: &Vec<int>, to: & mut Vec<int>) { for elem in from.iter() { to.push(*elem); } } … 1 2 from 1 3 to 2 3 1 elem

Recommend


More recommend