From b5db43550185c2acd62d3f27bc89f2f24b4fbb22 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Mar 2025 12:46:23 +0100 Subject: rtic-sync: introduce loom compat layer and apply it to `channel` --- rtic-sync/src/unsafecell.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 rtic-sync/src/unsafecell.rs (limited to 'rtic-sync/src/unsafecell.rs') diff --git a/rtic-sync/src/unsafecell.rs b/rtic-sync/src/unsafecell.rs new file mode 100644 index 0000000..e1774f8 --- /dev/null +++ b/rtic-sync/src/unsafecell.rs @@ -0,0 +1,43 @@ +//! Compat layer for [`core::cell::UnsafeCell`] and `loom::cell::UnsafeCell`. + +#[cfg(loom)] +pub use loom::cell::UnsafeCell; + +#[cfg(not(loom))] +pub use core::UnsafeCell; + +#[cfg(not(loom))] +mod core { + /// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with + /// loom's UnsafeCell. + #[derive(Debug)] + pub struct UnsafeCell(core::cell::UnsafeCell); + + impl UnsafeCell { + /// Create a new `UnsafeCell`. + pub const fn new(data: T) -> UnsafeCell { + UnsafeCell(core::cell::UnsafeCell::new(data)) + } + + /// Access the contents of the `UnsafeCell` through a mut pointer. + pub fn get_mut(&self) -> MutPtr { + MutPtr(self.0.get()) + } + + pub unsafe fn with_mut(&self, f: F) -> R + where + F: FnOnce(*mut T) -> R, + { + f(self.0.get()) + } + } + + pub struct MutPtr(*mut T); + + impl MutPtr { + #[allow(clippy::mut_from_ref)] + pub unsafe fn deref(&self) -> &mut T { + &mut *self.0 + } + } +} -- cgit v1.2.3