Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct Peripherals {
  3. serial1: Option<u32>,
  4. serial2: Option<u32>
  5. }
  6.  
  7. impl Peripherals {
  8. fn take_serial(&mut self, idx: usize) -> u32 {
  9. match idx {
  10. 0 => self.serial1.take().unwrap(),
  11. _ => self.serial2.take().unwrap()
  12. }
  13. }
  14. }
  15.  
  16. static mut PERIPHERALS: Peripherals = Peripherals {
  17. serial1: Some(1),
  18. serial2: Some(2)
  19. };
  20.  
  21. fn main() {
  22. println!("PERIPHERALS:{:?}", unsafe { &PERIPHERALS });
  23. let s1 = foo();
  24. let s2 = bar();
  25. println!("s1:{}, s2:{}", s1, s2);
  26. println!("PERIPHERALS:{:?}", unsafe { &PERIPHERALS });
  27. }
  28.  
  29. fn foo() -> u32 {
  30. unsafe { PERIPHERALS.take_serial(0) }
  31. }
  32.  
  33. fn bar() -> u32 {
  34. unsafe { PERIPHERALS.take_serial(0) }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement