diff options
author | 2019-11-07 14:12:16 +0100 | |
---|---|---|
committer | 2019-11-07 14:12:16 +0100 | |
commit | 76e234599f6da0b7ee21b98da80201a3984ee323 (patch) | |
tree | c3153c9227a63e31164cd2deb1ecdef152c8b561 /examples/destructure.rs | |
parent | 72e84cb297ef06d9496d18402bc8ea2d88cd1665 (diff) | |
download | rtic-76e234599f6da0b7ee21b98da80201a3984ee323.tar.gz rtic-76e234599f6da0b7ee21b98da80201a3984ee323.tar.zst rtic-76e234599f6da0b7ee21b98da80201a3984ee323.zip |
Added struct de-structure-ing example in tips & tricks
Diffstat (limited to '')
-rw-r--r-- | examples/destructure.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/destructure.rs b/examples/destructure.rs new file mode 100644 index 00000000..0cc3c1f1 --- /dev/null +++ b/examples/destructure.rs @@ -0,0 +1,47 @@ +//! examples/destructure.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::hprintln; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtfm::app(device = lm3s6965)] +const APP: () = { + struct Resources { + // Some resources to work with + #[init(0)] + a: u32, + #[init(0)] + b: u32, + #[init(0)] + c: u32, + } + + #[init] + fn init(_: init::Context) { + rtfm::pend(Interrupt::UART0); + rtfm::pend(Interrupt::UART1); + } + + // Direct destructure + #[task(binds = UART0, resources = [a, b, c])] + fn uart0(cx: uart0::Context) { + let a = cx.resources.a; + let b = cx.resources.b; + let c = cx.resources.c; + + hprintln!("UART0: a = {}, b = {}, c = {}", a, b, c).unwrap(); + } + + // De-structure-ing syntax + #[task(binds = UART1, resources = [a, b, c])] + fn uart1(cx: uart1::Context) { + let uart1::Resources { a, b, c } = cx.resources; + + hprintln!("UART0: a = {}, b = {}, c = {}", a, b, c).unwrap(); + } +}; |