From 22d758ddacddda429c9aaeaa0e132ae1c1fd4a2f Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 4 Nov 2018 19:46:49 +0100 Subject: [PATCH] fix codegen --- macros/src/codegen.rs | 22 +++++++++++++++------- tests/cpass/resource.rs | 14 +++++++------- tests/cpass/singleton.rs | 8 ++++---- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 6d01d32d11..416b0070ac 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -711,18 +711,26 @@ fn prelude( exprs.push(quote!(#name: <#name as owned_singleton::Singleton>::new())); } else { needs_unsafe = true; - if ownership.is_owned() { - defs.push(quote!(pub #name: &'a mut #name)); - exprs.push(quote!( - #name: &mut <#name as owned_singleton::Singleton>::new() + if ownership.is_owned() || mut_.is_none() { + defs.push(quote!(pub #name: &'a #mut_ #name)); + let alias = mk_ident(); + items.push(quote!( + let #mut_ #alias = unsafe { + <#name as owned_singleton::Singleton>::new() + }; )); + exprs.push(quote!(#name: &#mut_ #alias)); } else { may_call_lock = true; defs.push(quote!(pub #name: rtfm::Exclusive<'a, #name>)); + let alias = mk_ident(); + items.push(quote!( + let #mut_ #alias = unsafe { + <#name as owned_singleton::Singleton>::new() + }; + )); exprs.push(quote!( - #name: rtfm::Exclusive( - &mut <#name as owned_singleton::Singleton>::new() - ) + #name: rtfm::Exclusive(&mut #alias) )); } } diff --git a/tests/cpass/resource.rs b/tests/cpass/resource.rs index 6a7a873c80..5718a4ab3d 100644 --- a/tests/cpass/resource.rs +++ b/tests/cpass/resource.rs @@ -8,7 +8,7 @@ extern crate lm3s6965; extern crate panic_halt; extern crate rtfm; -use rtfm::app; +use rtfm::{app, Exclusive}; #[app(device = lm3s6965)] const APP: () = { @@ -59,11 +59,11 @@ const APP: () = { // owned by interrupt == `&mut` let _: &mut u32 = resources.O3; - // no `Mutex` when access from highest priority task - let _: &mut u32 = resources.S1; + // no `Mutex` proxy when access from highest priority task + let _: Exclusive = resources.S1; - // no `Mutex` when co-owned by cooperative (same priority) tasks - let _: &mut u32 = resources.S2; + // no `Mutex` proxy when co-owned by cooperative (same priority) tasks + let _: Exclusive = resources.S2; // `&` if read-only let _: &u32 = resources.S3; @@ -74,7 +74,7 @@ const APP: () = { // owned by interrupt == `&` if read-only let _: &u32 = resources.O5; - // no `Mutex` when co-owned by cooperative (same priority) tasks - let _: &mut u32 = resources.S2; + // no `Mutex` proxy when co-owned by cooperative (same priority) tasks + let _: Exclusive = resources.S2; } }; diff --git a/tests/cpass/singleton.rs b/tests/cpass/singleton.rs index 77159f3f09..75b736c1b1 100644 --- a/tests/cpass/singleton.rs +++ b/tests/cpass/singleton.rs @@ -7,7 +7,7 @@ extern crate owned_singleton; extern crate panic_halt; extern crate rtfm; -use rtfm::app; +use rtfm::{app, Exclusive}; #[app(device = lm3s6965)] const APP: () = { @@ -27,7 +27,7 @@ const APP: () = { #[Singleton] static mut S1: u32 = 0; #[Singleton] - static mut S2: u32 = 0; + static S2: u32 = 0; #[init(resources = [O1, O2, O3, O4, O5, O6, S1, S2])] fn init() { @@ -55,13 +55,13 @@ const APP: () = { let _: &mut O3 = resources.O3; let _: &O6 = resources.O6; - let _: &mut S1 = resources.S1; + let _: Exclusive = resources.S1; let _: &S2 = resources.S2; } #[interrupt(resources = [S1, S2])] fn UART1() { - let _: &mut S1 = resources.S1; + let _: Exclusive = resources.S1; let _: &S2 = resources.S2; } };