Skip to content

Commit 0b5b00c

Browse files
committed
rtic-sync: re-wrap UnsafeCell instead
1 parent c9204cb commit 0b5b00c

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

rtic-sync/src/channel.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ impl<T, const N: usize> Channel<T, N> {
104104

105105
/// Split the queue into a `Sender`/`Receiver` pair.
106106
pub fn split(&mut self) -> (Sender<'_, T, N>, Receiver<'_, T, N>) {
107-
// SAFETY: we have exclusive access to `self`.
108-
let freeq = self.freeq.get_mut();
109-
let freeq = unsafe { freeq.deref() };
107+
let freeq = self.freeq.as_mut();
110108

111109
// Fill free queue
112110
for idx in 0..N as u8 {
@@ -123,8 +121,7 @@ impl<T, const N: usize> Channel<T, N> {
123121
debug_assert!(freeq.is_full());
124122

125123
// There is now 1 sender
126-
// SAFETY: we have exclusive access to `self`.
127-
unsafe { *self.num_senders.get_mut().deref() = 1 };
124+
*self.num_senders.as_mut() = 1;
128125

129126
(Sender(self), Receiver(self))
130127
}

rtic-sync/src/unsafecell.rs

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,68 @@
11
//! Compat layer for [`core::cell::UnsafeCell`] and `loom::cell::UnsafeCell`.
22
33
#[cfg(loom)]
4-
pub use loom::cell::UnsafeCell;
4+
use loom::cell::UnsafeCell as InnerUnsafeCell;
55

6-
#[cfg(not(loom))]
7-
pub use core::UnsafeCell;
6+
#[cfg(loom)]
7+
pub use loom::cell::MutPtr;
88

99
#[cfg(not(loom))]
10-
mod core {
11-
/// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with
12-
/// loom's UnsafeCell.
13-
#[derive(Debug)]
14-
pub struct UnsafeCell<T>(core::cell::UnsafeCell<T>);
15-
16-
impl<T> UnsafeCell<T> {
17-
/// Create a new `UnsafeCell`.
18-
pub const fn new(data: T) -> UnsafeCell<T> {
19-
UnsafeCell(core::cell::UnsafeCell::new(data))
20-
}
10+
use core::cell::UnsafeCell as InnerUnsafeCell;
2111

22-
/// Access the contents of the `UnsafeCell` through a mut pointer.
23-
pub fn get_mut(&self) -> MutPtr<T> {
24-
MutPtr(self.0.get())
25-
}
12+
/// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with
13+
/// loom's UnsafeCell.
14+
#[derive(Debug)]
15+
pub struct UnsafeCell<T>(InnerUnsafeCell<T>);
16+
17+
impl<T> UnsafeCell<T> {
18+
/// Create a new `UnsafeCell`.
19+
#[cfg(not(loom))]
20+
pub const fn new(data: T) -> UnsafeCell<T> {
21+
UnsafeCell(InnerUnsafeCell::new(data))
2622
}
2723

28-
pub struct MutPtr<T>(*mut T);
24+
#[cfg(loom)]
25+
pub fn new(data: T) -> UnsafeCell<T> {
26+
UnsafeCell(InnerUnsafeCell::new(data))
27+
}
28+
29+
/// Access the contents of the `UnsafeCell` through a tracked mut pointer.
30+
pub fn get_mut(&self) -> MutPtr<T> {
31+
#[cfg(loom)]
32+
return self.0.get_mut();
33+
34+
#[cfg(not(loom))]
35+
return MutPtr(self.0.get());
36+
}
2937

30-
impl<T> MutPtr<T> {
31-
#[allow(clippy::mut_from_ref)]
32-
/// SAFETY: the caller must guarantee that the contained `*mut T` is not
33-
/// null, and must uphold the same safety requirements as for
34-
/// [`core::primitive::pointer::as_mut`] for the contained `*mut T`.
35-
pub unsafe fn deref(&self) -> &mut T {
36-
&mut *self.0
38+
/// Access the contents of the `UnsafeCell` mutably.
39+
pub fn as_mut(&mut self) -> &mut T {
40+
#[cfg(not(loom))]
41+
return self.0.get_mut();
42+
43+
#[cfg(loom)]
44+
{
45+
// SAFETY: we have exclusive access to `self`.
46+
let ptr = self.get_mut();
47+
let ptr = unsafe { ptr.deref() };
48+
49+
// SAFETY: we have exclusive access to `self` for the duration of
50+
// the borrow.
51+
unsafe { core::mem::transmute(ptr) }
3752
}
3853
}
3954
}
55+
56+
#[cfg(not(loom))]
57+
pub struct MutPtr<T>(*mut T);
58+
59+
#[cfg(not(loom))]
60+
impl<T> MutPtr<T> {
61+
#[allow(clippy::mut_from_ref)]
62+
/// SAFETY: the caller must guarantee that the contained `*mut T` is not
63+
/// null, and must uphold the same safety requirements as for
64+
/// [`core::primitive::pointer::as_mut`] for the contained `*mut T`.
65+
pub unsafe fn deref(&self) -> &mut T {
66+
&mut *self.0
67+
}
68+
}

0 commit comments

Comments
 (0)