Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Anna Liao - Pycascades 2018

73 views

Published on

RaspberryPy to RustyPi: Porting a Python module to Rust

Published in: Software
  • Be the first to comment

  • Be the first to like this

Anna Liao - Pycascades 2018

  1. 1. Anna Liao
  2. 2. Designed for modern computing, concurrency
  3. 3. Rust • Created in 2010 • Maximized safety and performance • Complex syntax and semantics Go • Created in 2009 • Easier to learn • Productivity • Data races • Garbage collector Designed for multi-core systems semantics = Underlying processes syntax = arrangement of words and phrases
  4. 4. Who uses Rust? - Mozilla (Servo / Quantum) - Other projects using Rust in production: - https://www.rust-lang.org/en-US/friends.html
  5. 5. Rust is Downside? Steep learning curve Complexity Still evolving
  6. 6. Raspberry Pi ($35 USD) Sense HAT ($40 USD) HAT = Hardware Attached onTop- Embedded computer running Linux LED matrix
  7. 7. Python example: rainbow LED
  8. 8. fb = framebuffer (device driver)
  9. 9. See what’s in /sys/class/graphics/fb* on the RPi
  10. 10. _get_fb_device(): device = /dev/fb1 BCM2708 refers to CPU on RPi ???
  11. 11. Find Sense HAT fb device driver 1. Look for all FBs listed in `/sys/class/graphics/fb*` 2. Read the `name` file in each, if it exists 3. Check if the file contents match the given `SENSE_HAT_FB_NAME` 4. Return device path (i.e., /dev/fb1)
  12. 12. Define types `?` is shorthand for a type of error handling More specific error handling to check “name” file exists 1.Static types 2.Error handling Still working on better ergonomics Human programmer friendly 
  13. 13. • Option<T>
  14. 14. form /dev/fb1 Check /dev/fb1 exists
  15. 15. This is for recoverable error handling, when you don’t want the program to stop immediately when the error occurs.
  16. 16. Stack - Fixed size data - Removed at the end of a function - Data can be copied (within the function) - Types: Path, &str (string slice), array Heap: - data that can grow in size - Ownership moves, unless a deep copy (clone) trait is implemented - Used to pass memory between functions - Types: PathBuf, String,Vec
  17. 17. https://doc.rust-lang.org/stable/book/second-edition/ch04-01-what-is-ownership.html Does NOT compile! Central feature of Rust Memory safety without garbage collector
  18. 18. stack heap
  19. 19. !! Memory unsafe !!
  20. 20. This compiles! 
  21. 21. Stack-only data array Stack - Fixed size data - Removed at end of function call - Data can be copied (within context of function) This compiles! 
  22. 22. Heap allocated data o is invalid! Ownership moves to pixel_list[0] Compiler complains that `copy` trait not implemented
  23. 23. Use stack-only data if possible! Makes life easier This compiles! 
  24. 24. This compiles! 
  25. 25. Rust Path: slice of path (fixed size, stack-only) &Path: reference to a `Path` PathBuf: owned, mutable path (data allocated on heap) PathBuf to pass to another function Passing data on the heap Name Value Ptr &Path Name Value &str “/sys/class/graphics” Path
  26. 26. Difficult and confusing concept! Read more at: https://doc.rust-lang.org/book/second-edition/ch04-01-what-is-ownership.html
  27. 27. Example of cryptic compiler errors Working version ”import” statements Doesn’t work
  28. 28. Binary encoding Python Rust Shift bits and apply mask Intel (Macbook Pro) = Little Endian, ARM (RPi) = Big Endian 24-bit pixel (8-bit R, 8-bit G, 8-bit B) to 16 bit RGB565 format `int` type `bytes` type
  29. 29. See full writeLED-rs script at github.com/anna-liao Yay! It works!
  30. 30. Success!!
  31. 31. - Rust books: - The Rust Programming Language (intro) - Programming Rust (more in-depth) - exercism.io - Rust Playground: play.rust- lang.org play.rust-lang.org
  32. 32. Connect with others in the Rust community - Meetups - users.rust-lang.org - Rust conferences: - RustConf (West) - Rust Belt Rust (Midwest) - Rust Fest (EU) - Full-day RustBridge workshop for beginners - #rust-beginners IRC channel
  33. 33. https://github.com/thejpster/pi-workshop-rs) https://projects.raspberrypi.org/en/projects/getting-started-with- the-sense-hat medium.com/@aliao22
  34. 34. What isYOUR programming experiment for 2018? outside of your comfort zone 

×