Skip to content

Commit df7c6e7

Browse files
committed
Use core::mem::swap() in example to move things in/out of Mutex
Signed-off-by: Daniel Egger <[email protected]>
1 parent e6ae626 commit df7c6e7

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

examples/flash_systick_fancier.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripher
1111
use cortex_m_rt::{entry, exception};
1212

1313
use core::cell::RefCell;
14+
use core::mem::swap;
1415

1516
// A type definition for the GPIO pin to be used for our LED
1617
type LEDPIN = gpiob::PB3<Output<PushPull>>;
@@ -31,7 +32,7 @@ fn main() -> ! {
3132
let led = gpioa.pb3.into_push_pull_output(cs);
3233

3334
// Transfer GPIO into a shared structure
34-
*GPIO.borrow(cs).borrow_mut() = Some(led);
35+
swap(&mut Some(led), &mut GPIO.borrow(cs).borrow_mut());
3536

3637
let mut syst = cp.SYST;
3738

@@ -88,8 +89,8 @@ fn SysTick() {
8889
else {
8990
// Enter critical section
9091
cortex_m::interrupt::free(|cs| {
91-
// Move LED pin here, leaving a None in its place
92-
LED.replace(GPIO.borrow(cs).replace(None).unwrap());
92+
// Swap globally stored data with SysTick private data
93+
swap(LED, &mut GPIO.borrow(cs).borrow_mut());
9394
});
9495
}
9596
}

0 commit comments

Comments
 (0)