Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cell::Cell;
- /*
- #[derive(Debug,Copy,Clone)]
- struct Locker (u32);
- impl Locker {
- fn lock(self) -> u32 {
- self.0
- }
- }
- */
- #[derive(Debug)]
- struct Peripherals {
- serial1: Cell<u32>,
- serial2: Cell<u32>
- }
- impl Peripherals {
- fn take_serial(&self, idx: usize) -> u32 {
- match idx {
- 0 => self.serial1.replace(0),
- _ => self.serial2.replace(0)
- }
- }
- }
- //static mut SERIAL1: Locker = Locker(1);
- //static mut SERIAL2: Locker = Locker(2);
- fn main() {
- /*static*/ let PERIPHERALS: Peripherals = Peripherals {
- serial1: Cell::new(1),
- serial2: Cell::new(2)
- };
- //fn main() {
- println!("PERIPHERALS:{:?}", unsafe { &PERIPHERALS });
- let s1 = PERIPHERALS.take_serial(0);
- let s2 = PERIPHERALS.take_serial(1);
- println!("s1:{}, s2:{}", s1, s2);
- println!("PERIPHERALS:{:?}", unsafe { &PERIPHERALS });
- }
- /*
- fn foo() -> u32 {
- unsafe { PERIPHERALS.take_serial(0) }
- }
- fn bar() -> u32 {
- unsafe { PERIPHERALS.take_serial(0) }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement