Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 0327c7e

Browse files
bors[bot]korken89
andauthored
Merge #7
7: Exclusive should be a wrapper around a reference to the original reso… r=therealprof a=korken89 Exclusive should be a wrapper around a reference to the original resource. Else it becomes quite useless in conjunction with real resources. This was noticed when moving RTFM to `mutex_trait`, see here: https://github.com/rtfm-rs/cortex-m-rtfm/blob/8073f648cfc967d9efbb2f936e3f0e10a580a64f/examples/generics.rs#L50 (this currently does not compile as we are not taking a reference to the original resource) Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
2 parents f1ac15b + 467abb8 commit 0327c7e

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,46 +183,46 @@ impl<T> Mutex for &'_ RefCell<T> {
183183
/// Wraps a `T` and provides exclusive access via a `Mutex` impl.
184184
///
185185
/// This provides an no-op `Mutex` implementation for data that does not need a real mutex.
186-
#[derive(Copy, Clone, Debug)]
187-
pub struct Exclusive<T>(T);
186+
#[derive(Debug)]
187+
pub struct Exclusive<'a, T>(&'a mut T);
188188

189-
impl<T> Exclusive<T> {
189+
impl<'a, T> Exclusive<'a, T> {
190190
/// Creates a new `Exclusive` object wrapping `data`.
191-
pub const fn new(data: T) -> Self {
191+
pub fn new(data: &'a mut T) -> Self {
192192
Exclusive(data)
193193
}
194194

195195
/// Consumes this `Exclusive` instance and returns the wrapped value.
196-
pub fn into_inner(self) -> T {
196+
pub fn into_inner(self) -> &'a mut T {
197197
self.0
198198
}
199199
}
200200

201-
impl<T> From<T> for Exclusive<T> {
202-
fn from(data: T) -> Self {
201+
impl<'a, T> From<&'a mut T> for Exclusive<'a, T> {
202+
fn from(data: &'a mut T) -> Self {
203203
Exclusive(data)
204204
}
205205
}
206206

207-
impl<T> Deref for Exclusive<T> {
207+
impl<'a, T> Deref for Exclusive<'a, T> {
208208
type Target = T;
209209

210210
fn deref(&self) -> &T {
211-
&self.0
211+
self.0
212212
}
213213
}
214214

215-
impl<T> DerefMut for Exclusive<T> {
215+
impl<'a, T> DerefMut for Exclusive<'a, T> {
216216
fn deref_mut(&mut self) -> &mut T {
217-
&mut self.0
217+
self.0
218218
}
219219
}
220220

221-
impl<T> Mutex for Exclusive<T> {
221+
impl<'a, T> Mutex for Exclusive<'a, T> {
222222
type Data = T;
223223

224224
fn lock<R>(&mut self, f: impl FnOnce(&mut T) -> R) -> R {
225-
f(&mut self.0)
225+
f(self.0)
226226
}
227227
}
228228

@@ -316,10 +316,11 @@ mod tests {
316316

317317
#[test]
318318
fn exclusive() {
319-
let mut excl = Exclusive(0);
319+
let mut var = 0;
320+
let mut excl = Exclusive(&mut var);
320321

321322
excl.lock(|val| *val += 1);
322323

323-
assert_eq!(excl.into_inner(), 1);
324+
assert_eq!(*excl.into_inner(), 1);
324325
}
325326
}

0 commit comments

Comments
 (0)