From df7c6e7c8cd8026a945b4eb2ebcd2d8288d34bf8 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Sun, 29 Mar 2020 17:28:59 +0200 Subject: [PATCH] Use core::mem::swap() in example to move things in/out of Mutex Signed-off-by: Daniel Egger --- examples/flash_systick_fancier.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/flash_systick_fancier.rs b/examples/flash_systick_fancier.rs index 4413693..6a6d4cf 100644 --- a/examples/flash_systick_fancier.rs +++ b/examples/flash_systick_fancier.rs @@ -11,6 +11,7 @@ use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripher use cortex_m_rt::{entry, exception}; use core::cell::RefCell; +use core::mem::swap; // A type definition for the GPIO pin to be used for our LED type LEDPIN = gpiob::PB3>; @@ -31,7 +32,7 @@ fn main() -> ! { let led = gpioa.pb3.into_push_pull_output(cs); // Transfer GPIO into a shared structure - *GPIO.borrow(cs).borrow_mut() = Some(led); + swap(&mut Some(led), &mut GPIO.borrow(cs).borrow_mut()); let mut syst = cp.SYST; @@ -88,8 +89,8 @@ fn SysTick() { else { // Enter critical section cortex_m::interrupt::free(|cs| { - // Move LED pin here, leaving a None in its place - LED.replace(GPIO.borrow(cs).replace(None).unwrap()); + // Swap globally stored data with SysTick private data + swap(LED, &mut GPIO.borrow(cs).borrow_mut()); }); } }