diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2017-04-12 10:43:36 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2017-04-12 10:43:36 -0500 |
| commit | fb20b5152822462de2b5482f590c8f18078ec70b (patch) | |
| tree | 5a5aa2cb55ce0705fcab688c1e77f4351df53107 | |
| parent | d9300f01f8caf619c65de15f2bbe76672bde9980 (diff) | |
add `claim_mut`
| -rw-r--r-- | src/lib.rs | 13 | ||||
| -rw-r--r-- | tests/cfail/claim_mut.rs | 34 |
2 files changed, 47 insertions, 0 deletions
@@ -83,6 +83,19 @@ where unsafe { &*self.data.get() } } + /// Like [Resource.claim](struct.Resource.html#method.claim) but returns a + /// `&mut-` reference + pub fn claim_mut<'task, PRIORITY>( + &'static self, + _priority: &'task mut P<PRIORITY>, + ) -> &'task mut T + where + CEILING: Cmp<PRIORITY, Output = Equal>, + P<PRIORITY>: Priority, + { + unsafe { &mut *self.data.get() } + } + /// Locks the resource for the duration of the critical section `f` /// /// For the duration of the critical section, tasks whose priority level is diff --git a/tests/cfail/claim_mut.rs b/tests/cfail/claim_mut.rs new file mode 100644 index 0000000..0d1dffd --- /dev/null +++ b/tests/cfail/claim_mut.rs @@ -0,0 +1,34 @@ +#![feature(const_fn)] + +extern crate cortex_m_srp; + +use cortex_m_srp::{C2, P2, Resource}; + +static R1: Resource<i32, C2> = Resource::new(0); + +fn j1(mut prio: P2) { + // OK only one `&mut-` reference to the data + let r1: &mut i32 = R1.claim_mut(&mut prio); +} + +fn j2(prio: P2) { + // OK two `&-` references to the same data + let r1: &i32 = R1.claim(&prio); + let another_r1: &i32 = R1.claim(&prio); +} + +fn j3(mut prio: P2) { + // CAN'T have a `&-` reference and a `&mut-` reference to the same data + let r1: &i32 = R1.claim(&prio); + let another_r1: &mut i32 = R1.claim_mut(&mut prio); + //~^ error +} + +fn j4(mut prio: P2) { + // CAN'T have two `&mut-` references to the same data + let r1: &mut i32 = R1.claim_mut(&mut prio); + let another_r1: &mut i32 = R1.claim_mut(&mut prio); + //~^ error +} + +fn main() {} |
